MADtoBAD commited on
Commit
6be76b8
·
verified ·
1 Parent(s): fa1a3e8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +208 -170
app.py CHANGED
@@ -2,75 +2,125 @@ import os
2
  import gradio as gr
3
  import requests
4
  import pandas as pd
5
- from transformers import pipeline
6
- import torch
7
- import re
 
 
 
8
 
9
  # --- Константы ---
10
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
11
 
12
- class DiagnosticAgent:
 
13
  def __init__(self):
14
- print("DiagnosticAgent initializing...")
15
-
16
- def test_api_connection(self, api_url: str) -> dict:
17
- """Тестирует соединение с API и возвращает статус"""
18
- results = {}
19
 
20
- # Тестируем базовый URL
21
  try:
22
- response = requests.get(api_url, timeout=10)
23
- results['base_url'] = {
24
- 'status_code': response.status_code,
25
- 'headers': dict(response.headers),
26
- 'content_preview': response.text[:500] if response.text else 'No content'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  except Exception as e:
29
- results['base_url'] = {'error': str(e)}
 
 
 
 
 
 
 
30
 
31
- # Тестируем различные возможные endpoints
32
- endpoints = [
33
- '/questions',
34
- '/api/questions',
35
- '/v1/questions',
36
- '/get-questions',
37
- '/'
38
- ]
39
 
40
- for endpoint in endpoints:
41
- try:
42
- test_url = f"{api_url}{endpoint}"
43
- response = requests.get(test_url, timeout=10)
44
- results[endpoint] = {
45
- 'status_code': response.status_code,
46
- 'content_preview': response.text[:200] if response.text else 'No content'
47
- }
48
- except Exception as e:
49
- results[endpoint] = {'error': str(e)}
50
 
51
- return results
 
 
 
 
52
 
 
 
 
 
 
 
53
  def __call__(self, question: str) -> str:
54
- # Простой агент для тестирования
55
- return f"Test answer for: {question}"
56
-
57
- def diagnose_api():
58
- """Функция для диагностики API"""
59
- agent = DiagnosticAgent()
60
- results = agent.test_api_connection(DEFAULT_API_URL)
61
-
62
- print("=== API DIAGNOSTICS ===")
63
- for endpoint, result in results.items():
64
- print(f"\n{endpoint}:")
65
- for key, value in result.items():
66
- print(f" {key}: {value}")
67
-
68
- return results
69
 
70
- # --- Основная функция с улучшенной обработкой ошибок ---
71
  def run_and_submit_all(profile: gr.OAuthProfile | None):
72
  """
73
- Fetches all questions, runs the Agent on them, submits all answers,
74
  and displays the results.
75
  """
76
  space_id = os.getenv("SPACE_ID")
@@ -83,60 +133,16 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
83
  return "Please Login to Hugging Face with the button.", None
84
 
85
  api_url = DEFAULT_API_URL
86
-
87
- # Сначала выполним диагностику
88
- print("🔍 Running API diagnostics...")
89
- diagnostic_results = diagnose_api()
90
-
91
- # Пробуем разные возможные endpoints для вопросов
92
- possible_question_endpoints = [
93
- '/questions',
94
- '/api/questions',
95
- '/v1/questions',
96
- '/get-questions',
97
- '/'
98
- ]
99
-
100
- questions_data = None
101
- used_endpoint = None
102
-
103
- for endpoint in possible_question_endpoints:
104
- questions_url = f"{api_url}{endpoint}"
105
- print(f"Trying endpoint: {questions_url}")
106
-
107
- try:
108
- response = requests.get(questions_url, timeout=30)
109
- if response.status_code == 200:
110
- questions_data = response.json()
111
- used_endpoint = endpoint
112
- print(f"✅ Success with endpoint: {endpoint}")
113
- break
114
- else:
115
- print(f"❌ Endpoint {endpoint} returned status: {response.status_code}")
116
- except Exception as e:
117
- print(f"❌ Error with endpoint {endpoint}: {e}")
118
-
119
- if questions_data is None:
120
- error_msg = f"""
121
- 🚨 Could not fetch questions from any endpoint!
122
-
123
- Diagnostic results:
124
- {diagnostic_results}
125
 
126
- Possible solutions:
127
- 1. Check if the API URL is correct: {DEFAULT_API_URL}
128
- 2. The API might be temporarily down
129
- 3. The endpoint might have changed
130
- 4. There might be network restrictions
131
-
132
- Please check the assignment instructions for the correct API URL.
133
- """
134
- print(error_msg)
135
- return error_msg, None
136
-
137
- # Instantiate Agent
138
  try:
139
- agent = DiagnosticAgent()
 
 
 
 
140
  except Exception as e:
141
  print(f"Error instantiating agent: {e}")
142
  return f"Error initializing agent: {e}", None
@@ -144,9 +150,32 @@ Please check the assignment instructions for the correct API URL.
144
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
145
  print(f"Agent code URL: {agent_code}")
146
 
147
- print(f"Fetched {len(questions_data)} questions from endpoint: {used_endpoint}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
148
 
149
- # Run your Agent
150
  results_log = []
151
  answers_payload = []
152
  print(f"Running agent on {len(questions_data)} questions...")
@@ -159,7 +188,7 @@ Please check the assignment instructions for the correct API URL.
159
  print(f"Skipping item with missing task_id or question: {item}")
160
  continue
161
 
162
- print(f"🔍 Processing question {i+1}/{len(questions_data)}")
163
 
164
  try:
165
  submitted_answer = agent(question_text)
@@ -173,94 +202,103 @@ Please check the assignment instructions for the correct API URL.
173
  print("Agent did not produce any answers to submit.")
174
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
175
 
176
- # Prepare Submission
177
  submission_data = {
178
  "username": username.strip(),
179
  "agent_code": agent_code,
180
  "answers": answers_payload
181
  }
182
 
183
- status_update = f"✅ Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
184
  print(status_update)
185
 
186
- # Try different submit endpoints
187
- possible_submit_endpoints = [
188
- '/submit',
189
- '/api/submit',
190
- '/v1/submit',
191
- '/post-answers'
192
- ]
193
-
194
- result_data = None
195
-
196
- for endpoint in possible_submit_endpoints:
197
- submit_url = f"{api_url}{endpoint}"
198
- print(f"Trying submit endpoint: {submit_url}")
199
-
200
  try:
201
  response = requests.post(submit_url, json=submission_data, timeout=120)
202
- if response.status_code == 200:
203
- result_data = response.json()
204
- print(f"✅ Submit successful with endpoint: {endpoint}")
205
- break
206
- else:
207
- print(f" Submit endpoint {endpoint} returned status: {response.status_code}")
 
 
 
 
 
 
 
 
208
  except Exception as e:
209
- print(f"❌ Error with submit endpoint {endpoint}: {e}")
210
-
211
- if result_data is None:
212
- error_message = "❌ Submission Failed: Could not submit to any endpoint"
213
- print(error_message)
 
 
 
 
 
 
 
 
 
 
214
  results_df = pd.DataFrame(results_log)
215
- return error_message, results_df
216
-
217
- final_status = (
218
- f"🎉 Submission Successful!\n"
219
- f"👤 User: {result_data.get('username')}\n"
220
- f"📊 Overall Score: {result_data.get('score', 'N/A')}% "
221
- f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
222
- f"💬 Message: {result_data.get('message', 'No message received.')}"
223
- )
224
- print("✅ Submission successful.")
225
- results_df = pd.DataFrame(results_log)
226
- return final_status, results_df
227
 
228
  # --- Gradio Interface ---
229
- with gr.Blocks() as demo:
230
- gr.Markdown("# 🔍 API Diagnostic Agent")
231
  gr.Markdown("""
232
- This agent will diagnose API connectivity issues and try multiple endpoints.
 
233
 
234
- **If you're getting 404 errors:**
235
- 1. Check if the API URL is correct
236
- 2. Try using VPN if there are network restrictions
237
- 3. Check the assignment instructions for updated URLs
238
- """)
239
 
240
- gr.Markdown(f"**Current API URL:** `{DEFAULT_API_URL}`")
 
 
 
 
 
 
241
 
242
- # Добавляем кнопку для диагностики
243
- diagnose_btn = gr.Button("🔍 Run API Diagnostics", variant="secondary")
244
- diagnostic_output = gr.Textbox(label="Diagnostic Results", lines=10, interactive=False)
 
 
 
245
 
246
- gr.LoginButton()
247
- run_button = gr.Button("🚀 Run Evaluation & Submit All Answers", variant="primary")
 
 
248
 
249
- status_output = gr.Textbox(label="📊 Run Status / Submission Result", lines=5, interactive=False)
250
- results_table = gr.DataFrame(label="📝 Questions and Agent Answers", wrap=True)
 
 
 
 
 
 
 
 
 
 
251
 
252
- # Обработчик для диагностики
253
- diagnose_btn.click(
254
- fn=lambda: str(diagnose_api()),
255
- outputs=[diagnostic_output]
256
- )
257
-
258
- # Обработчик для основной функции
259
  run_button.click(
260
  fn=run_and_submit_all,
261
  outputs=[status_output, results_table]
262
  )
263
 
264
  if __name__ == "__main__":
265
- print("🚀 Launching Diagnostic Agent Interface...")
266
  demo.launch(debug=True, share=False)
 
2
  import gradio as gr
3
  import requests
4
  import pandas as pd
5
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel
6
+ import logging
7
+
8
+ # Настройка логирования
9
+ logging.basicConfig(level=logging.INFO)
10
+ logger = logging.getLogger(__name__)
11
 
12
  # --- Константы ---
13
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
14
 
15
+ # --- Internet-Enabled Agent Definition ---
16
+ class InternetAgent:
17
  def __init__(self):
18
+ print("🌐 InternetAgent initializing with web search capabilities...")
 
 
 
 
19
 
 
20
  try:
21
+ # Используем модель от Hugging Face и инструмент поиска
22
+ self.model = HfApiModel("Qwen/Qwen2.5-Coder-32B-Instruct")
23
+ self.search_tool = DuckDuckGoSearchTool()
24
+
25
+ # Создаем агента с доступом к поиску
26
+ self.agent = CodeAgent(
27
+ tools=[self.search_tool],
28
+ model=self.model,
29
+ max_steps=6, # Ограничиваем шаги для скорости
30
+ add_base_tools=False # Используем только наши инструменты
31
+ )
32
+ print("✅ InternetAgent initialized successfully with web search")
33
+
34
+ except Exception as e:
35
+ print(f"❌ Error initializing InternetAgent: {e}")
36
+ self.agent = None
37
+ # Резервная база знаний на случай ошибки
38
+ self.fallback_knowledge = {
39
+ "capital of france": "Paris",
40
+ "capital of germany": "Berlin",
41
+ "capital of uk": "London",
42
+ "capital of usa": "Washington D.C.",
43
+ "2+2": "4",
44
+ "largest planet": "Jupiter",
45
  }
46
+
47
+ def __call__(self, question: str) -> str:
48
+ print(f"🤖 Processing: {question}")
49
+
50
+ if not self.agent:
51
+ # Используем резервную базу знаний если агент не инициализирован
52
+ question_lower = question.lower()
53
+ for key, answer in self.fallback_knowledge.items():
54
+ if key in question_lower:
55
+ return answer
56
+ return "I need internet access to answer this question properly."
57
+
58
+ try:
59
+ # Создаем оптимизированный промпт для лучших результатов
60
+ optimized_prompt = f"""
61
+ Please provide a clear, concise, and accurate answer to the following question.
62
+ If you need to search for information, use the search tool.
63
+ Keep your answer brief and to the point.
64
+
65
+ Question: {question}
66
+
67
+ Answer:
68
+ """
69
+
70
+ # Запускаем агента
71
+ response = self.agent.run(optimized_prompt)
72
+
73
+ # Очищаем ответ
74
+ clean_response = self.clean_response(response)
75
+ print(f"✅ Answer: {clean_response[:100]}...")
76
+
77
+ return clean_response
78
+
79
  except Exception as e:
80
+ print(f"❌ Error in agent execution: {e}")
81
+ return f"I encountered an error while searching for the answer: {str(e)}"
82
+
83
+ def clean_response(self, response: str) -> str:
84
+ """Очищает ответ от лишней информации"""
85
+ # Удаляем мета-комментарии агента
86
+ lines = response.split('\n')
87
+ clean_lines = []
88
 
89
+ for line in lines:
90
+ # Пропускаем строки с инструментами или процессами
91
+ if any(term in line.lower() for term in ['tool:', 'searching', 'step', 'using tool']):
92
+ continue
93
+ # Пропускаем пустые строки в начале
94
+ if not clean_lines and not line.strip():
95
+ continue
96
+ clean_lines.append(line)
97
 
98
+ clean_response = '\n'.join(clean_lines).strip()
 
 
 
 
 
 
 
 
 
99
 
100
+ # Если ответ слишком длинный, берем первую часть
101
+ if len(clean_response) > 500:
102
+ clean_response = clean_response[:497] + "..."
103
+
104
+ return clean_response if clean_response else "I couldn't find a clear answer to that question."
105
 
106
+ # --- Упрощенная версия для тестирования ---
107
+ class LiteInternetAgent:
108
+ def __init__(self):
109
+ print("🌐 LiteInternetAgent initializing...")
110
+ self.search_tool = DuckDuckGoSearchTool()
111
+
112
  def __call__(self, question: str) -> str:
113
+ try:
114
+ # Прямой поиск через инструмент
115
+ result = self.search_tool(question)
116
+ return f"According to web search: {result[:300]}..." if len(result) > 300 else result
117
+ except Exception as e:
118
+ return f"Search failed: {str(e)}"
 
 
 
 
 
 
 
 
 
119
 
120
+ # --- Основная функция ---
121
  def run_and_submit_all(profile: gr.OAuthProfile | None):
122
  """
123
+ Fetches all questions, runs the InternetAgent on them, submits all answers,
124
  and displays the results.
125
  """
126
  space_id = os.getenv("SPACE_ID")
 
133
  return "Please Login to Hugging Face with the button.", None
134
 
135
  api_url = DEFAULT_API_URL
136
+ questions_url = f"{api_url}/questions"
137
+ submit_url = f"{api_url}/submit"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
 
139
+ # 1. Instantiate our Internet Agent
 
 
 
 
 
 
 
 
 
 
 
140
  try:
141
+ # Пробуем полную версию, если не работает - упрощенную
142
+ agent = InternetAgent()
143
+ if agent.agent is None:
144
+ agent = LiteInternetAgent()
145
+ print("✅ InternetAgent created successfully")
146
  except Exception as e:
147
  print(f"Error instantiating agent: {e}")
148
  return f"Error initializing agent: {e}", None
 
150
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
151
  print(f"Agent code URL: {agent_code}")
152
 
153
+ # 2. Fetch Questions
154
+ print(f"Fetching questions from: {questions_url}")
155
+ try:
156
+ response = requests.get(questions_url, timeout=30)
157
+ response.raise_for_status()
158
+ questions_data = response.json()
159
+ if not questions_data:
160
+ print("Fetched questions list is empty.")
161
+ return "Fetched questions list is empty or invalid format.", None
162
+ print(f"✅ Fetched {len(questions_data)} questions.")
163
+ except Exception as e:
164
+ error_msg = f"❌ Error fetching questions: {e}"
165
+ print(error_msg)
166
+
167
+ # Демо-режим с разными типами вопросов
168
+ demo_questions = [
169
+ {"task_id": "demo1", "question": "What is the capital of France?"},
170
+ {"task_id": "demo2", "question": "What is the current weather in Tokyo?"},
171
+ {"task_id": "demo3", "question": "Who won the Nobel Prize in Physics in 2023?"},
172
+ {"task_id": "demo4", "question": "What is the population of Brazil?"},
173
+ {"task_id": "demo5", "question": "Explain quantum computing in simple terms"},
174
+ ]
175
+ questions_data = demo_questions
176
+ print("🚨 Using demo questions since API is unavailable")
177
 
178
+ # 3. Run your Agent
179
  results_log = []
180
  answers_payload = []
181
  print(f"Running agent on {len(questions_data)} questions...")
 
188
  print(f"Skipping item with missing task_id or question: {item}")
189
  continue
190
 
191
+ print(f"🔍 Processing question {i+1}/{len(questions_data)}: {question_text[:50]}...")
192
 
193
  try:
194
  submitted_answer = agent(question_text)
 
202
  print("Agent did not produce any answers to submit.")
203
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
204
 
205
+ # 4. Prepare Submission
206
  submission_data = {
207
  "username": username.strip(),
208
  "agent_code": agent_code,
209
  "answers": answers_payload
210
  }
211
 
212
+ status_update = f"✅ Agent finished. Processed {len(answers_payload)} answers for user '{username}'"
213
  print(status_update)
214
 
215
+ # 5. Submit answers (только если вопросы реальные, не демо)
216
+ if "demo" not in str(questions_data[0].get("task_id", "")):
217
+ print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
 
 
 
 
 
 
 
 
 
 
 
218
  try:
219
  response = requests.post(submit_url, json=submission_data, timeout=120)
220
+ response.raise_for_status()
221
+ result_data = response.json()
222
+
223
+ final_status = (
224
+ f"🎉 Submission Successful!\n"
225
+ f"👤 User: {result_data.get('username')}\n"
226
+ f"📊 Overall Score: {result_data.get('score', 'N/A')}% "
227
+ f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
228
+ f"💬 Message: {result_data.get('message', 'No message received.')}"
229
+ )
230
+ print("✅ Submission successful.")
231
+ results_df = pd.DataFrame(results_log)
232
+ return final_status, results_df
233
+
234
  except Exception as e:
235
+ error_message = f"❌ Submission Failed: {str(e)}"
236
+ print(error_message)
237
+ results_df = pd.DataFrame(results_log)
238
+ return error_message, results_df
239
+ else:
240
+ # Демо-режим: показываем ответы но не отправляем
241
+ demo_status = (
242
+ f"🧪 DEMO MODE (API Unavailable)\n"
243
+ f"👤 User: {username}\n"
244
+ f"📊 Processed: {len(answers_payload)} demo questions\n"
245
+ f"🌐 Agent used web search for answers\n"
246
+ f"💬 Real submission disabled - API not accessible\n\n"
247
+ f"Check the web-powered answers below!"
248
+ )
249
+ print("✅ Demo completed - showing results without submission")
250
  results_df = pd.DataFrame(results_log)
251
+ return demo_status, results_df
 
 
 
 
 
 
 
 
 
 
 
252
 
253
  # --- Gradio Interface ---
254
+ with gr.Blocks(title="Internet-Enabled AI Agent") as demo:
 
255
  gr.Markdown("""
256
+ # 🌐 Internet-Enabled AI Agent
257
+ **Powered by web search and large language models**
258
 
259
+ ### 🔧 Capabilities:
260
+ - **Web Search**: Real-time information from DuckDuckGo
261
+ - **LLM Power**: Qwen2.5-32B model for understanding
262
+ - **Multi-step Reasoning**: Complex question answering
 
263
 
264
+ ### 📚 Example questions:
265
+ - *"Current weather in any city"*
266
+ - *"Latest news headlines"*
267
+ - *"Historical facts and data"*
268
+ - *"Scientific explanations"*
269
+ - *"Complex calculations"*
270
+ """)
271
 
272
+ gr.Markdown("""
273
+ ### ⚠️ Important Notes:
274
+ - This agent requires internet access
275
+ - Responses may take longer due to web searches
276
+ - Some questions might not have clear online answers
277
+ """)
278
 
279
+ with gr.Row():
280
+ with gr.Column():
281
+ gr.LoginButton()
282
+ run_button = gr.Button("🚀 Run Evaluation", variant="primary")
283
 
284
+ with gr.Row():
285
+ with gr.Column():
286
+ status_output = gr.Textbox(
287
+ label="Status",
288
+ lines=4,
289
+ interactive=False
290
+ )
291
+ with gr.Column():
292
+ results_table = gr.DataFrame(
293
+ label="Questions & Answers",
294
+ wrap=True
295
+ )
296
 
 
 
 
 
 
 
 
297
  run_button.click(
298
  fn=run_and_submit_all,
299
  outputs=[status_output, results_table]
300
  )
301
 
302
  if __name__ == "__main__":
303
+ print("🚀 Starting Internet-Enabled AI Agent...")
304
  demo.launch(debug=True, share=False)