Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from langchain.document_loaders import PyPDFLoader
|
3 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
4 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
5 |
+
from langchain.vectorstores import FAISS
|
6 |
+
from langchain.chains import RetrievalQA
|
7 |
+
from langchain.llms import HuggingFacePipeline
|
8 |
+
from transformers import pipeline
|
9 |
+
import tempfile
|
10 |
+
|
11 |
+
def qa_from_pdf(pdf_file, question):
|
12 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_file:
|
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)
|
20 |
+
documents = splitter.split_documents(pages)
|
21 |
+
|
22 |
+
embedding_model = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2')
|
23 |
+
db = FAISS.from_documents(documents, embedding_model)
|
24 |
+
|
25 |
+
hf_pipeline = pipeline('text-generation', model='tiiuae/falcon-rw-1b', max_length=512)
|
26 |
+
llm = HuggingFacePipeline(pipeline=hf_pipeline)
|
27 |
+
|
28 |
+
qa_chain = RetrievalQA.from_chain_type(llm=llm, retriever=db.as_retriever())
|
29 |
+
|
30 |
+
answer = qa_chain.run(question)
|
31 |
+
return answer
|
32 |
+
|
33 |
+
iface = gr.Interface(
|
34 |
+
fn=qa_from_pdf,
|
35 |
+
inputs=[
|
36 |
+
gr.File(label="PDF Dosyası Yükle", file_types=[".pdf"]),
|
37 |
+
gr.Textbox(label="Sorunuzu yazın")
|
38 |
+
],
|
39 |
+
outputs="text",
|
40 |
+
title="📄 RAG Demo: PDF üzerinden Soru-Cevap",
|
41 |
+
description="Bir PDF yükleyin ve ona sorular sorun. Falcon-RW-1B ile çalışır."
|
42 |
+
)
|
43 |
+
|
44 |
+
if __name__ == "__main__":
|
45 |
+
iface.launch()
|