Spaces:
Runtime error
Runtime error
Upload 5 files
#2
by sunifjagirdar1989 - opened
Files changed (6)
- .gitattributes+1
- README.md+183-13
- app.py+125-125
- dds_logo.pngAdded
- insurance.pdfAdded+3
- requirements.txt+8-8
35 unmodified lines36373835 unmodified linesinsurance.pdf filter=lfs diff=lfs merge=lfs -textdata/insurance.pdf filter=lfs diff=lfs merge=lfs -textdata/dds_logo.png filter=lfs diff=lfs merge=lfs -text35 unmodified lines3637383935 unmodified linesinsurance.pdf filter=lfs diff=lfs merge=lfs -textdata/insurance.pdf filter=lfs diff=lfs merge=lfs -textdata/dds_logo.png filter=lfs diff=lfs merge=lfs -textdds_logo.png filter=lfs diff=lfs merge=lfs -text
12345678910111213---title: Insuranceomantelemoji: 🐨colorFrom: purplecolorTo: bluesdk: gradiosdk_version: 5.47.0app_file: app.pypinned: falseshort_description: insuranceomantel---Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183DDS Insurance Q&A — RAG Assistant (Pinecone + OpenAI + Gradio)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.What You’ll GetDeployed Space URL you can share.Grounded answers (no docs → the bot politely says it can’t find it).Simple UI with an FAQ dropdown + free-text question box.Clean structure designed for easy replication.FeaturesAnswers strictly from your data/ documents (RAG).Pinecone serverless index (AWS us-east-1, cosine, 1536-dim).OpenAI for embeddings (text-embedding-3-small) and LLM (gpt-4o-mini).Gradio interface with a centered required logo (data/dds_logo.png).Beginner-friendly defaults and error messages.Repository Structure.├─ data/ # Your insurance docs + required logo│ └─ dds_logo.png # REQUIRED (shown in header)├─ app.py # Main app: indexing + query + Gradio UI├─ requirements.txt # Dependencies└─ README.md # This fileConfiguration (in app.py)EMBED_MODEL = "text-embedding-3-small" # 1536-dimLLM_MODEL = "gpt-4o-mini"TOP_K = 4 # retrieval depthSystem Prompt (keeps answers grounded + polite):SYSTEM_PROMPT = """You are Aisha, a polite and professional Insurance assistant.Answer ONLY using the information found in the indexed insurance document(s).If the answer is not in the document(s), say: "I couldn’t find that in the document."Keep responses concise, helpful, and courteous."""FAQ List (editable):FAQS = ["","What benefits are covered under the policy?","How do I file a claim and what documents are required?","What are the exclusions and limitations?","Is pre-authorization needed for hospitalization?","What is the reimbursement timeline?","How are outpatient vs inpatient services handled?","How can I check my network hospitals/clinics?","What is the co-pay or deductible policy?",]Deploy to Hugging Face Spaces (Beginner-Friendly)1) Create a SpaceGo to Hugging Face → Spaces → New SpaceSDK: GradioVisibility/licensing: your choice2) Add Project FilesUpload these into your Space:app.pyrequirements.txtREADME.mdCreate folder data/ and upload:Your insurance documents (PDF/TXT/MD…)dds_logo.png (mandatory; exact filename)Tip: Your Space file tree should match the Repository Structure above.3) Set Secrets (Environment Variables)In Space → Settings → Variables and secrets, add:OPENAI_API_KEY → your OpenAI keyPINECONE_API_KEY → your Pinecone keyNo legacy Pinecone environment URL needed. This app uses pinecone-client ≥ 5 with serverless.4) Build & RunSpaces auto-install from requirements.txt.Default CPU hardware is fine.Entry point auto-detected from app.py.On first start, the app will:Ensure a Pinecone serverless index:dds-insurance-index · cosine · 1536-dim · aws/us-east-1Read and index documents from data/Launch the Gradio UIYour deployed link is simply the Space URL once its status is Running.5) Updating Documents LaterUpload/change files in data/Click Restart on the Space so it re-indexes your documentsTroubleshooting (Common Issues)“Missing PINECONE_API_KEY or OPENAI_API_KEY”Add both secrets in Space → Settings → Variables and secrets.Pinecone 401 / “Malformed domain”Ensure you’re on pinecone-client>=5.0.1 (already in requirements.txt).Use a valid Pinecone API key; no environment URL needed for serverless.“Logo not found: data/dds_logo.png”Upload an image named exactly dds_logo.png into the data/ folder.“No documents found in data/”Upload at least one doc (PDF/TXT/MD) into data/, then Restart the Space.OpenAI authorization/rate-limit errorsConfirm key validity and model access; reduce usage if rate-limited.Slow first loadFirst run installs dependencies and builds the index; later runs are faster.Manual Test ChecklistAsk a question clearly answered in your docs → response should quote that knowledge.Ask something not in your docs → bot should say it can’t find it.Adjust TOP_K in app.py to see how answer completeness changes.Requirements (from requirements.txt)gradio>=4.44.0pinecone-client>=5.0.1openai>=1.51.0llama-index>=0.11.0llama-index-vector-stores-pinecone>=0.3.0llama-index-embeddings-openai>=0.3.0llama-index-llms-openai>=0.2.0tiktoken>=0.7.0Customization IdeasSwap LLMs by editing LLM_MODEL.Add a file uploader to refresh docs from the UI.Add metadata filters (e.g., policy type).Log queries to refine the FAQ list.LicenseAdd your chosen license (e.g., MIT) as LICENSE.AcknowledgmentsThanks to LlamaIndex, Pinecone, OpenAI, and Gradio for the tooling that makes this simple and reproducible.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125# app.py — Insurance Q&A (RAG) with system prompt + simple configimport osimport gradio as grfrom pinecone import Pinecone, ServerlessSpecfrom llama_index.core import VectorStoreIndex, SimpleDirectoryReader, StorageContext, Settingsfrom llama_index.vector_stores.pinecone import PineconeVectorStorefrom llama_index.embeddings.openai import OpenAIEmbeddingfrom llama_index.llms.openai import OpenAI# --- System Prompt (polite + answer-from-document constraint) ---SYSTEM_PROMPT = """You are Aisha, a polite and professional Insurance assistant.Answer ONLY using the information found in the indexed insurance document(s).If the answer is not in the document(s), say: "I couldn’t find that in the document."Keep responses concise, helpful, and courteous."""# ===== Minimal CONFIG (only necessary keys) =====PINECONE_API_KEY = os.getenv("PINECONE_API_KEY")OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")if not PINECONE_API_KEY or not OPENAI_API_KEY:raise RuntimeError("Missing PINECONE_API_KEY or OPENAI_API_KEY (set them in Space → Settings → Variables).")DATA_DIR = "data" # Put insurance docs here (e.g., data/insurance.pdf)LOGO_PATH = os.path.join(DATA_DIR, "dds_logo.png") # Mandatory logoif not os.path.exists(LOGO_PATH):raise RuntimeError("Logo not found: data/dds_logo.png.png (commit it to your Space repo).")EMBED_MODEL = "text-embedding-3-small" # 1536-dimLLM_MODEL = "gpt-4o-mini"TOP_K = 4 # internal similarity_top_k# ===== LlamaIndex / Pinecone (simple, fixed serverless: aws/us-east-1) =====Settings.embed_model = OpenAIEmbedding(model=EMBED_MODEL, api_key=OPENAI_API_KEY)Settings.llm = OpenAI(model=LLM_MODEL, api_key=OPENAI_API_KEY, system_prompt=SYSTEM_PROMPT)pc = Pinecone(api_key=PINECONE_API_KEY)def ensure_index(name: str, dim: int = 1536):names = [i["name"] for i in pc.list_indexes()]if name not in names:pc.create_index(name=name, dimension=dim, metric="cosine",spec=ServerlessSpec(cloud="aws", region="us-east-1"),)return pc.Index(name)# Fixed index name for simplicitypinecone_index = ensure_index("dds-insurance-index", dim=1536)vector_store = PineconeVectorStore(pinecone_index=pinecone_index)def bootstrap_index():if not os.path.isdir(DATA_DIR):raise RuntimeError("No 'data/' directory found. Commit your documents to data/ in the Space repo.")docs = SimpleDirectoryReader(DATA_DIR).load_data()if not docs:raise RuntimeError("No documents found in data/. Add e.g., data/insurance.pdf")storage_ctx = StorageContext.from_defaults(vector_store=vector_store)VectorStoreIndex.from_documents(docs, storage_context=storage_ctx, show_progress=True)bootstrap_index()def answer(query: str) -> str:if not query.strip():return "Please enter a question (or select one from the FAQ list)."index = VectorStoreIndex.from_vector_store(vector_store)resp = index.as_query_engine(similarity_top_k=TOP_K).query(query)return str(resp)FAQS = ["","What benefits are covered under the policy?","How do I file a claim and what documents are required?","What are the exclusions and limitations?","Is pre-authorization needed for hospitalization?","What is the reimbursement timeline?","How are outpatient vs inpatient services handled?","How can I check my network hospitals/clinics?","What is the co-pay or deductible policy?",]def use_faq(selected_faq: str, free_text: str):prompt = (selected_faq or "").strip() or (free_text or "").strip()if not prompt:return "", "Please select a FAQ or type your question."return prompt, answer(prompt)# ===== UI =====CSS = """.header { display:flex; flex-direction:column; align-items:center; gap:6px; }.logo img { width:300px; height:300px; object-fit:contain; } /* fixed 300x300 */.title { text-align:center; font-weight:700; font-size:1.4rem; margin:6px 0 0 0; }.subnote { text-align:center; margin-top:-2px; opacity:0.8; }"""with gr.Blocks(css=CSS, theme=gr.themes.Soft()) as demo:with gr.Row():with gr.Column():gr.Markdown("<div class='header'>")gr.Image(value=LOGO_PATH, show_label=False, elem_classes=["logo"])gr.Markdown("<h1 class='title'>DDS Insurance Q&A — RAG Assistant</h1>""<p class='subnote'>Answers strictly from your insurance document(s)</p>")gr.Markdown("</div>")with gr.Row():with gr.Column(scale=1):gr.Markdown("### Ask from Frequently Asked Questions")faq = gr.Dropdown(choices=FAQS, value=FAQS[0], label="Select a common question")gr.Markdown("### Or type your question")user_q = gr.Textbox(label="Your question",placeholder="e.g., What is covered under outpatient benefits?",lines=2)ask_btn = gr.Button("Ask", variant="primary")with gr.Column(scale=1):chosen_prompt = gr.Textbox(label="Query sent", interactive=False)answer_box = gr.Markdown()ask_btn.click(use_faq, inputs=[faq, user_q], outputs=[chosen_prompt, answer_box])if __name__ == "__main__":demo.launch()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125# app.py — Insurance Q&A (RAG) with system prompt + simple configimport osimport gradio as grfrom pinecone import Pinecone, ServerlessSpecfrom llama_index.core import VectorStoreIndex, SimpleDirectoryReader, StorageContext, Settingsfrom llama_index.vector_stores.pinecone import PineconeVectorStorefrom llama_index.embeddings.openai import OpenAIEmbeddingfrom llama_index.llms.openai import OpenAI# --- System Prompt (polite + answer-from-document constraint) ---SYSTEM_PROMPT = """You are Aisha, a polite and professional Insurance assistant.Answer ONLY using the information found in the indexed insurance document(s).If the answer is not in the document(s), say: "I couldn’t find that in the document."Keep responses concise, helpful, and courteous."""# ===== Minimal CONFIG (only necessary keys) =====PINECONE_API_KEY = os.getenv("PINECONE_API_KEY")OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")if not PINECONE_API_KEY or not OPENAI_API_KEY:raise RuntimeError("Missing PINECONE_API_KEY or OPENAI_API_KEY (set them in Space → Settings → Variables).")DATA_DIR = "data" # Put insurance docs here (e.g., data/insurance.pdf)LOGO_PATH = os.path.join(DATA_DIR, "dds_logo.png") # Mandatory logoif not os.path.exists(LOGO_PATH):raise RuntimeError("Logo not found: data/dds_logo.png.png (commit it to your Space repo).")EMBED_MODEL = "text-embedding-3-small" # 1536-dimLLM_MODEL = "gpt-4o-mini"TOP_K = 4 # internal similarity_top_k# ===== LlamaIndex / Pinecone (simple, fixed serverless: aws/us-east-1) =====Settings.embed_model = OpenAIEmbedding(model=EMBED_MODEL, api_key=OPENAI_API_KEY)Settings.llm = OpenAI(model=LLM_MODEL, api_key=OPENAI_API_KEY, system_prompt=SYSTEM_PROMPT)pc = Pinecone(api_key=PINECONE_API_KEY)def ensure_index(name: str, dim: int = 1536):names = [i["name"] for i in pc.list_indexes()]if name not in names:pc.create_index(name=name, dimension=dim, metric="cosine",spec=ServerlessSpec(cloud="aws", region="us-east-1"),)return pc.Index(name)# Fixed index name for simplicitypinecone_index = ensure_index("dds-insurance-index", dim=1536)vector_store = PineconeVectorStore(pinecone_index=pinecone_index)def bootstrap_index():if not os.path.isdir(DATA_DIR):raise RuntimeError("No 'data/' directory found. Commit your documents to data/ in the Space repo.")docs = SimpleDirectoryReader(DATA_DIR).load_data()if not docs:raise RuntimeError("No documents found in data/. Add e.g., data/insurance.pdf")storage_ctx = StorageContext.from_defaults(vector_store=vector_store)VectorStoreIndex.from_documents(docs, storage_context=storage_ctx, show_progress=True)bootstrap_index()def answer(query: str) -> str:if not query.strip():return "Please enter a question (or select one from the FAQ list)."index = VectorStoreIndex.from_vector_store(vector_store)resp = index.as_query_engine(similarity_top_k=TOP_K).query(query)return str(resp)FAQS = ["","What benefits are covered under the policy?","How do I file a claim and what documents are required?","What are the exclusions and limitations?","Is pre-authorization needed for hospitalization?","What is the reimbursement timeline?","How are outpatient vs inpatient services handled?","How can I check my network hospitals/clinics?","What is the co-pay or deductible policy?",]def use_faq(selected_faq: str, free_text: str):prompt = (selected_faq or "").strip() or (free_text or "").strip()if not prompt:return "", "Please select a FAQ or type your question."return prompt, answer(prompt)# ===== UI =====CSS = """.header { display:flex; flex-direction:column; align-items:center; gap:6px; }.logo img { width:300px; height:300px; object-fit:contain; } /* fixed 300x300 */.title { text-align:center; font-weight:700; font-size:1.4rem; margin:6px 0 0 0; }.subnote { text-align:center; margin-top:-2px; opacity:0.8; }"""with gr.Blocks(css=CSS, theme=gr.themes.Soft()) as demo:with gr.Row():with gr.Column():gr.Markdown("<div class='header'>")gr.Image(value=LOGO_PATH, show_label=False, elem_classes=["logo"])gr.Markdown("<h1 class='title'>DDS Insurance Q&A — RAG Assistant</h1>""<p class='subnote'>Answers strictly from your insurance document(s)</p>")gr.Markdown("</div>")with gr.Row():with gr.Column(scale=1):gr.Markdown("### Ask from Frequently Asked Questions")faq = gr.Dropdown(choices=FAQS, value=FAQS[0], label="Select a common question")gr.Markdown("### Or type your question")user_q = gr.Textbox(label="Your question",placeholder="e.g., What is covered under outpatient benefits?",lines=2)ask_btn = gr.Button("Ask", variant="primary")with gr.Column(scale=1):chosen_prompt = gr.Textbox(label="Query sent", interactive=False)answer_box = gr.Markdown()ask_btn.click(use_faq, inputs=[faq, user_q], outputs=[chosen_prompt, answer_box])if __name__ == "__main__":demo.launch()
123version https://git-lfs.github.com/spec/v1oid sha256:536603a97eea5752c1447b7411ad4c03054d6d0f3a3bc1c887f3dc26de8e7892size 134158612345678gradio>=4.44.0pinecone-client>=5.0.1openai>=1.51.0llama-index>=0.11.0llama-index-vector-stores-pinecone>=0.3.0llama-index-embeddings-openai>=0.3.0llama-index-llms-openai>=0.2.0tiktoken>=0.7.012345678gradio>=4.44.0pinecone-client>=5.0.1openai>=1.51.0llama-index>=0.11.0llama-index-vector-stores-pinecone>=0.3.0llama-index-embeddings-openai>=0.3.0llama-index-llms-openai>=0.2.0tiktoken>=0.7.0
.gitattributes+1
README.md+183-13
app.py+125-125
dds_logo.pngAdded

Large File Pointer Details
- SHA256: b42f21a6a20156eabe67a0b0bfe99984b05ca38324186c5a1277d1d0a51e20a8
- Pointer size: 132 Bytes
- Size of remote file: 1.42 MB
insurance.pdfAdded+3
requirements.txt+8-8