Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,15 @@
|
|
| 1 |
-
import os
|
| 2 |
import gradio as gr
|
| 3 |
from langchain_community.vectorstores import FAISS
|
| 4 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 5 |
from langchain_community.document_loaders import PyPDFLoader
|
| 6 |
-
from
|
| 7 |
from langchain_community.llms import HuggingFaceHub
|
| 8 |
|
|
|
|
| 9 |
# -----------------------------
|
| 10 |
-
#
|
| 11 |
# -----------------------------
|
| 12 |
-
def
|
| 13 |
loader = PyPDFLoader(file.name)
|
| 14 |
pages = loader.load()
|
| 15 |
|
|
@@ -23,7 +23,7 @@ def load_pdf(file):
|
|
| 23 |
|
| 24 |
|
| 25 |
# -----------------------------
|
| 26 |
-
#
|
| 27 |
# -----------------------------
|
| 28 |
def create_db(docs):
|
| 29 |
embeddings = HuggingFaceEmbeddings(
|
|
@@ -35,16 +35,15 @@ def create_db(docs):
|
|
| 35 |
|
| 36 |
|
| 37 |
# -----------------------------
|
| 38 |
-
#
|
| 39 |
# -----------------------------
|
| 40 |
def ask_question(file, question):
|
| 41 |
if file is None:
|
| 42 |
return "Please upload a PDF first."
|
| 43 |
|
| 44 |
-
docs =
|
| 45 |
db = create_db(docs)
|
| 46 |
|
| 47 |
-
# Better retrieval (IMPORTANT FIX)
|
| 48 |
retrieved_docs = db.similarity_search(question, k=5)
|
| 49 |
|
| 50 |
context = "\n\n".join([doc.page_content for doc in retrieved_docs])
|
|
@@ -53,12 +52,10 @@ def ask_question(file, question):
|
|
| 53 |
return "No relevant information found in the document."
|
| 54 |
|
| 55 |
prompt = f"""
|
| 56 |
-
You are a
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
- Do not guess
|
| 61 |
-
- Be concise
|
| 62 |
|
| 63 |
Context:
|
| 64 |
{context}
|
|
@@ -69,30 +66,27 @@ Question:
|
|
| 69 |
Answer:
|
| 70 |
"""
|
| 71 |
|
| 72 |
-
# Using free HuggingFace model (works on HF Spaces CPU)
|
| 73 |
llm = HuggingFaceHub(
|
| 74 |
repo_id="google/flan-t5-base",
|
| 75 |
model_kwargs={"temperature": 0.3, "max_length": 256}
|
| 76 |
)
|
| 77 |
|
| 78 |
-
|
| 79 |
-
return
|
| 80 |
|
| 81 |
|
| 82 |
# -----------------------------
|
| 83 |
-
#
|
| 84 |
# -----------------------------
|
| 85 |
-
with gr.Blocks(
|
| 86 |
-
|
| 87 |
-
gr.Markdown("# 🧠 Medical PDF Question Answering (RAG)")
|
| 88 |
|
| 89 |
file = gr.File(label="Upload PDF")
|
| 90 |
-
question = gr.Textbox(label="Ask
|
| 91 |
|
| 92 |
btn = gr.Button("Get Answer")
|
| 93 |
output = gr.Textbox(label="Answer")
|
| 94 |
|
| 95 |
btn.click(fn=ask_question, inputs=[file, question], outputs=output)
|
| 96 |
|
| 97 |
-
|
| 98 |
app.launch()
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from langchain_community.vectorstores import FAISS
|
| 3 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 4 |
from langchain_community.document_loaders import PyPDFLoader
|
| 5 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 6 |
from langchain_community.llms import HuggingFaceHub
|
| 7 |
|
| 8 |
+
|
| 9 |
# -----------------------------
|
| 10 |
+
# LOAD & SPLIT PDF
|
| 11 |
# -----------------------------
|
| 12 |
+
def process_pdf(file):
|
| 13 |
loader = PyPDFLoader(file.name)
|
| 14 |
pages = loader.load()
|
| 15 |
|
|
|
|
| 23 |
|
| 24 |
|
| 25 |
# -----------------------------
|
| 26 |
+
# CREATE VECTOR DB
|
| 27 |
# -----------------------------
|
| 28 |
def create_db(docs):
|
| 29 |
embeddings = HuggingFaceEmbeddings(
|
|
|
|
| 35 |
|
| 36 |
|
| 37 |
# -----------------------------
|
| 38 |
+
# QUESTION ANSWERING
|
| 39 |
# -----------------------------
|
| 40 |
def ask_question(file, question):
|
| 41 |
if file is None:
|
| 42 |
return "Please upload a PDF first."
|
| 43 |
|
| 44 |
+
docs = process_pdf(file)
|
| 45 |
db = create_db(docs)
|
| 46 |
|
|
|
|
| 47 |
retrieved_docs = db.similarity_search(question, k=5)
|
| 48 |
|
| 49 |
context = "\n\n".join([doc.page_content for doc in retrieved_docs])
|
|
|
|
| 52 |
return "No relevant information found in the document."
|
| 53 |
|
| 54 |
prompt = f"""
|
| 55 |
+
You are a helpful assistant. Answer ONLY using the given context.
|
| 56 |
|
| 57 |
+
If the answer is not present, reply:
|
| 58 |
+
"Not found in report"
|
|
|
|
|
|
|
| 59 |
|
| 60 |
Context:
|
| 61 |
{context}
|
|
|
|
| 66 |
Answer:
|
| 67 |
"""
|
| 68 |
|
|
|
|
| 69 |
llm = HuggingFaceHub(
|
| 70 |
repo_id="google/flan-t5-base",
|
| 71 |
model_kwargs={"temperature": 0.3, "max_length": 256}
|
| 72 |
)
|
| 73 |
|
| 74 |
+
result = llm.invoke(prompt)
|
| 75 |
+
return result
|
| 76 |
|
| 77 |
|
| 78 |
# -----------------------------
|
| 79 |
+
# GRADIO UI
|
| 80 |
# -----------------------------
|
| 81 |
+
with gr.Blocks() as app:
|
| 82 |
+
gr.Markdown("# 🧠 Medical Report Q&A (RAG)")
|
|
|
|
| 83 |
|
| 84 |
file = gr.File(label="Upload PDF")
|
| 85 |
+
question = gr.Textbox(label="Ask your question")
|
| 86 |
|
| 87 |
btn = gr.Button("Get Answer")
|
| 88 |
output = gr.Textbox(label="Answer")
|
| 89 |
|
| 90 |
btn.click(fn=ask_question, inputs=[file, question], outputs=output)
|
| 91 |
|
|
|
|
| 92 |
app.launch()
|