Update app.py
Browse files
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 |
-
|
| 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="
|
| 16 |
|
| 17 |
class AlfredAgent:
|
| 18 |
def __init__(self):
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 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=
|
| 42 |
-
add_base_tools=
|
| 43 |
-
|
| 44 |
system_prompt=CUSTOM_SYSTEM_PROMPT
|
| 45 |
)
|
| 46 |
|
| 47 |
def __call__(self, question: str) -> str:
|
| 48 |
try:
|
| 49 |
-
#
|
| 50 |
-
|
| 51 |
-
result = self.agent.run(full_prompt)
|
| 52 |
-
|
| 53 |
-
# İlk temizlik
|
| 54 |
ans = str(result).strip()
|
| 55 |
|
| 56 |
-
#
|
| 57 |
-
if
|
| 58 |
-
ans = ans.split("Final Answer:")
|
| 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 |
-
|
| 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
|
| 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("#
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
)
|
| 217 |
-
|
| 218 |
-
|
| 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]
|