Update app.py
Browse files
app.py
CHANGED
@@ -226,114 +226,6 @@ prompt_template = ChatPromptTemplate.from_messages([
|
|
226 |
])
|
227 |
|
228 |
|
229 |
-
|
230 |
-
# def process_question(question: str):
|
231 |
-
# """
|
232 |
-
# Process the question and yield the answer progressively.
|
233 |
-
# """
|
234 |
-
# # Check cache first
|
235 |
-
# if question in question_cache:
|
236 |
-
# yield question_cache[question] # Retourne directement depuis le cache si disponible
|
237 |
-
|
238 |
-
# relevant_docs = retriever(question)
|
239 |
-
# context = "\n".join([doc.page_content for doc in relevant_docs])
|
240 |
-
|
241 |
-
# prompt = prompt_template.format_messages(
|
242 |
-
# context=context,
|
243 |
-
# question=question
|
244 |
-
# )
|
245 |
-
|
246 |
-
# response = "" # Initialise la réponse
|
247 |
-
# # Ici, nous supposons que 'llm.stream' est un générateur qui renvoie des chunks
|
248 |
-
# for chunk in llm.stream(prompt): # suppose que llm.stream renvoie des chunks de réponse
|
249 |
-
# if isinstance(chunk, str):
|
250 |
-
# response += chunk # Accumulez la réponse si c'est déjà une chaîne
|
251 |
-
# else:
|
252 |
-
# response += chunk.content # Sinon, prenez le contenu du chunk (si chunk est un type d'objet spécifique)
|
253 |
-
|
254 |
-
# yield response, context # Renvoie la réponse mise à jour et le contexte
|
255 |
-
|
256 |
-
# # Mettez le résultat en cache à la fin
|
257 |
-
# question_cache[question] = (response, context)
|
258 |
-
|
259 |
-
# # Custom CSS for right-aligned text in textboxes
|
260 |
-
# custom_css = """
|
261 |
-
# .rtl-text {
|
262 |
-
# text-align: right !important;
|
263 |
-
# direction: rtl !important;
|
264 |
-
# }
|
265 |
-
# .rtl-text textarea {
|
266 |
-
# text-align: right !important;
|
267 |
-
# direction: rtl !important;
|
268 |
-
# }
|
269 |
-
# """
|
270 |
-
|
271 |
-
# # Gradio interface with queue
|
272 |
-
# with gr.Blocks(css=custom_css) as iface:
|
273 |
-
# with gr.Column():
|
274 |
-
# input_text = gr.Textbox(
|
275 |
-
# label="السؤال",
|
276 |
-
# placeholder="اكتب سؤالك هنا...",
|
277 |
-
# lines=2,
|
278 |
-
# elem_classes="rtl-text"
|
279 |
-
# )
|
280 |
-
|
281 |
-
# with gr.Row():
|
282 |
-
# answer_box = gr.Textbox(
|
283 |
-
# label="الإجابة",
|
284 |
-
# lines=4,
|
285 |
-
# elem_classes="rtl-text"
|
286 |
-
# )
|
287 |
-
# context_box = gr.Textbox(
|
288 |
-
# label="السياق المستخدم",
|
289 |
-
# lines=8,
|
290 |
-
# elem_classes="rtl-text"
|
291 |
-
# )
|
292 |
-
|
293 |
-
# submit_btn = gr.Button("إرسال")
|
294 |
-
|
295 |
-
# submit_btn.click(
|
296 |
-
# fn=process_question,
|
297 |
-
# inputs=input_text,
|
298 |
-
# outputs=[answer_box, context_box],
|
299 |
-
# api_name="predict",
|
300 |
-
# queue=True # Utiliser le système de queue pour un traitement asynchrone
|
301 |
-
# )
|
302 |
-
|
303 |
-
# if __name__ == "__main__":
|
304 |
-
# iface.launch(
|
305 |
-
# share=True,
|
306 |
-
# server_name="0.0.0.0",
|
307 |
-
# server_port=7860,
|
308 |
-
# max_threads=3, # Controls concurrency
|
309 |
-
# show_error=True
|
310 |
-
# )
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
# def process_question(question: str):
|
315 |
-
# """
|
316 |
-
# Process the question and return the answer and context
|
317 |
-
# """
|
318 |
-
# # Check cache first
|
319 |
-
# if question in question_cache:
|
320 |
-
# return question_cache[question], "" # Retourne la réponse cachée et un statut vide
|
321 |
-
# relevant_docs = retriever(question)
|
322 |
-
# context = "\n".join([doc.page_content for doc in relevant_docs])
|
323 |
-
# prompt = prompt_template.format_messages(
|
324 |
-
# context=context,
|
325 |
-
# question=question
|
326 |
-
# )
|
327 |
-
# response = ""
|
328 |
-
# for chunk in llm.stream(prompt):
|
329 |
-
# if isinstance(chunk, str):
|
330 |
-
# response += chunk
|
331 |
-
# else:
|
332 |
-
# response += chunk.content
|
333 |
-
# # Mettez le résultat en cache à la fin
|
334 |
-
# question_cache[question] = (response, context)
|
335 |
-
# return response, context
|
336 |
-
|
337 |
def process_question(question: str):
|
338 |
"""
|
339 |
Process the question and yield the answer progressively.
|
@@ -488,18 +380,18 @@ with gr.Blocks(css=custom_css) as iface:
|
|
488 |
)
|
489 |
|
490 |
with gr.Row():
|
491 |
-
with gr.Column(
|
492 |
answer_box = gr.Textbox(
|
493 |
label="الإجابة",
|
494 |
lines=4,
|
495 |
elem_classes="rtl-text textbox-container"
|
496 |
)
|
497 |
-
with gr.Column(scale=1):
|
498 |
-
|
499 |
-
|
500 |
-
|
501 |
-
|
502 |
-
|
503 |
|
504 |
submit_btn = gr.Button(
|
505 |
"إرسال السؤال",
|
@@ -508,18 +400,6 @@ with gr.Blocks(css=custom_css) as iface:
|
|
508 |
)
|
509 |
|
510 |
|
511 |
-
# def on_submit(question):
|
512 |
-
# response = ""
|
513 |
-
# context = ""
|
514 |
-
# for chunk in process_question(question):
|
515 |
-
# if isinstance(chunk, tuple):
|
516 |
-
# chunk_content = chunk[0] # Assurez-vous de prendre la bonne partie du tuple
|
517 |
-
# else:
|
518 |
-
# chunk_content = chunk
|
519 |
-
|
520 |
-
# response += chunk_content
|
521 |
-
# yield [(question, response), context] # Renvoie la réponse mise à jour et le contexte
|
522 |
-
|
523 |
def on_submit(question):
|
524 |
# Appeler process_question pour obtenir la réponse complète et le contexte
|
525 |
response = ""
|
@@ -530,20 +410,16 @@ with gr.Blocks(css=custom_css) as iface:
|
|
530 |
|
531 |
yield response, context # Renvoie la réponse et le contexte final après la fin de la boucle
|
532 |
|
533 |
-
|
534 |
-
|
535 |
-
|
536 |
|
537 |
submit_btn.click(
|
538 |
fn=on_submit,
|
539 |
inputs=input_text,
|
540 |
-
outputs=
|
541 |
api_name="predict",
|
542 |
queue=True
|
543 |
)
|
544 |
|
545 |
|
546 |
-
|
547 |
if __name__ == "__main__":
|
548 |
iface.launch(
|
549 |
share=True,
|
@@ -553,3 +429,110 @@ if __name__ == "__main__":
|
|
553 |
show_error=True
|
554 |
)
|
555 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
226 |
])
|
227 |
|
228 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
229 |
def process_question(question: str):
|
230 |
"""
|
231 |
Process the question and yield the answer progressively.
|
|
|
380 |
)
|
381 |
|
382 |
with gr.Row():
|
383 |
+
with gr.Column():
|
384 |
answer_box = gr.Textbox(
|
385 |
label="الإجابة",
|
386 |
lines=4,
|
387 |
elem_classes="rtl-text textbox-container"
|
388 |
)
|
389 |
+
# with gr.Column(scale=1):
|
390 |
+
# context_box = gr.Textbox(
|
391 |
+
# label="السياق المستخدم",
|
392 |
+
# lines=4,
|
393 |
+
# elem_classes="rtl-text textbox-container"
|
394 |
+
# )
|
395 |
|
396 |
submit_btn = gr.Button(
|
397 |
"إرسال السؤال",
|
|
|
400 |
)
|
401 |
|
402 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
403 |
def on_submit(question):
|
404 |
# Appeler process_question pour obtenir la réponse complète et le contexte
|
405 |
response = ""
|
|
|
410 |
|
411 |
yield response, context # Renvoie la réponse et le contexte final après la fin de la boucle
|
412 |
|
|
|
|
|
|
|
413 |
|
414 |
submit_btn.click(
|
415 |
fn=on_submit,
|
416 |
inputs=input_text,
|
417 |
+
outputs=answer_box
|
418 |
api_name="predict",
|
419 |
queue=True
|
420 |
)
|
421 |
|
422 |
|
|
|
423 |
if __name__ == "__main__":
|
424 |
iface.launch(
|
425 |
share=True,
|
|
|
429 |
show_error=True
|
430 |
)
|
431 |
|
432 |
+
# def process_question(question: str):
|
433 |
+
# """
|
434 |
+
# Process the question and yield the answer progressively.
|
435 |
+
# """
|
436 |
+
# # Check cache first
|
437 |
+
# if question in question_cache:
|
438 |
+
# yield question_cache[question] # Retourne directement depuis le cache si disponible
|
439 |
+
|
440 |
+
# relevant_docs = retriever(question)
|
441 |
+
# context = "\n".join([doc.page_content for doc in relevant_docs])
|
442 |
+
|
443 |
+
# prompt = prompt_template.format_messages(
|
444 |
+
# context=context,
|
445 |
+
# question=question
|
446 |
+
# )
|
447 |
+
|
448 |
+
# response = "" # Initialise la réponse
|
449 |
+
# # Ici, nous supposons que 'llm.stream' est un générateur qui renvoie des chunks
|
450 |
+
# for chunk in llm.stream(prompt): # suppose que llm.stream renvoie des chunks de réponse
|
451 |
+
# if isinstance(chunk, str):
|
452 |
+
# response += chunk # Accumulez la réponse si c'est déjà une chaîne
|
453 |
+
# else:
|
454 |
+
# response += chunk.content # Sinon, prenez le contenu du chunk (si chunk est un type d'objet spécifique)
|
455 |
+
|
456 |
+
# yield response, context # Renvoie la réponse mise à jour et le contexte
|
457 |
+
|
458 |
+
# # Mettez le résultat en cache à la fin
|
459 |
+
# question_cache[question] = (response, context)
|
460 |
+
|
461 |
+
# # Custom CSS for right-aligned text in textboxes
|
462 |
+
# custom_css = """
|
463 |
+
# .rtl-text {
|
464 |
+
# text-align: right !important;
|
465 |
+
# direction: rtl !important;
|
466 |
+
# }
|
467 |
+
# .rtl-text textarea {
|
468 |
+
# text-align: right !important;
|
469 |
+
# direction: rtl !important;
|
470 |
+
# }
|
471 |
+
# """
|
472 |
+
|
473 |
+
# # Gradio interface with queue
|
474 |
+
# with gr.Blocks(css=custom_css) as iface:
|
475 |
+
# with gr.Column():
|
476 |
+
# input_text = gr.Textbox(
|
477 |
+
# label="السؤال",
|
478 |
+
# placeholder="اكتب سؤالك هنا...",
|
479 |
+
# lines=2,
|
480 |
+
# elem_classes="rtl-text"
|
481 |
+
# )
|
482 |
+
|
483 |
+
# with gr.Row():
|
484 |
+
# answer_box = gr.Textbox(
|
485 |
+
# label="الإجابة",
|
486 |
+
# lines=4,
|
487 |
+
# elem_classes="rtl-text"
|
488 |
+
# )
|
489 |
+
# context_box = gr.Textbox(
|
490 |
+
# label="السياق المستخدم",
|
491 |
+
# lines=8,
|
492 |
+
# elem_classes="rtl-text"
|
493 |
+
# )
|
494 |
+
|
495 |
+
# submit_btn = gr.Button("إرسال")
|
496 |
+
|
497 |
+
# submit_btn.click(
|
498 |
+
# fn=process_question,
|
499 |
+
# inputs=input_text,
|
500 |
+
# outputs=[answer_box, context_box],
|
501 |
+
# api_name="predict",
|
502 |
+
# queue=True # Utiliser le système de queue pour un traitement asynchrone
|
503 |
+
# )
|
504 |
+
|
505 |
+
# if __name__ == "__main__":
|
506 |
+
# iface.launch(
|
507 |
+
# share=True,
|
508 |
+
# server_name="0.0.0.0",
|
509 |
+
# server_port=7860,
|
510 |
+
# max_threads=3, # Controls concurrency
|
511 |
+
# show_error=True
|
512 |
+
# )
|
513 |
+
|
514 |
+
|
515 |
+
|
516 |
+
# def process_question(question: str):
|
517 |
+
# """
|
518 |
+
# Process the question and return the answer and context
|
519 |
+
# """
|
520 |
+
# # Check cache first
|
521 |
+
# if question in question_cache:
|
522 |
+
# return question_cache[question], "" # Retourne la réponse cachée et un statut vide
|
523 |
+
# relevant_docs = retriever(question)
|
524 |
+
# context = "\n".join([doc.page_content for doc in relevant_docs])
|
525 |
+
# prompt = prompt_template.format_messages(
|
526 |
+
# context=context,
|
527 |
+
# question=question
|
528 |
+
# )
|
529 |
+
# response = ""
|
530 |
+
# for chunk in llm.stream(prompt):
|
531 |
+
# if isinstance(chunk, str):
|
532 |
+
# response += chunk
|
533 |
+
# else:
|
534 |
+
# response += chunk.content
|
535 |
+
# # Mettez le résultat en cache à la fin
|
536 |
+
# question_cache[question] = (response, context)
|
537 |
+
# return response, context
|
538 |
+
|