Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,15 +2,14 @@ import os
|
|
2 |
import gradio as gr
|
3 |
from langchain_community.vectorstores import Chroma
|
4 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
|
|
5 |
from langchain_community.document_loaders import PyPDFLoader
|
6 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
7 |
from langchain.chains import RetrievalQA
|
8 |
-
from langchain.llms import HuggingFaceHub
|
9 |
-
from langchain.prompts import PromptTemplate
|
10 |
-
from huggingface_hub import login
|
11 |
|
12 |
-
#
|
13 |
-
|
|
|
14 |
|
15 |
def process_pdf_and_answer(pdf_path, question):
|
16 |
# تحميل ملف PDF
|
@@ -21,32 +20,37 @@ def process_pdf_and_answer(pdf_path, question):
|
|
21 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
|
22 |
texts = text_splitter.split_documents(pages)
|
23 |
|
24 |
-
# التضمين
|
25 |
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2")
|
26 |
vectorstore = Chroma.from_documents(texts, embedding=embeddings)
|
27 |
|
28 |
-
# إعداد LLM
|
29 |
-
llm =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
-
#
|
32 |
qa_chain = RetrievalQA.from_chain_type(llm=llm, retriever=vectorstore.as_retriever(), return_source_documents=True)
|
33 |
|
34 |
-
#
|
35 |
result = qa_chain({"query": question})
|
36 |
-
|
37 |
-
return answer
|
38 |
|
39 |
# واجهة Gradio
|
40 |
with gr.Blocks() as demo:
|
41 |
-
gr.Markdown("##
|
42 |
-
|
43 |
with gr.Row():
|
44 |
-
file_input = gr.File(label="
|
45 |
-
|
46 |
-
question_input = gr.Textbox(label="❓ اكتب سؤالك هنا", placeholder="ما هو محتوى الفصل الأول؟")
|
47 |
output = gr.Textbox(label="📝 الإجابة", lines=10)
|
48 |
-
|
49 |
-
submit_btn = gr.Button("🔍 استخرج الإجابة")
|
50 |
|
51 |
def handle_submit(file, question):
|
52 |
if file is None or question.strip() == "":
|
@@ -55,6 +59,6 @@ with gr.Blocks() as demo:
|
|
55 |
|
56 |
submit_btn.click(handle_submit, inputs=[file_input, question_input], outputs=output)
|
57 |
|
58 |
-
#
|
59 |
if __name__ == "__main__":
|
60 |
demo.launch()
|
|
|
2 |
import gradio as gr
|
3 |
from langchain_community.vectorstores import Chroma
|
4 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
5 |
+
from langchain_community.llms import CTransformers
|
6 |
from langchain_community.document_loaders import PyPDFLoader
|
7 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
8 |
from langchain.chains import RetrievalQA
|
|
|
|
|
|
|
9 |
|
10 |
+
# مسار النموذج المحلي (يمكن تحميله مسبقًا ووضعه في هذا المسار)
|
11 |
+
MODEL_PATH = "TheBloke/Mistral-7B-Instruct-v0.2-GGUF" # استخدم gguf فقط
|
12 |
+
MODEL_FILE = "mistral-7b-instruct-v0.2.Q4_K_M.gguf"
|
13 |
|
14 |
def process_pdf_and_answer(pdf_path, question):
|
15 |
# تحميل ملف PDF
|
|
|
20 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
|
21 |
texts = text_splitter.split_documents(pages)
|
22 |
|
23 |
+
# التضمين باستخدام نموذج مجاني
|
24 |
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2")
|
25 |
vectorstore = Chroma.from_documents(texts, embedding=embeddings)
|
26 |
|
27 |
+
# إعداد LLM محلي عبر ctransformers
|
28 |
+
llm = CTransformers(
|
29 |
+
model=MODEL_FILE,
|
30 |
+
model_path=MODEL_PATH,
|
31 |
+
model_type="mistral",
|
32 |
+
config={
|
33 |
+
"max_new_tokens": 512,
|
34 |
+
"temperature": 0.1
|
35 |
+
}
|
36 |
+
)
|
37 |
|
38 |
+
# بناء سلسلة RAG
|
39 |
qa_chain = RetrievalQA.from_chain_type(llm=llm, retriever=vectorstore.as_retriever(), return_source_documents=True)
|
40 |
|
41 |
+
# الإجابة على السؤال
|
42 |
result = qa_chain({"query": question})
|
43 |
+
return result["result"]
|
|
|
44 |
|
45 |
# واجهة Gradio
|
46 |
with gr.Blocks() as demo:
|
47 |
+
gr.Markdown("## 📄🤖 مساعد PDF الذكي - بدون API")
|
48 |
+
|
49 |
with gr.Row():
|
50 |
+
file_input = gr.File(label="📥 ملف PDF", type="filepath", file_types=[".pdf"])
|
51 |
+
question_input = gr.Textbox(label="❓ سؤالك", placeholder="ما هو موضوع الصفحة الأولى؟")
|
|
|
52 |
output = gr.Textbox(label="📝 الإجابة", lines=10)
|
53 |
+
submit_btn = gr.Button("🔎 استخرج الإجابة")
|
|
|
54 |
|
55 |
def handle_submit(file, question):
|
56 |
if file is None or question.strip() == "":
|
|
|
59 |
|
60 |
submit_btn.click(handle_submit, inputs=[file_input, question_input], outputs=output)
|
61 |
|
62 |
+
# تشغيل التطبيق
|
63 |
if __name__ == "__main__":
|
64 |
demo.launch()
|