Spaces:
Sleeping
Sleeping
MohammedNasser
commited on
Commit
•
f348de6
1
Parent(s):
0c4a24a
Update app.py
Browse files
app.py
CHANGED
@@ -157,53 +157,52 @@ p {
|
|
157 |
}
|
158 |
"""
|
159 |
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
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 |
|