dawood HF Staff commited on
Commit
cf056ec
·
verified ·
1 Parent(s): e85d876

Create app.py

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