FinnNick commited on
Commit
80601e2
·
verified ·
1 Parent(s): 6e31989

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -81
app.py CHANGED
@@ -1,120 +1,99 @@
1
  import os
2
  import gradio as gr
3
  import requests
4
- import inspect
5
- import pandas as pd
6
  import time
 
7
 
8
- # (Keep Constants as is)
9
  # --- Constants ---
10
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
11
 
12
- # --- Basic Agent Definition ---
13
- # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
14
- class BasicAgent:
15
  def __init__(self):
16
- self.api_key = os.getenv("GEMINI_API_KEY") # Убедись, что ключ установлен в переменных окружения
17
- self.api_url = "https://generativelanguage.googleapis.com/v1/models/gemini-pro:generateContent?key=" + self.api_key
18
  if not self.api_key:
19
- raise ValueError(" Отсутствует переменная окружения GEMINI_API_KEY")
20
- print("✅ Gemini Agent initialized.")
 
21
 
22
  def __call__(self, question: str) -> str:
23
  headers = {
24
  "Content-Type": "application/json"
25
  }
26
-
27
  payload = {
28
- "contents": [
29
- {
30
- "role": "user",
31
- "parts": [{"text": question}]
32
- }
33
- ]
34
  }
35
 
36
- for attempt in range(3): # Повтор до 3 раз
37
- try:
38
- response = requests.post(self.api_url, headers=headers, json=payload, timeout=30)
39
-
40
- if response.status_code == 429:
41
- wait_time = 5 * (attempt + 1)
42
- print(f"⚠️ Попытка {attempt+1}: 429 Too Many Requests. Ждём {wait_time} сек...")
43
- time.sleep(wait_time)
44
- continue
45
-
46
- if response.status_code == 400:
47
- print("Ошибка 400. Ответ:", response.text)
48
- return "Ошибка 400: Неверный формат запроса"
49
-
50
- response.raise_for_status()
51
-
52
- data = response.json()
53
- return data["candidates"][0]["content"]["parts"][0]["text"]
54
-
55
- except Exception as e:
56
- print("❌ Ошибка Gemini API:", e)
57
- return f"Ошибка Gemini API: {e}"
58
-
59
- return "❌ Не удалось получить ответ от Gemini API после нескольких попыток."
60
-
61
 
 
62
  def run_and_submit_all(profile: gr.OAuthProfile | None):
63
  space_id = os.getenv("SPACE_ID")
64
-
65
  if profile:
66
- username = f"{profile.username}"
67
- print(f"User logged in: {username}")
68
  else:
69
- print("User not logged in.")
70
- return "Please Login to Hugging Face with the button.", None
71
 
72
- api_url = DEFAULT_API_URL
73
- questions_url = f"{api_url}/questions"
74
- submit_url = f"{api_url}/submit"
75
 
76
  try:
77
- agent = BasicAgent()
78
  except Exception as e:
79
  return f"Error initializing agent: {e}", None
80
 
81
- agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
82
- print(agent_code)
83
-
84
- print(f"Fetching questions from: {questions_url}")
85
  try:
86
  response = requests.get(questions_url, timeout=15)
87
  response.raise_for_status()
88
  questions_data = response.json()
89
- if not questions_data:
90
- return "Fetched questions list is empty or invalid format.", None
91
- print(f"Fetched {len(questions_data)} questions.")
92
  except Exception as e:
93
  return f"Error fetching questions: {e}", None
94
 
95
- results_log = []
96
  answers_payload = []
97
- print(f"Running agent on {len(questions_data)} questions...")
98
 
99
  for item in questions_data:
100
  task_id = item.get("task_id")
101
- question_text = item.get("question")
102
- if not task_id or question_text is None:
103
  continue
 
104
  try:
105
- submitted_answer = agent(question_text)
106
- answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
107
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
108
  except Exception as e:
109
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
110
 
111
- time.sleep(3) # ⏳ Задержка между запросами
112
 
113
  if not answers_payload:
114
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
115
 
116
- submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
117
- print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
 
 
 
 
118
 
119
  try:
120
  response = requests.post(submit_url, json=submission_data, timeout=60)
@@ -123,26 +102,29 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
123
  final_status = (
124
  f"Submission Successful!\n"
125
  f"User: {result_data.get('username')}\n"
126
- f"Overall Score: {result_data.get('score', 'N/A')}% "
127
- f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
128
- f"Message: {result_data.get('message', 'No message received.')}"
129
  )
130
  return final_status, pd.DataFrame(results_log)
131
  except Exception as e:
132
  return f"Submission Failed: {e}", pd.DataFrame(results_log)
133
 
134
-
135
  # --- Gradio UI ---
136
  with gr.Blocks() as demo:
137
- gr.Markdown("# Gemini Agent Evaluation (Unit 4)")
138
- gr.Markdown("Log in to Hugging Face and submit your answers to the GAIA benchmark.")
139
-
 
 
 
140
  gr.LoginButton()
141
- run_button = gr.Button("Run Evaluation & Submit All Answers")
142
- status_output = gr.Textbox(label="Status", lines=5, interactive=False)
143
- results_table = gr.DataFrame(label="Results", wrap=True)
144
 
145
- run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
146
 
147
  if __name__ == "__main__":
148
- demo.launch(debug=True)
 
 
1
  import os
2
  import gradio as gr
3
  import requests
 
 
4
  import time
5
+ import pandas as pd
6
 
 
7
  # --- Constants ---
8
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
9
 
10
+ # --- Gemini Agent ---
11
+ class GeminiAgent:
 
12
  def __init__(self):
13
+ self.api_key = os.getenv("GEMINI_API_KEY")
 
14
  if not self.api_key:
15
+ raise ValueError("GEMINI_API_KEY environment variable not set.")
16
+ self.api_url = f"https://generativelanguage.googleapis.com/v1/models/gemini-1.5-pro:generateContent?key={self.api_key}"
17
+ print("GeminiAgent initialized.")
18
 
19
  def __call__(self, question: str) -> str:
20
  headers = {
21
  "Content-Type": "application/json"
22
  }
 
23
  payload = {
24
+ "contents": [{
25
+ "role": "user",
26
+ "parts": [{"text": question}]
27
+ }]
 
 
28
  }
29
 
30
+ try:
31
+ response = requests.post(self.api_url, headers=headers, json=payload)
32
+ if response.status_code == 429:
33
+ print("Rate limit hit. Waiting 3 seconds before retrying...")
34
+ time.sleep(3)
35
+ response = requests.post(self.api_url, headers=headers, json=payload)
36
+
37
+ response.raise_for_status()
38
+ data = response.json()
39
+ return data['candidates'][0]['content']['parts'][0]['text']
40
+ except Exception as e:
41
+ print(f"Ошибка при вызове Gemini API: {e}")
42
+ return f"ERROR: {e}"
 
 
 
 
 
 
 
 
 
 
 
 
43
 
44
+ # --- Evaluation and Submission ---
45
  def run_and_submit_all(profile: gr.OAuthProfile | None):
46
  space_id = os.getenv("SPACE_ID")
 
47
  if profile:
48
+ username = profile.username
 
49
  else:
50
+ return "Please login with Hugging Face to continue.", None
 
51
 
52
+ questions_url = f"{DEFAULT_API_URL}/questions"
53
+ submit_url = f"{DEFAULT_API_URL}/submit"
54
+ agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
55
 
56
  try:
57
+ agent = GeminiAgent()
58
  except Exception as e:
59
  return f"Error initializing agent: {e}", None
60
 
61
+ # Fetch questions
 
 
 
62
  try:
63
  response = requests.get(questions_url, timeout=15)
64
  response.raise_for_status()
65
  questions_data = response.json()
 
 
 
66
  except Exception as e:
67
  return f"Error fetching questions: {e}", None
68
 
69
+ # Answer questions
70
  answers_payload = []
71
+ results_log = []
72
 
73
  for item in questions_data:
74
  task_id = item.get("task_id")
75
+ question = item.get("question")
76
+ if not task_id or question is None:
77
  continue
78
+
79
  try:
80
+ answer = agent(question)
81
+ answers_payload.append({"task_id": task_id, "submitted_answer": answer})
82
+ results_log.append({"Task ID": task_id, "Question": question, "Submitted Answer": answer})
83
  except Exception as e:
84
+ results_log.append({"Task ID": task_id, "Question": question, "Submitted Answer": f"ERROR: {e}"})
85
 
86
+ time.sleep(2) # ⏳ delay to avoid rate limits
87
 
88
  if not answers_payload:
89
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
90
 
91
+ # Submit answers
92
+ submission_data = {
93
+ "username": username,
94
+ "agent_code": agent_code,
95
+ "answers": answers_payload
96
+ }
97
 
98
  try:
99
  response = requests.post(submit_url, json=submission_data, timeout=60)
 
102
  final_status = (
103
  f"Submission Successful!\n"
104
  f"User: {result_data.get('username')}\n"
105
+ f"Score: {result_data.get('score', 'N/A')}% "
106
+ f"({result_data.get('correct_count')}/{result_data.get('total_attempted')})\n"
107
+ f"Message: {result_data.get('message')}"
108
  )
109
  return final_status, pd.DataFrame(results_log)
110
  except Exception as e:
111
  return f"Submission Failed: {e}", pd.DataFrame(results_log)
112
 
 
113
  # --- Gradio UI ---
114
  with gr.Blocks() as demo:
115
+ gr.Markdown("## Gemini Agent Evaluation (Unit 4)")
116
+ gr.Markdown("""
117
+ 1. Убедитесь, что вы установили `GEMINI_API_KEY` как переменную окружения.
118
+ 2. Нажмите кнопку авторизации.
119
+ 3. Нажмите **Run Evaluation** для запуска и отправки.
120
+ """)
121
  gr.LoginButton()
122
+ run_btn = gr.Button("Run Evaluation & Submit All Answers")
123
+ status = gr.Textbox(label="Status")
124
+ table = gr.DataFrame(label="Answers Log", wrap=True)
125
 
126
+ run_btn.click(fn=run_and_submit_all, outputs=[status, table])
127
 
128
  if __name__ == "__main__":
129
+ print("Launching Gemini Agent Evaluation...")
130
+ demo.launch()