beyzapehlivan commited on
Commit
af175c6
·
verified ·
1 Parent(s): 08647f9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -73
app.py CHANGED
@@ -2,73 +2,43 @@ import os
2
  import gradio as gr
3
  import requests
4
  import pandas as pd
5
- # ADDED BY BEYZAPEHLIVAN
6
  from smolagents import CodeAgent, HfApiModel, DuckDuckGoSearchTool, VisitWebpageTool
7
- # (Keep Constants as is)
8
  # --- Constants ---
9
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
10
-
11
- # --- Basic Agent Definition ---
12
- # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
13
- # --- YENİ ALFRED AJANI ---
14
  token = os.getenv("HF_TOKEN")
15
- model = HfApiModel(model_id="Qwen/Qwen2.5-72B-Instruct", token=token)
16
 
17
  class AlfredAgent:
18
  def __init__(self):
19
- CUSTOM_SYSTEM_PROMPT = """You are a helpful and agile assistant that has access to a set of tools.
20
- For every step, you must provide your reasoning in a 'Thoughts:' section and then the tool call in a 'Code:' section.
21
-
22
- Format:
23
- Thoughts: I need to search for...
24
- Code:
25
- ```py
26
- print(web_search("question"))
27
- ```<end_action>
28
-
29
- Rules:
30
- 1. Provide ONLY the final answer value. No sentences.
31
- 2. For dates, use YYYY-MM-DD.
32
- 3. If you find the info, stop and give the answer immediately.
33
-
34
- Tools:
35
- {{managed_agents_descriptions}}
36
- {{authorized_imports}}"""
37
 
38
  self.agent = CodeAgent(
39
  tools=[VisitWebpageTool(), DuckDuckGoSearchTool()],
40
  model=model,
41
- max_steps=10,
42
- add_base_tools=False,
43
- planning_interval=2,
44
  system_prompt=CUSTOM_SYSTEM_PROMPT
45
  )
46
 
47
  def __call__(self, question: str) -> str:
48
  try:
49
- # Soruya net kural ekliyoruz
50
- full_prompt = f"Solve this GAIA task precisely: {question}. Return ONLY the value."
51
- result = self.agent.run(full_prompt)
52
-
53
- # İlk temizlik
54
  ans = str(result).strip()
55
 
56
- # Eğer model "Final Answer: 1928" dediyse sadece 1928'i al
57
- if "Final Answer:" in ans:
58
- ans = ans.split("Final Answer:")[-1].strip()
59
-
60
- # Kelime bazlı temizlik (gereksiz ekleri atar)
61
- for word in ["is:", "answer:", "result:", "the answer is"]:
62
- if word in ans.lower():
63
- ans = ans.lower().split(word)[-1].strip()
64
-
65
- # Hala çok uzunsa (muhtemelen paragraf yazdı), sadece en son satırı/kelimeyi al
66
- if len(ans) > 60:
67
- ans = ans.split('\n')[-1].strip(" .\"'")
68
-
69
  return ans
70
- except Exception:
71
- # Hata anında bile "None" yerine "Unknown" dönerek formatı koruyoruz
72
  return "Unknown"
73
 
74
  def run_and_submit_all( profile: gr.OAuthProfile | None):
@@ -121,7 +91,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
121
  print(f"An unexpected error occurred fetching questions: {e}")
122
  return f"An unexpected error occurred fetching questions: {e}", None
123
 
124
- # 3. Run your Agent (Bu kısmı bul ve değiştir)
125
  results_log = []
126
  answers_payload = []
127
 
@@ -198,31 +168,27 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
198
 
199
 
200
  # --- Build Gradio Interface using Blocks ---
201
- with gr.Blocks() as demo:
202
- gr.Markdown("# Basic Agent Evaluation Runner")
203
- gr.Markdown(
204
- """
205
- **Instructions:**
206
-
207
- 1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
208
- 2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
209
- 3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
210
-
211
- ---
212
- **Disclaimers:**
213
- Once clicking on the "submit button, it can take quite some time ( this is the time for the agent to go through all the questions).
214
- This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution. For instance for the delay process of the submit button, a solution could be to cache the answers and submit in a seperate action or even to answer the questions in async.
215
- """
216
- )
217
-
218
- gr.LoginButton()
219
-
220
- run_button = gr.Button("Run Evaluation & Submit All Answers")
221
-
222
- status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
223
- # Removed max_rows=10 from DataFrame constructor
224
- results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
225
 
 
226
  run_button.click(
227
  fn=run_and_submit_all,
228
  outputs=[status_output, results_table]
 
2
  import gradio as gr
3
  import requests
4
  import pandas as pd
 
5
  from smolagents import CodeAgent, HfApiModel, DuckDuckGoSearchTool, VisitWebpageTool
6
+
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
  class AlfredAgent:
13
  def __init__(self):
14
+ # %30 BARACI İÇİN KRİTİK PROMPT
15
+ CUSTOM_SYSTEM_PROMPT = """You are a world-class GAIA solver.
16
+ 1. Use web_search for facts.
17
+ 2. Use visit_webpage for specific URLs or deep reading.
18
+ 3. If you find multiple numbers, verify which one exactly answers the question.
19
+ 4. Your final output must be ONLY the result (e.g., '15', 'Paris', '2022-03-01'). No explanations."""
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  self.agent = CodeAgent(
22
  tools=[VisitWebpageTool(), DuckDuckGoSearchTool()],
23
  model=model,
24
+ max_steps=15, # Soruları yarım bırakmaması için artırdık
25
+ add_base_tools=True,
26
+ additional_authorized_imports=['requests', 'bs4', 'pandas', 'json', 'math'],
27
  system_prompt=CUSTOM_SYSTEM_PROMPT
28
  )
29
 
30
  def __call__(self, question: str) -> str:
31
  try:
32
+ # Planlı düşünme ve çözme
33
+ result = self.agent.run(f"Carefully solve this task and give a concise final answer: {question}")
 
 
 
34
  ans = str(result).strip()
35
 
36
+ # Gereksiz kalabalığı temizleyen final filtresi
37
+ if len(ans) > 50:
38
+ ans = ans.split('\n')[-1].replace("Final Answer:", "").strip(" .\"'")
 
 
 
 
 
 
 
 
 
 
39
  return ans
40
+ except Exception as e:
41
+ print(f"Agent Error: {e}")
42
  return "Unknown"
43
 
44
  def run_and_submit_all( profile: gr.OAuthProfile | None):
 
91
  print(f"An unexpected error occurred fetching questions: {e}")
92
  return f"An unexpected error occurred fetching questions: {e}", None
93
 
94
+ # 3. Run your Agent
95
  results_log = []
96
  answers_payload = []
97
 
 
168
 
169
 
170
  # --- Build Gradio Interface using Blocks ---
171
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
172
+ gr.Markdown("# 🦉 Alfred GAIA Solver - Sertifika Takip Paneli")
173
+
174
+ with gr.Row():
175
+ with gr.Column(scale=1):
176
+ gr.LoginButton()
177
+ # Sadece TEK bir buton tanımlıyoruz
178
+ run_button = gr.Button("🚀 Sınavı Başlat ve Gönder", variant="primary")
179
+
180
+ with gr.Column(scale=2):
181
+ # Sadece TEK bir sonuç kutusu
182
+ status_output = gr.Textbox(label="📊 Güncel Skor ve Durum", lines=5)
183
+
184
+ # Alfred'in ne yaptığını görmek istersen bu kalsın, istemezsen silebilirsin.
185
+ # Ama şu an fonksiyonumuz 2 çıktı verdiği için bunu 'outputs' listesine eklemiyoruz.
186
+ agent_logs = gr.Textbox(label="🧠 Alfred'in İşlem Akışı", lines=10, interactive=False, visible=False)
187
+
188
+ # Sadece TEK bir tablo
189
+ results_table = gr.DataFrame(label="📝 Cevaplanan Sorular")
 
 
 
 
 
190
 
191
+ # TEK BİR TIKLAMA OLAYI (Senin fonksiyonun 2 veri döndürdüğü için 2 çıktı yazdık)
192
  run_button.click(
193
  fn=run_and_submit_all,
194
  outputs=[status_output, results_table]