garima-mahato commited on
Commit
fce4adb
·
verified ·
1 Parent(s): 3dc3050

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -19
app.py CHANGED
@@ -1,27 +1,60 @@
1
  import gradio as gr
2
- import random
3
  import time
4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  with gr.Blocks() as demo:
6
- chatbot = gr.Chatbot()
7
- msg = gr.Textbox()
8
- clear = gr.Button("Clear")
9
-
10
- def user(user_message, history):
11
- return "", history + [[user_message, None]]
12
-
13
- def bot(history):
14
- bot_message = random.choice(["How are you?", "I love you", "I'm very hungry"])
15
- history[-1][1] = ""
16
- for character in bot_message:
17
- history[-1][1] += character
18
- time.sleep(0.05)
19
- yield history
20
-
21
- msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
 
 
 
 
 
22
  bot, chatbot, chatbot
23
  )
24
- clear.click(lambda: None, None, chatbot, queue=False)
25
-
 
 
26
  demo.queue()
27
  demo.launch()
 
1
  import gradio as gr
2
+ import os
3
  import time
4
 
5
+ # Chatbot demo with multimodal input (text, markdown, LaTeX, code blocks, image, audio, & video). Plus shows support for streaming text.
6
+
7
+
8
+ def print_like_dislike(x: gr.LikeData):
9
+ print(x.index, x.value, x.liked)
10
+
11
+
12
+ def add_text(history, text):
13
+ history = history + [(text, None)]
14
+ return history, gr.Textbox(value="", interactive=False)
15
+
16
+
17
+ def add_file(history, file):
18
+ history = history + [((file.name,), None)]
19
+ return history
20
+
21
+
22
+ def bot(history):
23
+ response = "**That's cool!**"
24
+ history[-1][1] = ""
25
+ for character in response:
26
+ history[-1][1] += character
27
+ time.sleep(0.05)
28
+ yield history
29
+
30
+
31
  with gr.Blocks() as demo:
32
+ chatbot = gr.Chatbot(
33
+ [],
34
+ elem_id="chatbot",
35
+ bubble_full_width=False,
36
+ avatar_images=(None, (os.path.join(os.path.dirname(__file__), "avatar.png"))),
37
+ )
38
+
39
+ with gr.Row():
40
+ txt = gr.Textbox(
41
+ scale=4,
42
+ show_label=False,
43
+ placeholder="Enter text and press enter, or upload an image",
44
+ container=False,
45
+ )
46
+ btn = gr.UploadButton("📁", file_types=["image", "video", "audio"])
47
+
48
+ txt_msg = txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
49
+ bot, chatbot, chatbot, api_name="bot_response"
50
+ )
51
+ txt_msg.then(lambda: gr.Textbox(interactive=True), None, [txt], queue=False)
52
+ file_msg = btn.upload(add_file, [chatbot, btn], [chatbot], queue=False).then(
53
  bot, chatbot, chatbot
54
  )
55
+
56
+ chatbot.like(print_like_dislike, None, None)
57
+
58
+
59
  demo.queue()
60
  demo.launch()