tiahchia commited on
Commit
e0a6302
·
verified ·
1 Parent(s): bf9e5c5

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +37 -23
utils.py CHANGED
@@ -1,40 +1,54 @@
1
  import openai
2
- import os
 
 
 
3
 
4
- openai.api_key = os.getenv("OPENAI_API_KEY")
5
 
6
  def generate_lesson(topic):
7
- """Generates a short, beginner-friendly code lesson for the chosen topic."""
8
- prompt = f"Write a clear, beginner-friendly coding lesson on {topic}. Include a short example with code comments."
9
-
 
 
 
 
 
 
10
  response = openai.chat.completions.create(
11
  model="gpt-4o-mini",
12
- messages=[
13
- {"role": "system", "content": "You are an expert code instructor who writes short, readable lessons."},
14
- {"role": "user", "content": prompt}
15
- ]
16
  )
 
17
 
18
- text = response.choices[0].message.content.strip()
19
- file_path = f"/tmp/{topic.replace(' ', '_')}_lesson.txt"
20
-
21
- with open(file_path, "w", encoding="utf-8") as f:
22
- f.write(text)
23
-
24
- return file_path, text
25
 
26
 
27
  def generate_image(topic):
28
- """Generates a minimal dark terminal-style code screenshot for the lesson topic."""
29
- image_prompt = (
30
- f"A minimal terminal screenshot showing coding related to {topic}. "
31
- "Dark background, bright syntax highlighting, subtle glow, realistic developer aesthetic."
 
 
32
  )
33
-
34
  response = openai.images.generate(
35
  model="gpt-image-1",
36
- prompt=image_prompt,
37
  size="1024x1024"
38
  )
39
 
40
- return response.data[0].url
 
 
 
 
 
 
 
1
  import openai
2
+ from pathlib import Path
3
+ from PIL import Image
4
+ from io import BytesIO
5
+ import base64
6
 
7
+ # Make sure your OPENAI_API_KEY is set in environment variables
8
 
9
  def generate_lesson(topic):
10
+ """
11
+ Generates lesson text for a given topic.
12
+ Returns a tuple: (file_path, lesson_text)
13
+ """
14
+ tutorial_prompt = f"""
15
+ Create a beginner-friendly coding tutorial for '{topic}'.
16
+ Include explanations and a minimal Python/JS/HTML/CSS code snippet relevant to the topic.
17
+ End with a short practice section.
18
+ """
19
  response = openai.chat.completions.create(
20
  model="gpt-4o-mini",
21
+ messages=[{"role": "user", "content": tutorial_prompt}],
22
+ temperature=0.7,
 
 
23
  )
24
+ lesson_text = response.choices[0].message.content.strip()
25
 
26
+ # Save lesson to a .txt file
27
+ output_path = Path("outputs") / f"{topic.replace(' ', '_')}.txt"
28
+ output_path.parent.mkdir(exist_ok=True)
29
+ with open(output_path, "w", encoding="utf-8") as f:
30
+ f.write(lesson_text)
31
+ return str(output_path), lesson_text
 
32
 
33
 
34
  def generate_image(topic):
35
+ """
36
+ Generates a terminal-style image snippet for a lesson topic.
37
+ """
38
+ prompt = (
39
+ f"A minimal terminal screenshot showing coding for a lesson on '{topic}'. "
40
+ "Show a code snippet or highlighted portion of code, dark background, light text, Consolas font."
41
  )
 
42
  response = openai.images.generate(
43
  model="gpt-image-1",
44
+ prompt=prompt,
45
  size="1024x1024"
46
  )
47
 
48
+ image_data = response.data[0].b64_json
49
+ image = Image.open(BytesIO(base64.b64decode(image_data)))
50
+
51
+ output_path = Path("outputs") / f"{topic.replace(' ', '_')}.png"
52
+ output_path.parent.mkdir(exist_ok=True)
53
+ image.save(output_path)
54
+ return str(output_path)