Spaces:
Sleeping
Sleeping
File size: 2,412 Bytes
74a2182 aca6db8 74a2182 aca6db8 74a2182 aca6db8 74a2182 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
import os
import tempfile
import shutil
import chromadb
from langchain_community.document_loaders import PyPDFLoader
from langchain_community.vectorstores import Chroma
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain.chains import RetrievalQA
from langchain_community.llms import HuggingFaceHub
import gradio as gr
DB_DIR = "chroma_db"
os.makedirs(DB_DIR, exist_ok=True)
def load_and_index_pdf(pdf_file):
with tempfile.TemporaryDirectory() as tmpdir:
pdf_path = os.path.join(tmpdir, pdf_file.name)
shutil.copy(pdf_file.name, pdf_path)
loader = PyPDFLoader(pdf_path)
documents = loader.load_and_split()
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
vectordb = Chroma.from_documents(documents, embedding=embeddings, persist_directory=DB_DIR)
vectordb.persist()
return "✅ PDF تمت معالجته بنجاح! يمكنك الآن طرح الأسئلة."
def answer_question(question):
if not os.path.exists(DB_DIR) or not os.listdir(DB_DIR):
return "❌ الرجاء رفع ملف PDF أولًا."
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
vectordb = Chroma(persist_directory=DB_DIR, embedding_function=embeddings)
retriever = vectordb.as_retriever()
llm = HuggingFaceHub(repo_id="mistralai/Mistral-7B-Instruct-v0.2", model_kwargs={"temperature": 0.5, "max_new_tokens": 512})
qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=retriever)
return qa.run(question)
with gr.Blocks() as demo:
gr.Markdown("## 🤖 Smart PDF Assistant - مساعدك الذكي في قراءة وفهم ملفات PDF")
with gr.Tab("📁 تحميل PDF"):
pdf_input = gr.File(label="ارفع ملف PDF", file_types=[".pdf"])
upload_output = gr.Textbox(label="حالة المعالجة")
upload_btn = gr.Button("📄 معالجة الملف")
upload_btn.click(fn=load_and_index_pdf, inputs=pdf_input, outputs=upload_output)
with gr.Tab("❓ اسأل سؤالك"):
question = gr.Textbox(label="اكتب سؤالك هنا")
answer = gr.Textbox(label="الإجابة", lines=5)
ask_btn = gr.Button("🔍 إرسال السؤال")
ask_btn.click(fn=answer_question, inputs=question, outputs=answer)
demo.launch()
|