Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import datetime | |
| import openai | |
| import os | |
| openai.api_key = os.getenv("OPENAI_API_KEY") | |
| def generate_story(age, reading_age, gender_pronouns, situation, additional_notes): | |
| today = datetime.date.today().strftime("%d %B %Y") | |
| prompt = f""" | |
| Create a social story suitable for a student aged {age} with a reading age of {reading_age}. | |
| Use British English and age-appropriate vocabulary. | |
| Refer to the student using '{gender_pronouns}'. | |
| The story is to help prepare the student for: {situation}. | |
| Use reassuring, inclusive language and structure the story clearly. | |
| {additional_notes if additional_notes else ""} | |
| Use placeholder [student name] wherever a name would normally appear. | |
| """ | |
| try: | |
| response = openai.ChatCompletion.create( | |
| model="gpt-4", | |
| messages=[ | |
| {"role": "system", "content": "You are a teacher writing simple, clear, and supportive social stories for children with SEND."}, | |
| {"role": "user", "content": prompt} | |
| ], | |
| temperature=0.7 | |
| ) | |
| story = response['choices'][0]['message']['content'] | |
| return f"**Date:** {today}\n\n{story}" | |
| except Exception as e: | |
| return f"Error generating story: {e}" | |
| interface = gr.Interface( | |
| fn=generate_story, | |
| inputs=[ | |
| gr.Number(label="Student's Age", value=10), | |
| gr.Number(label="Reading Age", value=8), | |
| gr.Radio(["he/him", "she/her", "they/them"], label="Pronouns"), | |
| gr.Textbox(lines=2, label="What is the situation you're preparing them for?"), | |
| gr.Textbox(lines=2, label="Any extra information (optional)") | |
| ], | |
| outputs="markdown", | |
| title="Tower Hamlets Social Story Generator", | |
| description="This tool creates age-appropriate social stories in British English to support children with SEND. Names are anonymised." | |
| ) | |
| if __name__ == "__main__": | |
| interface.launch() |