|
| 1 | +import os |
| 2 | +import asyncio |
| 3 | +import pandas as pd |
| 4 | +from openai import AsyncOpenAI |
| 5 | +import os |
| 6 | + |
| 7 | +client = AsyncOpenAI(api_key=os.getenv('OPENAI_API_KEY')) |
| 8 | + |
| 9 | +async def generate_gpt_response(subject, learning_outcomes, age_range, lesson_duration): |
| 10 | + prompt = ( |
| 11 | + "Based on the following requirements:\n" |
| 12 | + f"Subject: {subject}\n" |
| 13 | + f"Learning Outcomes: {learning_outcomes}\n" |
| 14 | + f"Age Range/Grade: {age_range}\n" |
| 15 | + f"Lesson Duration: {lesson_duration}\n" |
| 16 | + "Generate a Minecraft Education build challenge that includes:\n" |
| 17 | + "a) A build challenge prompt for the students.\n" |
| 18 | + "b) An explanation of the build that the teacher can expect to see.\n" |
| 19 | + "c) Some ideas for assessment for that build challenge." |
| 20 | + ) |
| 21 | + |
| 22 | + completion = await client.chat.completions.create( |
| 23 | + model="gpt-3.5-turbo", |
| 24 | + messages=[ |
| 25 | + {"role": "system", "content": "You are a helpful assistant."}, |
| 26 | + {"role": "user", "content": prompt} |
| 27 | + ] |
| 28 | + ) |
| 29 | + |
| 30 | + return completion.choices[-1].message.content.strip() |
| 31 | + |
| 32 | +def safe_filename(filename): |
| 33 | + """Generate a safe filename by removing or replacing invalid characters.""" |
| 34 | + invalid_chars = '<>:"/\\|?*' |
| 35 | + for char in invalid_chars: |
| 36 | + filename = filename.replace(char, '-') |
| 37 | + return filename[:200] # Limit the filename length to 200 characters for compatibility |
| 38 | + |
| 39 | +async def process_and_save_response(row, base_dir): |
| 40 | + response = await generate_gpt_response(row['Subject'], row['LearningOutcomes'], row['AgeRange'], row['LessonLength']) |
| 41 | + |
| 42 | + # Format the Markdown content |
| 43 | + markdown_content = f"# Minecraft Mini Build Challenge\n\n" \ |
| 44 | + f"**Subject:** {row['Subject']}\n\n" \ |
| 45 | + f"**Learning Outcomes:** {row['LearningOutcomes']}\n\n" \ |
| 46 | + f"**Age Range:** {row['AgeRange']}\n\n" \ |
| 47 | + f"**Lesson Duration:** {row['LessonLength']}\n\n" \ |
| 48 | + "---\n\n" \ |
| 49 | + f"{response}" |
| 50 | + |
| 51 | + # Use the subject as the filename (or modify as needed to ensure uniqueness) |
| 52 | + filename = safe_filename(row['Subject']) + ".md" |
| 53 | + filepath = os.path.join(base_dir, filename) |
| 54 | + |
| 55 | + # Save the formatted content to a Markdown file |
| 56 | + with open(filepath, 'w', encoding='utf-8') as file: |
| 57 | + file.write(markdown_content) |
| 58 | + |
| 59 | + print(f"Saved response to {filepath}") |
| 60 | + |
| 61 | +async def main(): |
| 62 | + csv_file_path = os.path.expanduser("~/Downloads/101ways.csv") |
| 63 | + df = pd.read_csv(csv_file_path) |
| 64 | + |
| 65 | + # Determine the base directory from the CSV file path |
| 66 | + base_dir = os.path.dirname(csv_file_path) |
| 67 | + |
| 68 | + # Process and save responses for each row |
| 69 | + tasks = [process_and_save_response(row, base_dir) for _, row in df.iterrows()] |
| 70 | + await asyncio.gather(*tasks) |
| 71 | + |
| 72 | +if __name__ == "__main__": |
| 73 | + asyncio.run(main()) |
0 commit comments