neovalle commited on
Commit
3022b90
·
verified ·
1 Parent(s): 9daaf08

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -76
app.py CHANGED
@@ -1,77 +1,45 @@
1
- import gradio as gr
2
- import csv
3
- import re
4
- import tempfile
5
- import os
6
- import requests
7
-
8
- # 1. Load system prompt from a file
9
- with open("system_instructions.txt", "r", encoding="utf-8") as f:
10
- ECO_PROMPT = f.read()
11
-
12
- # DeepSeek API configuration
13
- DEEPSEEK_API_KEY = os.environ.get("DEEPSEEK_API_KEY")
14
- DEEPSEEK_API_URL = "https://api.deepseek.com/v1/chat/completions" # Verify actual API endpoint
15
-
16
- def score_qa(question, answer):
17
- """Query DeepSeek API to get a score for Q&A pair"""
18
- try:
19
- # Format the prompt using our template
20
- prompt = ECO_PROMPT.format(question=question, answer=answer)
21
-
22
- headers = {
23
- "Authorization": f"Bearer {DEEPSEEK_API_KEY}",
24
- "Content-Type": "application/json"
25
- }
26
-
27
- payload = {
28
- "model": "deepseek-chat", # Verify correct model name
29
- "messages": [{
30
- "role": "user",
31
- "content": prompt
32
- }],
33
- "temperature": 0.1, # More deterministic output for scoring
34
- "max_tokens": 5
35
- }
36
-
37
- response = requests.post(DEEPSEEK_API_URL, json=payload, headers=headers)
38
- response.raise_for_status()
39
-
40
- # Parse response (adjust based on actual API response structure)
41
- output = response.json()['choices'][0]['message']['content']
42
-
43
- # Extract score using same logic as before
44
- match = re.search(r"\d+", output)
45
- return int(match.group(0)) if match else 1
46
-
47
- except Exception as e:
48
- print(f"API Error: {str(e)}")
49
- return 1 # Fallback score on error
50
-
51
  def judge_ecolinguistics_from_csv(csv_file):
52
- """Existing CSV processing function remains the same"""
53
- # [Keep the existing implementation exactly as you have it]
54
- # ... (same file processing logic)
55
- # ... (same CSV writing logic)
56
- # ... (same percentage calculation)
57
-
58
- return out_path, percentage_display
59
-
60
- # [Keep the Gradio interface configuration exactly as is]
61
- demo = gr.Interface(
62
- fn=judge_ecolinguistics_from_csv,
63
- inputs=gr.File(label="Upload CSV with question_number, question, answer"),
64
- outputs=[
65
- gr.File(label="Download scored CSV"),
66
- gr.HTML(label="Percentage Score")
67
- ],
68
- title="Ecolinguistics Q&A Scoring",
69
- description=(
70
- "Upload a CSV with columns [question_number, question, answer]. "
71
- "DeepSeek scores each answer from 0–5, then shows a final "
72
- "percentage score. A detailed CSV with individual scores is provided."
73
- )
74
- )
75
-
76
- if __name__ == "__main__":
77
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  def judge_ecolinguistics_from_csv(csv_file):
2
+ """
3
+ Reads CSV of Q&A pairs, scores each,
4
+ returns a new CSV and a percentage score.
5
+ """
6
+ # Read the CSV
7
+ rows = []
8
+ with open(csv_file.name, "r", encoding="utf-8") as f:
9
+ reader = csv.DictReader(f)
10
+ for r in reader:
11
+ rows.append(r)
12
+
13
+ results = []
14
+ total_score = 0
15
+
16
+ # Score each Q&A
17
+ for r in rows:
18
+ q_num = r.get("question_number", "")
19
+ question = r.get("question", "")
20
+ answer = r.get("answer", "")
21
+
22
+ sc = score_qa(question, answer)
23
+ total_score += sc
24
+ results.append({"question_number": q_num, "score": sc})
25
+
26
+ # Write results to a new CSV in a temp file
27
+ with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".csv", encoding="utf-8") as out_file:
28
+ fieldnames = ["question_number", "score"]
29
+ writer = csv.DictWriter(out_file, fieldnames=fieldnames)
30
+ writer.writeheader()
31
+ for row in results:
32
+ writer.writerow(row)
33
+ writer.writerow({"question_number": "Total", "score": total_score})
34
+ out_path = out_file.name # This is the critical line
35
+
36
+ # Compute % score: total / (num_questions * 5) * 100
37
+ num_questions = len(rows)
38
+ if num_questions > 0:
39
+ percentage = (total_score / (num_questions * 5)) * 100
40
+ else:
41
+ percentage = 0.0
42
+
43
+ percentage_display = f"<h2 style='font-size:2em; color:blue;'>Overall Score: {percentage:.1f}%</h2>"
44
+
45
+ return out_path, percentage_display # Now out_path is defined