BrontoBytez commited on
Commit
ed66e0a
·
verified ·
1 Parent(s): 2a60948

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -22
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 (make sure it's set in the Hugging Face Secrets)
7
- openai.api_key = os.environ.get("OPENAI_API_KEY")
 
 
8
 
 
9
  prompt = PromptTemplate(
10
  input_variables=["student_input"],
11
- template="You are a helpful writing coach. Give supportive feedback on the following student writing: {student_input}."
 
 
 
12
  )
13
 
14
- # Optional: you can define this as a reusable variable if you plan to expand the prompt later
15
- def generate_feedback(student_input):
16
- full_prompt = prompt.format(student_input=student_input)
17
- response = openai.ChatCompletion.create(
18
- model="gpt-3.5-turbo",
19
- messages = [
20
- {"role": "system", "content": "You are a helpful writing coach."},
21
- {"role": "user", "content": full_prompt}
22
- ]
23
- )
24
- return response.choices[0].message["content"]
25
-
26
- # Build the UI
 
 
 
 
 
 
 
 
 
 
27
  with gr.Blocks() as demo:
28
  gr.Markdown("## ✍️ AI Feedback System\nEnter your writing to receive helpful feedback.")
29
- input_text = gr.Textbox(lines=10, label="Your Writing")
 
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
- demo.launch()
 
 
 
 
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()