DenviWorking commited on
Commit
7254638
·
verified ·
1 Parent(s): e2c77e5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +214 -35
app.py CHANGED
@@ -1,45 +1,224 @@
 
1
  import gradio as gr
2
- from langchain.agents import AgentExecutor, Tool
3
- from langchain.tools import DuckDuckGoSearchRun
4
- from langchain.llms import HuggingFaceHub
5
  import requests
 
 
 
 
 
 
 
6
 
7
- # 1. Инициализация инструментов
8
- search = DuckDuckGoSearchRun()
9
- tools = [
10
- Tool(
11
- name="Search",
12
- func=search.run,
13
- description="Для поиска фактов в интернете"
14
- )
15
- ]
16
 
17
- # 2. Выбор модели (бесплатной)
18
- llm = HuggingFaceHub(
19
- repo_id="mistralai/Mistral-7B-Instruct-v0.1",
20
- huggingfacehub_api_token="ваш_API_токен" # Замените на токен из https://huggingface.co/settings/tokens
21
- )
22
 
23
- # 3. Создание агента
24
- agent = AgentExecutor.from_llm_and_tools(llm=llm, tools=tools)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
- # 4. Функция для обработки вопросов GAIA
27
- def answer_question(question):
28
  try:
29
- response = agent.run({"input": question})
30
- return response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  except Exception as e:
32
- return f"Ошибка: {str(e)}"
 
 
 
 
33
 
34
- # 5. Интерфейс Gradio
35
  with gr.Blocks() as demo:
36
- gr.Markdown("## GAIA Agent Evaluation")
37
-
38
- with gr.Row():
39
- question = gr.Textbox(label="Вопрос GAIA", lines=3)
40
- answer = gr.Textbox(label="Ответ агента", lines=3)
41
-
42
- submit_btn = gr.Button("Отправить ответ")
43
- submit_btn.click(answer_question, inputs=question, outputs=answer)
44
-
45
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
  import gradio as gr
 
 
 
3
  import requests
4
+ import pandas as pd
5
+ from smolagents import (
6
+ CodeAgent,
7
+ OpenAIServerModel,
8
+ GoogleSearchTool,
9
+ )
10
+ from tools import read_image, transcribe_audio, run_video, search_wikipedia, read_code
11
 
12
+ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
13
+ model_id = "gpt-4o-mini"
 
 
 
 
 
 
 
14
 
 
 
 
 
 
15
 
16
+ class BasicAgent:
17
+ def __init__(self, model_id=model_id):
18
+ model = OpenAIServerModel(model_id=model_id, temperature=0.1)
19
+ google_search = GoogleSearchTool()
20
+ self.agent = CodeAgent(
21
+ model=model,
22
+ tools=[
23
+ read_image,
24
+ transcribe_audio,
25
+ read_code,
26
+ run_video,
27
+ search_wikipedia,
28
+ google_search,
29
+ ],
30
+ additional_authorized_imports=["numpy", "pandas"],
31
+ max_steps=20,
32
+ )
33
+ add_sys_prompt = f"""\n\nIf a file_url is available or an url is given in question statement, then request and use the content to answer the question. \
34
+ If a code file, such as .py file, is given, do not attempt to execute it but rather open it as a text file and analyze the content. \
35
+ When a tabluar file, such as csv, tsv, xlsx, is given, read it using pandas.
36
+
37
+ Make sure you provide the answer in accordance with the instruction provided in the question. Do not return the result of tool as a final_answer.
38
+ Do Not add any additional information, explanation, unnecessary words or symbols. The answer is likely as simple as one word."""
39
+ self.agent.prompt_templates["system_prompt"] += add_sys_prompt
40
+
41
+ def __call__(self, question: str) -> str:
42
+ answer = self.agent.run(question)
43
+ return answer
44
+
45
+
46
+ def run_and_submit_all(profile: gr.OAuthProfile | None):
47
+ """
48
+ Fetches all questions, runs the BasicAgent on them, submits all answers,
49
+ and displays the results.
50
+ """
51
+ space_id = os.getenv("SPACE_ID")
52
+
53
+ if profile:
54
+ username = f"{profile.username}"
55
+ print(f"User logged in: {username}")
56
+ else:
57
+ print("User not logged in.")
58
+ return "Please Login to Hugging Face with the button.", None
59
+
60
+ api_url = DEFAULT_API_URL
61
+ questions_url = f"{api_url}/questions"
62
+ submit_url = f"{api_url}/submit"
63
+
64
+ try:
65
+ agent = BasicAgent()
66
+ except Exception as e:
67
+ print(f"Error instantiating agent: {e}")
68
+ return f"Error initializing agent: {e}", None
69
+ agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
70
+ print(agent_code)
71
+
72
+ print(f"Fetching questions from: {questions_url}")
73
+ try:
74
+ response = requests.get(questions_url, timeout=15)
75
+ response.raise_for_status()
76
+ questions_data = response.json()
77
+ if not questions_data:
78
+ print("Fetched questions list is empty.")
79
+ return "Fetched questions list is empty or invalid format.", None
80
+ print(f"Fetched {len(questions_data)} questions.")
81
+ except requests.exceptions.RequestException as e:
82
+ print(f"Error fetching questions: {e}")
83
+ return f"Error fetching questions: {e}", None
84
+ except requests.exceptions.JSONDecodeError as e:
85
+ print(f"Error decoding JSON response from questions endpoint: {e}")
86
+ print(f"Response text: {response.text[:500]}")
87
+ return f"Error decoding server response for questions: {e}", None
88
+ except Exception as e:
89
+ print(f"An unexpected error occurred fetching questions: {e}")
90
+ return f"An unexpected error occurred fetching questions: {e}", None
91
+
92
+ results_log = []
93
+ answers_payload = []
94
+ print(f"Running agent on {len(questions_data)} questions...")
95
+ for item in questions_data:
96
+ task_id = item.get("task_id")
97
+ question_text = item.get("question")
98
+ file_name = item.get("file_name")
99
+ if file_name:
100
+ file_url = f"{DEFAULT_API_URL}/files/{task_id}"
101
+ else:
102
+ file_url = "No URL provided"
103
+ extension = file_name.split(".")[-1]
104
+
105
+ question_text += f"\n\nfile_url : {file_url} \nfile_extension : {extension}"
106
+ if not task_id or question_text is None:
107
+ print(f"Skipping item with missing task_id or question: {item}")
108
+ continue
109
+ try:
110
+ submitted_answer = agent(question_text)
111
+ answers_payload.append(
112
+ {"task_id": task_id, "submitted_answer": submitted_answer}
113
+ )
114
+ results_log.append(
115
+ {
116
+ "Task ID": task_id,
117
+ "Question": question_text,
118
+ "Submitted Answer": submitted_answer,
119
+ }
120
+ )
121
+ except Exception as e:
122
+ print(f"Error running agent on task {task_id}: {e}")
123
+ results_log.append(
124
+ {
125
+ "Task ID": task_id,
126
+ "Question": question_text,
127
+ "Submitted Answer": f"AGENT ERROR: {e}",
128
+ }
129
+ )
130
+
131
+ if not answers_payload:
132
+ print("Agent did not produce any answers to submit.")
133
+ return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
134
+
135
+ submission_data = {
136
+ "username": username.strip(),
137
+ "agent_code": agent_code,
138
+ "answers": answers_payload,
139
+ }
140
+ status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
141
+ print(status_update)
142
 
143
+ print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
 
144
  try:
145
+ response = requests.post(submit_url, json=submission_data, timeout=60)
146
+ response.raise_for_status()
147
+ result_data = response.json()
148
+ final_status = (
149
+ f"Submission Successful!\n"
150
+ f"User: {result_data.get('username')}\n"
151
+ f"Overall Score: {result_data.get('score', 'N/A')}% "
152
+ f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
153
+ f"Message: {result_data.get('message', 'No message received.')}"
154
+ )
155
+ print("Submission successful.")
156
+ results_df = pd.DataFrame(results_log)
157
+ return final_status, results_df
158
+ except requests.exceptions.HTTPError as e:
159
+ error_detail = f"Server responded with status {e.response.status_code}."
160
+ try:
161
+ error_json = e.response.json()
162
+ error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
163
+ except requests.exceptions.JSONDecodeError:
164
+ error_detail += f" Response: {e.response.text[:500]}"
165
+ status_message = f"Submission Failed: {error_detail}"
166
+ print(status_message)
167
+ results_df = pd.DataFrame(results_log)
168
+ return status_message, results_df
169
+ except requests.exceptions.Timeout:
170
+ status_message = "Submission Failed: The request timed out."
171
+ print(status_message)
172
+ results_df = pd.DataFrame(results_log)
173
+ return status_message, results_df
174
+ except requests.exceptions.RequestException as e:
175
+ status_message = f"Submission Failed: Network error - {e}"
176
+ print(status_message)
177
+ results_df = pd.DataFrame(results_log)
178
+ return status_message, results_df
179
  except Exception as e:
180
+ status_message = f"An unexpected error occurred during submission: {e}"
181
+ print(status_message)
182
+ results_df = pd.DataFrame(results_log)
183
+ return status_message, results_df
184
+
185
 
 
186
  with gr.Blocks() as demo:
187
+ gr.Markdown("# Evaluation")
188
+ gr.LoginButton()
189
+
190
+ run_button = gr.Button("Run Evaluation & Submit All Answers")
191
+
192
+ status_output = gr.Textbox(
193
+ label="Run Status / Submission Result", lines=5, interactive=False
194
+ )
195
+ results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
196
+
197
+ run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
198
+
199
+ if __name__ == "__main__":
200
+ print("\n" + "-" * 30 + " App Starting " + "-" * 30)
201
+ space_host_startup = os.getenv("SPACE_HOST")
202
+ space_id_startup = os.getenv("SPACE_ID")
203
+
204
+ if space_host_startup:
205
+ print(f"✅ SPACE_HOST found: {space_host_startup}")
206
+ print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
207
+ else:
208
+ print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
209
+
210
+ if space_id_startup:
211
+ print(f"✅ SPACE_ID found: {space_id_startup}")
212
+ print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
213
+ print(
214
+ f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main"
215
+ )
216
+ else:
217
+ print(
218
+ "ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined."
219
+ )
220
+
221
+ print("-" * (60 + len(" App Starting ")) + "\n")
222
+
223
+ print("Launching Gradio Interface for Basic Agent Evaluation...")
224
+ demo.launch(debug=True, share=False)