Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from gradio_client import Client
|
2 |
+
import gradio as gr
|
3 |
+
import os
|
4 |
+
import time
|
5 |
+
|
6 |
+
client = Client(os.getenv('ASSISTANT_SPACE'), hf_token=os.getenv('HF_TOKEN'))
|
7 |
+
|
8 |
+
def predict(history):
|
9 |
+
global client
|
10 |
+
history[-1][1] = ""
|
11 |
+
job = client.submit(history={"headers":["1"],"data":history,"metadata":None}, api_name="/get_response")
|
12 |
+
for res in job:
|
13 |
+
history[-1][1] += res
|
14 |
+
#time.sleep(0.03) # To get a smooth letters entry. need something smarter in the future.
|
15 |
+
yield history
|
16 |
+
|
17 |
+
def add_message(history, message):
|
18 |
+
if history is None:
|
19 |
+
history=[]
|
20 |
+
for x in message["files"]:
|
21 |
+
history.append(((x,), None))
|
22 |
+
if message["text"] is not None:
|
23 |
+
history.append((message["text"], None))
|
24 |
+
return history, gr.MultimodalTextbox(value=None, interactive=False)
|
25 |
+
|
26 |
+
|
27 |
+
# Theme logic
|
28 |
+
theme = gr.themes.Default(
|
29 |
+
primary_hue="indigo",
|
30 |
+
)
|
31 |
+
|
32 |
+
js_func = """
|
33 |
+
function refresh() {
|
34 |
+
const url = new URL(window.location);
|
35 |
+
|
36 |
+
if (url.searchParams.get('__theme') !== 'light') {
|
37 |
+
url.searchParams.set('__theme', 'light');
|
38 |
+
window.location.href = url.href;
|
39 |
+
}
|
40 |
+
}
|
41 |
+
"""
|
42 |
+
example_questions = ["Quelle est la date du prochain relevé des compteurs ?",
|
43 |
+
"Il y a un dégât des eaux au-dessus de chez moi. Que dois-je faire ?",
|
44 |
+
"Est-ce que mon colis est arrivé ?",
|
45 |
+
"J'ai perdu mes clés. Pouvez-vous m'aider ?"]
|
46 |
+
|
47 |
+
with gr.Blocks(theme=theme, js=js_func, fill_height=True) as demo:
|
48 |
+
chatbot = gr.Chatbot(
|
49 |
+
elem_id="chatbot",
|
50 |
+
bubble_full_width=False,
|
51 |
+
scale=1,
|
52 |
+
height="60 vh",
|
53 |
+
placeholder= "\n\n\n".join(example_questions),
|
54 |
+
)
|
55 |
+
|
56 |
+
chat_input = gr.MultimodalTextbox(interactive=True,
|
57 |
+
file_count="multiple",
|
58 |
+
placeholder="Entrez un message ou ajoutez des fichiers...", show_label=False)
|
59 |
+
|
60 |
+
chat_msg = chat_input.submit(add_message, [chatbot, chat_input], [chatbot, chat_input])
|
61 |
+
bot_msg = chat_msg.then(predict, chatbot, chatbot, api_name="bot_response")
|
62 |
+
bot_msg.then(lambda: gr.MultimodalTextbox(interactive=True), None, [chat_input])
|
63 |
+
|
64 |
+
demo.launch()
|