MohammedNasser commited on
Commit
f348de6
1 Parent(s): 0c4a24a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -48
app.py CHANGED
@@ -157,53 +157,52 @@ p {
157
  }
158
  """
159
 
160
- def main():
161
- # Create the Gradio interface
162
- with gr.Blocks(css=custom_css) as demo:
163
- pdf_input = gr.File(label="Upload PDF")
164
- chat_input = gr.Textbox(label="Your question")
165
- chat_output = gr.Textbox(label="Assistant's response")
166
- audio_output = gr.Audio(label="Audio response")
167
- submit_button = gr.Button("Submit")
168
-
169
- # Define the logic for processing the PDF and generating responses
170
- def process_pdf_and_chat(pdf_file, user_input):
171
- data = load_pdf(pdf_file)
172
- vectorstore = prepare_vectorstore(data)
173
- chain = create_chain(vectorstore)
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
-
178
- When responding, ensure the following:
179
- - Your answer directly reflects the content of the document.
180
- - If the requested information is not available in the document, clearly state that.
181
- - Keep your response concise yet comprehensive, addressing the question fully.
182
- - Always respond in formal Arabic, without using English.\n
183
-
184
- Question: {user_input}\n
185
- Helpful Answer:"""
186
-
187
- response = chain({"question": prompt})
188
- assistant_response = response["answer"]
189
-
190
- # Generate a unique identifier for the audio file
191
- audio_id = str(uuid.uuid4())
192
-
193
- # Create audio file
194
- tts = gTTS(text=assistant_response, lang='ar')
195
- audio_file = f"{audio_id}.mp3"
196
- tts.save(audio_file)
197
-
198
- return assistant_response, audio_file
199
-
200
- # Connect the button to the processing function
201
- submit_button.click(process_pdf_and_chat, inputs=[pdf_input, chat_input], outputs=[chat_output, audio_output])
202
-
203
- # Launch the Gradio app
204
- demo.launch()
205
-
206
- if __name__ == '__main__':
207
- main()
208
 
209
 
 
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()
206
+
 
207
 
208