DarkMindForever commited on
Commit
b9890ed
·
verified ·
1 Parent(s): 4d2bdaa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -50
app.py CHANGED
@@ -1,60 +1,40 @@
1
  import gradio as gr
2
- from smolagents import CodeAgent, DuckDuckGoSearchTool, Tool, LiteLLMModel
3
- from llama_cpp import Llama
4
  from datetime import datetime
5
- from huggingface_hub import hf_hub_download
6
 
7
- # --- 1. SETUP THE GGUF MODEL ---
8
- # We use a 3B model that fits well in standard HF Space RAM
9
- model_path = hf_hub_download(
10
- repo_id="bartowski/Llama-3.2-3B-Instruct-GGUF",
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
- # --- 2. DEFINE CUSTOM TOOLS ---
34
- def get_current_time_tool():
35
- """Returns the current date and time."""
36
- return f"Today is {datetime.now().strftime('%A, %B %d, %Y')}. The current time is {datetime.now().strftime('%H:%M:%S')}."
 
37
 
38
- # --- 3. INITIALIZE THE AGENT ---
39
- # DuckDuckGoSearchTool handles "visiting websites" and "news"
40
  agent = CodeAgent(
41
- tools=[DuckDuckGoSearchTool(), get_current_time_tool],
42
- model=LocalGGUFModel(),
 
 
 
43
  add_base_tools=True
44
  )
45
 
46
- # --- 4. GRADIO INTERFACE ---
47
- def chat_fn(history, message):
48
- # Process the user request through the agent
49
- response = agent.run(message)
50
- history.append({"role": "user", "content": message})
51
- history.append({"role": "assistant", "content": str(response)})
52
- return history, ""
53
-
54
- with gr.Blocks() as demo:
55
- gr.Markdown("# 🤖 GGUF Smart Agent")
56
- chatbot = gr.Chatbot(type="messages")
57
- msg = gr.Textbox(placeholder="Ask me for news or the date...")
58
- msg.submit(chat_fn, [chatbot, msg], [chatbot, msg])
59
 
60
- demo.launch()
 
 
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()