ramysaidagieb commited on
Commit
5259cb2
·
verified ·
1 Parent(s): d99e578

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -72
app.py DELETED
@@ -1,72 +0,0 @@
1
- import os
2
- import shutil
3
- import gradio as gr
4
-
5
- from langchain_community.document_loaders import PyPDFLoader
6
- from langchain.text_splitter import RecursiveCharacterTextSplitter
7
- from langchain.vectorstores import Chroma
8
- from langchain.embeddings import HuggingFaceEmbeddings
9
- from langchain.chains import RetrievalQA
10
- from langchain_community.llms import HuggingFaceHub
11
-
12
- # إعداد مجلد الحفظ
13
- os.makedirs("docs", exist_ok=True)
14
-
15
- # نسخ آمن للملف لتفادي الخطأ
16
- def safe_copy(src_path, dst_folder="docs"):
17
- filename = os.path.basename(src_path)
18
- dst_path = os.path.join(dst_folder, filename)
19
- if os.path.abspath(src_path) != os.path.abspath(dst_path):
20
- shutil.copy(src_path, dst_path)
21
- return dst_path
22
-
23
- # تحميل ومعالجة الملف
24
- def process_file(file):
25
- try:
26
- file_path = safe_copy(file.name)
27
- loader = PyPDFLoader(file_path)
28
- pages = loader.load_and_split()
29
- text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
30
- texts = text_splitter.split_documents(pages)
31
- embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2")
32
- db = Chroma.from_documents(texts, embeddings)
33
- retriever = db.as_retriever(search_kwargs={"k": 3})
34
- llm = HuggingFaceHub(repo_id="mistralai/Mistral-7B-Instruct-v0.2", model_kwargs={"temperature":0.5, "max_new_tokens":512})
35
- qa_chain = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=retriever, return_source_documents=True)
36
- return qa_chain, "تم تحميل الملف بنجاح، يمكنك الآن طرح الأسئلة."
37
- except Exception as e:
38
- return None, f"حدث خطأ: {e}"
39
-
40
- # متغير عالمي للاحتفاظ بالسلسلة
41
- qa_chain = None
42
-
43
- # الدالة التي تجيب على السؤال
44
- def answer_question(question):
45
- if qa_chain is None:
46
- return "الرجاء رفع ملف أولاً."
47
- result = qa_chain({"query": question})
48
- answer = result["result"]
49
- sources = "\n".join([doc.metadata.get("source", "") for doc in result["source_documents"]])
50
- return f"🔹 **الإجابة:**\n{answer}\n\n🔹 **المصدر:**\n{sources}"
51
-
52
- # واجهة Gradio
53
- with gr.Blocks(title="Smart PDF Assistant") as demo:
54
- gr.Markdown("## 🤖 مساعد الكتب الذكي - Smart PDF Assistant")
55
- file_input = gr.File(label="📄 ارفع ملف PDF", type="file")
56
- upload_button = gr.Button("🔁 تحميل الملف ومعالجته")
57
- status_output = gr.Textbox(label="📢 حالة التحميل", interactive=False)
58
-
59
- question_input = gr.Textbox(label="❓ اكتب سؤالك هنا")
60
- answer_output = gr.Markdown(label="📘 الإجابة")
61
-
62
- def handle_upload(file):
63
- global qa_chain
64
- qa_chain, status = process_file(file)
65
- return status
66
-
67
- upload_button.click(fn=handle_upload, inputs=[file_input], outputs=[status_output])
68
- question_input.submit(fn=answer_question, inputs=[question_input], outputs=[answer_output])
69
-
70
- # شغّل التطبيق
71
- if __name__ == "__main__":
72
- demo.launch()