Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,19 +1,14 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
3 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
4 |
-
from
|
5 |
-
from
|
6 |
from langchain.chains import RetrievalQA
|
7 |
-
from
|
8 |
from transformers import pipeline
|
9 |
-
import tempfile
|
10 |
|
11 |
-
def qa_from_pdf(
|
12 |
-
|
13 |
-
tmp_file.write(pdf_file.read())
|
14 |
-
tmp_path = tmp_file.name
|
15 |
-
|
16 |
-
loader = PyPDFLoader(tmp_path)
|
17 |
pages = loader.load()
|
18 |
|
19 |
splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=100)
|
@@ -23,23 +18,21 @@ def qa_from_pdf(pdf_file, question):
|
|
23 |
db = FAISS.from_documents(documents, embedding_model)
|
24 |
|
25 |
hf_pipeline = pipeline('text-generation', model='sshleifer/tiny-gpt2', max_length=100)
|
26 |
-
|
27 |
llm = HuggingFacePipeline(pipeline=hf_pipeline)
|
28 |
|
29 |
qa_chain = RetrievalQA.from_chain_type(llm=llm, retriever=db.as_retriever())
|
30 |
-
|
31 |
answer = qa_chain.run(question)
|
32 |
return answer
|
33 |
|
34 |
iface = gr.Interface(
|
35 |
fn=qa_from_pdf,
|
36 |
inputs=[
|
37 |
-
gr.File(label="PDF Dosyası Yükle", file_types=[".pdf"]),
|
38 |
gr.Textbox(label="Sorunuzu yazın")
|
39 |
],
|
40 |
outputs="text",
|
41 |
title="📄 RAG Demo: PDF üzerinden Soru-Cevap",
|
42 |
-
description="Bir PDF yükleyin ve ona sorular sorun.
|
43 |
)
|
44 |
|
45 |
if __name__ == "__main__":
|
|
|
1 |
import gradio as gr
|
2 |
+
from langchain_community.document_loaders import PyPDFLoader
|
3 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
4 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
5 |
+
from langchain_community.vectorstores import FAISS
|
6 |
from langchain.chains import RetrievalQA
|
7 |
+
from langchain_community.llms import HuggingFacePipeline
|
8 |
from transformers import pipeline
|
|
|
9 |
|
10 |
+
def qa_from_pdf(pdf_path, question):
|
11 |
+
loader = PyPDFLoader(pdf_path)
|
|
|
|
|
|
|
|
|
12 |
pages = loader.load()
|
13 |
|
14 |
splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=100)
|
|
|
18 |
db = FAISS.from_documents(documents, embedding_model)
|
19 |
|
20 |
hf_pipeline = pipeline('text-generation', model='sshleifer/tiny-gpt2', max_length=100)
|
|
|
21 |
llm = HuggingFacePipeline(pipeline=hf_pipeline)
|
22 |
|
23 |
qa_chain = RetrievalQA.from_chain_type(llm=llm, retriever=db.as_retriever())
|
|
|
24 |
answer = qa_chain.run(question)
|
25 |
return answer
|
26 |
|
27 |
iface = gr.Interface(
|
28 |
fn=qa_from_pdf,
|
29 |
inputs=[
|
30 |
+
gr.File(label="PDF Dosyası Yükle", file_types=[".pdf"], type="filepath"),
|
31 |
gr.Textbox(label="Sorunuzu yazın")
|
32 |
],
|
33 |
outputs="text",
|
34 |
title="📄 RAG Demo: PDF üzerinden Soru-Cevap",
|
35 |
+
description="Bir PDF yükleyin ve ona sorular sorun. Küçük dil modeliyle çalışan demo."
|
36 |
)
|
37 |
|
38 |
if __name__ == "__main__":
|