Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,69 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import faiss
|
| 3 |
-
import numpy as np
|
| 4 |
-
import torch
|
| 5 |
-
from sentence_transformers import SentenceTransformer
|
| 6 |
-
from transformers import pipeline
|
| 7 |
-
from PyPDF2 import PdfReader
|
| 8 |
-
|
| 9 |
-
# Load embedding model & LLM
|
| 10 |
-
embedding_model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
| 11 |
-
llm_pipeline = pipeline("text2text-generation", model="google/flan-t5-small")
|
| 12 |
-
|
| 13 |
-
# Initialize FAISS index
|
| 14 |
-
index = None
|
| 15 |
-
chunks = []
|
| 16 |
-
|
| 17 |
-
# Load and process document
|
| 18 |
-
def load_document(file):
|
| 19 |
-
global index, chunks
|
| 20 |
-
text = ""
|
| 21 |
-
|
| 22 |
-
if file.name.endswith(".pdf"):
|
| 23 |
-
reader = PdfReader(file.name)
|
| 24 |
-
text = "\n".join([page.extract_text() for page in reader.pages if page.extract_text()])
|
| 25 |
-
else:
|
| 26 |
-
text = file.read().decode("utf-8")
|
| 27 |
-
|
| 28 |
-
# Split text into chunks
|
| 29 |
-
sentences = text.split(". ")
|
| 30 |
-
chunks = [" ".join(sentences[i:i + 5]) for i in range(0, len(sentences), 5)]
|
| 31 |
-
|
| 32 |
-
# Create embeddings and FAISS index
|
| 33 |
-
embeddings = np.array([embedding_model.encode(chunk) for chunk in chunks])
|
| 34 |
-
index = faiss.IndexFlatL2(embeddings.shape[1])
|
| 35 |
-
index.add(embeddings)
|
| 36 |
-
|
| 37 |
-
return "Document uploaded and indexed!"
|
| 38 |
-
|
| 39 |
-
# Retrieve and generate response
|
| 40 |
-
def get_answer(query):
|
| 41 |
-
if index is None:
|
| 42 |
-
return "Please upload a document first."
|
| 43 |
-
|
| 44 |
-
query_embedding = embedding_model.encode(query).reshape(1, -1)
|
| 45 |
-
distances, indices = index.search(query_embedding, 3)
|
| 46 |
-
retrieved_text = " ".join([chunks[i] for i in indices[0]])
|
| 47 |
-
|
| 48 |
-
input_text = f"Answer the question: {query}\nContext: {retrieved_text}"
|
| 49 |
-
response = llm_pipeline(input_text, max_length=100)[0]['generated_text']
|
| 50 |
-
|
| 51 |
-
return response
|
| 52 |
-
|
| 53 |
-
# Gradio Interface
|
| 54 |
-
with gr.Blocks() as demo:
|
| 55 |
-
gr.Markdown("# 📄 Simple RAG App")
|
| 56 |
-
|
| 57 |
-
file_input = gr.File(label="Upload a PDF or Text File")
|
| 58 |
-
upload_button = gr.Button("Process Document")
|
| 59 |
-
status_text = gr.Textbox(label="Status", interactive=False)
|
| 60 |
-
|
| 61 |
-
query_input = gr.Textbox(label="Enter your question:")
|
| 62 |
-
query_button = gr.Button("Get Answer")
|
| 63 |
-
output_text = gr.Textbox(label="AI Answer", interactive=False)
|
| 64 |
-
|
| 65 |
-
upload_button.click(load_document, inputs=file_input, outputs=status_text)
|
| 66 |
-
query_button.click(get_answer, inputs=query_input, outputs=output_text)
|
| 67 |
-
|
| 68 |
-
# Launch App
|
| 69 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|