mohdadrian commited on
Commit
ab71633
·
verified ·
1 Parent(s): 0e4f97b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -0
app.py CHANGED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, InferenceClientModel, tool
4
+
5
+ @tool
6
+ def calculator(operation: str) -> str:
7
+ """
8
+ Performs basic math calculations.
9
+
10
+ Args:
11
+ operation: A math expression like "2 + 2" or "10 * 5"
12
+
13
+ Returns:
14
+ The result of the calculation as a string
15
+ """
16
+ try:
17
+ allowed_chars = set('0123456789+-*/(). ')
18
+ if all(c in allowed_chars for c in operation):
19
+ result = eval(operation)
20
+ return f"The result of {operation} is: {result}"
21
+ else:
22
+ return "Invalid operation. Please use only numbers and basic operators."
23
+ except Exception as e:
24
+ return f"Could not calculate. Error: {str(e)}"
25
+
26
+ @tool
27
+ def get_current_time() -> str:
28
+ """
29
+ Gets the current date and time in UTC.
30
+
31
+ Returns:
32
+ The current date and time as a string
33
+ """
34
+ from datetime import datetime
35
+ now = datetime.utcnow()
36
+ return f"Current date and time (UTC): {now.strftime('%Y-%m-%d %H:%M:%S')}"
37
+
38
+ model = InferenceClientModel(
39
+ model_id="Qwen/Qwen2.5-72B-Instruct",
40
+ token=os.environ.get("HF_TOKEN"),
41
+ provider="hf-inference"
42
+ )
43
+
44
+ alfred = CodeAgent(
45
+ tools=[
46
+ DuckDuckGoSearchTool(),
47
+ calculator,
48
+ get_current_time,
49
+ ],
50
+ model=model,
51
+ max_steps=5,
52
+ )
53
+
54
+ def chat_with_alfred(message, history):
55
+ if not message.strip():
56
+ return "Please ask me something. How may I assist you?"
57
+
58
+ try:
59
+ response = alfred.run(message)
60
+ return f"Alfred: {str(response)}"
61
+ except Exception as e:
62
+ return f"I encountered an issue: {str(e)}"
63
+
64
+ with gr.Blocks(title="Alfred - AI Butler Agent") as demo:
65
+
66
+ gr.Markdown(
67
+ """
68
+ # Alfred - Your AI Butler
69
+
70
+ I can help you with:
71
+ - Web searches
72
+ - Calculations
73
+ - Current time
74
+ """
75
+ )
76
+
77
+ chatbot = gr.ChatInterface(
78
+ fn=chat_with_alfred,
79
+ examples=[
80
+ "What is artificial intelligence?",
81
+ "Calculate 25 * 4 + 10",
82
+ "What time is it now?",
83
+ ],
84
+ )
85
+
86
+ if __name__ == "__main__":
87
+ demo.launch()