Trabis commited on
Commit
41fe914
·
verified ·
1 Parent(s): 17f8a7f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -285
app.py CHANGED
@@ -225,77 +225,7 @@ prompt_template = ChatPromptTemplate.from_messages([
225
  ("human", "{question}")
226
  ])
227
 
228
- # def process_question(question: str) -> tuple[str, str]:
229
- # # Check cache first
230
- # if question in question_cache:
231
- # return question_cache[question]
232
-
233
- # relevant_docs = retriever(question)
234
- # context = "\n".join([doc.page_content for doc in relevant_docs])
235
-
236
- # prompt = prompt_template.format_messages(
237
- # context=context,
238
- # question=question
239
- # )
240
-
241
- # response = llm(prompt)
242
- # result = (response.content, context)
243
-
244
- # # Cache the result
245
- # question_cache[question] = result
246
- # return result
247
-
248
- # # Custom CSS for right-aligned text in textboxes
249
- # custom_css = """
250
- # .rtl-text {
251
- # text-align: right !important;
252
- # direction: rtl !important;
253
- # }
254
- # .rtl-text textarea {
255
- # text-align: right !important;
256
- # direction: rtl !important;
257
- # }
258
- # """
259
-
260
- # # Gradio interface with queue
261
- # with gr.Blocks(css=custom_css) as iface:
262
- # with gr.Column():
263
- # input_text = gr.Textbox(
264
- # label="السؤال",
265
- # placeholder="اكتب سؤالك هنا...",
266
- # lines=2,
267
- # elem_classes="rtl-text"
268
- # )
269
-
270
- # with gr.Row():
271
- # answer_box = gr.Textbox(
272
- # label="الإجابة",
273
- # lines=4,
274
- # elem_classes="rtl-text"
275
- # )
276
- # context_box = gr.Textbox(
277
- # label="السياق المستخدم",
278
- # lines=8,
279
- # elem_classes="rtl-text"
280
- # )
281
-
282
- # submit_btn = gr.Button("إرسال")
283
-
284
- # submit_btn.click(
285
- # fn=process_question,
286
- # inputs=input_text,
287
- # outputs=[answer_box, context_box],
288
- # api_name="predict"
289
- # )
290
 
291
- # if __name__ == "__main__":
292
- # iface.launch(
293
- # share=True,
294
- # server_name="0.0.0.0",
295
- # server_port=7860,
296
- # max_threads=3, # Controls concurrency
297
- # show_error=True
298
- # )
299
 
300
  def process_question(question: str):
301
  """
@@ -326,222 +256,56 @@ def process_question(question: str):
326
  # Mettez le résultat en cache à la fin
327
  question_cache[question] = (response, context)
328
 
329
- # # Custom CSS for right-aligned text in textboxes
330
- # custom_css = """
331
- # .rtl-text {
332
- # text-align: right !important;
333
- # direction: rtl !important;
334
- # }
335
- # .rtl-text textarea {
336
- # text-align: right !important;
337
- # direction: rtl !important;
338
- # }
339
- # """
340
-
341
- # # Gradio interface with queue
342
- # with gr.Blocks(css=custom_css) as iface:
343
- # with gr.Column():
344
- # input_text = gr.Textbox(
345
- # label="السؤال",
346
- # placeholder="اكتب سؤالك هنا...",
347
- # lines=2,
348
- # elem_classes="rtl-text"
349
- # )
350
-
351
- # with gr.Row():
352
- # answer_box = gr.Textbox(
353
- # label="الإجابة",
354
- # lines=4,
355
- # elem_classes="rtl-text"
356
- # )
357
- # context_box = gr.Textbox(
358
- # label="السياق المستخدم",
359
- # lines=8,
360
- # elem_classes="rtl-text"
361
- # )
362
-
363
- # submit_btn = gr.Button("إرسال")
364
-
365
- # submit_btn.click(
366
- # fn=process_question,
367
- # inputs=input_text,
368
- # outputs=[answer_box, context_box],
369
- # api_name="predict",
370
- # queue=True # Utiliser le système de queue pour un traitement asynchrone
371
- # )
372
-
373
- # if __name__ == "__main__":
374
- # iface.launch(
375
- # share=True,
376
- # server_name="0.0.0.0",
377
- # server_port=7860,
378
- # max_threads=3, # Controls concurrency
379
- # show_error=True
380
- # )
381
-
382
- # CSS personnalisé avec l'importation de Google Fonts
383
- # CSS personnalisé avec l'importation de Google Fonts
384
- custom_css = """
385
- /* Import Google Fonts - Noto Sans Arabic */
386
- @import url('https://fonts.googleapis.com/css2?family=Noto+Sans+Arabic:wght@300;400;500;600;700&display=swap');
387
-
388
- /* Styles généraux */
389
- :root {
390
- --primary-color: #2D3748;
391
- --secondary-color: #4A5568;
392
- --accent-color: #4299E1;
393
- --background-color: #F7FAFC;
394
- --border-radius: 8px;
395
- --font-family-arabic: 'Noto Sans Arabic', Arial, sans-serif;
396
- }
397
-
398
- /* Style de base */
399
- body {
400
- font-family: var(--font-family-arabic);
401
- background-color: var(--background-color);
402
- }
403
-
404
- /* Styles pour le texte RTL */
405
- .rtl-text {
406
- text-align: right !important;
407
- direction: rtl !important;
408
- font-family: var(--font-family-arabic) !important;
409
- }
410
-
411
- .rtl-text textarea {
412
- text-align: right !important;
413
- direction: rtl !important;
414
- padding: 1rem !important;
415
- border-radius: var(--border-radius) !important;
416
- border: 1px solid #E2E8F0 !important;
417
- background-color: white !important;
418
- font-size: 1.1rem !important;
419
- line-height: 1.6 !important;
420
- font-family: var(--font-family-arabic) !important;
421
- }
422
-
423
- /* Style du titre */
424
- .app-title {
425
- font-family: var(--font-family-arabic) !important;
426
- font-size: 2rem !important;
427
- font-weight: 700 !important;
428
- color: var(--primary-color) !important;
429
- margin-bottom: 2rem !important;
430
- text-align: center !important;
431
- }
432
-
433
- /* Styles des étiquettes */
434
- .rtl-text label {
435
- font-family: var(--font-family-arabic) !important;
436
- font-size: 1.2rem !important;
437
- font-weight: 600 !important;
438
- color: var(--primary-color) !important;
439
- margin-bottom: 0.5rem !important;
440
- }
441
-
442
- /* Style du bouton */
443
- button.primary-button {
444
- font-family: var(--font-family-arabic) !important;
445
- background-color: var(--accent-color) !important;
446
- color: white !important;
447
- padding: 0.75rem 1.5rem !important;
448
- border-radius: var(--border-radius) !important;
449
- font-weight: 600 !important;
450
- font-size: 1.1rem !important;
451
- transition: all 0.3s ease !important;
452
- }
453
-
454
- button.primary-button:hover {
455
- background-color: #3182CE !important;
456
- transform: translateY(-1px) !important;
457
- }
458
-
459
- /* Styles des boîtes de texte */
460
- .textbox-container {
461
- background-color: white !important;
462
- padding: 1.5rem !important;
463
- border-radius: var(--border-radius) !important;
464
- box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1) !important;
465
- margin-bottom: 1rem !important;
466
- }
467
-
468
- /* Animation de chargement */
469
- .loading {
470
- animation: pulse 2s infinite;
471
- }
472
-
473
- @keyframes pulse {
474
- 0% { opacity: 1; }
475
- 50% { opacity: 0.5; }
476
- 100% { opacity: 1; }
477
- }
478
-
479
- /* Style du statut */
480
- .status-text {
481
- font-family: var(--font-family-arabic) !important;
482
- text-align: center !important;
483
- color: var(--secondary-color) !important;
484
- font-size: 1rem !important;
485
- margin-top: 1rem !important;
486
- }
487
- """
488
-
489
- # Interface Gradio avec la nouvelle police
490
- with gr.Blocks(css=custom_css) as iface:
491
- with gr.Column(elem_classes="container"):
492
- gr.Markdown(
493
- "# نظام الأسئلة والأجوبة الذكي",
494
- elem_classes="app-title rtl-text"
495
- )
496
-
497
- with gr.Column(elem_classes="textbox-container"):
498
- input_text = gr.Textbox(
499
- label="السؤال",
500
- placeholder="اكتب سؤالك هنا...",
501
- lines=2,
502
- elem_classes="rtl-text"
503
- )
504
-
505
- with gr.Row():
506
- with gr.Column(scale=2):
507
- answer_box = gr.Textbox(
508
- label="الإجابة",
509
- lines=4,
510
- elem_classes="rtl-text textbox-container"
511
- )
512
- with gr.Column(scale=1):
513
- context_box = gr.Textbox(
514
- label="السياق المستخدم",
515
- lines=8,
516
- elem_classes="rtl-text textbox-container"
517
- )
518
-
519
- submit_btn = gr.Button(
520
- "إرسال السؤال",
521
- elem_classes="primary-button",
522
- variant="primary"
523
- )
524
 
525
- # Statut avec la nouvelle police
526
- status_text = gr.Markdown("", elem_classes="rtl-text status-text")
 
 
 
 
 
 
 
 
 
527
 
528
- def on_submit(question):
529
- status_text.value = "جاري معالجة السؤال..."
530
- return process_question(question)
531
 
532
- submit_btn.click(
533
- fn=on_submit,
534
- inputs=input_text,
535
- outputs=[answer_box, context_box, status_text],
536
- api_name="predict",
537
- queue=True
538
- )
 
 
 
 
 
 
 
 
 
539
 
540
- if __name__ == "__main__":
541
- iface.launch(
542
- share=True,
543
- server_name="0.0.0.0",
544
- server_port=7860,
545
- max_threads=3,
546
- show_error=True
547
- )
 
225
  ("human", "{question}")
226
  ])
227
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
228
 
 
 
 
 
 
 
 
 
229
 
230
  def process_question(question: str):
231
  """
 
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