Spaces:
Sleeping
Sleeping
MohammedNasser
commited on
Commit
•
b958c67
1
Parent(s):
73f55fa
Update app.py
Browse files
app.py
CHANGED
@@ -42,28 +42,6 @@ for folder in [UPLOAD_FOLDER, AUDIO_FOLDER]:
|
|
42 |
|
43 |
|
44 |
|
45 |
-
# Function to check if the file is a valid PDF in Arabic and less than 10MB
|
46 |
-
def validate_pdf(pdf):
|
47 |
-
if pdf is None:
|
48 |
-
return "لم يتم اختيار أي ملف", False
|
49 |
-
if not pdf.name.endswith(".pdf"):
|
50 |
-
return "الملف الذي اخترته ليس PDF", False
|
51 |
-
if os.path.getsize(pdf.name) > 10 * 1024 * 1024:
|
52 |
-
return "حجم الملف أكبر من 10 ميجا بايت", False
|
53 |
-
|
54 |
-
# Check if PDF content is Arabic
|
55 |
-
reader = PdfReader(pdf.name)
|
56 |
-
text = ""
|
57 |
-
for page in reader.pages:
|
58 |
-
text += page.extract_text()
|
59 |
-
|
60 |
-
try:
|
61 |
-
if detect(text) != "ar":
|
62 |
-
return "الملف ليس باللغة العربية", False
|
63 |
-
except:
|
64 |
-
return "فشل في تحليل اللغة", False
|
65 |
-
|
66 |
-
return "الملف صالح للدردشة", True
|
67 |
|
68 |
|
69 |
def load_pdf(file_path):
|
@@ -197,6 +175,28 @@ label {
|
|
197 |
text-align: right; /* Ensure RTL alignment */
|
198 |
}
|
199 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
200 |
|
201 |
def upload_pdf(pdf_file):
|
202 |
global vectorstore, chain # Use global variables to store state
|
@@ -253,18 +253,18 @@ with gr.Blocks(css=custom_css) as demo:
|
|
253 |
|
254 |
if is_valid:
|
255 |
# Enable the upload button if the file is valid
|
256 |
-
|
257 |
-
return
|
258 |
else:
|
259 |
-
|
260 |
-
return
|
261 |
|
262 |
def process_pdf_and_enable_components(pdf):
|
263 |
# Process PDF and activate the other components
|
264 |
output_label.value='<span style="color:blue">جاري معالجة الملف...</span>'
|
265 |
message, is_valid = upload_pdf(pdf)
|
266 |
-
|
267 |
-
return
|
268 |
|
269 |
|
270 |
# When the user uploads a file, validate it and then allow PDF upload
|
|
|
42 |
|
43 |
|
44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
|
47 |
def load_pdf(file_path):
|
|
|
175 |
text-align: right; /* Ensure RTL alignment */
|
176 |
}
|
177 |
"""
|
178 |
+
# Function to check if the file is a valid PDF in Arabic and less than 10MB
|
179 |
+
def validate_pdf(pdf):
|
180 |
+
if pdf is None:
|
181 |
+
return "لم يتم اختيار أي ملف", False
|
182 |
+
if not pdf.name.endswith(".pdf"):
|
183 |
+
return "الملف الذي اخترته ليس PDF", False
|
184 |
+
if os.path.getsize(pdf.name) > 10 * 1024 * 1024:
|
185 |
+
return "حجم الملف أكبر من 10 ميجا بايت", False
|
186 |
+
|
187 |
+
# Check if PDF content is Arabic
|
188 |
+
reader = PdfReader(pdf.name)
|
189 |
+
text = ""
|
190 |
+
for page in reader.pages:
|
191 |
+
text += page.extract_text()
|
192 |
+
|
193 |
+
try:
|
194 |
+
if detect(text) != "ar":
|
195 |
+
return "الملف ليس باللغة العربية", False
|
196 |
+
except:
|
197 |
+
return "فشل في تحليل اللغة", False
|
198 |
+
|
199 |
+
return "الملف صالح للدردشة", True
|
200 |
|
201 |
def upload_pdf(pdf_file):
|
202 |
global vectorstore, chain # Use global variables to store state
|
|
|
253 |
|
254 |
if is_valid:
|
255 |
# Enable the upload button if the file is valid
|
256 |
+
value=''
|
257 |
+
return gr.HTML.update(value=value), gr.update(interactive=True), gr.update(interactive=False), gr.update(label=""), gr.update(label="")
|
258 |
else:
|
259 |
+
value=f'<span style="color:{color}">{message}</span>'
|
260 |
+
return gr.HTML.update(value=value), gr.update(interactive=False), gr.update(interactive=False), gr.update(label=""), gr.update(label="")
|
261 |
|
262 |
def process_pdf_and_enable_components(pdf):
|
263 |
# Process PDF and activate the other components
|
264 |
output_label.value='<span style="color:blue">جاري معالجة الملف...</span>'
|
265 |
message, is_valid = upload_pdf(pdf)
|
266 |
+
value=f'<span style="color:green">{message}</span>'
|
267 |
+
return gr.HTML.update(value=value), gr.update(interactive=True), gr.update(interactive=False)
|
268 |
|
269 |
|
270 |
# When the user uploads a file, validate it and then allow PDF upload
|