saadawaissheikh commited on
Commit
6238318
Β·
verified Β·
1 Parent(s): ad5477c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -27
app.py CHANGED
@@ -12,13 +12,12 @@ from langchain.prompts import PromptTemplate
12
  from langchain_openai import ChatOpenAI
13
 
14
 
15
-
16
  os.environ["OPENAI_API_KEY"] = os.environ["OPENROUTER_API_KEY"]
17
  os.environ["OPENAI_API_BASE"] = "https://openrouter.ai/api/v1"
18
  os.environ["OPENAI_API_HEADERS"] = '{"HTTP-Referer":"https://huggingface.co", "X-Title":"PDF-RAG"}'
19
 
20
-
21
- #Section-aware PDF extractor
22
  def extract_clean_sections(file_path):
23
  with pdfplumber.open(file_path) as pdf:
24
  full_text = ""
@@ -40,8 +39,7 @@ def extract_clean_sections(file_path):
40
  docs.append(Document(page_content=f"{title}:\n{content}", metadata={"section": title}))
41
  return docs
42
 
43
-
44
- #TF-IDF Embedding for RAG
45
  class TfidfEmbedding(Embeddings):
46
  def __init__(self):
47
  self.vectorizer = TfidfVectorizer()
@@ -55,8 +53,7 @@ class TfidfEmbedding(Embeddings):
55
  def embed_query(self, text):
56
  return self.vectorizer.transform([text]).toarray()[0]
57
 
58
-
59
- # prompt
60
  TEMPLATE = """
61
  You are a strict healthcare policy checker for Systems Ltd.
62
 
@@ -72,17 +69,12 @@ Use the following policy information to support your answer.
72
  Question: {question}
73
  Answer:
74
  """
75
-
76
  custom_prompt = PromptTemplate(template=TEMPLATE, input_variables=["context", "question"])
77
 
78
- # Global state
79
- retriever = None
80
- qa_chain = None
81
-
82
 
83
- # βœ… Process the PDF once when button is clicked
84
- def load_policy():
85
- global retriever, qa_chain
86
  docs = extract_clean_sections("healthcare_policy.pdf")
87
  texts = [doc.page_content for doc in docs]
88
  embedder = TfidfEmbedding()
@@ -101,7 +93,7 @@ def load_policy():
101
  temperature=0.0
102
  )
103
 
104
- qa_chain_local = RetrievalQA.from_chain_type(
105
  llm=llm,
106
  chain_type="stuff",
107
  retriever=retriever,
@@ -109,27 +101,23 @@ def load_policy():
109
  chain_type_kwargs={"prompt": custom_prompt}
110
  )
111
 
112
- qa_chain = qa_chain_local
113
- return "Policy loaded. You may now ask questions."
114
-
115
-
116
- # βœ… Answer a claim question
117
  def ask_policy_question(question):
118
  if qa_chain is None:
119
- return "Please click 'Ask about claim' to load the policy first."
120
  try:
121
  return qa_chain.run(question)
122
  except Exception as e:
123
  return f"Error: {str(e)}"
124
 
125
 
126
- # βœ… Gradio UI
 
 
 
127
  with gr.Blocks() as demo:
128
  gr.Markdown("## SL HealthCare Claim Checker (RAG)")
129
-
130
- load_btn = gr.Button("πŸ“₯ Ask about claim (Load Policy)")
131
- load_status = gr.Textbox(label="Status")
132
- load_btn.click(fn=load_policy, outputs=load_status)
133
 
134
  with gr.Row():
135
  question = gr.Textbox(label="Enter your claim question")
@@ -138,4 +126,13 @@ with gr.Blocks() as demo:
138
  answer = gr.Textbox(label="Answer", lines=6)
139
  ask_btn.click(fn=ask_policy_question, inputs=question, outputs=answer)
140
 
 
 
 
 
 
 
 
 
 
141
  demo.launch()
 
12
  from langchain_openai import ChatOpenAI
13
 
14
 
15
+ # βœ… OpenRouter API setup (use Hugging Face Secret)
16
  os.environ["OPENAI_API_KEY"] = os.environ["OPENROUTER_API_KEY"]
17
  os.environ["OPENAI_API_BASE"] = "https://openrouter.ai/api/v1"
18
  os.environ["OPENAI_API_HEADERS"] = '{"HTTP-Referer":"https://huggingface.co", "X-Title":"PDF-RAG"}'
19
 
20
+ # βœ… Load and clean the policy PDF
 
21
  def extract_clean_sections(file_path):
22
  with pdfplumber.open(file_path) as pdf:
23
  full_text = ""
 
39
  docs.append(Document(page_content=f"{title}:\n{content}", metadata={"section": title}))
40
  return docs
41
 
42
+ # βœ… TF-IDF Embeddings
 
43
  class TfidfEmbedding(Embeddings):
44
  def __init__(self):
45
  self.vectorizer = TfidfVectorizer()
 
53
  def embed_query(self, text):
54
  return self.vectorizer.transform([text]).toarray()[0]
55
 
56
+ # βœ… Prompt Template (no emojis, no markdown)
 
57
  TEMPLATE = """
58
  You are a strict healthcare policy checker for Systems Ltd.
59
 
 
69
  Question: {question}
70
  Answer:
71
  """
 
72
  custom_prompt = PromptTemplate(template=TEMPLATE, input_variables=["context", "question"])
73
 
 
 
 
 
74
 
75
+ # βœ… Load the policy at startup
76
+ def initialize_policy():
77
+ global qa_chain
78
  docs = extract_clean_sections("healthcare_policy.pdf")
79
  texts = [doc.page_content for doc in docs]
80
  embedder = TfidfEmbedding()
 
93
  temperature=0.0
94
  )
95
 
96
+ qa_chain = RetrievalQA.from_chain_type(
97
  llm=llm,
98
  chain_type="stuff",
99
  retriever=retriever,
 
101
  chain_type_kwargs={"prompt": custom_prompt}
102
  )
103
 
104
+ # βœ… Run QA on user question
 
 
 
 
105
  def ask_policy_question(question):
106
  if qa_chain is None:
107
+ return "The policy is still loading. Please wait."
108
  try:
109
  return qa_chain.run(question)
110
  except Exception as e:
111
  return f"Error: {str(e)}"
112
 
113
 
114
+ # βœ… Gradio Interface
115
+ qa_chain = None
116
+ status_text = "Loading..." # Initial status
117
+
118
  with gr.Blocks() as demo:
119
  gr.Markdown("## SL HealthCare Claim Checker (RAG)")
120
+ status_box = gr.Textbox(label="Status", value=status_text, interactive=False)
 
 
 
121
 
122
  with gr.Row():
123
  question = gr.Textbox(label="Enter your claim question")
 
126
  answer = gr.Textbox(label="Answer", lines=6)
127
  ask_btn.click(fn=ask_policy_question, inputs=question, outputs=answer)
128
 
129
+ # Load the policy on startup
130
+ def startup():
131
+ global status_text
132
+ initialize_policy()
133
+ status_text = "Policy loaded. You may now ask questions."
134
+ return status_text
135
+
136
+ demo.load(fn=startup, outputs=status_box)
137
+
138
  demo.launch()