antoineandrieu commited on
Commit
a2c6e56
·
1 Parent(s): 95fcc62

Add feedback comment

Browse files
Files changed (1) hide show
  1. app.py +124 -40
app.py CHANGED
@@ -50,54 +50,110 @@ async def respond(message, history, thread_state):
50
  run_id = chunk.data["run_id"]
51
  token = chunk.data["data"]["chunk"]["content"]
52
  response += token
53
- yield history + [{"role": "user", "content": message},
54
- {"role": "assistant", "content": response}], thread_state, run_id
 
 
 
55
 
56
  def clear_conversation():
57
- return [], None
58
-
59
- async def give_positive_feedback(run_id):
60
- if run_id is not None:
61
- await log_feedback(run_id, 1.0)
62
- else:
63
- logging.warning("Attempted to give positive feedback but run_id was None")
64
-
65
- async def give_negative_feedback(run_id):
66
- if run_id is not None:
67
- await log_feedback(run_id, 0.0)
68
- else:
69
- logging.warning("Attempted to give negative feedback but run_id was None")
70
-
71
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
72
- with gr.Column(scale=3):
73
- gr.Markdown("### Assistant R&D Agricole")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  chatbot = gr.Chatbot(
75
- height=600,
76
  avatar_images=(
77
  "https://em-content.zobj.net/source/microsoft-teams/337/farmer_1f9d1-200d-1f33e.png",
78
  "https://em-content.zobj.net/source/microsoft-teams/363/robot_1f916.png"
79
  ),
80
- container=True,
81
  show_label=False,
 
 
82
  type="messages"
83
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
 
85
  with gr.Row():
86
  txt = gr.Textbox(
87
- placeholder="Posez votre question ici concernant les données R&D agricoles...",
88
  show_label=False,
89
  container=False,
90
- scale=9,
 
 
91
  )
92
- submit_btn = gr.Button("Envoyer", scale=1)
 
93
 
94
  with gr.Row():
95
- clear_btn = gr.Button("Effacer la conversation")
96
- thumbs_up = gr.Button("👍")
97
- thumbs_down = gr.Button("👎")
 
 
 
 
 
 
98
 
 
 
 
99
  thread_state = gr.State()
100
  current_run_id = gr.State()
 
101
 
102
  txt.submit(
103
  respond,
@@ -105,9 +161,9 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
105
  [chatbot, thread_state, current_run_id],
106
  api_name=False
107
  ).then(
108
- lambda: "",
109
  None,
110
- [txt]
111
  )
112
 
113
  submit_btn.click(
@@ -116,29 +172,57 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
116
  [chatbot, thread_state, current_run_id],
117
  api_name=False
118
  ).then(
119
- lambda: "",
120
  None,
121
- [txt]
122
  )
123
 
 
 
 
 
 
 
 
 
124
  thumbs_up.click(
125
- lambda x: asyncio.run(give_positive_feedback(x)),
126
- [current_run_id],
127
- None,
128
- api_name=False
129
  )
130
 
131
  thumbs_down.click(
132
- lambda x: asyncio.run(give_negative_feedback(x)),
133
- [current_run_id],
134
- None,
135
- api_name=False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
136
  )
137
 
138
  clear_btn.click(
139
  clear_conversation,
140
  None,
141
- [chatbot, thread_state],
142
  api_name=False
143
  )
144
 
 
50
  run_id = chunk.data["run_id"]
51
  token = chunk.data["data"]["chunk"]["content"]
52
  response += token
53
+ yield [
54
+ *history,
55
+ {"role": "user", "content": message},
56
+ {"role": "assistant", "content": response}
57
+ ], thread_state, run_id
58
 
59
  def clear_conversation():
60
+ return [], None, gr.update(visible=False)
61
+
62
+ with gr.Blocks(theme=gr.themes.Soft(), css="""
63
+ h1 {
64
+ text-align: center;
65
+ }
66
+ .gradio-container {
67
+ max-width: 800px !important;
68
+ }
69
+ .feedback-card {
70
+ border: 1px solid #e0e0e0;
71
+ border-radius: 8px;
72
+ padding: 16px;
73
+ margin-top: 12px;
74
+ }
75
+ .feedback-buttons {
76
+ gap: 8px;
77
+ }
78
+ .selected-thumb {
79
+ background: #e0e0e0 !important;
80
+ border-color: #4CAF50 !important;
81
+ }
82
+ .examples .example {
83
+ background: #f0f0f0;
84
+ padding: 10px;
85
+ margin: 5px;
86
+ border-radius: 5px;
87
+ cursor: pointer;
88
+ }
89
+ .examples .example:hover {
90
+ background: #e0e0e0;
91
+ }
92
+ """) as demo:
93
+ with gr.Column():
94
+ with gr.Row():
95
+ with gr.Column(scale=9):
96
+ gr.Markdown("# Le Chat Bottes 👢😺👢")
97
+
98
  chatbot = gr.Chatbot(
99
+ height=640,
100
  avatar_images=(
101
  "https://em-content.zobj.net/source/microsoft-teams/337/farmer_1f9d1-200d-1f33e.png",
102
  "https://em-content.zobj.net/source/microsoft-teams/363/robot_1f916.png"
103
  ),
 
104
  show_label=False,
105
+ bubble_full_width=False,
106
+ container=False,
107
  type="messages"
108
  )
109
+
110
+ with gr.Column(visible=False) as feedback_card:
111
+ with gr.Row():
112
+ thumbs_up = gr.Button("👍", elem_classes="feedback-btn")
113
+ thumbs_down = gr.Button("👎", elem_classes="feedback-btn")
114
+ with gr.Row(visible=False) as feedback_input_row:
115
+ feedback_comment = gr.Textbox(
116
+ placeholder="Commentaire (optionnel)",
117
+ show_label=False,
118
+ lines=2,
119
+ max_lines=3
120
+ )
121
+ feedback_send_btn = gr.Button("Envoyer", size="sm")
122
+
123
+ thank_you_message = gr.HTML(
124
+ "<div style='color: #4CAF50; margin-top: 8px; display: none'>Merci pour votre retour !</div>",
125
+ visible=False
126
+ )
127
 
128
  with gr.Row():
129
  txt = gr.Textbox(
130
+ placeholder="Posez votre question ici...",
131
  show_label=False,
132
  container=False,
133
+ scale=7,
134
+ autofocus=True,
135
+ max_lines=3,
136
  )
137
+ submit_btn = gr.Button("Envoyer", scale=1, size="sm")
138
+ clear_btn = gr.Button("Effacer", scale=1, size="sm")
139
 
140
  with gr.Row():
141
+ gr.Examples(
142
+ examples=[
143
+ "Comment stocker l'eau de pluie sur une exploitation agricole ?",
144
+ "Quelles rotations culturales favorisent l'adaptation climatique ?",
145
+ "Comment réduire l'impact du gel printanier sur les vignes ?"
146
+ ],
147
+ inputs=txt,
148
+ label=""
149
+ )
150
 
151
+
152
+ feedback_alert = gr.HTML(visible=False)
153
+
154
  thread_state = gr.State()
155
  current_run_id = gr.State()
156
+ selected_thumb = gr.State()
157
 
158
  txt.submit(
159
  respond,
 
161
  [chatbot, thread_state, current_run_id],
162
  api_name=False
163
  ).then(
164
+ lambda: ("", gr.update(visible=True), gr.update(visible=False), gr.update(visible=True), gr.update(visible=True)),
165
  None,
166
+ [txt, feedback_card, thank_you_message, thumbs_up, thumbs_down]
167
  )
168
 
169
  submit_btn.click(
 
172
  [chatbot, thread_state, current_run_id],
173
  api_name=False
174
  ).then(
175
+ lambda: ("", gr.update(visible=True), gr.update(visible=False), gr.update(visible=True), gr.update(visible=True)),
176
  None,
177
+ [txt, feedback_card, thank_you_message, thumbs_up, thumbs_down]
178
  )
179
 
180
+ def show_feedback_input(thumb):
181
+ return (
182
+ gr.update(visible=True), # feedback_input_row
183
+ thumb, # selected_thumb
184
+ gr.update(visible=False), # thumbs_up
185
+ gr.update(visible=False) # thumbs_down
186
+ )
187
+
188
  thumbs_up.click(
189
+ show_feedback_input,
190
+ inputs=[gr.Number(1, visible=False)],
191
+ outputs=[feedback_input_row, selected_thumb, thumbs_up, thumbs_down]
 
192
  )
193
 
194
  thumbs_down.click(
195
+ show_feedback_input,
196
+ inputs=[gr.Number(0, visible=False)],
197
+ outputs=[feedback_input_row, selected_thumb, thumbs_up, thumbs_down]
198
+ )
199
+
200
+ def submit_feedback(run_id, score, comment):
201
+ asyncio.run(log_feedback(run_id, score, comment))
202
+ return (
203
+ gr.update(visible=True), # thank_you_message
204
+ gr.update(value=""), # feedback_comment
205
+ gr.update(visible=False), # feedback_input_row
206
+ gr.update(visible=False), # thumbs_up
207
+ gr.update(visible=False) # thumbs_down
208
+ )
209
+
210
+ feedback_send_btn.click(
211
+ submit_feedback,
212
+ [current_run_id, selected_thumb, feedback_comment],
213
+ [thank_you_message, feedback_comment, feedback_input_row, thumbs_up, thumbs_down]
214
+ )
215
+
216
+ feedback_comment.submit(
217
+ submit_feedback,
218
+ [current_run_id, selected_thumb, feedback_comment],
219
+ [thank_you_message, feedback_comment, feedback_input_row, thumbs_up, thumbs_down]
220
  )
221
 
222
  clear_btn.click(
223
  clear_conversation,
224
  None,
225
+ [chatbot, thread_state, feedback_card, thank_you_message],
226
  api_name=False
227
  )
228