ramysaidagieb commited on
Commit
9872af4
·
verified ·
1 Parent(s): c62a9b9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -18
app.py CHANGED
@@ -1,6 +1,4 @@
1
  import gradio as gr
2
- import tempfile
3
- import os
4
  import faiss
5
  import numpy as np
6
  from transformers import AutoTokenizer, AutoModel
@@ -36,25 +34,21 @@ def process_files(files, progress=gr.Progress()):
36
  return "⚠️ لم يتم رفع أي ملفات. الرجاء رفع كتاب واحد على الأقل."
37
 
38
  texts = []
39
- temp_dir = tempfile.mkdtemp()
40
 
41
  try:
42
  # Step 1: Extract text
43
  progress(0.1, desc="جاري استخراج النصوص من الكتب...")
44
- for file in files:
45
- file_path = os.path.join(temp_dir, file.name)
46
- with open(file_path, "wb") as f:
47
- f.write(file.file.read())
48
-
49
- if file.name.endswith(".pdf"):
50
- text = extract_text_from_pdf(file_path)
51
- elif file.name.endswith(".docx") or file.name.endswith(".doc"):
52
- text = extract_text_from_docx(file_path)
53
- else:
54
- continue
55
-
56
- if text:
57
- texts.append(text)
58
 
59
  if len(texts) == 0:
60
  return "⚠️ لم يتم استخراج نصوص صالحة من الملفات."
@@ -105,7 +99,7 @@ def answer_question(question):
105
  return f"❌ حدث خطأ أثناء الإجابة: {str(e)}"
106
 
107
  with gr.Blocks() as demo:
108
- gr.Markdown("# 📚 نظام محاكاة دماغ المؤلف العربي\nرفع كتبك ودرب النظام للإجابة على أسئلتك باللغة العربية فقط.")
109
 
110
  with gr.Row():
111
  file_input = gr.File(label="📄 ارفع ملفات الكتب (PDF أو DOCX)", file_types=['.pdf', '.docx', '.doc'], file_count="multiple")
 
1
  import gradio as gr
 
 
2
  import faiss
3
  import numpy as np
4
  from transformers import AutoTokenizer, AutoModel
 
34
  return "⚠️ لم يتم رفع أي ملفات. الرجاء رفع كتاب واحد على الأقل."
35
 
36
  texts = []
 
37
 
38
  try:
39
  # Step 1: Extract text
40
  progress(0.1, desc="جاري استخراج النصوص من الكتب...")
41
+ for file_path in files:
42
+ if isinstance(file_path, str):
43
+ if file_path.endswith(".pdf"):
44
+ text = extract_text_from_pdf(file_path)
45
+ elif file_path.endswith(".docx") or file_path.endswith(".doc"):
46
+ text = extract_text_from_docx(file_path)
47
+ else:
48
+ continue
49
+
50
+ if text:
51
+ texts.append(text)
 
 
 
52
 
53
  if len(texts) == 0:
54
  return "⚠️ لم يتم استخراج نصوص صالحة من الملفات."
 
99
  return f"❌ حدث خطأ أثناء الإجابة: {str(e)}"
100
 
101
  with gr.Blocks() as demo:
102
+ gr.Markdown("# 📚 نظام محاكاة دماغ المؤلف العربي\nارفع كتبك ودرب النظام للإجابة على أسئلتك باللغة العربية فقط.")
103
 
104
  with gr.Row():
105
  file_input = gr.File(label="📄 ارفع ملفات الكتب (PDF أو DOCX)", file_types=['.pdf', '.docx', '.doc'], file_count="multiple")