Spaces:
Sleeping
Sleeping
julienlesbegueries
commited on
Commit
β’
10b0fa9
1
Parent(s):
c4382c5
add chatbot
Browse files- .vscode/settings.json +4 -0
- app.py +64 -2
.vscode/settings.json
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"python.analysis.autoImportCompletions": true,
|
3 |
+
"python.analysis.typeCheckingMode": "basic"
|
4 |
+
}
|
app.py
CHANGED
@@ -1,7 +1,69 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
|
3 |
def greet(name):
|
4 |
return "Hello " + name + "!!"
|
5 |
|
6 |
-
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
7 |
-
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import os
|
3 |
+
import time
|
4 |
|
5 |
def greet(name):
|
6 |
return "Hello " + name + "!!"
|
7 |
|
8 |
+
#iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
9 |
+
#iface.launch()
|
10 |
+
|
11 |
+
|
12 |
+
# Chatbot demo with multimodal input (text, markdown, LaTeX, code blocks, image, audio, & video). Plus shows support for streaming text.
|
13 |
+
|
14 |
+
|
15 |
+
def print_like_dislike(x: gr.LikeData):
|
16 |
+
print(x.index, x.value, x.liked)
|
17 |
+
|
18 |
+
|
19 |
+
def add_text(history, text):
|
20 |
+
history = history + [(text, None)]
|
21 |
+
return history, gr.Textbox(value="", interactive=False)
|
22 |
+
|
23 |
+
|
24 |
+
def add_file(history, file):
|
25 |
+
history = history + [((file.name,), None)]
|
26 |
+
return history
|
27 |
+
|
28 |
+
|
29 |
+
def bot(history):
|
30 |
+
response = "**That's cool!**"
|
31 |
+
history[-1][1] = ""
|
32 |
+
for character in response:
|
33 |
+
history[-1][1] += character
|
34 |
+
time.sleep(0.05)
|
35 |
+
yield history
|
36 |
+
|
37 |
+
|
38 |
+
with gr.Blocks() as demo:
|
39 |
+
chatbot = gr.Chatbot(
|
40 |
+
[],
|
41 |
+
elem_id="chatbot",
|
42 |
+
bubble_full_width=False,
|
43 |
+
avatar_images=(None, (os.path.join(os.path.dirname(__file__), "avatar.png"))),
|
44 |
+
)
|
45 |
+
|
46 |
+
with gr.Row():
|
47 |
+
txt = gr.Textbox(
|
48 |
+
scale=4,
|
49 |
+
show_label=False,
|
50 |
+
placeholder="Enter text and press enter, or upload an image",
|
51 |
+
container=False,
|
52 |
+
)
|
53 |
+
btn = gr.UploadButton("π", file_types=["image", "video", "audio"])
|
54 |
+
|
55 |
+
txt_msg = txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
|
56 |
+
bot, chatbot, chatbot, api_name="bot_response"
|
57 |
+
)
|
58 |
+
txt_msg.then(lambda: gr.Textbox(interactive=True), None, [txt], queue=False)
|
59 |
+
file_msg = btn.upload(add_file, [chatbot, btn], [chatbot], queue=False).then(
|
60 |
+
bot, chatbot, chatbot
|
61 |
+
)
|
62 |
+
|
63 |
+
chatbot.like(print_like_dislike, None, None)
|
64 |
+
|
65 |
+
|
66 |
+
demo.queue()
|
67 |
+
if __name__ == "__main__":
|
68 |
+
demo.launch()
|
69 |
+
|