Afeefa123 commited on
Commit
719b2ba
·
verified ·
1 Parent(s): 25fba1a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -22
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 langchain.text_splitter import RecursiveCharacterTextSplitter
7
  from langchain_community.llms import HuggingFaceHub
8
 
 
9
  # -----------------------------
10
- # 1. LOAD & PROCESS PDF
11
  # -----------------------------
12
- def load_pdf(file):
13
  loader = PyPDFLoader(file.name)
14
  pages = loader.load()
15
 
@@ -23,7 +23,7 @@ def load_pdf(file):
23
 
24
 
25
  # -----------------------------
26
- # 2. BUILD VECTOR DATABASE
27
  # -----------------------------
28
  def create_db(docs):
29
  embeddings = HuggingFaceEmbeddings(
@@ -35,16 +35,15 @@ def create_db(docs):
35
 
36
 
37
  # -----------------------------
38
- # 3. QA ENGINE (FIXED RETRIEVAL)
39
  # -----------------------------
40
  def ask_question(file, question):
41
  if file is None:
42
  return "Please upload a PDF first."
43
 
44
- docs = load_pdf(file)
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 precise assistant that answers ONLY from the provided medical report.
57
 
58
- Rules:
59
- - If answer is not in context, say "Not found in report"
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
- response = llm.invoke(prompt)
79
- return response
80
 
81
 
82
  # -----------------------------
83
- # 4. GRADIO UI
84
  # -----------------------------
85
- with gr.Blocks(title="Medical PDF RAG App") as app:
86
-
87
- gr.Markdown("# 🧠 Medical PDF Question Answering (RAG)")
88
 
89
  file = gr.File(label="Upload PDF")
90
- question = gr.Textbox(label="Ask a Question")
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()