Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,53 +1,70 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
from openai import OpenAI
|
| 4 |
-
from langchain.prompts import PromptTemplate
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
"
|
| 16 |
-
|
| 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 =
|
| 27 |
-
|
| 28 |
try:
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 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()
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
# Try the new SDK first, fall back to legacy if unavailable
|
| 5 |
+
NEW_SDK = False
|
| 6 |
+
try:
|
| 7 |
+
from openai import OpenAI # v1+
|
| 8 |
+
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 9 |
+
NEW_SDK = True
|
| 10 |
+
except Exception:
|
| 11 |
+
import openai # legacy (< v1)
|
| 12 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 13 |
|
| 14 |
+
# Optional LangChain prompt (falls back to plain string if LC not installed)
|
| 15 |
+
try:
|
| 16 |
+
from langchain.prompts import PromptTemplate
|
| 17 |
+
prompt = PromptTemplate(
|
| 18 |
+
input_variables=["student_input"],
|
| 19 |
+
template=(
|
| 20 |
+
"You are a helpful writing coach. "
|
| 21 |
+
"Give supportive, specific, and actionable feedback on the following student writing:\n\n{student_input}"
|
| 22 |
+
),
|
| 23 |
+
)
|
| 24 |
+
def build_prompt(text: str) -> str:
|
| 25 |
+
return prompt.format(student_input=text)
|
| 26 |
+
except Exception:
|
| 27 |
+
def build_prompt(text: str) -> str:
|
| 28 |
+
return (
|
| 29 |
+
"You are a helpful writing coach. "
|
| 30 |
+
"Give supportive, specific, and actionable feedback on the following student writing:\n\n" + text
|
| 31 |
+
)
|
| 32 |
|
| 33 |
def generate_feedback(student_input: str) -> str:
|
|
|
|
| 34 |
text = (student_input or "").strip()
|
| 35 |
if not text:
|
| 36 |
return "Please paste some writing first 🙂"
|
| 37 |
|
| 38 |
+
full_prompt = build_prompt(text)
|
|
|
|
| 39 |
try:
|
| 40 |
+
if NEW_SDK:
|
| 41 |
+
resp = client.chat.completions.create(
|
| 42 |
+
model=os.getenv("OPENAI_MODEL_NEW", "gpt-4o-mini"),
|
| 43 |
+
temperature=0.4,
|
| 44 |
+
messages=[
|
| 45 |
+
{"role": "system", "content": "You are a helpful writing coach."},
|
| 46 |
+
{"role": "user", "content": full_prompt},
|
| 47 |
+
],
|
| 48 |
+
)
|
| 49 |
+
return resp.choices[0].message.content.strip()
|
| 50 |
+
else:
|
| 51 |
+
resp = openai.ChatCompletion.create(
|
| 52 |
+
model=os.getenv("OPENAI_MODEL_OLD", "gpt-3.5-turbo"),
|
| 53 |
+
temperature=0.4,
|
| 54 |
+
messages=[
|
| 55 |
+
{"role": "system", "content": "You are a helpful writing coach."},
|
| 56 |
+
{"role": "user", "content": full_prompt},
|
| 57 |
+
],
|
| 58 |
+
)
|
| 59 |
+
return resp["choices"][0]["message"]["content"].strip()
|
| 60 |
except Exception as e:
|
|
|
|
| 61 |
return f"Oops—couldn't generate feedback: {e}"
|
| 62 |
|
|
|
|
| 63 |
with gr.Blocks() as demo:
|
| 64 |
gr.Markdown("## ✍️ AI Feedback System\nEnter your writing to receive helpful feedback.")
|
| 65 |
+
input_text = gr.Textbox(lines=12, label="Your Writing", placeholder="Paste your text here…")
|
|
|
|
| 66 |
output_text = gr.Textbox(label="AI Feedback")
|
| 67 |
submit_btn = gr.Button("Get Feedback", variant="primary")
|
|
|
|
| 68 |
submit_btn.click(fn=generate_feedback, inputs=input_text, outputs=output_text)
|
| 69 |
|
|
|
|
| 70 |
demo.queue().launch()
|