Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,75 +1,97 @@
|
|
1 |
import gradio as gr
|
2 |
-
import openai
|
3 |
import pandas as pd
|
4 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
openai.api_key = os.getenv("OPENAI_API_KEY") # Add this in the HF Secrets tab
|
7 |
-
|
8 |
-
# π Prompt Generator
|
9 |
-
def generate_prompt(question, solution, instructions):
|
10 |
-
return f"""
|
11 |
{instructions}
|
12 |
|
13 |
---
|
14 |
|
15 |
-
Question
|
16 |
{question}
|
17 |
|
18 |
-
Student's Solution
|
19 |
{solution}
|
20 |
|
21 |
-
Evaluate the
|
22 |
|
23 |
-
|
24 |
-
|
|
|
25 |
"""
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
try:
|
39 |
-
|
40 |
-
|
41 |
-
messages=[{"role": "user", "content": prompt}],
|
42 |
-
temperature=0.2
|
43 |
-
)
|
44 |
-
output = response.choices[0].message.content
|
45 |
-
feedback_list.append(output)
|
46 |
-
|
47 |
-
# Extract Yes/No from output
|
48 |
-
status = "Yes" if "Yes" in output.splitlines()[-1] else "No"
|
49 |
-
status_list.append(status)
|
50 |
-
|
51 |
except Exception as e:
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
df['Is_Correct'] = status_list
|
57 |
-
|
58 |
-
output_path = "evaluated_output.csv"
|
59 |
df.to_csv(output_path, index=False)
|
60 |
return output_path
|
61 |
|
62 |
-
#
|
63 |
with gr.Blocks() as demo:
|
64 |
-
gr.Markdown("
|
65 |
-
gr.Markdown("
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import pandas as pd
|
3 |
import os
|
4 |
+
import google.generativeai as genai
|
5 |
+
import fitz # PyMuPDF to read PDF
|
6 |
+
|
7 |
+
# Load Gemini API key from Hugging Face secret
|
8 |
+
genai.configure(api_key=os.getenv("AIzaSyDX-JAUDT7NB65Ry2P5ymEFlzKNohWKTlg"))
|
9 |
+
model = genai.GenerativeModel("gemini-2.0-flash")
|
10 |
+
|
11 |
+
# PDF reading utility
|
12 |
+
def extract_text_from_pdf(pdf_file):
|
13 |
+
text = ""
|
14 |
+
doc = fitz.open(pdf_file.name)
|
15 |
+
for page in doc:
|
16 |
+
text += page.get_text()
|
17 |
+
return text.strip()
|
18 |
+
|
19 |
+
# Gemini prompting logic
|
20 |
+
def evaluate_prompt(question, solution, instructions):
|
21 |
+
prompt = f"""
|
22 |
+
You are an expert math evaluator. Follow these instructions carefully:
|
23 |
|
|
|
|
|
|
|
|
|
|
|
24 |
{instructions}
|
25 |
|
26 |
---
|
27 |
|
28 |
+
**Question:**
|
29 |
{question}
|
30 |
|
31 |
+
**Student's Solution:**
|
32 |
{solution}
|
33 |
|
34 |
+
Evaluate the solution. Say whether it's correct or not. If incorrect, give a clear and detailed explanation of why it's wrong and how to fix it.
|
35 |
|
36 |
+
Respond in this format:
|
37 |
+
Correct?: [Yes/No]
|
38 |
+
Reasoning: <explanation>
|
39 |
"""
|
40 |
+
response = model.generate_content(prompt)
|
41 |
+
return response.text.strip()
|
42 |
+
|
43 |
+
# Handler for single text input
|
44 |
+
def evaluate_text_inputs(question, solution, instructions_file, instructions_text):
|
45 |
+
instructions = instructions_text or extract_text_from_pdf(instructions_file) if instructions_file else ""
|
46 |
+
result = evaluate_prompt(question, solution, instructions)
|
47 |
+
return result
|
48 |
+
|
49 |
+
# Handler for CSV batch input
|
50 |
+
def evaluate_csv(csv_file, instructions_file, instructions_text):
|
51 |
+
instructions = instructions_text or extract_text_from_pdf(instructions_file) if instructions_file else ""
|
52 |
+
df = pd.read_csv(csv_file.name)
|
53 |
+
|
54 |
+
evaluations = []
|
55 |
+
for _, row in df.iterrows():
|
56 |
+
q = row.get('question', '')
|
57 |
+
s = row.get('solution', '')
|
58 |
try:
|
59 |
+
eval_result = evaluate_prompt(q, s, instructions)
|
60 |
+
evaluations.append(eval_result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
except Exception as e:
|
62 |
+
evaluations.append(f"Error: {e}")
|
63 |
+
|
64 |
+
df['evaluation'] = evaluations
|
65 |
+
output_path = "evaluated_results.csv"
|
|
|
|
|
|
|
66 |
df.to_csv(output_path, index=False)
|
67 |
return output_path
|
68 |
|
69 |
+
# Gradio UI
|
70 |
with gr.Blocks() as demo:
|
71 |
+
gr.Markdown("## π€ Solution Evaluator (Gemini API)")
|
72 |
+
gr.Markdown("Evaluate student solutions using Google's Gemini API.")
|
73 |
+
|
74 |
+
with gr.Tab("π Single Evaluation"):
|
75 |
+
with gr.Row():
|
76 |
+
question_input = gr.Textbox(label="Question", lines=3)
|
77 |
+
solution_input = gr.Textbox(label="Solution", lines=6)
|
78 |
+
with gr.Row():
|
79 |
+
instructions_text = gr.Textbox(label="Instructions (text)", lines=6)
|
80 |
+
instructions_file = gr.File(label="Upload Instructions PDF", file_types=[".pdf"])
|
81 |
+
output_single = gr.Textbox(label="Evaluation Result", lines=8)
|
82 |
+
btn_single = gr.Button("Evaluate Solution")
|
83 |
+
btn_single.click(fn=evaluate_text_inputs,
|
84 |
+
inputs=[question_input, solution_input, instructions_file, instructions_text],
|
85 |
+
outputs=output_single)
|
86 |
+
|
87 |
+
with gr.Tab("π Batch CSV Evaluation"):
|
88 |
+
csv_file = gr.File(label="Upload CSV (columns: question, solution)", file_types=[".csv"])
|
89 |
+
inst_text = gr.Textbox(label="Instructions (text)", lines=6)
|
90 |
+
inst_pdf = gr.File(label="Upload Instructions PDF", file_types=[".pdf"])
|
91 |
+
output_csv = gr.File(label="Download Evaluated CSV")
|
92 |
+
btn_batch = gr.Button("Evaluate All")
|
93 |
+
btn_batch.click(fn=evaluate_csv,
|
94 |
+
inputs=[csv_file, inst_pdf, inst_text],
|
95 |
+
outputs=output_csv)
|
96 |
|
97 |
demo.launch()
|