MohammedNasser commited on
Commit
6d432a5
1 Parent(s): 351445c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -17
app.py CHANGED
@@ -207,10 +207,11 @@ def upload_pdf(pdf_file):
207
  chain = create_chain(vectorstore)
208
  return "تم تحميل الملف بنجاح !", True
209
 
210
-
211
  def chat(user_input):
212
- global chain # Access the global chain variable
213
 
 
214
 
215
  prompt = f"""
216
  You are an expert Arabic-language assistant specialized in analyzing and responding to queries about Arabic PDF documents. Your responses should be precise, informative, and reflect the professional tone and structure expected in formal Arabic communication. Focus on extracting and presenting relevant information from the document clearly and systematically, while avoiding colloquial or informal language.
@@ -225,6 +226,7 @@ def chat(user_input):
225
  response = chain({"question": prompt})
226
  assistant_response = response["answer"]
227
 
 
228
  # Generate a unique identifier for the audio file
229
  audio_id = str(uuid.uuid4())
230
 
@@ -233,20 +235,32 @@ def chat(user_input):
233
  audio_file = f"{audio_id}.mp3"
234
  tts.save(audio_file)
235
 
236
-
237
- return assistant_response, audio_file
238
 
239
  with gr.Blocks(css=custom_css) as demo:
240
- pdf_input = gr.File(label="اختر ملف PDF")
241
- output_label = gr.HTML(value='') # Replaced Textbox with HTML for label
242
- submit_button_pdf = gr.Button("ارفع الملف", interactive=False)
 
 
 
 
 
 
 
 
243
 
244
- chat_input = gr.Textbox(label="أدخل سؤالك هنا 💬", rtl=True, visible=False)
245
- chat_output = gr.Textbox(label="الرد الآلي 🤖", rtl=True, visible=False)
246
- audio_output = gr.Audio(label="🔊", visible=False)
 
 
247
  with gr.Group():
248
- submit_button_chat = gr.Button("إرسال", interactive=True, visible=False)
249
- clear_btn = gr.Button("مسح", interactive=True, visible=False)
 
 
250
 
251
  def handle_file_upload(pdf):
252
  output_label.value=''
@@ -271,16 +285,16 @@ with gr.Blocks(css=custom_css) as demo:
271
  return gr.update(value=value), gr.update(visible=True), gr.update(interactive=False), gr.update(visible=True), gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
272
 
273
  def clear_chat():
274
- return "", "", None
275
 
276
  # When the user uploads a file, validate it and then allow PDF upload
277
- pdf_input.change(handle_file_upload, inputs=pdf_input, outputs=[output_label,submit_button_pdf, submit_button_chat, chat_output, chat_input, audio_output, clear_btn])
278
 
279
  # When the user presses the upload button, process the PDF and enable other components
280
- submit_button_pdf.click(process_pdf_and_enable_components, inputs=pdf_input, outputs=[output_label, submit_button_chat, submit_button_pdf, chat_output, chat_input, audio_output, clear_btn])
281
- clear_btn.click(clear_chat, outputs=[chat_input, chat_output, audio_output])
282
  # Chat button connection
283
- submit_button_chat.click(chat, inputs=chat_input, outputs=[chat_output, audio_output])
284
 
285
 
286
  # Launch the Gradio app
 
207
  chain = create_chain(vectorstore)
208
  return "تم تحميل الملف بنجاح !", True
209
 
210
+ chat_history = []
211
  def chat(user_input):
212
+ global chain chat_history # Access the global chain variable
213
 
214
+ chat_history.append({"role": "user", "content": user_input})
215
 
216
  prompt = f"""
217
  You are an expert Arabic-language assistant specialized in analyzing and responding to queries about Arabic PDF documents. Your responses should be precise, informative, and reflect the professional tone and structure expected in formal Arabic communication. Focus on extracting and presenting relevant information from the document clearly and systematically, while avoiding colloquial or informal language.
 
226
  response = chain({"question": prompt})
227
  assistant_response = response["answer"]
228
 
229
+ chat_history.append({"role": "assistant", "content": assistant_response})
230
  # Generate a unique identifier for the audio file
231
  audio_id = str(uuid.uuid4())
232
 
 
235
  audio_file = f"{audio_id}.mp3"
236
  tts.save(audio_file)
237
 
238
+ history_display = [(h["role"], h["content"]) for h in chat_history]
239
+ return history_display, audio_file
240
 
241
  with gr.Blocks(css=custom_css) as demo:
242
+ with gr.Row():
243
+ gr.Markdown("<h2 style='text-align: center; color: #4A90E2;'>المساع العربي للدردشة مع الـ PDF</h2>")
244
+
245
+ with gr.Row():
246
+ pdf_input = gr.File(label="اختر ملف PDF")
247
+ output_label = gr.HTML(value='') # Replaced Textbox with HTML for label
248
+ with gr.Row():
249
+ submit_button_pdf = gr.Button("ارفع الملف", interactive=False)
250
+ with gr.Row():
251
+ with gr.Column(scale=3):
252
+ chatbot = gr.Chatbot(label="الدردشة العربية", elem_id="chatbox").style(height=400, rounded=True, shadow=True)
253
 
254
+ with gr.Row():
255
+ chat_input = gr.Textbox(label="أدخل سؤالك هنا 💬", rtl=True, visible=Falseو placeholder="أخل سؤالك هنا ..", elem_id="inputbox", lines=2)
256
+ #chat_output = gr.Textbox(label="الرد الآلي 🤖", rtl=True, visible=False)
257
+ with gr.Row():
258
+ audio_output = gr.Audio(label="🔊", visible=False)
259
  with gr.Group():
260
+ with gr.Row():
261
+ submit_button_chat = gr.Button("إرسال", interactive=True, visible=False).style(full_width=True)
262
+ with gr.Row():
263
+ clear_btn = gr.Button("مسح", interactive=True, visible=False).style(full_width=True)
264
 
265
  def handle_file_upload(pdf):
266
  output_label.value=''
 
285
  return gr.update(value=value), gr.update(visible=True), gr.update(interactive=False), gr.update(visible=True), gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
286
 
287
  def clear_chat():
288
+ return "", None
289
 
290
  # When the user uploads a file, validate it and then allow PDF upload
291
+ pdf_input.change(handle_file_upload, inputs=pdf_input, outputs=[output_label,submit_button_pdf, submit_button_chat, chatbot, chat_input, audio_output, clear_btn])
292
 
293
  # When the user presses the upload button, process the PDF and enable other components
294
+ submit_button_pdf.click(process_pdf_and_enable_components, inputs=pdf_input, outputs=[output_label, submit_button_chat, submit_button_pdf, chatbot, chat_input, audio_output, clear_btn])
295
+ clear_btn.click(clear_chat, outputs=[chat_input, audio_output])
296
  # Chat button connection
297
+ submit_button_chat.click(chat, inputs=chat_input, outputs=[chatbot, audio_output])
298
 
299
 
300
  # Launch the Gradio app