ysharma HF staff commited on
Commit
3e00788
·
verified ·
1 Parent(s): 1062239

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +122 -0
app.py ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from gradio import ChatMessage
3
+ import time
4
+ import random
5
+
6
+ def calculator_tool(expression):
7
+ """Simple calculator tool"""
8
+ try:
9
+ return eval(expression)
10
+ except:
11
+ return "Error: Could not evaluate expression"
12
+
13
+ def weather_tool(location):
14
+ """Simulated weather tool"""
15
+ conditions = ["sunny", "cloudy", "rainy", "snowy"]
16
+ temp = random.randint(0, 35)
17
+ return f"{random.choice(conditions)}, {temp}°C in {location}"
18
+
19
+ def agent_with_tools(query, history):
20
+
21
+ # Initial thinking
22
+ thinking = ChatMessage(
23
+ role="assistant",
24
+ content="Let me think about this query...",
25
+ metadata={"title": "🧠 Thinking", "id": 1}
26
+ )
27
+ history.append(thinking)
28
+ yield history
29
+
30
+ # Decide which tool to use based on query
31
+ if "calculate" in query.lower() or any(op in query for op in ['+', '-', '*', '/']):
32
+ # Extract the expression (simplified for demo)
33
+ expression = query.split("calculate")[-1].strip() if "calculate" in query.lower() else query
34
+
35
+ # Show tool usage as nested thought
36
+ tool_call = ChatMessage(
37
+ role="assistant",
38
+ content=f"Expression to evaluate: {expression}",
39
+ metadata={
40
+ "title": "🧮 Calculator Tool",
41
+ "parent_id": 1,
42
+ "id": 2,
43
+ "status": "pending"
44
+ }
45
+ )
46
+ history.append(tool_call)
47
+ yield history
48
+
49
+ # Simulate calculation time
50
+ time.sleep(1)
51
+
52
+ # Get result and update tool call
53
+ result = calculator_tool(expression)
54
+ tool_call.content = f"Expression: {expression}\nResult: {result}"
55
+ tool_call.metadata["status"] = "done"
56
+ tool_call.metadata["duration"] = 0.8 # Simulated duration
57
+ yield history
58
+
59
+ # Final response
60
+ response = ChatMessage(
61
+ role="assistant",
62
+ content=f"I calculated that for you. The result is {result}."
63
+ )
64
+
65
+ elif "weather" in query.lower():
66
+ # Extract location (simplified)
67
+ location = query.split("weather in")[-1].strip() if "weather in" in query.lower() else "your location"
68
+
69
+ # Show tool usage
70
+ tool_call = ChatMessage(
71
+ role="assistant",
72
+ content=f"Checking weather for: {location}",
73
+ metadata={
74
+ "title": "🌤️ Weather Tool",
75
+ "parent_id": 1,
76
+ "id": 2,
77
+ "status": "pending"
78
+ }
79
+ )
80
+ history.append(tool_call)
81
+ yield history
82
+
83
+ # Simulate API call
84
+ time.sleep(1.5)
85
+
86
+ # Get result and update tool call
87
+ result = weather_tool(location)
88
+ tool_call.content = f"Location: {location}\nCurrent conditions: {result}"
89
+ tool_call.metadata["status"] = "done"
90
+ tool_call.metadata["duration"] = 1.2 # Simulated duration
91
+ yield history
92
+
93
+ # Final response
94
+ response = ChatMessage(
95
+ role="assistant",
96
+ content=f"I checked the weather for you. It's currently {result}."
97
+ )
98
+ else:
99
+ # Default response for other queries
100
+ time.sleep(1)
101
+ response = ChatMessage(
102
+ role="assistant",
103
+ content=f"I understand you're asking about '{query}', but I don't have a specific tool for that. I can help with calculations or weather."
104
+ )
105
+
106
+ # Add final response
107
+ history.append(response)
108
+ yield history
109
+
110
+ demo = gr.ChatInterface(
111
+ agent_with_tools,
112
+ title="Sample Agents with Tool Visualization using gr.ChatMessage",
113
+ description="Ask me to calculate something or check the weather!",
114
+ examples=[
115
+ "Calculate 145 * 32",
116
+ "What's the weather in Tokyo?",
117
+ "Tell me about quantum physics"
118
+ ],
119
+ type="messages"
120
+ )
121
+
122
+ demo.launch()