ramysaidagieb commited on
Commit
a503e7e
·
verified ·
1 Parent(s): 149f66c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -17
app.py CHANGED
@@ -1,17 +1,60 @@
1
- o install langchain-community run `pip install -U langchain-community`.
2
- from langchain.llms import LiteLLM
3
- Traceback (most recent call last):
4
- File "/home/user/app/app.py", line 12, in <module>
5
- from langchain.llms import LiteLLM
6
- ImportError: cannot import name 'LiteLLM' from 'langchain.llms' (/usr/local/lib/python3.10/site-packages/langchain/llms/__init__.py)
7
- /home/user/app/app.py:12: LangChainDeprecationWarning: Importing LLMs from langchain is deprecated. Importing from langchain will no longer be supported as of langchain==0.2.0. Please import from langchain-community instead:
8
-
9
- `from langchain_community.llms import LiteLLM`.
10
-
11
- To install langchain-community run `pip install -U langchain-community`.
12
- from langchain.llms import LiteLLM
13
- Traceback (most recent call last):
14
- File "/home/user/app/app.py", line 12, in <module>
15
- from langchain.llms import LiteLLM
16
- ImportError: cannot import name 'LiteLLM' from 'langchain.llms' (/usr/local/lib/python3.10/site-packages/langchain/llms/__init__.py)
17
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 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
+ # اختياري: تسجيل الدخول إذا كنت تستخدم مفتاح API
13
+ # login(token="your_huggingface_token")
14
+
15
+ def process_pdf_and_answer(pdf_path, question):
16
+ # تحميل ملف PDF
17
+ loader = PyPDFLoader(pdf_path)
18
+ pages = loader.load()
19
+
20
+ # تقسيم النصوص
21
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
22
+ texts = text_splitter.split_documents(pages)
23
+
24
+ # التضمين Embeddings
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 = HuggingFaceHub(repo_id="HuggingFaceH4/zephyr-7b-beta", model_kwargs={"temperature": 0.1, "max_new_tokens": 512})
30
+
31
+ # إعداد RAG
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
+ answer = result["result"]
37
+ return answer
38
+
39
+ # واجهة Gradio
40
+ with gr.Blocks() as demo:
41
+ gr.Markdown("## 🧠 مساعد PDF الذكي")
42
+
43
+ with gr.Row():
44
+ file_input = gr.File(label="📄 ارفع ملف PDF", type="filepath", file_types=[".pdf"])
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() == "":
53
+ return "يرجى رفع ملف PDF وكتابة سؤال."
54
+ return process_pdf_and_answer(file, question)
55
+
56
+ submit_btn.click(handle_submit, inputs=[file_input, question_input], outputs=output)
57
+
58
+ # لتشغيل التطبيق
59
+ if __name__ == "__main__":
60
+ demo.launch()