antoineandrieu commited on
Commit
ad32b40
·
1 Parent(s): 561577e

Make use of gradio state & improve UI

Browse files
Files changed (1) hide show
  1. app.py +41 -8
app.py CHANGED
@@ -9,18 +9,23 @@ client = get_client(url=LANGGRAPH_DEPLOYMENT)
9
 
10
  async def respond(
11
  message,
12
- history: list[tuple[str, str]],
13
  system_message,
 
14
  ):
15
  assistants = await client.assistants.search(
16
  graph_id="retrieval_graph", metadata={"created_by": "system"}
17
  )
18
- thread = await client.threads.create()
 
 
 
 
19
 
20
  response = ""
21
 
22
  async for chunk in client.runs.stream(
23
- thread_id=thread["thread_id"],
24
  assistant_id=assistants[0]["assistant_id"],
25
  input={
26
  "messages": message
@@ -31,13 +36,41 @@ async def respond(
31
  if chunk.data["event"] == "on_chat_model_stream":
32
  token = chunk.data["data"]["chunk"]["content"]
33
  response += token
34
- yield response
35
 
36
- demo = gr.ChatInterface(
37
- respond,
38
- additional_inputs=[
39
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  ],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  )
42
 
43
  if __name__ == "__main__":
 
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"]
24
 
25
  response = ""
26
 
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
 
36
  if chunk.data["event"] == "on_chat_model_stream":
37
  token = chunk.data["data"]["chunk"]["content"]
38
  response += token
39
+ yield history + [(message, response)], thread_state
40
 
41
+ demo = gr.Interface(
42
+ fn=respond,
43
+ inputs=[
44
+ gr.Textbox(
45
+ placeholder="Posez votre question ici concernant les données R&D agricoles...",
46
+ label="Votre question",
47
+ lines=2,
48
+ ),
49
+ gr.Chatbot(
50
+ height=600,
51
+ avatar_images=("👨‍🌾", "🤖")
52
+ ),
53
+ gr.State(value=None),
54
+ ],
55
+ outputs=[
56
+ gr.Chatbot(),
57
+ gr.State(),
58
  ],
59
+ title="Assistant R&D Agricole",
60
+ description="""
61
+ Bienvenue sur l'assistant de recherche R&D Agricole.
62
+ Je peux vous aider à :
63
+ - Rechercher des données techniques
64
+ - Trouver des résultats d'expérimentations
65
+ - Accéder aux synthèses des études
66
+ - Consulter les références disponibles
67
+ """,
68
+ theme=gr.themes.Soft(),
69
+ css="""
70
+ .gradio-container {background-color: #f5f7f5}
71
+ .title {color: #2e5d1d}
72
+ .description {color: #4a4a4a}
73
+ """
74
  )
75
 
76
  if __name__ == "__main__":