Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,89 +1,98 @@
|
|
1 |
-
|
2 |
import gradio as gr
|
3 |
import os
|
4 |
import tempfile
|
5 |
import shutil
|
6 |
-
import
|
7 |
-
import docx
|
8 |
-
import faiss
|
9 |
-
import torch
|
10 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
11 |
-
from sentence_transformers import SentenceTransformer
|
12 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
13 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
14 |
-
from
|
|
|
|
|
|
|
15 |
|
16 |
-
|
|
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
qa_model = AutoModelForCausalLM.from_pretrained(qa_model_name).to(device)
|
23 |
|
24 |
-
|
25 |
-
index = None
|
26 |
-
docs = []
|
27 |
|
28 |
-
def
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
elif
|
33 |
-
|
34 |
-
return "\n".join([para.text for para in doc.paragraphs])
|
35 |
else:
|
36 |
-
raise ValueError("صيغة
|
37 |
-
|
38 |
-
def process_files(files):
|
39 |
-
global index, docs
|
40 |
-
all_text = ""
|
41 |
-
for file in files:
|
42 |
-
text = extract_text(file.name)
|
43 |
-
all_text += text + "\n"
|
44 |
|
45 |
-
|
46 |
-
|
47 |
-
texts =
|
|
|
|
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
-
def
|
57 |
-
|
58 |
-
|
59 |
-
return "❌ الرجاء رفع الكتب أولاً."
|
60 |
|
61 |
-
|
62 |
-
|
63 |
-
|
|
|
|
|
|
|
64 |
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
answer = qa_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
70 |
-
return answer
|
71 |
|
72 |
-
with gr.Blocks(
|
73 |
-
gr.
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
|
86 |
-
|
87 |
-
ask_button.click(
|
88 |
|
89 |
-
demo.launch()
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
import tempfile
|
4 |
import shutil
|
5 |
+
from langchain_community.document_loaders import PyMuPDFLoader, UnstructuredWordDocumentLoader
|
|
|
|
|
|
|
|
|
|
|
6 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
7 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
8 |
+
from langchain_community.vectorstores import FAISS
|
9 |
+
from langchain.chains import RetrievalQA
|
10 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
11 |
+
import torch
|
12 |
|
13 |
+
EMBEDDING_MODEL_NAME = "CAMeL-Lab/bert-base-arabic-camelbert-mix"
|
14 |
+
QA_MODEL_NAME = "mosaicml/mpt-7b-storywriter"
|
15 |
|
16 |
+
embedding_model = HuggingFaceEmbeddings(model_name=EMBEDDING_MODEL_NAME)
|
17 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
18 |
+
qa_tokenizer = AutoTokenizer.from_pretrained(QA_MODEL_NAME, trust_remote_code=True)
|
19 |
+
qa_model = AutoModelForSeq2SeqLM.from_pretrained(QA_MODEL_NAME, trust_remote_code=True).to(device)
|
|
|
20 |
|
21 |
+
vectordb = None
|
|
|
|
|
22 |
|
23 |
+
def load_document(file_path):
|
24 |
+
ext = os.path.splitext(file_path)[1].lower()
|
25 |
+
if ext == ".pdf":
|
26 |
+
loader = PyMuPDFLoader(file_path)
|
27 |
+
elif ext in [".doc", ".docx"]:
|
28 |
+
loader = UnstructuredWordDocumentLoader(file_path)
|
|
|
29 |
else:
|
30 |
+
raise ValueError("صيغة الملف غير مدعومة.")
|
31 |
+
return loader.load()
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
+
def train_from_documents(documents):
|
34 |
+
splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
|
35 |
+
texts = splitter.split_documents(documents)
|
36 |
+
vectorstore = FAISS.from_documents(texts, embedding_model)
|
37 |
+
return vectorstore
|
38 |
|
39 |
+
def upload_files(files):
|
40 |
+
global vectordb
|
41 |
+
temp_dir = tempfile.mkdtemp()
|
42 |
+
all_docs = []
|
43 |
+
|
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.read())
|
48 |
+
docs = load_document(file_path)
|
49 |
+
all_docs.extend(docs)
|
50 |
+
|
51 |
+
vectordb = train_from_documents(all_docs)
|
52 |
+
shutil.rmtree(temp_dir)
|
53 |
+
return "✅ النظام جاهز للإجابة على أسئلتك!"
|
54 |
|
55 |
+
def answer_question(question):
|
56 |
+
if vectordb is None:
|
57 |
+
return "⚠️ الرجاء رفع الملفات أولاً."
|
|
|
58 |
|
59 |
+
retriever = vectordb.as_retriever(search_kwargs={"k": 5})
|
60 |
+
qa_chain = RetrievalQA.from_chain_type(
|
61 |
+
llm=None,
|
62 |
+
retriever=retriever,
|
63 |
+
return_source_documents=True
|
64 |
+
)
|
65 |
|
66 |
+
relevant_docs = qa_chain.retriever.get_relevant_documents(question)
|
67 |
+
context = "\n".join(doc.page_content for doc in relevant_docs)
|
68 |
+
|
69 |
+
inputs = qa_tokenizer(
|
70 |
+
f"أجب بالعربية فقط بناءً على السياق التالي:\n{context}\nالسؤال: {question}",
|
71 |
+
return_tensors="pt",
|
72 |
+
truncation=True,
|
73 |
+
max_length=1024
|
74 |
+
).to(device)
|
75 |
+
|
76 |
+
with torch.no_grad():
|
77 |
+
outputs = qa_model.generate(**inputs, max_length=300)
|
78 |
answer = qa_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
79 |
+
return answer
|
80 |
|
81 |
+
with gr.Blocks(title="محاكاة دماغ المؤلف") as demo:
|
82 |
+
with gr.Row():
|
83 |
+
with gr.Column():
|
84 |
+
gr.Markdown("## 📚 ارفع كتبك هنا")
|
85 |
+
file_uploader = gr.File(file_types=[".pdf", ".doc", ".docx"], file_count="multiple")
|
86 |
+
upload_button = gr.Button("🚀 ابدأ التدريب")
|
87 |
+
training_status = gr.Textbox(label="حالة التدريب", interactive=False)
|
88 |
+
|
89 |
+
with gr.Column():
|
90 |
+
gr.Markdown("## ❓ اطرح سؤالك")
|
91 |
+
question_input = gr.Textbox(label="سؤالك", placeholder="اكتب سؤالك هنا...")
|
92 |
+
ask_button = gr.Button("✉️ أرسل السؤال!")
|
93 |
+
answer_output = gr.Textbox(label="الإجابة", interactive=False)
|
94 |
|
95 |
+
upload_button.click(upload_files, inputs=[file_uploader], outputs=[training_status])
|
96 |
+
ask_button.click(answer_question, inputs=[question_input], outputs=[answer_output])
|
97 |
|
98 |
+
demo.launch(share=True)
|