File size: 2,090 Bytes
f4b4b3e
1a2993b
 
 
f4b4b3e
1a2993b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f4b4b3e
 
1a2993b
 
 
 
 
f4b4b3e
1a2993b
 
 
 
f4b4b3e
1a2993b
 
 
 
 
 
 
 
f4b4b3e
1a2993b
 
 
f4b4b3e
1a2993b
 
 
f4b4b3e
1a2993b
 
f4b4b3e
1a2993b
 
 
f4b4b3e
1a2993b
 
 
 
 
 
 
 
f4b4b3e
1a2993b
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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()