Update app.py
Browse files
app.py
CHANGED
|
@@ -1,59 +1,50 @@
|
|
| 1 |
-
# app.py — Omantel Insurance Q&A (RAG) with
|
| 2 |
import os
|
| 3 |
-
import logging
|
| 4 |
import gradio as gr
|
| 5 |
-
|
| 6 |
from pinecone import Pinecone, ServerlessSpec
|
| 7 |
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, StorageContext, Settings
|
| 8 |
from llama_index.vector_stores.pinecone import PineconeVectorStore
|
| 9 |
from llama_index.embeddings.openai import OpenAIEmbedding
|
| 10 |
from llama_index.llms.openai import OpenAI
|
| 11 |
|
| 12 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
PINECONE_API_KEY = os.getenv("PINECONE_API_KEY")
|
| 14 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
PINECONE_CLOUD = os.getenv("PINECONE_CLOUD", "aws")
|
| 19 |
-
EMBED_MODEL = os.getenv("EMBED_MODEL", "text-embedding-3-small") # 1536-dim
|
| 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 |
-
|
| 36 |
-
|
|
|
|
| 37 |
|
| 38 |
-
# ===== LlamaIndex / Pinecone =====
|
| 39 |
Settings.embed_model = OpenAIEmbedding(model=EMBED_MODEL, api_key=OPENAI_API_KEY)
|
| 40 |
-
Settings.llm = OpenAI(model=LLM_MODEL, api_key=OPENAI_API_KEY)
|
| 41 |
|
| 42 |
pc = Pinecone(api_key=PINECONE_API_KEY)
|
| 43 |
-
|
| 44 |
def ensure_index(name: str, dim: int = 1536):
|
| 45 |
names = [i["name"] for i in pc.list_indexes()]
|
| 46 |
if name not in names:
|
| 47 |
-
log.info(f"Creating Pinecone index '{name}' (dim={dim})...")
|
| 48 |
pc.create_index(
|
| 49 |
-
name=name,
|
| 50 |
-
|
| 51 |
-
metric="cosine",
|
| 52 |
-
spec=ServerlessSpec(cloud=PINECONE_CLOUD, region=PINECONE_REGION),
|
| 53 |
)
|
| 54 |
return pc.Index(name)
|
| 55 |
|
| 56 |
-
|
|
|
|
| 57 |
vector_store = PineconeVectorStore(pinecone_index=pinecone_index)
|
| 58 |
|
| 59 |
def bootstrap_index():
|
|
@@ -68,11 +59,10 @@ def bootstrap_index():
|
|
| 68 |
bootstrap_index()
|
| 69 |
|
| 70 |
def answer(query: str) -> str:
|
| 71 |
-
if not query
|
| 72 |
return "Please enter a question (or select one from the FAQ list)."
|
| 73 |
index = VectorStoreIndex.from_vector_store(vector_store)
|
| 74 |
-
|
| 75 |
-
resp = engine.query(query)
|
| 76 |
return str(resp)
|
| 77 |
|
| 78 |
FAQS = [
|
|
@@ -106,8 +96,10 @@ with gr.Blocks(css=CSS, theme=gr.themes.Soft()) as demo:
|
|
| 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(
|
| 110 |
-
|
|
|
|
|
|
|
| 111 |
gr.Markdown("</div>")
|
| 112 |
|
| 113 |
with gr.Row():
|
|
|
|
| 1 |
+
# app.py — Omantel 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, "Omantel_Logo_new.png") # Mandatory logo
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
if not os.path.exists(LOGO_PATH):
|
| 26 |
raise RuntimeError("Logo not found: data/Omantel_Logo_new.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():
|
|
|
|
| 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 = [
|
|
|
|
| 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'>Omantel 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():
|