Spaces:
Sleeping
Sleeping
Commit
·
d30ccfe
1
Parent(s):
ad32b40
Improve UI
Browse files
app.py
CHANGED
@@ -7,17 +7,11 @@ LANGGRAPH_DEPLOYMENT = "https://chambre-agricole-chatbot-686407044d7f59d29a1e494
|
|
7 |
|
8 |
client = get_client(url=LANGGRAPH_DEPLOYMENT)
|
9 |
|
10 |
-
async def respond(
|
11 |
-
message,
|
12 |
-
history,
|
13 |
-
system_message,
|
14 |
-
thread_state
|
15 |
-
):
|
16 |
assistants = await client.assistants.search(
|
17 |
graph_id="retrieval_graph", metadata={"created_by": "system"}
|
18 |
)
|
19 |
|
20 |
-
# Only create new thread if one doesn't exist
|
21 |
if not thread_state:
|
22 |
thread = await client.threads.create()
|
23 |
thread_state = thread["thread_id"]
|
@@ -27,9 +21,7 @@ async def respond(
|
|
27 |
async for chunk in client.runs.stream(
|
28 |
thread_id=thread_state,
|
29 |
assistant_id=assistants[0]["assistant_id"],
|
30 |
-
input={
|
31 |
-
"messages": message
|
32 |
-
},
|
33 |
stream_mode="events",
|
34 |
):
|
35 |
if chunk.event == "events":
|
@@ -38,40 +30,92 @@ async def respond(
|
|
38 |
response += token
|
39 |
yield history + [(message, response)], thread_state
|
40 |
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
|
76 |
if __name__ == "__main__":
|
77 |
demo.launch()
|
|
|
7 |
|
8 |
client = get_client(url=LANGGRAPH_DEPLOYMENT)
|
9 |
|
10 |
+
async def respond(message, history, thread_state):
|
|
|
|
|
|
|
|
|
|
|
11 |
assistants = await client.assistants.search(
|
12 |
graph_id="retrieval_graph", metadata={"created_by": "system"}
|
13 |
)
|
14 |
|
|
|
15 |
if not thread_state:
|
16 |
thread = await client.threads.create()
|
17 |
thread_state = thread["thread_id"]
|
|
|
21 |
async for chunk in client.runs.stream(
|
22 |
thread_id=thread_state,
|
23 |
assistant_id=assistants[0]["assistant_id"],
|
24 |
+
input={"messages": message},
|
|
|
|
|
25 |
stream_mode="events",
|
26 |
):
|
27 |
if chunk.event == "events":
|
|
|
30 |
response += token
|
31 |
yield history + [(message, response)], thread_state
|
32 |
|
33 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
34 |
+
with gr.Row():
|
35 |
+
with gr.Column(scale=1):
|
36 |
+
gr.Markdown("### Historique des conversations")
|
37 |
+
chat_list = gr.Chatbot(height=700, show_label=False)
|
38 |
+
|
39 |
+
with gr.Column(scale=3):
|
40 |
+
gr.Markdown("### Assistant R&D Agricole")
|
41 |
+
chatbot = gr.Chatbot(
|
42 |
+
height=600,
|
43 |
+
avatar_images=(
|
44 |
+
"https://em-content.zobj.net/source/microsoft-teams/363/person_1f9d1.png", # Person emoji
|
45 |
+
"https://em-content.zobj.net/source/microsoft-teams/363/robot_1f916.png" # Robot emoji
|
46 |
+
),
|
47 |
+
container=True,
|
48 |
+
show_label=False,
|
49 |
+
)
|
50 |
+
with gr.Row():
|
51 |
+
txt = gr.Textbox(
|
52 |
+
placeholder="Posez votre question ici concernant les données R&D agricoles...",
|
53 |
+
show_label=False,
|
54 |
+
container=False,
|
55 |
+
scale=9,
|
56 |
+
)
|
57 |
+
submit_btn = gr.Button("Envoyer", scale=1)
|
58 |
+
|
59 |
+
with gr.Row():
|
60 |
+
clear_btn = gr.Button("Effacer la conversation")
|
61 |
+
|
62 |
+
thread_state = gr.State(value=None)
|
63 |
+
|
64 |
+
def clear_conversation():
|
65 |
+
return [], None
|
66 |
+
|
67 |
+
txt.submit(
|
68 |
+
respond,
|
69 |
+
[txt, chatbot, thread_state],
|
70 |
+
[chatbot, thread_state],
|
71 |
+
api_name=False
|
72 |
+
).then(
|
73 |
+
lambda: "", # Clear the textbox after submission
|
74 |
+
None,
|
75 |
+
[txt]
|
76 |
+
)
|
77 |
+
|
78 |
+
submit_btn.click(
|
79 |
+
respond,
|
80 |
+
[txt, chatbot, thread_state],
|
81 |
+
[chatbot, thread_state],
|
82 |
+
api_name=False
|
83 |
+
).then(
|
84 |
+
lambda: "", # Clear the textbox after submission
|
85 |
+
None,
|
86 |
+
[txt]
|
87 |
+
)
|
88 |
+
|
89 |
+
clear_btn.click(
|
90 |
+
clear_conversation,
|
91 |
+
None,
|
92 |
+
[chatbot, thread_state],
|
93 |
+
api_name=False
|
94 |
+
)
|
95 |
+
|
96 |
+
gr.Markdown("""
|
97 |
+
<style>
|
98 |
+
.gradio-container {
|
99 |
+
background-color: #f5f7f5;
|
100 |
+
}
|
101 |
+
.contain {
|
102 |
+
max-width: 1200px !important;
|
103 |
+
margin: auto;
|
104 |
+
}
|
105 |
+
.message {
|
106 |
+
padding: 15px;
|
107 |
+
border-radius: 10px;
|
108 |
+
margin-bottom: 10px;
|
109 |
+
}
|
110 |
+
.user-message {
|
111 |
+
background-color: #e6f3ff;
|
112 |
+
}
|
113 |
+
.bot-message {
|
114 |
+
background-color: #f5f5f5;
|
115 |
+
}
|
116 |
+
footer {display: none !important}
|
117 |
+
</style>
|
118 |
+
""")
|
119 |
|
120 |
if __name__ == "__main__":
|
121 |
demo.launch()
|