Upload 5 files
Browse files- .gitattributes +1 -0
- README.md +183 -13
- app.py +125 -125
- dds_logo.png +3 -0
- insurance.pdf +3 -0
- requirements.txt +8 -8
.gitattributes
CHANGED
|
@@ -36,3 +36,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 36 |
insurance.pdf filter=lfs diff=lfs merge=lfs -text
|
| 37 |
data/insurance.pdf filter=lfs diff=lfs merge=lfs -text
|
| 38 |
data/dds_logo.png filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 36 |
insurance.pdf filter=lfs diff=lfs merge=lfs -text
|
| 37 |
data/insurance.pdf filter=lfs diff=lfs merge=lfs -text
|
| 38 |
data/dds_logo.png filter=lfs diff=lfs merge=lfs -text
|
| 39 |
+
dds_logo.png filter=lfs diff=lfs merge=lfs -text
|
README.md
CHANGED
|
@@ -1,13 +1,183 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
DDS Insurance Q&A — RAG Assistant (Pinecone + OpenAI + Gradio)
|
| 2 |
+
|
| 3 |
+
Summary: A beginner-friendly, document-grounded insurance bot that you can replicate and deploy on Hugging Face Spaces. It answers only from your uploaded insurance documents using LlamaIndex + Pinecone (serverless) + OpenAI with a simple, polite system prompt.
|
| 4 |
+
|
| 5 |
+
What You’ll Get
|
| 6 |
+
|
| 7 |
+
Deployed Space URL you can share.
|
| 8 |
+
|
| 9 |
+
Grounded answers (no docs → the bot politely says it can’t find it).
|
| 10 |
+
|
| 11 |
+
Simple UI with an FAQ dropdown + free-text question box.
|
| 12 |
+
|
| 13 |
+
Clean structure designed for easy replication.
|
| 14 |
+
|
| 15 |
+
Features
|
| 16 |
+
|
| 17 |
+
Answers strictly from your data/ documents (RAG).
|
| 18 |
+
|
| 19 |
+
Pinecone serverless index (AWS us-east-1, cosine, 1536-dim).
|
| 20 |
+
|
| 21 |
+
OpenAI for embeddings (text-embedding-3-small) and LLM (gpt-4o-mini).
|
| 22 |
+
|
| 23 |
+
Gradio interface with a centered required logo (data/dds_logo.png).
|
| 24 |
+
|
| 25 |
+
Beginner-friendly defaults and error messages.
|
| 26 |
+
|
| 27 |
+
Repository Structure
|
| 28 |
+
.
|
| 29 |
+
├─ data/ # Your insurance docs + required logo
|
| 30 |
+
│ └─ dds_logo.png # REQUIRED (shown in header)
|
| 31 |
+
├─ app.py # Main app: indexing + query + Gradio UI
|
| 32 |
+
├─ requirements.txt # Dependencies
|
| 33 |
+
└─ README.md # This file
|
| 34 |
+
|
| 35 |
+
Configuration (in app.py)
|
| 36 |
+
EMBED_MODEL = "text-embedding-3-small" # 1536-dim
|
| 37 |
+
LLM_MODEL = "gpt-4o-mini"
|
| 38 |
+
TOP_K = 4 # retrieval depth
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
System Prompt (keeps answers grounded + polite):
|
| 42 |
+
|
| 43 |
+
SYSTEM_PROMPT = """You are Aisha, a polite and professional Insurance assistant.
|
| 44 |
+
Answer ONLY using the information found in the indexed insurance document(s).
|
| 45 |
+
If the answer is not in the document(s), say: "I couldn’t find that in the document."
|
| 46 |
+
Keep responses concise, helpful, and courteous.
|
| 47 |
+
"""
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
FAQ List (editable):
|
| 51 |
+
|
| 52 |
+
FAQS = [
|
| 53 |
+
"",
|
| 54 |
+
"What benefits are covered under the policy?",
|
| 55 |
+
"How do I file a claim and what documents are required?",
|
| 56 |
+
"What are the exclusions and limitations?",
|
| 57 |
+
"Is pre-authorization needed for hospitalization?",
|
| 58 |
+
"What is the reimbursement timeline?",
|
| 59 |
+
"How are outpatient vs inpatient services handled?",
|
| 60 |
+
"How can I check my network hospitals/clinics?",
|
| 61 |
+
"What is the co-pay or deductible policy?",
|
| 62 |
+
]
|
| 63 |
+
|
| 64 |
+
Deploy to Hugging Face Spaces (Beginner-Friendly)
|
| 65 |
+
1) Create a Space
|
| 66 |
+
|
| 67 |
+
Go to Hugging Face → Spaces → New Space
|
| 68 |
+
|
| 69 |
+
SDK: Gradio
|
| 70 |
+
|
| 71 |
+
Visibility/licensing: your choice
|
| 72 |
+
|
| 73 |
+
2) Add Project Files
|
| 74 |
+
|
| 75 |
+
Upload these into your Space:
|
| 76 |
+
|
| 77 |
+
app.py
|
| 78 |
+
|
| 79 |
+
requirements.txt
|
| 80 |
+
|
| 81 |
+
README.md
|
| 82 |
+
|
| 83 |
+
Create folder data/ and upload:
|
| 84 |
+
|
| 85 |
+
Your insurance documents (PDF/TXT/MD…)
|
| 86 |
+
|
| 87 |
+
dds_logo.png (mandatory; exact filename)
|
| 88 |
+
|
| 89 |
+
Tip: Your Space file tree should match the Repository Structure above.
|
| 90 |
+
|
| 91 |
+
3) Set Secrets (Environment Variables)
|
| 92 |
+
|
| 93 |
+
In Space → Settings → Variables and secrets, add:
|
| 94 |
+
|
| 95 |
+
OPENAI_API_KEY → your OpenAI key
|
| 96 |
+
|
| 97 |
+
PINECONE_API_KEY → your Pinecone key
|
| 98 |
+
|
| 99 |
+
No legacy Pinecone environment URL needed. This app uses pinecone-client ≥ 5 with serverless.
|
| 100 |
+
|
| 101 |
+
4) Build & Run
|
| 102 |
+
|
| 103 |
+
Spaces auto-install from requirements.txt.
|
| 104 |
+
|
| 105 |
+
Default CPU hardware is fine.
|
| 106 |
+
|
| 107 |
+
Entry point auto-detected from app.py.
|
| 108 |
+
|
| 109 |
+
On first start, the app will:
|
| 110 |
+
|
| 111 |
+
Ensure a Pinecone serverless index:
|
| 112 |
+
dds-insurance-index · cosine · 1536-dim · aws/us-east-1
|
| 113 |
+
|
| 114 |
+
Read and index documents from data/
|
| 115 |
+
|
| 116 |
+
Launch the Gradio UI
|
| 117 |
+
|
| 118 |
+
Your deployed link is simply the Space URL once its status is Running.
|
| 119 |
+
|
| 120 |
+
5) Updating Documents Later
|
| 121 |
+
|
| 122 |
+
Upload/change files in data/
|
| 123 |
+
|
| 124 |
+
Click Restart on the Space so it re-indexes your documents
|
| 125 |
+
|
| 126 |
+
Troubleshooting (Common Issues)
|
| 127 |
+
|
| 128 |
+
“Missing PINECONE_API_KEY or OPENAI_API_KEY”
|
| 129 |
+
Add both secrets in Space → Settings → Variables and secrets.
|
| 130 |
+
|
| 131 |
+
Pinecone 401 / “Malformed domain”
|
| 132 |
+
|
| 133 |
+
Ensure you’re on pinecone-client>=5.0.1 (already in requirements.txt).
|
| 134 |
+
|
| 135 |
+
Use a valid Pinecone API key; no environment URL needed for serverless.
|
| 136 |
+
|
| 137 |
+
“Logo not found: data/dds_logo.png”
|
| 138 |
+
Upload an image named exactly dds_logo.png into the data/ folder.
|
| 139 |
+
|
| 140 |
+
“No documents found in data/”
|
| 141 |
+
Upload at least one doc (PDF/TXT/MD) into data/, then Restart the Space.
|
| 142 |
+
|
| 143 |
+
OpenAI authorization/rate-limit errors
|
| 144 |
+
Confirm key validity and model access; reduce usage if rate-limited.
|
| 145 |
+
|
| 146 |
+
Slow first load
|
| 147 |
+
First run installs dependencies and builds the index; later runs are faster.
|
| 148 |
+
|
| 149 |
+
Manual Test Checklist
|
| 150 |
+
|
| 151 |
+
Ask a question clearly answered in your docs → response should quote that knowledge.
|
| 152 |
+
|
| 153 |
+
Ask something not in your docs → bot should say it can’t find it.
|
| 154 |
+
|
| 155 |
+
Adjust TOP_K in app.py to see how answer completeness changes.
|
| 156 |
+
|
| 157 |
+
Requirements (from requirements.txt)
|
| 158 |
+
gradio>=4.44.0
|
| 159 |
+
pinecone-client>=5.0.1
|
| 160 |
+
openai>=1.51.0
|
| 161 |
+
llama-index>=0.11.0
|
| 162 |
+
llama-index-vector-stores-pinecone>=0.3.0
|
| 163 |
+
llama-index-embeddings-openai>=0.3.0
|
| 164 |
+
llama-index-llms-openai>=0.2.0
|
| 165 |
+
tiktoken>=0.7.0
|
| 166 |
+
|
| 167 |
+
Customization Ideas
|
| 168 |
+
|
| 169 |
+
Swap LLMs by editing LLM_MODEL.
|
| 170 |
+
|
| 171 |
+
Add a file uploader to refresh docs from the UI.
|
| 172 |
+
|
| 173 |
+
Add metadata filters (e.g., policy type).
|
| 174 |
+
|
| 175 |
+
Log queries to refine the FAQ list.
|
| 176 |
+
|
| 177 |
+
License
|
| 178 |
+
|
| 179 |
+
Add your chosen license (e.g., MIT) as LICENSE.
|
| 180 |
+
|
| 181 |
+
Acknowledgments
|
| 182 |
+
|
| 183 |
+
Thanks to LlamaIndex, Pinecone, OpenAI, and Gradio for the tooling that makes this simple and reproducible.
|
app.py
CHANGED
|
@@ -1,125 +1,125 @@
|
|
| 1 |
-
# app.py — Insurance Q&A (RAG) with system prompt + simple config
|
| 2 |
-
import os
|
| 3 |
-
import gradio as gr
|
| 4 |
-
from pinecone import Pinecone, ServerlessSpec
|
| 5 |
-
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, StorageContext, Settings
|
| 6 |
-
from llama_index.vector_stores.pinecone import PineconeVectorStore
|
| 7 |
-
from llama_index.embeddings.openai import OpenAIEmbedding
|
| 8 |
-
from llama_index.llms.openai import OpenAI
|
| 9 |
-
|
| 10 |
-
# --- System Prompt (polite + answer-from-document constraint) ---
|
| 11 |
-
SYSTEM_PROMPT = """You are Aisha, a polite and professional Insurance assistant.
|
| 12 |
-
Answer ONLY using the information found in the indexed insurance document(s).
|
| 13 |
-
If the answer is not in the document(s), say: "I couldn’t find that in the document."
|
| 14 |
-
Keep responses concise, helpful, and courteous.
|
| 15 |
-
"""
|
| 16 |
-
|
| 17 |
-
# ===== Minimal CONFIG (only necessary keys) =====
|
| 18 |
-
PINECONE_API_KEY = os.getenv("PINECONE_API_KEY")
|
| 19 |
-
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 20 |
-
if not PINECONE_API_KEY or not OPENAI_API_KEY:
|
| 21 |
-
raise RuntimeError("Missing PINECONE_API_KEY or OPENAI_API_KEY (set them in Space → Settings → Variables).")
|
| 22 |
-
|
| 23 |
-
DATA_DIR = "data" # Put insurance docs here (e.g., data/insurance.pdf)
|
| 24 |
-
LOGO_PATH = os.path.join(DATA_DIR, "dds_logo.png") # Mandatory logo
|
| 25 |
-
if not os.path.exists(LOGO_PATH):
|
| 26 |
-
raise RuntimeError("Logo not found: data/dds_logo.png.png (commit it to your Space repo).")
|
| 27 |
-
|
| 28 |
-
EMBED_MODEL = "text-embedding-3-small" # 1536-dim
|
| 29 |
-
LLM_MODEL = "gpt-4o-mini"
|
| 30 |
-
TOP_K = 4 # internal similarity_top_k
|
| 31 |
-
|
| 32 |
-
# ===== LlamaIndex / Pinecone (simple, fixed serverless: aws/us-east-1) =====
|
| 33 |
-
Settings.embed_model = OpenAIEmbedding(model=EMBED_MODEL, api_key=OPENAI_API_KEY)
|
| 34 |
-
Settings.llm = OpenAI(model=LLM_MODEL, api_key=OPENAI_API_KEY, system_prompt=SYSTEM_PROMPT)
|
| 35 |
-
|
| 36 |
-
pc = Pinecone(api_key=PINECONE_API_KEY)
|
| 37 |
-
def ensure_index(name: str, dim: int = 1536):
|
| 38 |
-
names = [i["name"] for i in pc.list_indexes()]
|
| 39 |
-
if name not in names:
|
| 40 |
-
pc.create_index(
|
| 41 |
-
name=name, dimension=dim, metric="cosine",
|
| 42 |
-
spec=ServerlessSpec(cloud="aws", region="us-east-1"),
|
| 43 |
-
)
|
| 44 |
-
return pc.Index(name)
|
| 45 |
-
|
| 46 |
-
# Fixed index name for simplicity
|
| 47 |
-
pinecone_index = ensure_index("dds-insurance-index", dim=1536)
|
| 48 |
-
vector_store = PineconeVectorStore(pinecone_index=pinecone_index)
|
| 49 |
-
|
| 50 |
-
def bootstrap_index():
|
| 51 |
-
if not os.path.isdir(DATA_DIR):
|
| 52 |
-
raise RuntimeError("No 'data/' directory found. Commit your documents to data/ in the Space repo.")
|
| 53 |
-
docs = SimpleDirectoryReader(DATA_DIR).load_data()
|
| 54 |
-
if not docs:
|
| 55 |
-
raise RuntimeError("No documents found in data/. Add e.g., data/insurance.pdf")
|
| 56 |
-
storage_ctx = StorageContext.from_defaults(vector_store=vector_store)
|
| 57 |
-
VectorStoreIndex.from_documents(docs, storage_context=storage_ctx, show_progress=True)
|
| 58 |
-
|
| 59 |
-
bootstrap_index()
|
| 60 |
-
|
| 61 |
-
def answer(query: str) -> str:
|
| 62 |
-
if not query.strip():
|
| 63 |
-
return "Please enter a question (or select one from the FAQ list)."
|
| 64 |
-
index = VectorStoreIndex.from_vector_store(vector_store)
|
| 65 |
-
resp = index.as_query_engine(similarity_top_k=TOP_K).query(query)
|
| 66 |
-
return str(resp)
|
| 67 |
-
|
| 68 |
-
FAQS = [
|
| 69 |
-
"",
|
| 70 |
-
"What benefits are covered under the policy?",
|
| 71 |
-
"How do I file a claim and what documents are required?",
|
| 72 |
-
"What are the exclusions and limitations?",
|
| 73 |
-
"Is pre-authorization needed for hospitalization?",
|
| 74 |
-
"What is the reimbursement timeline?",
|
| 75 |
-
"How are outpatient vs inpatient services handled?",
|
| 76 |
-
"How can I check my network hospitals/clinics?",
|
| 77 |
-
"What is the co-pay or deductible policy?",
|
| 78 |
-
]
|
| 79 |
-
|
| 80 |
-
def use_faq(selected_faq: str, free_text: str):
|
| 81 |
-
prompt = (selected_faq or "").strip() or (free_text or "").strip()
|
| 82 |
-
if not prompt:
|
| 83 |
-
return "", "Please select a FAQ or type your question."
|
| 84 |
-
return prompt, answer(prompt)
|
| 85 |
-
|
| 86 |
-
# ===== UI =====
|
| 87 |
-
CSS = """
|
| 88 |
-
.header { display:flex; flex-direction:column; align-items:center; gap:6px; }
|
| 89 |
-
.logo img { width:300px; height:300px; object-fit:contain; } /* fixed 300x300 */
|
| 90 |
-
.title { text-align:center; font-weight:700; font-size:1.4rem; margin:6px 0 0 0; }
|
| 91 |
-
.subnote { text-align:center; margin-top:-2px; opacity:0.8; }
|
| 92 |
-
"""
|
| 93 |
-
|
| 94 |
-
with gr.Blocks(css=CSS, theme=gr.themes.Soft()) as demo:
|
| 95 |
-
with gr.Row():
|
| 96 |
-
with gr.Column():
|
| 97 |
-
gr.Markdown("<div class='header'>")
|
| 98 |
-
gr.Image(value=LOGO_PATH, show_label=False, elem_classes=["logo"])
|
| 99 |
-
gr.Markdown(
|
| 100 |
-
"<h1 class='title'>DDS Insurance Q&A — RAG Assistant</h1>"
|
| 101 |
-
"<p class='subnote'>Answers strictly from your insurance document(s)</p>"
|
| 102 |
-
)
|
| 103 |
-
gr.Markdown("</div>")
|
| 104 |
-
|
| 105 |
-
with gr.Row():
|
| 106 |
-
with gr.Column(scale=1):
|
| 107 |
-
gr.Markdown("### Ask from Frequently Asked Questions")
|
| 108 |
-
faq = gr.Dropdown(choices=FAQS, value=FAQS[0], label="Select a common question")
|
| 109 |
-
|
| 110 |
-
gr.Markdown("### Or type your question")
|
| 111 |
-
user_q = gr.Textbox(
|
| 112 |
-
label="Your question",
|
| 113 |
-
placeholder="e.g., What is covered under outpatient benefits?",
|
| 114 |
-
lines=2
|
| 115 |
-
)
|
| 116 |
-
ask_btn = gr.Button("Ask", variant="primary")
|
| 117 |
-
|
| 118 |
-
with gr.Column(scale=1):
|
| 119 |
-
chosen_prompt = gr.Textbox(label="Query sent", interactive=False)
|
| 120 |
-
answer_box = gr.Markdown()
|
| 121 |
-
|
| 122 |
-
ask_btn.click(use_faq, inputs=[faq, user_q], outputs=[chosen_prompt, answer_box])
|
| 123 |
-
|
| 124 |
-
if __name__ == "__main__":
|
| 125 |
-
demo.launch()
|
|
|
|
| 1 |
+
# app.py — Insurance Q&A (RAG) with system prompt + simple config
|
| 2 |
+
import os
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from pinecone import Pinecone, ServerlessSpec
|
| 5 |
+
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, StorageContext, Settings
|
| 6 |
+
from llama_index.vector_stores.pinecone import PineconeVectorStore
|
| 7 |
+
from llama_index.embeddings.openai import OpenAIEmbedding
|
| 8 |
+
from llama_index.llms.openai import OpenAI
|
| 9 |
+
|
| 10 |
+
# --- System Prompt (polite + answer-from-document constraint) ---
|
| 11 |
+
SYSTEM_PROMPT = """You are Aisha, a polite and professional Insurance assistant.
|
| 12 |
+
Answer ONLY using the information found in the indexed insurance document(s).
|
| 13 |
+
If the answer is not in the document(s), say: "I couldn’t find that in the document."
|
| 14 |
+
Keep responses concise, helpful, and courteous.
|
| 15 |
+
"""
|
| 16 |
+
|
| 17 |
+
# ===== Minimal CONFIG (only necessary keys) =====
|
| 18 |
+
PINECONE_API_KEY = os.getenv("PINECONE_API_KEY")
|
| 19 |
+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 20 |
+
if not PINECONE_API_KEY or not OPENAI_API_KEY:
|
| 21 |
+
raise RuntimeError("Missing PINECONE_API_KEY or OPENAI_API_KEY (set them in Space → Settings → Variables).")
|
| 22 |
+
|
| 23 |
+
DATA_DIR = "data" # Put insurance docs here (e.g., data/insurance.pdf)
|
| 24 |
+
LOGO_PATH = os.path.join(DATA_DIR, "dds_logo.png") # Mandatory logo
|
| 25 |
+
if not os.path.exists(LOGO_PATH):
|
| 26 |
+
raise RuntimeError("Logo not found: data/dds_logo.png.png (commit it to your Space repo).")
|
| 27 |
+
|
| 28 |
+
EMBED_MODEL = "text-embedding-3-small" # 1536-dim
|
| 29 |
+
LLM_MODEL = "gpt-4o-mini"
|
| 30 |
+
TOP_K = 4 # internal similarity_top_k
|
| 31 |
+
|
| 32 |
+
# ===== LlamaIndex / Pinecone (simple, fixed serverless: aws/us-east-1) =====
|
| 33 |
+
Settings.embed_model = OpenAIEmbedding(model=EMBED_MODEL, api_key=OPENAI_API_KEY)
|
| 34 |
+
Settings.llm = OpenAI(model=LLM_MODEL, api_key=OPENAI_API_KEY, system_prompt=SYSTEM_PROMPT)
|
| 35 |
+
|
| 36 |
+
pc = Pinecone(api_key=PINECONE_API_KEY)
|
| 37 |
+
def ensure_index(name: str, dim: int = 1536):
|
| 38 |
+
names = [i["name"] for i in pc.list_indexes()]
|
| 39 |
+
if name not in names:
|
| 40 |
+
pc.create_index(
|
| 41 |
+
name=name, dimension=dim, metric="cosine",
|
| 42 |
+
spec=ServerlessSpec(cloud="aws", region="us-east-1"),
|
| 43 |
+
)
|
| 44 |
+
return pc.Index(name)
|
| 45 |
+
|
| 46 |
+
# Fixed index name for simplicity
|
| 47 |
+
pinecone_index = ensure_index("dds-insurance-index", dim=1536)
|
| 48 |
+
vector_store = PineconeVectorStore(pinecone_index=pinecone_index)
|
| 49 |
+
|
| 50 |
+
def bootstrap_index():
|
| 51 |
+
if not os.path.isdir(DATA_DIR):
|
| 52 |
+
raise RuntimeError("No 'data/' directory found. Commit your documents to data/ in the Space repo.")
|
| 53 |
+
docs = SimpleDirectoryReader(DATA_DIR).load_data()
|
| 54 |
+
if not docs:
|
| 55 |
+
raise RuntimeError("No documents found in data/. Add e.g., data/insurance.pdf")
|
| 56 |
+
storage_ctx = StorageContext.from_defaults(vector_store=vector_store)
|
| 57 |
+
VectorStoreIndex.from_documents(docs, storage_context=storage_ctx, show_progress=True)
|
| 58 |
+
|
| 59 |
+
bootstrap_index()
|
| 60 |
+
|
| 61 |
+
def answer(query: str) -> str:
|
| 62 |
+
if not query.strip():
|
| 63 |
+
return "Please enter a question (or select one from the FAQ list)."
|
| 64 |
+
index = VectorStoreIndex.from_vector_store(vector_store)
|
| 65 |
+
resp = index.as_query_engine(similarity_top_k=TOP_K).query(query)
|
| 66 |
+
return str(resp)
|
| 67 |
+
|
| 68 |
+
FAQS = [
|
| 69 |
+
"",
|
| 70 |
+
"What benefits are covered under the policy?",
|
| 71 |
+
"How do I file a claim and what documents are required?",
|
| 72 |
+
"What are the exclusions and limitations?",
|
| 73 |
+
"Is pre-authorization needed for hospitalization?",
|
| 74 |
+
"What is the reimbursement timeline?",
|
| 75 |
+
"How are outpatient vs inpatient services handled?",
|
| 76 |
+
"How can I check my network hospitals/clinics?",
|
| 77 |
+
"What is the co-pay or deductible policy?",
|
| 78 |
+
]
|
| 79 |
+
|
| 80 |
+
def use_faq(selected_faq: str, free_text: str):
|
| 81 |
+
prompt = (selected_faq or "").strip() or (free_text or "").strip()
|
| 82 |
+
if not prompt:
|
| 83 |
+
return "", "Please select a FAQ or type your question."
|
| 84 |
+
return prompt, answer(prompt)
|
| 85 |
+
|
| 86 |
+
# ===== UI =====
|
| 87 |
+
CSS = """
|
| 88 |
+
.header { display:flex; flex-direction:column; align-items:center; gap:6px; }
|
| 89 |
+
.logo img { width:300px; height:300px; object-fit:contain; } /* fixed 300x300 */
|
| 90 |
+
.title { text-align:center; font-weight:700; font-size:1.4rem; margin:6px 0 0 0; }
|
| 91 |
+
.subnote { text-align:center; margin-top:-2px; opacity:0.8; }
|
| 92 |
+
"""
|
| 93 |
+
|
| 94 |
+
with gr.Blocks(css=CSS, theme=gr.themes.Soft()) as demo:
|
| 95 |
+
with gr.Row():
|
| 96 |
+
with gr.Column():
|
| 97 |
+
gr.Markdown("<div class='header'>")
|
| 98 |
+
gr.Image(value=LOGO_PATH, show_label=False, elem_classes=["logo"])
|
| 99 |
+
gr.Markdown(
|
| 100 |
+
"<h1 class='title'>DDS Insurance Q&A — RAG Assistant</h1>"
|
| 101 |
+
"<p class='subnote'>Answers strictly from your insurance document(s)</p>"
|
| 102 |
+
)
|
| 103 |
+
gr.Markdown("</div>")
|
| 104 |
+
|
| 105 |
+
with gr.Row():
|
| 106 |
+
with gr.Column(scale=1):
|
| 107 |
+
gr.Markdown("### Ask from Frequently Asked Questions")
|
| 108 |
+
faq = gr.Dropdown(choices=FAQS, value=FAQS[0], label="Select a common question")
|
| 109 |
+
|
| 110 |
+
gr.Markdown("### Or type your question")
|
| 111 |
+
user_q = gr.Textbox(
|
| 112 |
+
label="Your question",
|
| 113 |
+
placeholder="e.g., What is covered under outpatient benefits?",
|
| 114 |
+
lines=2
|
| 115 |
+
)
|
| 116 |
+
ask_btn = gr.Button("Ask", variant="primary")
|
| 117 |
+
|
| 118 |
+
with gr.Column(scale=1):
|
| 119 |
+
chosen_prompt = gr.Textbox(label="Query sent", interactive=False)
|
| 120 |
+
answer_box = gr.Markdown()
|
| 121 |
+
|
| 122 |
+
ask_btn.click(use_faq, inputs=[faq, user_q], outputs=[chosen_prompt, answer_box])
|
| 123 |
+
|
| 124 |
+
if __name__ == "__main__":
|
| 125 |
+
demo.launch()
|
dds_logo.png
ADDED
|
Git LFS Details
|
insurance.pdf
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:536603a97eea5752c1447b7411ad4c03054d6d0f3a3bc1c887f3dc26de8e7892
|
| 3 |
+
size 1341586
|
requirements.txt
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
-
gradio>=4.44.0
|
| 2 |
-
pinecone-client>=5.0.1
|
| 3 |
-
openai>=1.51.0
|
| 4 |
-
llama-index>=0.11.0
|
| 5 |
-
llama-index-vector-stores-pinecone>=0.3.0
|
| 6 |
-
llama-index-embeddings-openai>=0.3.0
|
| 7 |
-
llama-index-llms-openai>=0.2.0
|
| 8 |
-
tiktoken>=0.7.0
|
|
|
|
| 1 |
+
gradio>=4.44.0
|
| 2 |
+
pinecone-client>=5.0.1
|
| 3 |
+
openai>=1.51.0
|
| 4 |
+
llama-index>=0.11.0
|
| 5 |
+
llama-index-vector-stores-pinecone>=0.3.0
|
| 6 |
+
llama-index-embeddings-openai>=0.3.0
|
| 7 |
+
llama-index-llms-openai>=0.2.0
|
| 8 |
+
tiktoken>=0.7.0
|