ysharma HF staff commited on
Commit
7563a2f
·
verified ·
1 Parent(s): 8967d8d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -0
app.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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'"},
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="Calling Weather API with {'city': 'San Francisco'}",
45
+ metadata={"title": "🛠️ Used tool 'Weather'", "id": "a"},
46
+ )
47
+ )
48
+
49
+ history.append(
50
+ ChatMessage(
51
+ role="assistant",
52
+ content="Result: Weather 72 degrees Fahrenheit with 20% chance of rain.",
53
+ metadata={"title": "🌤️ API Results", "id": "b", "parent_id": "a"},
54
+ )
55
+ )
56
+ yield history
57
+ time.sleep(0.25)
58
+
59
+ history.append(
60
+ ChatMessage(
61
+ role="assistant",
62
+ content="Now that the API succeeded I can complete my task.",
63
+ )
64
+ )
65
+ yield history
66
+ time.sleep(0.25)
67
+
68
+ history.append(
69
+ ChatMessage(
70
+ role="assistant",
71
+ 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!",
72
+ )
73
+ )
74
+ yield history
75
+
76
+ def like(evt: gr.LikeData):
77
+ print("User liked the response")
78
+ print(evt.index, evt.liked, evt.value)
79
+
80
+ with gr.Blocks() as demo:
81
+ chatbot = gr.Chatbot(type="messages", height=500, show_copy_button=True)
82
+ button = gr.Button("Get San Francisco Weather")
83
+ button.click(generate_response, chatbot, chatbot)
84
+ chatbot.like(like)
85
+
86
+ if __name__ == "__main__":
87
+ demo.launch()