Spaces:
Runtime error
Runtime error
import gradio as gr | |
import openai | |
import pandas as pd | |
import os | |
openai.api_key = os.getenv("OPENAI_API_KEY") # Add this in the HF Secrets tab | |
# π Prompt Generator | |
def generate_prompt(question, solution, instructions): | |
return f""" | |
{instructions} | |
--- | |
Question: | |
{question} | |
Student's Solution: | |
{solution} | |
Evaluate the student's solution and provide feedback. | |
End your response with: | |
Is the solution correct? [Yes/No] | |
""" | |
# π§ Evaluation Function | |
def evaluate_csv(file, instructions): | |
df = pd.read_csv(file.name) | |
feedback_list = [] | |
status_list = [] | |
for idx, row in df.iterrows(): | |
q = row['question'] | |
s = row['solution'] | |
prompt = generate_prompt(q, s, instructions) | |
try: | |
response = openai.ChatCompletion.create( | |
model="gpt-4", # or gpt-3.5-turbo | |
messages=[{"role": "user", "content": prompt}], | |
temperature=0.2 | |
) | |
output = response.choices[0].message.content | |
feedback_list.append(output) | |
# Extract Yes/No from output | |
status = "Yes" if "Yes" in output.splitlines()[-1] else "No" | |
status_list.append(status) | |
except Exception as e: | |
feedback_list.append(f"Error: {e}") | |
status_list.append("Error") | |
df['AI_Feedback'] = feedback_list | |
df['Is_Correct'] = status_list | |
output_path = "evaluated_output.csv" | |
df.to_csv(output_path, index=False) | |
return output_path | |
# ποΈ Gradio UI | |
with gr.Blocks() as demo: | |
gr.Markdown("# π Solution Evaluator (CSV)") | |
gr.Markdown("Upload your CSV with `question` and `solution` columns. Provide evaluation instructions.") | |
with gr.Row(): | |
csv_input = gr.File(label="Upload CSV", file_types=[".csv"]) | |
instructions_input = gr.Textbox(lines=6, label="Evaluation Instructions") | |
output_file = gr.File(label="Download Evaluated CSV") | |
run_btn = gr.Button("Evaluate Solutions") | |
run_btn.click(fn=evaluate_csv, inputs=[csv_input, instructions_input], outputs=output_file) | |
demo.launch() |