Trabis commited on
Commit
df0e374
1 Parent(s): 0022f8a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +103 -50
app.py CHANGED
@@ -245,13 +245,65 @@ prompt_template = ChatPromptTemplate.from_messages([
245
  # question_cache[question] = result
246
  # return result
247
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
248
  def process_question(question: str):
249
  """
250
  Process the question and yield the answer progressively.
251
  """
252
  # Check cache first
253
  if question in question_cache:
254
- return question_cache[question] # Retourne directement depuis le cache si disponible
255
 
256
  relevant_docs = retriever(question)
257
  context = "\n".join([doc.page_content for doc in relevant_docs])
@@ -272,56 +324,57 @@ def process_question(question: str):
272
  yield response, context # Renvoie la réponse mise à jour et le contexte
273
 
274
  # Mettez le résultat en cache à la fin
275
- question_cache[question] = (response, context)
276
-
277
- # Custom CSS for right-aligned text in textboxes
278
- custom_css = """
279
- .rtl-text {
280
- text-align: right !important;
281
- direction: rtl !important;
282
- }
283
- .rtl-text textarea {
284
- text-align: right !important;
285
- direction: rtl !important;
286
- }
287
- """
288
-
289
- # Gradio interface with queue
290
- with gr.Blocks(css=custom_css) as iface:
291
- with gr.Column():
292
- input_text = gr.Textbox(
293
- label="السؤال",
294
- placeholder="اكتب سؤالك هنا...",
295
- lines=2,
296
- elem_classes="rtl-text"
297
- )
298
 
299
- with gr.Row():
300
- answer_box = gr.Textbox(
301
- label="الإجابة",
302
- lines=4,
303
- elem_classes="rtl-text"
304
- )
305
- context_box = gr.Textbox(
306
- label="السياق المستخدم",
307
- lines=8,
308
- elem_classes="rtl-text"
309
- )
310
 
311
- submit_btn = gr.Button("إرسال")
312
 
313
- submit_btn.click(
314
- fn=process_question,
315
- inputs=input_text,
316
- outputs=[answer_box, context_box],
317
- api_name="predict"
318
- )
319
-
320
- if __name__ == "__main__":
321
- iface.launch(
322
- share=True,
323
- server_name="0.0.0.0",
324
- server_port=7860,
325
- max_threads=3, # Controls concurrency
326
- show_error=True
 
327
  )
 
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
  """
302
  Process the question and yield the answer progressively.
303
  """
304
  # Check cache first
305
  if question in question_cache:
306
+ yield question_cache[question] # Retourne directement depuis le cache si disponible
307
 
308
  relevant_docs = retriever(question)
309
  context = "\n".join([doc.page_content for doc in relevant_docs])
 
324
  yield response, context # Renvoie la réponse mise à jour et le contexte
325
 
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
  )