MohammedNasser commited on
Commit
4fa6022
1 Parent(s): f348de6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -25
app.py CHANGED
@@ -157,49 +157,65 @@ p {
157
  }
158
  """
159
 
 
 
160
 
161
- # Create the Gradio interface
162
- with gr.Blocks(css=custom_css) as demo:
163
- pdf_input = gr.File(label="ارففع ملف PDF")
164
- chat_input = gr.Textbox(label="أدخل سؤالك هنا")
165
- chat_output = gr.Textbox(label="الرد الآلي")
166
- audio_output = gr.Audio(label="استمع إلى الرد")
167
- submit_button = gr.Button("إرسال")
168
  data = load_pdf(pdf_file)
169
  vectorstore = prepare_vectorstore(data)
 
170
 
171
- # Define the logic for processing the PDF and generating responses
172
- def process_pdf_and_chat(pdf_file, user_input):
173
-
174
- chain = create_chain(vectorstore)
175
 
176
- prompt = f"""
177
- 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.
 
 
 
178
 
 
 
179
  When responding, ensure the following:
180
  - Your answer directly reflects the content of the document.
181
  - If the requested information is not available in the document, clearly state that.
182
  - Keep your response concise yet comprehensive, addressing the question fully.
183
  - Always respond in formal Arabic, without using English.\n
184
-
185
  Question: {user_input}\n
186
  Helpful Answer:"""
187
 
188
- response = chain({"question": prompt})
189
- assistant_response = response["answer"]
 
 
 
190
 
191
- # Generate a unique identifier for the audio file
192
- audio_id = str(uuid.uuid4())
 
 
193
 
194
- # Create audio file
195
- tts = gTTS(text=assistant_response, lang='ar')
196
- audio_file = f"{audio_id}.mp3"
197
- tts.save(audio_file)
 
 
 
 
 
 
 
 
 
 
 
198
 
199
- return assistant_response, audio_file
 
 
200
 
201
- # Connect the button to the processing function
202
- submit_button.click(process_pdf_and_chat, inputs=[pdf_input, chat_input], outputs=[chat_output, audio_output])
 
203
 
204
  # Launch the Gradio app
205
  demo.launch()
 
157
  }
158
  """
159
 
160
+ def upload_pdf(pdf_file):
161
+ global vectorstore, chain # Use global variables to store state
162
 
 
 
 
 
 
 
 
163
  data = load_pdf(pdf_file)
164
  vectorstore = prepare_vectorstore(data)
165
+ chain = create_chain(vectorstore)
166
 
167
+ return "PDF uploaded and processed successfully!"
 
 
 
168
 
169
+ def chat(user_input):
170
+ global chain # Access the global chain variable
171
+
172
+ if chain is None:
173
+ return "Please upload a PDF file first.", None # Prompt user to upload a PDF
174
 
175
+ prompt = f"""
176
+ 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.
177
  When responding, ensure the following:
178
  - Your answer directly reflects the content of the document.
179
  - If the requested information is not available in the document, clearly state that.
180
  - Keep your response concise yet comprehensive, addressing the question fully.
181
  - Always respond in formal Arabic, without using English.\n
 
182
  Question: {user_input}\n
183
  Helpful Answer:"""
184
 
185
+ response = chain({"question": prompt})
186
+ assistant_response = response["answer"]
187
+
188
+ # Generate a unique identifier for the audio file
189
+ audio_id = str(uuid.uuid4())
190
 
191
+ # Create audio file
192
+ tts = gTTS(text=assistant_response, lang='ar')
193
+ audio_file = f"{audio_id}.mp3"
194
+ tts.save(audio_file)
195
 
196
+ return assistant_response, audio_file
197
+
198
+ with gr.Blocks(css=custom_css) as demo:
199
+
200
+ pdf_input = gr.File(label="اختر ملف PDF")
201
+
202
+ submit_button_pdf = gr.Button("ارفع الملف")
203
+
204
+ chat_input = gr.Textbox(label="أدخل سؤالك هنا")
205
+
206
+ chat_output = gr.Textbox(label="الرد الآلي")
207
+
208
+ audio_output = gr.Audio(label="استمع إلى الرد")
209
+
210
+ submit_button_chat = gr.Button("إرسال")
211
 
212
+
213
+ # Connect upload button to upload function
214
+ submit_button_pdf.click(upload_pdf, inputs=[pdf_input], outputs="text")
215
 
216
+
217
+ # Connect chat button to chat function
218
+ submit_button_chat.click(chat, inputs=[chat_input], outputs=[chat_output, audio_output])
219
 
220
  # Launch the Gradio app
221
  demo.launch()