feat: add Gradio web UI
- app.py: Gradio Interface wrapping BoschProductRAG.query() - Index built once at startup (not per-request) - Markdown output: answer + linked sources + latency - Example prompts included for quick demo - Verified: launches on :7860, returns correct answers Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
60
app.py
Normal file
60
app.py
Normal file
@@ -0,0 +1,60 @@
|
||||
"""Gradio web UI for the Bosch HVAC Product Knowledge Bot.
|
||||
|
||||
Run with: python app.py
|
||||
"""
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
import gradio as gr
|
||||
|
||||
from src.rag import BoschProductRAG
|
||||
|
||||
load_dotenv()
|
||||
|
||||
CATALOG_PATH = os.path.join(os.path.dirname(__file__), "src", "products_catalog.json")
|
||||
|
||||
# Build the RAG index once at startup (not per-request).
|
||||
_rag = BoschProductRAG()
|
||||
_rag.build_index(_rag.load_products(CATALOG_PATH))
|
||||
|
||||
|
||||
def answer_question(question: str) -> str:
|
||||
if not question or not question.strip():
|
||||
return "Please enter a question."
|
||||
|
||||
result = _rag.query(question)
|
||||
|
||||
sources_md = "\n".join(
|
||||
f"- [{s['name']}]({s['url']})" if s["url"] else f"- {s['name']}"
|
||||
for s in result["sources"]
|
||||
)
|
||||
|
||||
return (
|
||||
f"### Answer\n{result['answer']}\n\n"
|
||||
f"### Sources\n{sources_md or '_none retrieved_'}\n\n"
|
||||
f"---\n*Latency: {result['latency_ms']:.0f}ms*"
|
||||
)
|
||||
|
||||
|
||||
demo = gr.Interface(
|
||||
fn=answer_question,
|
||||
inputs=gr.Textbox(
|
||||
label="Ask about Bosch HVAC products",
|
||||
placeholder="e.g. Which heat pump is best for retrofits?",
|
||||
lines=2,
|
||||
),
|
||||
outputs=gr.Markdown(label="Response"),
|
||||
title="Bosch HVAC Product Knowledge Bot",
|
||||
description=(
|
||||
"RAG-powered assistant for Bosch HVAC sales & spec questions. "
|
||||
"Retrieval: FAISS + Google Gemini embeddings. Generation: Gemini Flash Lite."
|
||||
),
|
||||
examples=[
|
||||
"Which heat pump is best for retrofits?",
|
||||
"What's the most energy-efficient system you offer?",
|
||||
"Do you have a smart thermostat?",
|
||||
"What cooling systems use R32 refrigerant?",
|
||||
],
|
||||
)
|
||||
|
||||
if __name__ == "__main__":
|
||||
demo.launch()
|
||||
Reference in New Issue
Block a user