tiahchia commited on
Commit
16699fe
·
verified ·
1 Parent(s): aa3ac82

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +10 -32
utils.py CHANGED
@@ -1,49 +1,27 @@
1
  import openai
2
  from pathlib import Path
3
- import importlib.util
4
 
5
- # --- Dynamically load generate.py from first_agent_template ---
6
- template_path = Path(__file__).parent / "First_agent_template" / "generate.py"
7
-
8
- spec = importlib.util.spec_from_file_location("generate", str(template_path))
9
- generate_module = importlib.util.module_from_spec(spec)
10
- spec.loader.exec_module(generate_module)
11
-
12
- # Now we can access the function
13
- generate_image_from_text = generate_module.generate_image_from_text
14
-
15
- # --- Lesson generation ---
16
- def generate_lesson(topic):
17
- tutorial_prompt = f"""
18
- Create a beginner-friendly coding tutorial for '{topic}'.
19
- Include explanations and a minimal Python/JS/HTML/CSS code snippet relevant to the topic.
20
- End with a short practice section.
21
  """
 
 
 
 
 
 
22
  response = openai.chat.completions.create(
23
  model="gpt-4o-mini",
24
  messages=[{"role": "user", "content": tutorial_prompt}],
25
  temperature=0.7,
26
  )
 
27
  lesson_text = response.choices[0].message.content.strip()
28
 
29
- # Save lesson to a .txt file
30
  output_path = Path("outputs") / f"{topic.replace(' ', '_')}.txt"
31
  output_path.parent.mkdir(exist_ok=True)
32
  with open(output_path, "w", encoding="utf-8") as f:
33
  f.write(lesson_text)
34
 
35
  return str(output_path), lesson_text
36
-
37
-
38
- # --- Image generation ---
39
- def generate_image_via_agent(lesson_text):
40
- """
41
- Generates an image using the local First_agent_template code.
42
- Returns the path to the saved image.
43
- """
44
- image = generate_image_from_text(lesson_text)
45
-
46
- output_path = Path("outputs") / "lesson_image.png"
47
- output_path.parent.mkdir(exist_ok=True)
48
- image.save(output_path)
49
- return str(output_path)
 
1
  import openai
2
  from pathlib import Path
3
+ from prompts import LESSON_PROMPT
4
 
5
+ def generate_lesson(topic, prompt_template=LESSON_PROMPT):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  """
7
+ Generates a beginner-friendly coding lesson for a given topic.
8
+ Returns a tuple: (file_path, lesson_text)
9
+ """
10
+ # Fill in the topic into the prompt
11
+ tutorial_prompt = f"{prompt_template}\n\nTopic: '{topic}'"
12
+
13
  response = openai.chat.completions.create(
14
  model="gpt-4o-mini",
15
  messages=[{"role": "user", "content": tutorial_prompt}],
16
  temperature=0.7,
17
  )
18
+
19
  lesson_text = response.choices[0].message.content.strip()
20
 
21
+ # Save to outputs folder
22
  output_path = Path("outputs") / f"{topic.replace(' ', '_')}.txt"
23
  output_path.parent.mkdir(exist_ok=True)
24
  with open(output_path, "w", encoding="utf-8") as f:
25
  f.write(lesson_text)
26
 
27
  return str(output_path), lesson_text