Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,4 @@
|
|
| 1 |
-
# app.py β Insurance Q&A (RAG) with
|
| 2 |
-
# Minimal changes; logic preserved. Uses Pinecone + LlamaIndex + OpenAI.
|
| 3 |
-
|
| 4 |
import os
|
| 5 |
import logging
|
| 6 |
import gradio as gr
|
|
@@ -22,15 +20,17 @@ EMBED_MODEL = os.getenv("EMBED_MODEL", "text-embedding-3-small") # 1536
|
|
| 22 |
LLM_MODEL = os.getenv("LLM_MODEL", "gpt-4o-mini")
|
| 23 |
|
| 24 |
DATA_DIR = "data"
|
| 25 |
-
DEFAULT_TOP_K = 4 # internal similarity_top_k
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
|
| 30 |
if not PINECONE_API_KEY:
|
| 31 |
-
raise RuntimeError("Missing PINECONE_API_KEY (
|
| 32 |
if not OPENAI_API_KEY:
|
| 33 |
-
raise RuntimeError("Missing OPENAI_API_KEY (
|
|
|
|
|
|
|
| 34 |
|
| 35 |
logging.basicConfig(level=logging.INFO)
|
| 36 |
log = logging.getLogger("dds-space")
|
|
@@ -59,14 +59,11 @@ vector_store = PineconeVectorStore(pinecone_index=pinecone_index)
|
|
| 59 |
def bootstrap_index():
|
| 60 |
if not os.path.isdir(DATA_DIR):
|
| 61 |
raise RuntimeError("No 'data/' directory found. Commit your documents to data/ in the Space repo.")
|
| 62 |
-
log.info("Loading documents from ./data ...")
|
| 63 |
docs = SimpleDirectoryReader(DATA_DIR).load_data()
|
| 64 |
if not docs:
|
| 65 |
raise RuntimeError("No documents found in data/. Add e.g., data/insurance.pdf")
|
| 66 |
-
log.info(f"Docs loaded: {len(docs)}. Upserting into Pineconeβ¦")
|
| 67 |
storage_ctx = StorageContext.from_defaults(vector_store=vector_store)
|
| 68 |
VectorStoreIndex.from_documents(docs, storage_context=storage_ctx, show_progress=True)
|
| 69 |
-
log.info("Index upsert complete.")
|
| 70 |
|
| 71 |
bootstrap_index()
|
| 72 |
|
|
@@ -98,23 +95,20 @@ def use_faq(selected_faq: str, free_text: str):
|
|
| 98 |
|
| 99 |
# ===== UI =====
|
| 100 |
CSS = """
|
| 101 |
-
.header {
|
| 102 |
-
.header
|
| 103 |
.title { text-align:center; font-weight:700; font-size:1.4rem; margin:6px 0 0 0; }
|
| 104 |
.subnote { text-align:center; margin-top:-2px; opacity:0.8; }
|
| 105 |
"""
|
| 106 |
|
| 107 |
with gr.Blocks(css=CSS, theme=gr.themes.Soft()) as demo:
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
</
|
| 114 |
-
|
| 115 |
-
<p class="subnote">Ask about coverage, claims, exclusions, and more β powered by LlamaIndex + Pinecone</p>
|
| 116 |
-
"""
|
| 117 |
-
)
|
| 118 |
|
| 119 |
with gr.Row():
|
| 120 |
with gr.Column(scale=1):
|
|
|
|
| 1 |
+
# app.py β Omantel Insurance Q&A (RAG) with local logo at top-center
|
|
|
|
|
|
|
| 2 |
import os
|
| 3 |
import logging
|
| 4 |
import gradio as gr
|
|
|
|
| 20 |
LLM_MODEL = os.getenv("LLM_MODEL", "gpt-4o-mini")
|
| 21 |
|
| 22 |
DATA_DIR = "data"
|
| 23 |
+
DEFAULT_TOP_K = 4 # internal similarity_top_k
|
| 24 |
|
| 25 |
+
# ---- Local logo (commit this image to your Space repo) ----
|
| 26 |
+
LOGO_PATH = os.path.join(DATA_DIR, "Omantel_Logo_new.png")
|
| 27 |
|
| 28 |
if not PINECONE_API_KEY:
|
| 29 |
+
raise RuntimeError("Missing PINECONE_API_KEY (Space β Settings β Variables).")
|
| 30 |
if not OPENAI_API_KEY:
|
| 31 |
+
raise RuntimeError("Missing OPENAI_API_KEY (Space β Settings β Variables).")
|
| 32 |
+
if not os.path.exists(LOGO_PATH):
|
| 33 |
+
raise RuntimeError("Logo not found: data/Omantel_Logo_new.png (commit it to your Space repo).")
|
| 34 |
|
| 35 |
logging.basicConfig(level=logging.INFO)
|
| 36 |
log = logging.getLogger("dds-space")
|
|
|
|
| 59 |
def bootstrap_index():
|
| 60 |
if not os.path.isdir(DATA_DIR):
|
| 61 |
raise RuntimeError("No 'data/' directory found. Commit your documents to data/ in the Space repo.")
|
|
|
|
| 62 |
docs = SimpleDirectoryReader(DATA_DIR).load_data()
|
| 63 |
if not docs:
|
| 64 |
raise RuntimeError("No documents found in data/. Add e.g., data/insurance.pdf")
|
|
|
|
| 65 |
storage_ctx = StorageContext.from_defaults(vector_store=vector_store)
|
| 66 |
VectorStoreIndex.from_documents(docs, storage_context=storage_ctx, show_progress=True)
|
|
|
|
| 67 |
|
| 68 |
bootstrap_index()
|
| 69 |
|
|
|
|
| 95 |
|
| 96 |
# ===== UI =====
|
| 97 |
CSS = """
|
| 98 |
+
.header { display:flex; flex-direction:column; align-items:center; gap:6px; }
|
| 99 |
+
.header .logo { max-height:80px; width:auto; }
|
| 100 |
.title { text-align:center; font-weight:700; font-size:1.4rem; margin:6px 0 0 0; }
|
| 101 |
.subnote { text-align:center; margin-top:-2px; opacity:0.8; }
|
| 102 |
"""
|
| 103 |
|
| 104 |
with gr.Blocks(css=CSS, theme=gr.themes.Soft()) as demo:
|
| 105 |
+
with gr.Row():
|
| 106 |
+
with gr.Column():
|
| 107 |
+
gr.Markdown("<div class='header'>")
|
| 108 |
+
gr.Image(value=LOGO_PATH, show_label=False, elem_classes=["logo"])
|
| 109 |
+
gr.Markdown("<h1 class='title'>Omantel Insurance Q&A β AI Assistant</h1>"
|
| 110 |
+
"<p class='subnote'>Ask about coverage, claims, exclusions, and more β powered by LlamaIndex + Pinecone</p>")
|
| 111 |
+
gr.Markdown("</div>")
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
with gr.Row():
|
| 114 |
with gr.Column(scale=1):
|