beyzapehlivan commited on
Commit
6a4f6b4
·
verified ·
1 Parent(s): 68877f8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -13
app.py CHANGED
@@ -2,8 +2,8 @@ import os
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")
@@ -11,8 +11,8 @@ model = HfApiModel(model_id="meta-llama/Llama-3.1-70B-Instruct", token=token)
11
 
12
  class AlfredAgent:
13
  def __init__(self):
14
- # Yeni kurallar gereği {{managed_agents_descriptions}} ve {{authorized_imports}} ekliyoruz
15
- CUSTOM_SYSTEM_PROMPT = """You are a world-class GAIA solver.
16
 
17
  Available tools:
18
  {{managed_agents_descriptions}}
@@ -20,28 +20,53 @@ class AlfredAgent:
20
  Authorized Python imports:
21
  {{authorized_imports}}
22
 
23
- 1. Use web_search for facts.
24
- 2. Use visit_webpage for specific URLs or deep reading.
25
- 3. Your final output must be ONLY the result (e.g., '15', 'Paris'). No explanations."""
 
 
 
 
 
 
 
 
 
 
26
 
27
  self.agent = CodeAgent(
28
  tools=[VisitWebpageTool()],
29
  model=model,
30
- max_steps=15,
 
31
  add_base_tools=True,
32
- additional_authorized_imports=['requests', 'bs4', 'pandas', 'json', 'math'],
 
33
  system_prompt=CUSTOM_SYSTEM_PROMPT
34
  )
35
 
36
  def __call__(self, question: str) -> str:
37
  try:
38
- # Planlı düşünme ve çözme
39
- result = self.agent.run(f"Carefully solve this task and give a concise final answer: {question}")
 
 
 
 
 
 
 
40
  ans = str(result).strip()
41
 
42
- # Gereksiz kalabalığı temizleyen final filtresi
43
- if len(ans) > 50:
44
- ans = ans.split('\n')[-1].replace("Final Answer:", "").strip(" .\"'")
 
 
 
 
 
 
45
  return ans
46
  except Exception as e:
47
  print(f"Agent Error: {e}")
 
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")
 
11
 
12
  class AlfredAgent:
13
  def __init__(self):
14
+ # --- SES VE DOSYA STRATEJİSİ EKLENDİ ---
15
+ CUSTOM_SYSTEM_PROMPT = """You are an expert GAIA benchmark solver.
16
 
17
  Available tools:
18
  {{managed_agents_descriptions}}
 
20
  Authorized Python imports:
21
  {{authorized_imports}}
22
 
23
+ STRATEGY & RULES:
24
+ 1. AUDIO/VIDEO FILES: You cannot "listen" or "watch". Instead:
25
+ - EXTRACT the filename or URL from the question.
26
+ - USE 'web_search' to search for that specific filename/URL to find its transcript, duration, or author.
27
+ - NEVER say "I cannot listen". Say "Searching for file metadata..."
28
+
29
+ 2. CODING:
30
+ - ALWAYS write Python code in a 'Code:' block.
31
+ - NEVER write plain text answers without code.
32
+
33
+ 3. FINAL ANSWER:
34
+ - You MUST execute: print("Final Answer: [your_short_answer]")
35
+ - Answer must be concise (e.g., "14", "Paris", "John Doe"). No sentences."""
36
 
37
  self.agent = CodeAgent(
38
  tools=[VisitWebpageTool()],
39
  model=model,
40
+ # Adım sayısını 20 yaptık, ses dosyalarını aramak vakit alır
41
+ max_steps=20,
42
  add_base_tools=True,
43
+ # 're' kütüphanesi dosya isimlerini ayıklamak için kritik
44
+ additional_authorized_imports=['requests', 'bs4', 'pandas', 'json', 'math', 're'],
45
  system_prompt=CUSTOM_SYSTEM_PROMPT
46
  )
47
 
48
  def __call__(self, question: str) -> str:
49
  try:
50
+ # Soruyu verirken onu ses dosyaları konusunda uyarıyoruz
51
+ full_prompt = f"""Task: {question}
52
+
53
+ IMPORTANT: If this involves an audio/video file, do NOT try to play it.
54
+ Search for the filename on the web to find the answer.
55
+
56
+ End your code with: print("Final Answer: <your_concise_answer>")"""
57
+
58
+ result = self.agent.run(full_prompt)
59
  ans = str(result).strip()
60
 
61
+ # --- CEVAP TEMİZLEME ---
62
+ if "Final Answer:" in ans:
63
+ ans = ans.split("Final Answer:")[-1].strip()
64
+
65
+ ans = ans.strip(" .\"'")
66
+ if len(ans) > 50:
67
+ if "\n" in ans: ans = ans.split('\n')[-1]
68
+ ans = ans[:50].strip()
69
+
70
  return ans
71
  except Exception as e:
72
  print(f"Agent Error: {e}")