File size: 1,264 Bytes
c7033f4
 
 
 
 
 
 
 
 
 
 
a363ce5
c7033f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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()