Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,35 +1,53 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import openai
|
| 3 |
import os
|
|
|
|
|
|
|
| 4 |
from langchain.prompts import PromptTemplate
|
| 5 |
|
| 6 |
-
# Load your API key from environment (
|
| 7 |
-
|
|
|
|
|
|
|
| 8 |
|
|
|
|
| 9 |
prompt = PromptTemplate(
|
| 10 |
input_variables=["student_input"],
|
| 11 |
-
template=
|
|
|
|
|
|
|
|
|
|
| 12 |
)
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
with gr.Blocks() as demo:
|
| 28 |
gr.Markdown("## ✍️ AI Feedback System\nEnter your writing to receive helpful feedback.")
|
| 29 |
-
|
|
|
|
| 30 |
output_text = gr.Textbox(label="AI Feedback")
|
| 31 |
-
submit_btn = gr.Button("Get Feedback")
|
| 32 |
|
| 33 |
-
submit_btn.click(generate_feedback, inputs=input_text, outputs=output_text)
|
| 34 |
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from openai import OpenAI
|
| 4 |
from langchain.prompts import PromptTemplate
|
| 5 |
|
| 6 |
+
# Load your API key from environment (e.g., in Hugging Face Spaces "Secrets")
|
| 7 |
+
# The OpenAI client will read OPENAI_API_KEY automatically, but we allow explicit passing too.
|
| 8 |
+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 9 |
+
client = OpenAI(api_key=OPENAI_API_KEY) if OPENAI_API_KEY else OpenAI()
|
| 10 |
|
| 11 |
+
# Build the prompt
|
| 12 |
prompt = PromptTemplate(
|
| 13 |
input_variables=["student_input"],
|
| 14 |
+
template=(
|
| 15 |
+
"You are a helpful writing coach. "
|
| 16 |
+
"Give supportive, specific, and actionable feedback on the following student writing:\n\n{student_input}"
|
| 17 |
+
),
|
| 18 |
)
|
| 19 |
|
| 20 |
+
def generate_feedback(student_input: str) -> str:
|
| 21 |
+
"""Call OpenAI Chat Completions with a safe, friendly prompt."""
|
| 22 |
+
text = (student_input or "").strip()
|
| 23 |
+
if not text:
|
| 24 |
+
return "Please paste some writing first 🙂"
|
| 25 |
+
|
| 26 |
+
full_prompt = prompt.format(student_input=text)
|
| 27 |
+
|
| 28 |
+
try:
|
| 29 |
+
resp = client.chat.completions.create(
|
| 30 |
+
model="gpt-4o-mini", # Use a current, lightweight model
|
| 31 |
+
temperature=0.4,
|
| 32 |
+
messages=[
|
| 33 |
+
{"role": "system", "content": "You are a helpful writing coach."},
|
| 34 |
+
{"role": "user", "content": full_prompt},
|
| 35 |
+
],
|
| 36 |
+
)
|
| 37 |
+
return resp.choices[0].message.content.strip()
|
| 38 |
+
except Exception as e:
|
| 39 |
+
# Simple, user-friendly error message (don’t leak internals)
|
| 40 |
+
return f"Oops—couldn't generate feedback: {e}"
|
| 41 |
+
|
| 42 |
+
# Gradio UI
|
| 43 |
with gr.Blocks() as demo:
|
| 44 |
gr.Markdown("## ✍️ AI Feedback System\nEnter your writing to receive helpful feedback.")
|
| 45 |
+
with gr.Row():
|
| 46 |
+
input_text = gr.Textbox(lines=12, label="Your Writing", placeholder="Paste your text here…")
|
| 47 |
output_text = gr.Textbox(label="AI Feedback")
|
| 48 |
+
submit_btn = gr.Button("Get Feedback", variant="primary")
|
| 49 |
|
| 50 |
+
submit_btn.click(fn=generate_feedback, inputs=input_text, outputs=output_text)
|
| 51 |
|
| 52 |
+
# Enable request queueing (recommended for Spaces) and launch
|
| 53 |
+
demo.queue().launch()
|