Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,11 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
| 5 |
-
import
|
| 6 |
|
| 7 |
from huggingface_hub import InferenceClient
|
| 8 |
|
| 9 |
-
from smolagents import CodeAgent, DuckDuckGoSearchTool, InferenceClientModel
|
| 10 |
-
|
| 11 |
# --- Constants ---
|
| 12 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 13 |
|
|
@@ -72,24 +70,22 @@ SYSTEM_PROMPT = (
|
|
| 72 |
|
| 73 |
class BasicAgent:
|
| 74 |
"""
|
| 75 |
-
Agente simples
|
| 76 |
-
para responder as questões do GAIA.
|
| 77 |
-
|
| 78 |
-
Não usa ferramentas externas (search), mas é estável no Space.
|
| 79 |
"""
|
| 80 |
|
| 81 |
def __init__(self):
|
| 82 |
-
print("Initializing Simple GAIA Agent with
|
| 83 |
|
| 84 |
hf_token = os.getenv("HF_TOKEN")
|
| 85 |
if not hf_token:
|
| 86 |
raise ValueError(
|
| 87 |
-
"HF_TOKEN not found!
|
| 88 |
)
|
| 89 |
|
| 90 |
-
#
|
| 91 |
self.client = InferenceClient(
|
| 92 |
-
model="
|
| 93 |
token=hf_token,
|
| 94 |
)
|
| 95 |
|
|
@@ -106,28 +102,40 @@ class BasicAgent:
|
|
| 106 |
def __call__(self, question: str) -> str:
|
| 107 |
print(f"\n=== NEW QUESTION ===\n{question}\n")
|
| 108 |
|
| 109 |
-
|
| 110 |
-
self.system_instructions
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
try:
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
max_new_tokens=64,
|
| 121 |
temperature=0.1,
|
| 122 |
top_p=0.9,
|
| 123 |
-
stop_sequences=["\n"], # para não vir um parágrafo gigante
|
| 124 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
print("RAW MODEL OUTPUT:", repr(raw))
|
| 126 |
final = clean_answer(raw)
|
| 127 |
print("CLEANED ANSWER:", repr(final))
|
| 128 |
return final
|
|
|
|
| 129 |
except Exception as e:
|
| 130 |
-
print("ERROR calling InferenceClient:", e)
|
| 131 |
return ""
|
| 132 |
|
| 133 |
# =========================================================
|
|
|
|
| 1 |
import os
|
| 2 |
+
import re
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
| 5 |
+
import gradio as gr
|
| 6 |
|
| 7 |
from huggingface_hub import InferenceClient
|
| 8 |
|
|
|
|
|
|
|
| 9 |
# --- Constants ---
|
| 10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 11 |
|
|
|
|
| 70 |
|
| 71 |
class BasicAgent:
|
| 72 |
"""
|
| 73 |
+
Agente simples usando InferenceClient.chat_completion
|
| 74 |
+
para responder as questões do GAIA em modo conversacional.
|
|
|
|
|
|
|
| 75 |
"""
|
| 76 |
|
| 77 |
def __init__(self):
|
| 78 |
+
print("Initializing Simple GAIA Agent with chat_completion...")
|
| 79 |
|
| 80 |
hf_token = os.getenv("HF_TOKEN")
|
| 81 |
if not hf_token:
|
| 82 |
raise ValueError(
|
| 83 |
+
"HF_TOKEN not found! Crie um Secret chamado HF_TOKEN em Settings → Variables."
|
| 84 |
)
|
| 85 |
|
| 86 |
+
# Modelo que sabemos ser suportado como 'conversational'
|
| 87 |
self.client = InferenceClient(
|
| 88 |
+
model="Qwen/Qwen2.5-72B-Instruct", # o mesmo que a infra mostrou no log
|
| 89 |
token=hf_token,
|
| 90 |
)
|
| 91 |
|
|
|
|
| 102 |
def __call__(self, question: str) -> str:
|
| 103 |
print(f"\n=== NEW QUESTION ===\n{question}\n")
|
| 104 |
|
| 105 |
+
messages = [
|
| 106 |
+
{"role": "system", "content": self.system_instructions},
|
| 107 |
+
{
|
| 108 |
+
"role": "user",
|
| 109 |
+
"content": (
|
| 110 |
+
question
|
| 111 |
+
+ "\n\nRemember: reply ONLY with the final answer, nothing else."
|
| 112 |
+
),
|
| 113 |
+
},
|
| 114 |
+
]
|
| 115 |
|
| 116 |
try:
|
| 117 |
+
completion = self.client.chat_completion(
|
| 118 |
+
messages=messages,
|
| 119 |
+
max_tokens=64,
|
|
|
|
| 120 |
temperature=0.1,
|
| 121 |
top_p=0.9,
|
|
|
|
| 122 |
)
|
| 123 |
+
|
| 124 |
+
# compatível com os dois formatos (.message["content"] ou .message.content)
|
| 125 |
+
choice = completion.choices[0]
|
| 126 |
+
message = choice.message
|
| 127 |
+
if isinstance(message, dict):
|
| 128 |
+
raw = message.get("content", "")
|
| 129 |
+
else:
|
| 130 |
+
raw = getattr(message, "content", "")
|
| 131 |
+
|
| 132 |
print("RAW MODEL OUTPUT:", repr(raw))
|
| 133 |
final = clean_answer(raw)
|
| 134 |
print("CLEANED ANSWER:", repr(final))
|
| 135 |
return final
|
| 136 |
+
|
| 137 |
except Exception as e:
|
| 138 |
+
print("ERROR calling InferenceClient.chat_completion:", e)
|
| 139 |
return ""
|
| 140 |
|
| 141 |
# =========================================================
|