Spaces:
Running
Running
| import openai | |
| from pathlib import Path | |
| from prompts import LESSON_PROMPT | |
| def generate_lesson(topic, prompt_template=LESSON_PROMPT): | |
| """ | |
| Generates a beginner-friendly coding lesson for a given topic. | |
| Returns a tuple: (file_path, lesson_text) | |
| """ | |
| # Fill in the topic into the prompt | |
| tutorial_prompt = f"{prompt_template}\n\nTopic: '{topic}'" | |
| response = openai.chat.completions.create( | |
| model="gpt-4o-mini", | |
| messages=[{"role": "user", "content": tutorial_prompt}], | |
| temperature=0.7, | |
| ) | |
| lesson_text = response.choices[0].message.content.strip() | |
| # Save to outputs folder | |
| output_path = Path("outputs") / f"{topic.replace(' ', '_')}.txt" | |
| output_path.parent.mkdir(exist_ok=True) | |
| with open(output_path, "w", encoding="utf-8") as f: | |
| f.write(lesson_text) | |
| return str(output_path), lesson_text | |