beyzapehlivan commited on
Commit
51cceff
·
verified ·
1 Parent(s): 30c5055

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -281
app.py CHANGED
@@ -2,49 +2,29 @@ import os
2
  import gradio as gr
3
  import requests
4
  import pandas as pd
5
- import re # <--- YENİ EKLENEN KISIM: Düzenli ifadeler için gerekli
6
- from smolagents import CodeAgent, HfApiModel, DuckDuckGoSearchTool, VisitWebpageTool
7
- # --- Constants ---
8
- DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
9
- token = os.getenv("HF_TOKEN")
10
- model = HfApiModel(model_id="meta-llama/Llama-3.1-70B-Instruct", token=token)
11
-
12
- import os
13
- import gradio as gr
14
- import requests
15
- import pandas as pd
16
- import re
17
  from smolagents import CodeAgent, HfApiModel, VisitWebpageTool
18
 
19
- # --- Constants ---
20
- DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
21
  token = os.getenv("HF_TOKEN")
22
  model = HfApiModel(model_id="meta-llama/Llama-3.1-70B-Instruct", token=token)
23
 
24
  class AlfredAgent:
25
  def __init__(self):
26
- # --- BASİT VE NET PROMPT ---
27
- # Karmaşık kuralları sildik. Modele güveniyoruz.
28
- CUSTOM_SYSTEM_PROMPT = """You are a brilliant AI capable of solving complex GAIA benchmark tasks.
 
29
 
30
- You have access to the following tools:
31
  {{managed_agents_descriptions}}
32
-
33
- Authorized Python imports:
34
  {{authorized_imports}}
35
 
36
- INSTRUCTIONS:
37
- 1. Use 'web_search' to find facts or file contents.
38
- 2. Use 'visit_webpage' to read detailed articles.
39
- 3. If you see a file (audio/video/image), search for its filename or context on the web.
40
- 4. Solve the problem step-by-step using Python code.
41
- 5. The LAST LINE of your code must evaluate to the final answer (e.g., just the variable name).
42
- 6. The final answer must be concise (e.g., '14', 'Paris')."""
43
 
44
  self.agent = CodeAgent(
45
  tools=[VisitWebpageTool()],
46
  model=model,
47
- max_steps=15, # 15 adım idealdir, fazlası kafasını karıştırıyor
48
  add_base_tools=True,
49
  additional_authorized_imports=['requests', 'bs4', 'pandas', 'json', 'math', 're', 'datetime'],
50
  system_prompt=CUSTOM_SYSTEM_PROMPT
@@ -52,274 +32,56 @@ class AlfredAgent:
52
 
53
  def __call__(self, question: str) -> str:
54
  try:
55
- # Sadece basit bir yönlendirme
56
- result = self.agent.run(f"Question: {question}")
57
- ans = str(result).strip()
58
-
59
- # --- TEMİZLİK ---
60
- # Gereksiz karakterleri temizle
61
- ans = ans.replace("Final Answer:", "").strip(" .\"'")
62
-
63
- # Eğer cevap çok uzunsa ve satır satırsa, muhtemelen son satır cevaptır
64
- if len(ans) > 50 and "\n" in ans:
65
- ans = ans.split('\n')[-1]
66
-
67
- return ans[:100] # Çok uzun cevapları kırp
68
  except Exception as e:
69
- print(f"Agent Error: {e}")
70
  return "Unknown"
71
 
72
- # --- AŞAĞISI STANDART, DEĞİŞTİRMENE GEREK YOK ---
73
  def run_and_submit_all(profile: gr.OAuthProfile | None):
74
- space_id = os.getenv("SPACE_ID")
75
- if profile is None:
76
- return "Lütfen giriş yapınız.", None
77
-
78
- username = profile.username
79
- print(f"User logged in: {username}")
80
-
81
- api_url = DEFAULT_API_URL
82
- questions_url = f"{api_url}/questions"
83
- submit_url = f"{api_url}/submit"
84
-
85
- try:
86
- agent = AlfredAgent()
87
- except Exception as e:
88
- return f"Error initializing agent: {e}", None
89
-
90
- agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
91
-
92
- try:
93
- print(f"Fetching questions from: {questions_url}")
94
- response = requests.get(questions_url, timeout=15)
95
- questions_data = response.json()
96
- except Exception as e:
97
- return f"Error fetching questions: {e}", None
98
-
99
- results_log = []
100
- answers_payload = []
101
 
102
- for item in questions_data:
103
- task_id = item.get("task_id")
104
- q_text = item.get("question")
105
- if not task_id: continue
106
-
107
- print(f"\n--- Görev: {task_id} ---")
108
- try:
109
- answer = agent(q_text)
110
- final_ans = str(answer).replace('"', '').replace("'", "").strip()
111
- if len(final_ans) > 100: final_ans = final_ans[:100] # Güvenlik önlemi
112
-
113
- print(f"Bulunan Cevap: {final_ans}")
114
- answers_payload.append({"task_id": task_id, "submitted_answer": final_ans})
115
- results_log.append({"Task ID": task_id, "Question": q_text, "Submitted Answer": final_ans})
116
- except:
117
- answers_payload.append({"task_id": task_id, "submitted_answer": "Unknown"})
118
-
119
- submission_data = {"username": username, "agent_code": agent_code, "answers": answers_payload}
120
- print(f"Submitting {len(answers_payload)} answers...")
121
-
122
- try:
123
- response = requests.post(submit_url, json=submission_data, timeout=60)
124
- result_data = response.json()
125
- final_status = (
126
- f"Submission Successful!\n"
127
- f"User: {result_data.get('username')}\n"
128
- f"Overall Score: {result_data.get('score', 'N/A')}% "
129
- f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)"
130
- )
131
- return final_status, pd.DataFrame(results_log)
132
- except Exception as e:
133
- return f"Submission Failed: {e}", pd.DataFrame(results_log)
134
-
135
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
136
- gr.Markdown("# 🦉 Alfred GAIA Solver - Basit & Güçlü")
137
- with gr.Row():
138
- with gr.Column(scale=1):
139
- gr.LoginButton()
140
- run_button = gr.Button("🚀 Başlat", variant="primary")
141
- with gr.Column(scale=2):
142
- status_output = gr.Textbox(label="Durum", lines=5)
143
- results_table = gr.DataFrame(label="Sonuçlar")
144
-
145
- run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
146
-
147
- if __name__ == "__main__":
148
- demo.launch()
149
-
150
- def run_and_submit_all( profile: gr.OAuthProfile | None):
151
- """
152
- Fetches all questions, runs the BasicAgent on them, submits all answers,
153
- and displays the results.
154
- """
155
- # --- Determine HF Space Runtime URL and Repo URL ---
156
- space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
157
-
158
- if profile:
159
- username= f"{profile.username}"
160
- print(f"User logged in: {username}")
161
- else:
162
- print("User not logged in.")
163
- return "Please Login to Hugging Face with the button.", None
164
-
165
- api_url = DEFAULT_API_URL
166
- questions_url = f"{api_url}/questions"
167
- submit_url = f"{api_url}/submit"
168
-
169
- # 1. Instantiate Agent ( modify this part to create your agent)
170
- try:
171
- agent = AlfredAgent()
172
- except Exception as e:
173
- print(f"Error instantiating agent: {e}")
174
- return f"Error initializing agent: {e}", None
175
- # In the case of an app running as a hugging Face space, this link points toward your codebase ( usefull for others so please keep it public)
176
- agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
177
- print(agent_code)
178
-
179
- # 2. Fetch Questions
180
- print(f"Fetching questions from: {questions_url}")
181
  try:
182
  response = requests.get(questions_url, timeout=15)
183
- response.raise_for_status()
184
  questions_data = response.json()
185
- if not questions_data:
186
- print("Fetched questions list is empty.")
187
- return "Fetched questions list is empty or invalid format.", None
188
- print(f"Fetched {len(questions_data)} questions.")
189
- except requests.exceptions.RequestException as e:
190
- print(f"Error fetching questions: {e}")
191
- return f"Error fetching questions: {e}", None
192
- except requests.exceptions.JSONDecodeError as e:
193
- print(f"Error decoding JSON response from questions endpoint: {e}")
194
- print(f"Response text: {response.text[:500]}")
195
- return f"Error decoding server response for questions: {e}", None
196
- except Exception as e:
197
- print(f"An unexpected error occurred fetching questions: {e}")
198
- return f"An unexpected error occurred fetching questions: {e}", None
199
 
200
- # 3. Run your Agent
201
- results_log = []
202
  answers_payload = []
 
203
 
204
  for item in questions_data:
205
  task_id = item.get("task_id")
206
- q_text = item.get("question")
207
- if not task_id: continue
208
-
209
- print(f"\n--- Görev: {task_id} ---")
210
- try:
211
- answer = agent(q_text)
212
-
213
- # Sınav formatına zorla (lowercase ve temizlik)
214
- final_ans = str(answer).replace('"', '').replace("'", "").strip()
215
-
216
- # Eğer model çok uzun bir şey döndürdüyse, GAIA bunu kabul etmez.
217
- # İlk 2-3 kelimeyi veya sayıyı almaya çalışalım.
218
- if len(final_ans) > 50:
219
- final_ans = final_ans[:47] + "..."
220
-
221
- answers_payload.append({"task_id": task_id, "submitted_answer": final_ans})
222
- results_log.append({"Task ID": task_id, "Question": q_text, "Submitted Answer": final_ans})
223
- print(f"Cevap Kaydedildi: {final_ans}")
224
- except:
225
- answers_payload.append({"task_id": task_id, "submitted_answer": "Unknown"})
226
-
227
- # 4. Prepare Submission
228
- submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
229
- status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
230
- print(status_update)
231
-
232
- # 5. Submit
233
- print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
234
- try:
235
- response = requests.post(submit_url, json=submission_data, timeout=60)
236
- response.raise_for_status()
237
- result_data = response.json()
238
- final_status = (
239
- f"Submission Successful!\n"
240
- f"User: {result_data.get('username')}\n"
241
- f"Overall Score: {result_data.get('score', 'N/A')}% "
242
- f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
243
- f"Message: {result_data.get('message', 'No message received.')}"
244
- )
245
- print("Submission successful.")
246
- results_df = pd.DataFrame(results_log)
247
- return final_status, results_df
248
- except requests.exceptions.HTTPError as e:
249
- error_detail = f"Server responded with status {e.response.status_code}."
250
- try:
251
- error_json = e.response.json()
252
- error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
253
- except requests.exceptions.JSONDecodeError:
254
- error_detail += f" Response: {e.response.text[:500]}"
255
- status_message = f"Submission Failed: {error_detail}"
256
- print(status_message)
257
- results_df = pd.DataFrame(results_log)
258
- return status_message, results_df
259
- except requests.exceptions.Timeout:
260
- status_message = "Submission Failed: The request timed out."
261
- print(status_message)
262
- results_df = pd.DataFrame(results_log)
263
- return status_message, results_df
264
- except requests.exceptions.RequestException as e:
265
- status_message = f"Submission Failed: Network error - {e}"
266
- print(status_message)
267
- results_df = pd.DataFrame(results_log)
268
- return status_message, results_df
269
- except Exception as e:
270
- status_message = f"An unexpected error occurred during submission: {e}"
271
- print(status_message)
272
- results_df = pd.DataFrame(results_log)
273
- return status_message, results_df
274
-
275
-
276
- # --- Build Gradio Interface using Blocks ---
277
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
278
- gr.Markdown("# 🦉 Alfred GAIA Solver - Sertifika Takip Paneli")
279
-
280
- with gr.Row():
281
- with gr.Column(scale=1):
282
- gr.LoginButton()
283
- # Sadece TEK bir buton tanımlıyoruz
284
- run_button = gr.Button("🚀 Sınavı Başlat ve Gönder", variant="primary")
285
 
286
- with gr.Column(scale=2):
287
- # Sadece TEK bir sonuç kutusu
288
- status_output = gr.Textbox(label="📊 Güncel Skor ve Durum", lines=5)
289
 
290
- # Alfred'in ne yaptığını görmek istersen bu kalsın, istemezsen silebilirsin.
291
- # Ama şu an fonksiyonumuz 2 çıktı verdiği için bunu 'outputs' listesine eklemiyoruz.
292
- agent_logs = gr.Textbox(label="🧠 Alfred'in İşlem Akışı", lines=10, interactive=False, visible=False)
293
-
294
- # Sadece TEK bir tablo
295
- results_table = gr.DataFrame(label="📝 Cevaplanan Sorular")
296
 
297
- # TEK BİR TIKLAMA OLAYI (Senin fonksiyonun 2 veri döndürdüğü için 2 çıktı yazdık)
298
- run_button.click(
299
- fn=run_and_submit_all,
300
- outputs=[status_output, results_table]
301
- )
 
 
 
 
 
 
 
302
 
303
  if __name__ == "__main__":
304
- print("\n" + "-"*30 + " App Starting " + "-"*30)
305
- # Check for SPACE_HOST and SPACE_ID at startup for information
306
- space_host_startup = os.getenv("SPACE_HOST")
307
- space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
308
-
309
- if space_host_startup:
310
- print(f"✅ SPACE_HOST found: {space_host_startup}")
311
- print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
312
- else:
313
- print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
314
-
315
- if space_id_startup: # Print repo URLs if SPACE_ID is found
316
- print(f"✅ SPACE_ID found: {space_id_startup}")
317
- print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
318
- print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
319
- else:
320
- print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
321
-
322
- print("-"*(60 + len(" App Starting ")) + "\n")
323
-
324
- print("Launching Gradio Interface for Basic Agent Evaluation...")
325
- demo.launch(debug=True, share=False)
 
2
  import gradio as gr
3
  import requests
4
  import pandas as pd
 
 
 
 
 
 
 
 
 
 
 
 
5
  from smolagents import CodeAgent, HfApiModel, VisitWebpageTool
6
 
7
+ # Model ve Token Ayarları
 
8
  token = os.getenv("HF_TOKEN")
9
  model = HfApiModel(model_id="meta-llama/Llama-3.1-70B-Instruct", token=token)
10
 
11
  class AlfredAgent:
12
  def __init__(self):
13
+ # SADE VE STANDART PROMPT (Kütüphanenin en iyi anladığı format)
14
+ CUSTOM_SYSTEM_PROMPT = """You are a helpful and expert AI assistant.
15
+ Solve the given task by writing Python code.
16
+ You have access to a web search tool and a tool to visit webpages.
17
 
 
18
  {{managed_agents_descriptions}}
 
 
19
  {{authorized_imports}}
20
 
21
+ If the task involves a file you cannot see, use web search to find info about it.
22
+ Always end your script with a variable that contains the final answer."""
 
 
 
 
 
23
 
24
  self.agent = CodeAgent(
25
  tools=[VisitWebpageTool()],
26
  model=model,
27
+ max_steps=15,
28
  add_base_tools=True,
29
  additional_authorized_imports=['requests', 'bs4', 'pandas', 'json', 'math', 're', 'datetime'],
30
  system_prompt=CUSTOM_SYSTEM_PROMPT
 
32
 
33
  def __call__(self, question: str) -> str:
34
  try:
35
+ # Modelin run fonksiyonu zaten en temiz cevabı döndürmeye çalışır
36
+ result = self.agent.run(question)
37
+ return str(result).strip()
 
 
 
 
 
 
 
 
 
 
38
  except Exception as e:
 
39
  return "Unknown"
40
 
41
+ # --- Gönderim ve Arayüz Fonksiyonu ---
42
  def run_and_submit_all(profile: gr.OAuthProfile | None):
43
+ if profile is None: return "Giriş gerekli.", None
44
+
45
+ questions_url = "https://agents-course-unit4-scoring.hf.space/questions"
46
+ submit_url = "https://agents-course-unit4-scoring.hf.space/submit"
47
+
48
+ agent = AlfredAgent()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  try:
51
  response = requests.get(questions_url, timeout=15)
 
52
  questions_data = response.json()
53
+ except: return "Sorular alınamadı.", None
 
 
 
 
 
 
 
 
 
 
 
 
 
54
 
 
 
55
  answers_payload = []
56
+ results_log = []
57
 
58
  for item in questions_data:
59
  task_id = item.get("task_id")
60
+ answer = agent(item.get("question"))
61
+ # Temiz ve kısa cevap formatı
62
+ clean_ans = str(answer).split('\n')[-1].replace("Final Answer:", "").strip(" .\"'")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
 
64
+ answers_payload.append({"task_id": task_id, "submitted_answer": clean_ans[:100]})
65
+ results_log.append({"Task ID": task_id, "Submitted Answer": clean_ans[:100]})
 
66
 
67
+ submission_data = {
68
+ "username": profile.username,
69
+ "agent_code": f"https://huggingface.co/spaces/{os.getenv('SPACE_ID')}/tree/main",
70
+ "answers": answers_payload
71
+ }
 
72
 
73
+ try:
74
+ res = requests.post(submit_url, json=submission_data, timeout=60).json()
75
+ return f"Skor: {res.get('score')}% ({res.get('correct_count')}/20)", pd.DataFrame(results_log)
76
+ except: return "Gönderim başarısız.", pd.DataFrame(results_log)
77
+
78
+ # Gradio Arayüzü
79
+ with gr.Blocks() as demo:
80
+ gr.LoginButton()
81
+ btn = gr.Button("Sınavı Başlat")
82
+ out = gr.Textbox(label="Sonuç")
83
+ tab = gr.DataFrame()
84
+ btn.click(run_and_submit_all, outputs=[out, tab])
85
 
86
  if __name__ == "__main__":
87
+ demo.launch()