suayptalha commited on
Commit
753292d
·
verified ·
1 Parent(s): 248eb14

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +144 -144
app.py CHANGED
@@ -1,21 +1,21 @@
1
  import gradio as gr
2
  from gradio_client import Client
3
 
4
- # Qwen API istemcisini başlatıyoruz.
5
  client = Client("Qwen/Qwen2.5-72B-Instruct")
6
 
7
  SYSTEM_PROMPT = (
8
- "Sen bir asistansın ve deniz canlıları hakkında evet ya da hayır soruları soracaksın. "
9
- "Amacın bu sorulardan kullanıcının aklında tuttuğu deniz canlısını bilmek. "
10
- "Bu görevden asla uzaklaşmayacaksın. En son sadece cevabı bulduğunda sadece cevabını <cevap></cevap> tagleri arasında yaz. "
11
- "Şimdi sorarak başla. Eğer verdiğin cevap yanlışsa sormaya devam et. Kullanıcıdan cevabı asla isteme."
12
  )
13
 
14
  def start_game():
15
  """
16
- Oyunu başlatır; ilk soruyu getirir.
17
- Soru çerçevesi görünür, final tahmin alanı temizlenir,
18
- cevap butonları görünür, değerlendirme ve yeniden başlatma/ devam butonları gizlidir.
19
  """
20
  history = []
21
  result = client.predict(
@@ -30,31 +30,31 @@ def start_game():
30
  answer_buttons_update = gr.update(visible=True)
31
  eval_buttons_update = gr.update(visible=False)
32
  restart_update = gr.update(visible=False)
33
- devamet_update = gr.update(visible=False)
34
 
35
  return (
36
  gr.update(visible=True, value=assistant_message), # assistant_display
37
  gr.update(visible=True, value=""), # final_answer_display
38
  history, # state
39
- answer_buttons_update, # btn_evet
40
- answer_buttons_update, # btn_hayir
41
- answer_buttons_update, # btn_bilmiyorum
42
- eval_buttons_update, # btn_dogru
43
- eval_buttons_update, # btn_yanlis
44
  "", # final_state
45
  restart_update, # btn_restart
46
- devamet_update # btn_devamet
47
  )
48
 
49
  def process_turn(user_answer, history):
50
  """
51
- Kullanıcının cevabını API'ye gönderir.
52
- Eğer API cevabında <cevap> tagleri varsa final tahmin yapılmıştır.
53
- - Bu durumda soru çerçevesi gizlenir,
54
- - Final tahmin büyük ve kalın gösterilir,
55
- - Cevap butonları gizlenir,
56
- - Değerlendirme butonları görünür.
57
- Eğer final tahmin yoksa, asistanın cevabı (soru) güncellenir.
58
  """
59
  result = client.predict(
60
  query=user_answer,
@@ -65,85 +65,85 @@ def process_turn(user_answer, history):
65
  assistant_message = result[1][-1][1]
66
  history.append((user_answer, assistant_message))
67
  final_answer = ""
68
- if "<cevap>" in assistant_message and "</cevap>" in assistant_message:
69
- start_idx = assistant_message.index("<cevap>") + len("<cevap>")
70
- end_idx = assistant_message.index("</cevap>")
71
  final_answer = assistant_message[start_idx:end_idx].strip()
72
  if final_answer:
73
- # Final tahmin yapıldı.
74
  assistant_update = gr.update(visible=False)
75
- final_text = f"**Tahminim:** **{final_answer}**"
76
  answer_update = gr.update(visible=False)
77
  eval_update = gr.update(visible=True)
78
  restart_update = gr.update(visible=False)
79
- devamet_update = gr.update(visible=False)
80
  else:
81
  assistant_update = gr.update(visible=True, value=assistant_message)
82
  final_text = ""
83
  answer_update = gr.update(visible=True)
84
  eval_update = gr.update(visible=False)
85
  restart_update = gr.update(visible=False)
86
- devamet_update = gr.update(visible=False)
87
  return (
88
  assistant_update, # assistant_display
89
  final_text, # final_answer_display
90
  history, # state
91
- answer_update, # btn_evet
92
- answer_update, # btn_hayir
93
- answer_update, # btn_bilmiyorum
94
- eval_update, # btn_dogru
95
- eval_update, # btn_yanlis
96
  final_answer, # final_state
97
  restart_update, # btn_restart
98
- devamet_update # btn_devamet
99
  )
100
 
101
- def process_evet(history):
102
- return process_turn("Evet", history)
103
 
104
- def process_hayir(history):
105
- return process_turn("Hayır", history)
106
 
107
- def process_bilmiyorum(history):
108
- return process_turn("Bilmiyorum", history)
109
 
110
  def evaluate_correct(final_state):
111
  """
112
- Eğer final tahmin doğru ise:
113
- - Final metin "Tahminim doğru! Tekrar oynayalım mı?" olarak güncellenir.
114
- - Değerlendirme butonları gizlenir.
115
- - "Tekrar Oyna" butonu görünür hale gelir.
116
- - "Devam Et" butonu gizli kalır.
117
  """
118
- new_text = "**Tahminim doğru! Tekrar oynayalım mı?**"
119
  return (
120
  new_text,
121
- gr.update(visible=False), # btn_dogru gizle
122
- gr.update(visible=False), # btn_yanlis gizle
123
- gr.update(visible=True, value="Tekrar Oyna"), # btn_restart görünür
124
- gr.update(visible=False) # btn_devamet gizli
125
  )
126
 
127
  def evaluate_incorrect(final_state):
128
  """
129
- Eğer final tahmin yanlış ise:
130
- - Final metin "Devam edelim, devam et butonuna basın." olarak güncellenir.
131
- - Değerlendirme butonları gizlenir.
132
- - "Devam Et" butonu görünür hale gelir.
133
  """
134
- new_text = "**Devam edelim, devam et butonuna basın.**"
135
  return (
136
  new_text,
137
- gr.update(visible=False), # btn_dogru gizle
138
- gr.update(visible=False), # btn_yanlis gizle
139
- gr.update(visible=False), # btn_restart gizle
140
- gr.update(visible=True, value="Devam Et") # btn_devamet görünür
141
  )
142
 
143
  def continue_game(history):
144
  """
145
- "Devam Et" butonuna tıklandığında mevcut history ile asistan yeni bir soru sorar.
146
- Konuşma geçmişi korunur, soru çerçevesi ve cevap butonları yeniden aktif hale gelir.
147
  """
148
  result = client.predict(
149
  query="",
@@ -157,25 +157,25 @@ def continue_game(history):
157
  gr.update(visible=True, value=assistant_message), # assistant_display
158
  gr.update(visible=True, value=""), # final_answer_display
159
  history, # state
160
- gr.update(visible=True), # btn_evet
161
- gr.update(visible=True), # btn_hayir
162
- gr.update(visible=True), # btn_bilmiyorum
163
- gr.update(visible=False), # btn_dogru
164
- gr.update(visible=False), # btn_yanlis
165
  "", # final_state
166
  gr.update(visible=False), # btn_restart
167
- gr.update(visible=False) # btn_devamet
168
  )
169
 
170
  def restart_game():
171
  """
172
- "Tekrar Oyna" butonuna tıklandığında oyunu en baştan başlatır (history sıfırlanır).
173
  """
174
  global client
175
  client = Client("Qwen/Qwen2.5-72B-Instruct")
176
  return start_game()
177
 
178
- # Özel CSS:
179
  css = """
180
  .question-box {
181
  border: 2px solid #ccc;
@@ -199,130 +199,130 @@ css = """
199
  """
200
 
201
  with gr.Blocks(css=css) as demo:
202
- gr.Markdown("### Deniz Canlıları Asistanı - Evet/Hayır Oyunu")
203
 
204
- # Asistanın sorduğu soru.
205
- assistant_display = gr.Markdown(label="Soru", elem_classes="question-box")
206
- # Final tahmin alanı.
207
- final_answer_display = gr.Markdown("", label="Tahmin", elem_classes="final-answer")
208
 
209
- # Cevap butonları: Evet, Hayır, Bilmiyorum.
210
  with gr.Row(elem_classes="button-group"):
211
- btn_evet = gr.Button("Evet")
212
- btn_hayir = gr.Button("Hayır")
213
- btn_bilmiyorum = gr.Button("Bilmiyorum")
214
 
215
- # Değerlendirme butonları: Doğru, Yanlış (başlangıçta gizli).
216
  with gr.Row(elem_classes="button-group"):
217
- btn_dogru = gr.Button("Doğru", visible=False)
218
- btn_yanlis = gr.Button("Yanlış", visible=False)
219
 
220
- # Final durumunda gösterilecek butonlar:
221
- # - Doğru ise "Tekrar Oyna"
222
- btn_restart = gr.Button("Tekrar Oyna", visible=False)
223
- # - Yanlış ise "Devam Et"
224
- btn_devamet = gr.Button("Devam Et", visible=False)
225
 
226
- # Gizli state'ler:
227
  state = gr.State([])
228
  final_state = gr.State("")
229
 
230
- # Sayfa yüklendiğinde oyunu başlat.
231
  demo.load(
232
  fn=start_game,
233
  outputs=[
234
  assistant_display,
235
  final_answer_display,
236
  state,
237
- btn_evet,
238
- btn_hayir,
239
- btn_bilmiyorum,
240
- btn_dogru,
241
- btn_yanlis,
242
  final_state,
243
  btn_restart,
244
- btn_devamet
245
  ]
246
  )
247
 
248
- # Cevap butonlarına tıklanıldığında:
249
- btn_evet.click(
250
- fn=process_evet,
251
  inputs=[state],
252
  outputs=[
253
  assistant_display,
254
  final_answer_display,
255
  state,
256
- btn_evet,
257
- btn_hayir,
258
- btn_bilmiyorum,
259
- btn_dogru,
260
- btn_yanlis,
261
  final_state,
262
  btn_restart,
263
- btn_devamet
264
  ]
265
  )
266
- btn_hayir.click(
267
- fn=process_hayir,
268
  inputs=[state],
269
  outputs=[
270
  assistant_display,
271
  final_answer_display,
272
  state,
273
- btn_evet,
274
- btn_hayir,
275
- btn_bilmiyorum,
276
- btn_dogru,
277
- btn_yanlis,
278
  final_state,
279
  btn_restart,
280
- btn_devamet
281
  ]
282
  )
283
- btn_bilmiyorum.click(
284
- fn=process_bilmiyorum,
285
  inputs=[state],
286
  outputs=[
287
  assistant_display,
288
  final_answer_display,
289
  state,
290
- btn_evet,
291
- btn_hayir,
292
- btn_bilmiyorum,
293
- btn_dogru,
294
- btn_yanlis,
295
  final_state,
296
  btn_restart,
297
- btn_devamet
298
  ]
299
  )
300
 
301
- # Değerlendirme butonları:
302
- btn_dogru.click(
303
  fn=evaluate_correct,
304
  inputs=[final_state],
305
  outputs=[
306
  final_answer_display,
307
- btn_dogru,
308
- btn_yanlis,
309
  btn_restart,
310
- btn_devamet
311
  ]
312
  )
313
- btn_yanlis.click(
314
  fn=evaluate_incorrect,
315
  inputs=[final_state],
316
  outputs=[
317
  final_answer_display,
318
- btn_dogru,
319
- btn_yanlis,
320
  btn_restart,
321
- btn_devamet
322
  ]
323
  )
324
 
325
- # "Tekrar Oyna" butonuna tıklanıldığında:
326
  btn_restart.click(
327
  fn=restart_game,
328
  inputs=[],
@@ -330,33 +330,33 @@ with gr.Blocks(css=css) as demo:
330
  assistant_display,
331
  final_answer_display,
332
  state,
333
- btn_evet,
334
- btn_hayir,
335
- btn_bilmiyorum,
336
- btn_dogru,
337
- btn_yanlis,
338
  final_state,
339
  btn_restart,
340
- btn_devamet
341
  ]
342
  )
343
 
344
- # "Devam Et" butonuna tıklanıldığında:
345
- btn_devamet.click(
346
  fn=continue_game,
347
  inputs=[state],
348
  outputs=[
349
  assistant_display,
350
  final_answer_display,
351
  state,
352
- btn_evet,
353
- btn_hayir,
354
- btn_bilmiyorum,
355
- btn_dogru,
356
- btn_yanlis,
357
  final_state,
358
  btn_restart,
359
- btn_devamet
360
  ]
361
  )
362
 
 
1
  import gradio as gr
2
  from gradio_client import Client
3
 
4
+ # Start the Qwen API client.
5
  client = Client("Qwen/Qwen2.5-72B-Instruct")
6
 
7
  SYSTEM_PROMPT = (
8
+ "You are an assistant and you will ask yes or no questions about sea creatures. "
9
+ "Your goal is to guess the sea creature that the user has in mind from these questions. "
10
+ "You will never deviate from this task. Only when you find the answer, write it between <answer></answer> tags. "
11
+ "Start asking now. If your answer is wrong, continue asking. Never ask the user for the answer."
12
  )
13
 
14
  def start_game():
15
  """
16
+ Starts the game; retrieves the first question.
17
+ The question box is visible, the final guess area is cleared,
18
+ the answer buttons are visible, the evaluation and restart/continue buttons are hidden.
19
  """
20
  history = []
21
  result = client.predict(
 
30
  answer_buttons_update = gr.update(visible=True)
31
  eval_buttons_update = gr.update(visible=False)
32
  restart_update = gr.update(visible=False)
33
+ continue_update = gr.update(visible=False)
34
 
35
  return (
36
  gr.update(visible=True, value=assistant_message), # assistant_display
37
  gr.update(visible=True, value=""), # final_answer_display
38
  history, # state
39
+ answer_buttons_update, # btn_yes
40
+ answer_buttons_update, # btn_no
41
+ answer_buttons_update, # btn_dont_know
42
+ eval_buttons_update, # btn_correct
43
+ eval_buttons_update, # btn_incorrect
44
  "", # final_state
45
  restart_update, # btn_restart
46
+ continue_update # btn_continue
47
  )
48
 
49
  def process_turn(user_answer, history):
50
  """
51
+ Sends the user's answer to the API.
52
+ If the API's response contains <answer> tags, the final guess has been made.
53
+ - In this case, the question box is hidden,
54
+ - The final guess is displayed in large and bold,
55
+ - The answer buttons are hidden,
56
+ - The evaluation buttons are visible.
57
+ If there is no final guess, the assistant's answer (question) is updated.
58
  """
59
  result = client.predict(
60
  query=user_answer,
 
65
  assistant_message = result[1][-1][1]
66
  history.append((user_answer, assistant_message))
67
  final_answer = ""
68
+ if "<answer>" in assistant_message and "</answer>" in assistant_message:
69
+ start_idx = assistant_message.index("<answer>") + len("<answer>")
70
+ end_idx = assistant_message.index("</answer>")
71
  final_answer = assistant_message[start_idx:end_idx].strip()
72
  if final_answer:
73
+ # Final guess made.
74
  assistant_update = gr.update(visible=False)
75
+ final_text = f"**My guess:** **{final_answer}**"
76
  answer_update = gr.update(visible=False)
77
  eval_update = gr.update(visible=True)
78
  restart_update = gr.update(visible=False)
79
+ continue_update = gr.update(visible=False)
80
  else:
81
  assistant_update = gr.update(visible=True, value=assistant_message)
82
  final_text = ""
83
  answer_update = gr.update(visible=True)
84
  eval_update = gr.update(visible=False)
85
  restart_update = gr.update(visible=False)
86
+ continue_update = gr.update(visible=False)
87
  return (
88
  assistant_update, # assistant_display
89
  final_text, # final_answer_display
90
  history, # state
91
+ answer_update, # btn_yes
92
+ answer_update, # btn_no
93
+ answer_update, # btn_dont_know
94
+ eval_update, # btn_correct
95
+ eval_update, # btn_incorrect
96
  final_answer, # final_state
97
  restart_update, # btn_restart
98
+ continue_update # btn_continue
99
  )
100
 
101
+ def process_yes(history):
102
+ return process_turn("Yes", history)
103
 
104
+ def process_no(history):
105
+ return process_turn("No", history)
106
 
107
+ def process_dont_know(history):
108
+ return process_turn("I don't know", history)
109
 
110
  def evaluate_correct(final_state):
111
  """
112
+ If the final guess is correct:
113
+ - The final text is updated to "My guess is correct! Shall we play again?"
114
+ - The evaluation buttons are hidden.
115
+ - The "Play Again" button becomes visible.
116
+ - The "Continue" button remains hidden.
117
  """
118
+ new_text = "**My guess is correct! Shall we play again?**"
119
  return (
120
  new_text,
121
+ gr.update(visible=False), # btn_correct hidden
122
+ gr.update(visible=False), # btn_incorrect hidden
123
+ gr.update(visible=True, value="Play Again"), # btn_restart visible
124
+ gr.update(visible=False) # btn_continue hidden
125
  )
126
 
127
  def evaluate_incorrect(final_state):
128
  """
129
+ If the final guess is incorrect:
130
+ - The final text is updated to "Let's continue, press the continue button."
131
+ - The evaluation buttons are hidden.
132
+ - The "Continue" button becomes visible.
133
  """
134
+ new_text = "**Let's continue, press the continue button.**"
135
  return (
136
  new_text,
137
+ gr.update(visible=False), # btn_correct hidden
138
+ gr.update(visible=False), # btn_incorrect hidden
139
+ gr.update(visible=False), # btn_restart hidden
140
+ gr.update(visible=True, value="Continue") # btn_continue visible
141
  )
142
 
143
  def continue_game(history):
144
  """
145
+ When the "Continue" button is clicked, the assistant asks a new question with the current history.
146
+ The conversation history is preserved, the question box and answer buttons are reactivated.
147
  """
148
  result = client.predict(
149
  query="",
 
157
  gr.update(visible=True, value=assistant_message), # assistant_display
158
  gr.update(visible=True, value=""), # final_answer_display
159
  history, # state
160
+ gr.update(visible=True), # btn_yes
161
+ gr.update(visible=True), # btn_no
162
+ gr.update(visible=True), # btn_dont_know
163
+ gr.update(visible=False), # btn_correct
164
+ gr.update(visible=False), # btn_incorrect
165
  "", # final_state
166
  gr.update(visible=False), # btn_restart
167
+ gr.update(visible=False) # btn_continue
168
  )
169
 
170
  def restart_game():
171
  """
172
+ When the "Play Again" button is clicked, the game starts over (history is reset).
173
  """
174
  global client
175
  client = Client("Qwen/Qwen2.5-72B-Instruct")
176
  return start_game()
177
 
178
+ # Custom CSS:
179
  css = """
180
  .question-box {
181
  border: 2px solid #ccc;
 
199
  """
200
 
201
  with gr.Blocks(css=css) as demo:
202
+ gr.Markdown("### Sea Creature Assistant - Yes/No Game")
203
 
204
+ # The question asked by the assistant.
205
+ assistant_display = gr.Markdown(label="Question", elem_classes="question-box")
206
+ # Final guess area.
207
+ final_answer_display = gr.Markdown("", label="Guess", elem_classes="final-answer")
208
 
209
+ # Answer buttons: Yes, No, I don't know.
210
  with gr.Row(elem_classes="button-group"):
211
+ btn_yes = gr.Button("Yes")
212
+ btn_no = gr.Button("No")
213
+ btn_dont_know = gr.Button("I don't know")
214
 
215
+ # Evaluation buttons: Correct, Incorrect (hidden initially).
216
  with gr.Row(elem_classes="button-group"):
217
+ btn_correct = gr.Button("Correct", visible=False)
218
+ btn_incorrect = gr.Button("Incorrect", visible=False)
219
 
220
+ # Buttons displayed in the final state:
221
+ # - If correct, "Play Again"
222
+ btn_restart = gr.Button("Play Again", visible=False)
223
+ # - If incorrect, "Continue"
224
+ btn_continue = gr.Button("Continue", visible=False)
225
 
226
+ # Hidden states:
227
  state = gr.State([])
228
  final_state = gr.State("")
229
 
230
+ # Start the game when the page loads.
231
  demo.load(
232
  fn=start_game,
233
  outputs=[
234
  assistant_display,
235
  final_answer_display,
236
  state,
237
+ btn_yes,
238
+ btn_no,
239
+ btn_dont_know,
240
+ btn_correct,
241
+ btn_incorrect,
242
  final_state,
243
  btn_restart,
244
+ btn_continue
245
  ]
246
  )
247
 
248
+ # When answer buttons are clicked:
249
+ btn_yes.click(
250
+ fn=process_yes,
251
  inputs=[state],
252
  outputs=[
253
  assistant_display,
254
  final_answer_display,
255
  state,
256
+ btn_yes,
257
+ btn_no,
258
+ btn_dont_know,
259
+ btn_correct,
260
+ btn_incorrect,
261
  final_state,
262
  btn_restart,
263
+ btn_continue
264
  ]
265
  )
266
+ btn_no.click(
267
+ fn=process_no,
268
  inputs=[state],
269
  outputs=[
270
  assistant_display,
271
  final_answer_display,
272
  state,
273
+ btn_yes,
274
+ btn_no,
275
+ btn_dont_know,
276
+ btn_correct,
277
+ btn_incorrect,
278
  final_state,
279
  btn_restart,
280
+ btn_continue
281
  ]
282
  )
283
+ btn_dont_know.click(
284
+ fn=process_dont_know,
285
  inputs=[state],
286
  outputs=[
287
  assistant_display,
288
  final_answer_display,
289
  state,
290
+ btn_yes,
291
+ btn_no,
292
+ btn_dont_know,
293
+ btn_correct,
294
+ btn_incorrect,
295
  final_state,
296
  btn_restart,
297
+ btn_continue
298
  ]
299
  )
300
 
301
+ # Evaluation buttons:
302
+ btn_correct.click(
303
  fn=evaluate_correct,
304
  inputs=[final_state],
305
  outputs=[
306
  final_answer_display,
307
+ btn_correct,
308
+ btn_incorrect,
309
  btn_restart,
310
+ btn_continue
311
  ]
312
  )
313
+ btn_incorrect.click(
314
  fn=evaluate_incorrect,
315
  inputs=[final_state],
316
  outputs=[
317
  final_answer_display,
318
+ btn_correct,
319
+ btn_incorrect,
320
  btn_restart,
321
+ btn_continue
322
  ]
323
  )
324
 
325
+ # When the "Play Again" button is clicked:
326
  btn_restart.click(
327
  fn=restart_game,
328
  inputs=[],
 
330
  assistant_display,
331
  final_answer_display,
332
  state,
333
+ btn_yes,
334
+ btn_no,
335
+ btn_dont_know,
336
+ btn_correct,
337
+ btn_incorrect,
338
  final_state,
339
  btn_restart,
340
+ btn_continue
341
  ]
342
  )
343
 
344
+ # When the "Continue" button is clicked:
345
+ btn_continue.click(
346
  fn=continue_game,
347
  inputs=[state],
348
  outputs=[
349
  assistant_display,
350
  final_answer_display,
351
  state,
352
+ btn_yes,
353
+ btn_no,
354
+ btn_dont_know,
355
+ btn_correct,
356
+ btn_incorrect,
357
  final_state,
358
  btn_restart,
359
+ btn_continue
360
  ]
361
  )
362