Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import openai | |
| import os | |
| # Load your API key from environment (make sure it's set in the Hugging Face Secrets) | |
| openai.api_key = os.environ.get("OPENAI_API_KEY") | |
| # Optional: you can define this as a reusable variable if you plan to expand the prompt later | |
| def generate_feedback(user_input): | |
| try: | |
| response = openai.ChatCompletion.create( | |
| model="gpt-5-mini", # Required for compatibility | |
| messages=[ | |
| {"role": "system", "content": "You are a helpful writing coach."}, | |
| {"role": "user", "content": f"Here is a student’s answer: {user_input}\nGive 2–3 sentences of thoughtful, supportive feedback."} | |
| ] | |
| ) | |
| return response.choices[0].message['content'] | |
| except Exception as e: | |
| print("Error during OpenAI call:", e) | |
| return f"Error: {str(e)}" | |
| # Build the UI | |
| with gr.Blocks() as demo: | |
| gr.Markdown("## ✍️ AI Feedback System\nEnter your writing below to receive helpful feedback.") | |
| input_text = gr.Textbox(lines=10, label="Your Writing") | |
| output_text = gr.Textbox(label="AI Feedback") | |
| submit_btn = gr.Button("Get Feedback") | |
| submit_btn.click(generate_feedback, inputs=input_text, outputs=output_text) | |
| demo.launch() |