Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,60 +1,40 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from smolagents import CodeAgent, DuckDuckGoSearchTool,
|
| 3 |
-
from llama_cpp import Llama
|
| 4 |
from datetime import datetime
|
| 5 |
-
from huggingface_hub import hf_hub_download
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
filename="Llama-3.2-3B-Instruct-Q4_K_M.gguf"
|
| 12 |
-
)
|
| 13 |
-
|
| 14 |
-
# Initialize the local LLM engine
|
| 15 |
-
llm = Llama(model_path=model_path, n_ctx=2048, verbose=False)
|
| 16 |
-
|
| 17 |
-
# Custom wrapper to make llama-cpp compatible with smolagents
|
| 18 |
-
class LocalGGUFModel:
|
| 19 |
-
def __call__(self, messages, stop_sequences=None):
|
| 20 |
-
# Convert messages to a string prompt
|
| 21 |
-
prompt = ""
|
| 22 |
-
for msg in messages:
|
| 23 |
-
prompt += f"{msg['role']}: {msg['content']}\n"
|
| 24 |
-
prompt += "assistant: "
|
| 25 |
-
|
| 26 |
-
response = llm(
|
| 27 |
-
prompt,
|
| 28 |
-
stop=["user:", "assistant:"] + (stop_sequences or []),
|
| 29 |
-
max_tokens=500
|
| 30 |
-
)
|
| 31 |
-
return response["choices"][0]["text"]
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
def
|
| 35 |
-
"""
|
| 36 |
-
|
|
|
|
| 37 |
|
| 38 |
-
#
|
| 39 |
-
# DuckDuckGoSearchTool handles "visiting websites" and "news"
|
| 40 |
agent = CodeAgent(
|
| 41 |
-
tools=[
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
| 43 |
add_base_tools=True
|
| 44 |
)
|
| 45 |
|
| 46 |
-
#
|
| 47 |
-
def
|
| 48 |
-
#
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
msg.submit(chat_fn, [chatbot, msg], [chatbot, msg])
|
| 59 |
|
| 60 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel
|
|
|
|
| 3 |
from datetime import datetime
|
|
|
|
| 4 |
|
| 5 |
+
# 1. Setup the "Brain"
|
| 6 |
+
# This uses the HF API so you don't have to build llama-cpp.
|
| 7 |
+
# It's faster and handles tools much better.
|
| 8 |
+
model = HfApiModel(model_id="meta-llama/Llama-3.2-3B-Instruct")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# 2. Define Custom Tools
|
| 11 |
+
def get_current_info():
|
| 12 |
+
"""A tool that fetches the exact current date and time."""
|
| 13 |
+
now = datetime.now()
|
| 14 |
+
return f"Current Date: {now.strftime('%A, %B %d, %Y')}\nTime: {now.strftime('%H:%M:%S')}"
|
| 15 |
|
| 16 |
+
# 3. Create the Agent
|
|
|
|
| 17 |
agent = CodeAgent(
|
| 18 |
+
tools=[
|
| 19 |
+
DuckDuckGoSearchTool(), # For News and Website visiting
|
| 20 |
+
get_current_info # For Current Date
|
| 21 |
+
],
|
| 22 |
+
model=model,
|
| 23 |
add_base_tools=True
|
| 24 |
)
|
| 25 |
|
| 26 |
+
# 4. Gradio Interface Setup
|
| 27 |
+
def interact(message, history):
|
| 28 |
+
# This runs the agentic logic: Search -> Think -> Answer
|
| 29 |
+
result = agent.run(message)
|
| 30 |
+
return str(result)
|
| 31 |
+
|
| 32 |
+
demo = gr.ChatInterface(
|
| 33 |
+
fn=interact,
|
| 34 |
+
title="🚀 Super-Smart AI Agent",
|
| 35 |
+
description="I can visit websites, check the news, and I know exactly what day it is.",
|
| 36 |
+
examples=["What's the latest news in tech today?", "What is the date today?", "Visit google.com and tell me what's trending."]
|
| 37 |
+
)
|
|
|
|
| 38 |
|
| 39 |
+
if __name__ == "__main__":
|
| 40 |
+
demo.launch()
|