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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -58
app.py CHANGED
@@ -11,12 +11,11 @@ with open("system_instructions.txt", "r", encoding="utf-8") as f:
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 = {
@@ -26,10 +25,7 @@ def score_qa(question, answer):
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
  }
@@ -46,85 +42,79 @@ def score_qa(question, answer):
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)
56
- for r in reader:
57
- rows.append(r)
58
 
59
  results = []
60
  total_score = 0
61
 
62
  for r in rows:
63
- q_num = r.get("question_number", "")
64
- question = r.get("question", "")
65
- answer = r.get("answer", "")
66
-
67
- sc = score_qa(question, answer)
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)
75
  writer.writeheader()
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()
 
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"
15
 
16
  def score_qa(question, answer):
17
  """Query DeepSeek API to get a score for Q&A pair"""
18
  try:
 
19
  prompt = ECO_PROMPT.format(question=question, answer=answer)
20
 
21
  headers = {
 
25
 
26
  payload = {
27
  "model": "deepseek-chat",
28
+ "messages": [{"role": "user", "content": prompt}],
 
 
 
29
  "temperature": 0.1,
30
  "max_tokens": 5
31
  }
 
42
  return 1
43
 
44
  def judge_ecolinguistics_from_csv(csv_file):
45
+ """Process CSV and return results"""
 
 
 
46
  rows = []
47
  with open(csv_file.name, "r", encoding="utf-8") as f:
48
  reader = csv.DictReader(f)
49
+ rows = list(reader)
 
50
 
51
  results = []
52
  total_score = 0
53
 
54
  for r in rows:
55
+ sc = score_qa(r.get("question", ""), r.get("answer", ""))
 
 
 
 
56
  total_score += sc
57
+ results.append({"question_number": r.get("question_number", ""), "score": sc})
58
 
 
59
  with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".csv", encoding="utf-8") as out_file:
60
+ writer = csv.DictWriter(out_file, fieldnames=["question_number", "score"])
 
61
  writer.writeheader()
62
+ writer.writerows(results)
 
63
  writer.writerow({"question_number": "Total", "score": total_score})
64
  out_path = out_file.name
65
 
66
+ percentage = (total_score / (len(rows) * 5)) * 100 if rows else 0.0
 
 
67
  percentage_display = f"""
68
+ <div style="padding: 20px; background: #f0fff4; border-radius: 10px; margin-top: 20px;">
69
+ <h3 style="color: #22543d; margin: 0;">Overall Score: {percentage:.1f}%</h3>
 
 
 
 
 
 
 
 
70
  </div>
71
  """
72
 
73
  return out_path, percentage_display
74
 
75
+ # Custom theme
76
  custom_theme = gr.themes.Default().set(
77
+ body_background_fill="#f0fff4",
78
+ button_primary_background_fill="#38a169",
79
+ button_primary_text_color="#ffffff",
80
  )
81
 
82
  with gr.Blocks(theme=custom_theme) as demo:
83
+ # Header with logo and title
 
 
 
 
 
 
 
 
 
84
  with gr.Row():
85
+ gr.Image("logo.png",
86
+ show_label=False,
87
+ width=200,
88
+ height=200,
89
+ elem_id="logo",
90
+ show_download_button=False)
91
+
92
+ gr.Markdown("""
93
+ <div style="margin-left: 20px;">
94
+ <h1 style="margin-bottom: 0; color: #22543d;">🌿 EcoLingua</h1>
95
+ <p style="margin-top: 0.5em; color: #38a169; font-size: 1.1em;">
96
+ Sustainable Communication Evaluator
97
+ </p>
98
+ </div>
99
+ """)
100
+
101
+ # Main interface
102
+ with gr.Column():
103
+ gr.Markdown("## 📤 Upload Q&A CSV")
104
+ with gr.Row():
105
+ csv_input = gr.File(label="Upload your CSV", file_types=[".csv"])
106
+ csv_output = gr.File(label="Download Results", interactive=False)
107
+
108
  html_output = gr.HTML()
109
+
110
+ csv_input.change(
111
+ judge_ecolinguistics_from_csv,
112
+ inputs=csv_input,
113
+ outputs=[csv_output, html_output]
114
+ )
115
+
116
+ # Footer
117
+ gr.Markdown("---\n*System powered by DeepSeek AI | Sustainable communication analysis*")
118
 
119
  if __name__ == "__main__":
120
  demo.launch()