Spaces:
Running
Running
Update utils.py
Browse files
utils.py
CHANGED
|
@@ -1,40 +1,54 @@
|
|
| 1 |
import openai
|
| 2 |
-
import
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
|
| 6 |
def generate_lesson(topic):
|
| 7 |
-
"""
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
response = openai.chat.completions.create(
|
| 11 |
model="gpt-4o-mini",
|
| 12 |
-
messages=[
|
| 13 |
-
|
| 14 |
-
{"role": "user", "content": prompt}
|
| 15 |
-
]
|
| 16 |
)
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
with open(
|
| 22 |
-
f.write(
|
| 23 |
-
|
| 24 |
-
return file_path, text
|
| 25 |
|
| 26 |
|
| 27 |
def generate_image(topic):
|
| 28 |
-
"""
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
| 32 |
)
|
| 33 |
-
|
| 34 |
response = openai.images.generate(
|
| 35 |
model="gpt-image-1",
|
| 36 |
-
prompt=
|
| 37 |
size="1024x1024"
|
| 38 |
)
|
| 39 |
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|