Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,24 +1,47 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
def calculator(num1, operation, num2):
|
4 |
-
if operation == "add":
|
5 |
-
return num1 + num2
|
6 |
-
elif operation == "subtract":
|
7 |
-
return num1 - num2
|
8 |
-
elif operation == "multiply":
|
9 |
-
return num1 * num2
|
10 |
-
elif operation == "divide":
|
11 |
-
return num1 / num2
|
12 |
-
|
13 |
-
demo = gr.Interface(
|
14 |
-
calculator,
|
15 |
-
[
|
16 |
-
"number",
|
17 |
-
gr.Radio(["add", "subtract", "multiply", "divide"]),
|
18 |
-
"number"
|
19 |
-
],
|
20 |
-
"number",
|
21 |
-
live=True,
|
22 |
-
)
|
23 |
if __name__ == "__main__":
|
24 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import time
|
3 |
+
|
4 |
+
# Chatbot demo with multimodal input (text, markdown, LaTeX, code blocks, image, audio, & video). Plus shows support for streaming text.
|
5 |
+
|
6 |
+
|
7 |
+
def print_like_dislike(x: gr.LikeData):
|
8 |
+
print(x.index, x.value, x.liked)
|
9 |
+
|
10 |
+
|
11 |
+
def add_message(history, message):
|
12 |
+
for x in message["files"]:
|
13 |
+
history.append({"role": "user", "content": {"path": x}})
|
14 |
+
if message["text"] is not None:
|
15 |
+
history.append({"role": "user", "content": message["text"]})
|
16 |
+
return history, gr.MultimodalTextbox(value=None, interactive=False)
|
17 |
+
|
18 |
+
|
19 |
+
def bot(history: list):
|
20 |
+
response = "**That's cool!**"
|
21 |
+
history.append({"role": "assistant", "content": ""})
|
22 |
+
for character in response:
|
23 |
+
history[-1]["content"] += character
|
24 |
+
time.sleep(0.05)
|
25 |
+
yield history
|
26 |
+
|
27 |
+
|
28 |
+
with gr.Blocks() as demo:
|
29 |
+
chatbot = gr.Chatbot(elem_id="chatbot", bubble_full_width=False, type="messages")
|
30 |
+
|
31 |
+
chat_input = gr.MultimodalTextbox(
|
32 |
+
interactive=True,
|
33 |
+
file_count="multiple",
|
34 |
+
placeholder="Enter message or upload file...",
|
35 |
+
show_label=False,
|
36 |
+
)
|
37 |
+
|
38 |
+
chat_msg = chat_input.submit(
|
39 |
+
add_message, [chatbot, chat_input], [chatbot, chat_input]
|
40 |
+
)
|
41 |
+
bot_msg = chat_msg.then(bot, chatbot, chatbot, api_name="bot_response")
|
42 |
+
bot_msg.then(lambda: gr.MultimodalTextbox(interactive=True), None, [chat_input])
|
43 |
+
|
44 |
+
chatbot.like(print_like_dislike, None, None, like_user_message=True)
|
45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
if __name__ == "__main__":
|
47 |
demo.launch()
|