Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,6 +9,8 @@ from smolagents import DuckDuckGoSearchTool
|
|
| 9 |
from smolagents import Tool
|
| 10 |
import traceback
|
| 11 |
from openai import OpenAI
|
|
|
|
|
|
|
| 12 |
# (Keep Constants as is)
|
| 13 |
# --- Constants ---
|
| 14 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
@@ -54,42 +56,36 @@ CalculatorTool=Tool(
|
|
| 54 |
)
|
| 55 |
|
| 56 |
class BasicAgent:
|
| 57 |
-
def __init__(self):
|
| 58 |
try:
|
| 59 |
-
# Load your HF token
|
| 60 |
-
hf_token = os.getenv("chatbotagenthf")
|
| 61 |
-
if not hf_token:
|
| 62 |
-
raise ValueError("HF token not found in environment variable 'chatbotagenthf'.")
|
| 63 |
-
|
| 64 |
-
# --- Connect to Hugging Face router ---
|
| 65 |
-
self.client = OpenAI(
|
| 66 |
-
base_url="https://router.huggingface.co/v1",
|
| 67 |
-
api_key=hf_token
|
| 68 |
-
)
|
| 69 |
-
|
| 70 |
-
# --- Choose any accessible instruct model ---
|
| 71 |
-
self.model_id = "moonshotai/Kimi-K2-Thinking:novita"
|
| 72 |
-
# (You can change to: mistralai/Mistral-7B-Instruct-v0.3, zephyr-7b-beta, etc.)
|
| 73 |
|
| 74 |
-
|
|
|
|
|
|
|
| 75 |
self.agent = ToolCallingAgent(
|
| 76 |
tools=[DuckDuckGoSearchTool()],
|
| 77 |
-
model=None
|
| 78 |
)
|
| 79 |
-
|
| 80 |
-
print("✅ Agent initialized successfully using Hugging Face Router.")
|
| 81 |
|
| 82 |
except Exception as e:
|
| 83 |
print(f"❌ Error initializing agent: {e}")
|
| 84 |
traceback.print_exc()
|
| 85 |
raise e
|
| 86 |
|
| 87 |
-
def __call__(self, question: str) -> str:
|
| 88 |
-
"""Run a chat completion on the HF model."""
|
| 89 |
-
try:
|
| 90 |
-
print(f"\n➡️ Agent received question: {question[:80]}")
|
| 91 |
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
response = self.client.chat.completions.create(
|
| 94 |
model=self.model_id,
|
| 95 |
messages=[
|
|
@@ -97,15 +93,16 @@ class BasicAgent:
|
|
| 97 |
{"role": "user", "content": question}
|
| 98 |
],
|
| 99 |
)
|
| 100 |
-
|
| 101 |
answer = response.choices[0].message["content"].strip()
|
| 102 |
-
print(f"✅ Agent returning answer: {answer}")
|
| 103 |
-
return answer
|
| 104 |
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
|
| 111 |
|
|
|
|
| 9 |
from smolagents import Tool
|
| 10 |
import traceback
|
| 11 |
from openai import OpenAI
|
| 12 |
+
from transformers import pipeline
|
| 13 |
+
|
| 14 |
# (Keep Constants as is)
|
| 15 |
# --- Constants ---
|
| 16 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
|
| 56 |
)
|
| 57 |
|
| 58 |
class BasicAgent:
|
| 59 |
+
def __init__(self, use_local_model=True):
|
| 60 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
print("🚀 Loading local Hugging Face model...")
|
| 63 |
+
self.pipe = pipeline("text-generation", model="WeiboAI/VibeThinker-1.5B")
|
| 64 |
+
self.model_type = "local"
|
| 65 |
self.agent = ToolCallingAgent(
|
| 66 |
tools=[DuckDuckGoSearchTool()],
|
| 67 |
+
model=None
|
| 68 |
)
|
| 69 |
+
print("✅ Agent initialized successfully.")
|
|
|
|
| 70 |
|
| 71 |
except Exception as e:
|
| 72 |
print(f"❌ Error initializing agent: {e}")
|
| 73 |
traceback.print_exc()
|
| 74 |
raise e
|
| 75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
+
def __call__(self, question: str) -> str:
|
| 78 |
+
"""Run either local HF pipeline or remote API chat."""
|
| 79 |
+
try:
|
| 80 |
+
print(f"\n➡️ Agent received question: {question[:80]}")
|
| 81 |
+
|
| 82 |
+
if getattr(self, "model_type", None) == "local":
|
| 83 |
+
# Use local model
|
| 84 |
+
messages = [{"role": "user", "content": question}]
|
| 85 |
+
response = self.pipe(messages, max_new_tokens=200)
|
| 86 |
+
answer = response[0]["generated_text"]
|
| 87 |
+
else:
|
| 88 |
+
# Use Hugging Face router (OpenAI-style)
|
| 89 |
response = self.client.chat.completions.create(
|
| 90 |
model=self.model_id,
|
| 91 |
messages=[
|
|
|
|
| 93 |
{"role": "user", "content": question}
|
| 94 |
],
|
| 95 |
)
|
|
|
|
| 96 |
answer = response.choices[0].message["content"].strip()
|
|
|
|
|
|
|
| 97 |
|
| 98 |
+
print(f"✅ Agent returning answer: {answer[:100]}...")
|
| 99 |
+
return answer
|
| 100 |
+
|
| 101 |
+
except Exception as e:
|
| 102 |
+
print(f"❌ Agent encountered an error: {e}")
|
| 103 |
+
traceback.print_exc()
|
| 104 |
+
return f"Error generating answer: {e}"
|
| 105 |
+
|
| 106 |
|
| 107 |
|
| 108 |
|