neovalle commited on
Commit
b1b2448
·
verified ·
1 Parent(s): cf3e185

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +96 -11
app.py CHANGED
@@ -1,9 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)
@@ -13,7 +59,6 @@ def judge_ecolinguistics_from_csv(csv_file):
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", "")
@@ -23,7 +68,7 @@ def judge_ecolinguistics_from_csv(csv_file):
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)
@@ -31,15 +76,55 @@ def judge_ecolinguistics_from_csv(csv_file):
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
 
 
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",
29
+ "messages": [{
30
+ "role": "user",
31
+ "content": prompt
32
+ }],
33
+ "temperature": 0.1,
34
+ "max_tokens": 5
35
+ }
36
+
37
+ response = requests.post(DEEPSEEK_API_URL, json=payload, headers=headers)
38
+ response.raise_for_status()
39
+
40
+ output = response.json()['choices'][0]['message']['content']
41
+ match = re.search(r"\d+", output)
42
+ return int(match.group(0)) if match else 1
43
+
44
+ except Exception as e:
45
+ print(f"API Error: {str(e)}")
46
+ return 1
47
+
48
  def judge_ecolinguistics_from_csv(csv_file):
49
  """
50
  Reads CSV of Q&A pairs, scores each,
51
  returns a new CSV and a percentage score.
52
  """
 
53
  rows = []
54
  with open(csv_file.name, "r", encoding="utf-8") as f:
55
  reader = csv.DictReader(f)
 
59
  results = []
60
  total_score = 0
61
 
 
62
  for r in rows:
63
  q_num = r.get("question_number", "")
64
  question = r.get("question", "")
 
68
  total_score += sc
69
  results.append({"question_number": q_num, "score": sc})
70
 
71
+ # Create temporary file with proper path handling
72
  with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".csv", encoding="utf-8") as out_file:
73
  fieldnames = ["question_number", "score"]
74
  writer = csv.DictWriter(out_file, fieldnames=fieldnames)
 
76
  for row in results:
77
  writer.writerow(row)
78
  writer.writerow({"question_number": "Total", "score": total_score})
79
+ out_path = out_file.name
80
 
 
81
  num_questions = len(rows)
82
+ percentage = (total_score / (num_questions * 5)) * 100 if num_questions > 0 else 0.0
 
 
 
83
 
84
+ percentage_display = f"""
85
+ <div style="text-align: center;">
86
+ <h2 style="color: #2c3e50; font-size: 1.5em; margin-bottom: 20px;">
87
+ Ecolinguistics Assessment Results
88
+ </h2>
89
+ <img src="https://i.imgur.com/YK3Rk4D.png" alt="Ecology Icon" style="width: 150px; margin: 20px auto;">
90
+ <div style="background-color: #f8f9fa; padding: 20px; border-radius: 10px;">
91
+ <p style="font-size: 1.2em; color: #27ae60; font-weight: bold;">
92
+ Final Score: {percentage:.1f}%
93
+ </p>
94
+ </div>
95
+ </div>
96
+ """
97
+
98
+ return out_path, percentage_display
99
+
100
+ # Custom HTML with image support
101
+ custom_theme = gr.themes.Default().set(
102
+ body_background_fill="#e8f5e9",
103
+ button_primary_background_fill="#2e7d32",
104
+ button_primary_background_fill_hover="#1b5e20",
105
+ )
106
+
107
+ with gr.Blocks(theme=custom_theme) as demo:
108
+ gr.Markdown("# 🌱 Ecolinguistics Assessment System")
109
+
110
+ # Add your uploaded image here (replace "your_image.jpg" with your actual filename)
111
+ gr.Image("banner.jpg", elem_id="banner", show_label=False, height=200)
112
+
113
+ gr.Markdown("""
114
+ ## Upload CSV for Evaluation
115
+ Your CSV should contain columns: `question_number`, `question`, and `answer`
116
+ """)
117
+
118
+ with gr.Row():
119
+ csv_input = gr.File(label="Upload CSV File", file_types=[".csv"])
120
+ csv_output = gr.File(label="Download Results")
121
+ html_output = gr.HTML()
122
+
123
+ csv_input.change(
124
+ fn=judge_ecolinguistics_from_csv,
125
+ inputs=csv_input,
126
+ outputs=[csv_output, html_output]
127
+ )
128
 
129
+ if __name__ == "__main__":
130
+ demo.launch()