Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from gradio import ChatMessage
|
3 |
+
import time
|
4 |
+
|
5 |
+
def generate_response(history):
|
6 |
+
history.append(
|
7 |
+
ChatMessage(
|
8 |
+
role="user", content="What is the weather in San Francisco right now?"
|
9 |
+
)
|
10 |
+
)
|
11 |
+
yield history
|
12 |
+
time.sleep(0.25)
|
13 |
+
history.append(
|
14 |
+
ChatMessage(
|
15 |
+
role="assistant",
|
16 |
+
content="In order to find the current weather in San Francisco, I will need to use my weather tool.",
|
17 |
+
)
|
18 |
+
)
|
19 |
+
yield history
|
20 |
+
time.sleep(0.25)
|
21 |
+
|
22 |
+
history.append(
|
23 |
+
ChatMessage(
|
24 |
+
role="assistant",
|
25 |
+
content="API Error when connecting to weather service.",
|
26 |
+
metadata={"title": "💥 Error using tool 'Weather'", "id": 1},
|
27 |
+
)
|
28 |
+
)
|
29 |
+
yield history
|
30 |
+
time.sleep(0.25)
|
31 |
+
|
32 |
+
history.append(
|
33 |
+
ChatMessage(
|
34 |
+
role="assistant",
|
35 |
+
content="I will try again",
|
36 |
+
)
|
37 |
+
)
|
38 |
+
yield history
|
39 |
+
time.sleep(0.25)
|
40 |
+
|
41 |
+
history.append(
|
42 |
+
ChatMessage(
|
43 |
+
role="assistant",
|
44 |
+
content="Pending",
|
45 |
+
metadata={"title": "🛠️ Pinging weather API", "id": 2},
|
46 |
+
)
|
47 |
+
)
|
48 |
+
|
49 |
+
history.append(
|
50 |
+
ChatMessage(
|
51 |
+
role="assistant",
|
52 |
+
content="Weather 72 degrees Fahrenheit with 20% chance of rain.",
|
53 |
+
metadata={"title": "🛠️ Successfully retrieved weather data", "id": 3, "parent_id": 2},
|
54 |
+
)
|
55 |
+
)
|
56 |
+
|
57 |
+
history.append(
|
58 |
+
ChatMessage(
|
59 |
+
role="assistant",
|
60 |
+
content="blah blah blah.",
|
61 |
+
metadata={"title": "✔️ Now that I have the weather data, I can complete my task.", "id": 4, "parent_id": 3},
|
62 |
+
)
|
63 |
+
)
|
64 |
+
|
65 |
+
yield history
|
66 |
+
time.sleep(0.25)
|
67 |
+
|
68 |
+
history.append(
|
69 |
+
ChatMessage(
|
70 |
+
role="assistant",
|
71 |
+
content="Now that the API succeeded I can complete my task.",
|
72 |
+
)
|
73 |
+
)
|
74 |
+
yield history
|
75 |
+
time.sleep(0.25)
|
76 |
+
|
77 |
+
history.append(
|
78 |
+
ChatMessage(
|
79 |
+
role="assistant",
|
80 |
+
content="It's a sunny day in San Francisco with a current temperature of 72 degrees Fahrenheit and a 20% chance of rain. Enjoy the weather!",
|
81 |
+
)
|
82 |
+
)
|
83 |
+
yield history
|
84 |
+
|
85 |
+
def like(evt: gr.LikeData):
|
86 |
+
print("User liked the response")
|
87 |
+
print(evt.index, evt.liked, evt.value)
|
88 |
+
|
89 |
+
with gr.Blocks() as demo:
|
90 |
+
chatbot = gr.Chatbot(type="messages", height=700, show_copy_button=True)
|
91 |
+
button = gr.Button("Get San Francisco Weather")
|
92 |
+
button.click(generate_response, chatbot, chatbot)
|
93 |
+
chatbot.like(like)
|
94 |
+
|
95 |
+
if __name__ == "__main__":
|
96 |
+
demo.launch()
|