Spaces:
Sleeping
Sleeping
Commit
Β·
f648189
1
Parent(s):
f129d48
Prod ready streamlit
Browse files- src/streamlit_app.py +67 -3
src/streamlit_app.py
CHANGED
|
@@ -1,13 +1,77 @@
|
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
| 2 |
from ingest import build_index
|
|
|
|
|
|
|
|
|
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
st.title("π OTT Bot")
|
| 5 |
|
|
|
|
|
|
|
|
|
|
| 6 |
if st.button("π¨ Build Index"):
|
| 7 |
-
with st.spinner("Building FAISS index..."):
|
| 8 |
build_index()
|
| 9 |
-
st.success("Index built successfully
|
| 10 |
|
| 11 |
st.divider()
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
import streamlit as st
|
| 3 |
+
import numpy as np
|
| 4 |
+
from sentence_transformers import SentenceTransformer
|
| 5 |
from ingest import build_index
|
| 6 |
+
from rag import retrieve
|
| 7 |
+
from groq import Groq
|
| 8 |
+
from config import EMBEDDING_MODEL
|
| 9 |
|
| 10 |
+
# ---------------- CONFIG ----------------
|
| 11 |
+
st.set_page_config(page_title="OTT Bot", layout="wide")
|
| 12 |
+
|
| 13 |
+
# ---------------- LOAD MODELS ----------------
|
| 14 |
+
@st.cache_resource
|
| 15 |
+
def load_embedder():
|
| 16 |
+
return SentenceTransformer(EMBEDDING_MODEL)
|
| 17 |
+
|
| 18 |
+
embedder = load_embedder()
|
| 19 |
+
|
| 20 |
+
# ---------------- GROQ CLIENT ----------------
|
| 21 |
+
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
| 22 |
+
if not GROQ_API_KEY:
|
| 23 |
+
st.error("β GROQ_API_KEY not set in HF Secrets")
|
| 24 |
+
st.stop()
|
| 25 |
+
|
| 26 |
+
client = Groq(api_key=GROQ_API_KEY)
|
| 27 |
+
|
| 28 |
+
# ---------------- UI ----------------
|
| 29 |
st.title("π OTT Bot")
|
| 30 |
|
| 31 |
+
st.markdown("Upload PDFs via HF Dataset and query them using AI")
|
| 32 |
+
|
| 33 |
+
# ---------------- INGESTION ----------------
|
| 34 |
if st.button("π¨ Build Index"):
|
| 35 |
+
with st.spinner("Building FAISS index from dataset..."):
|
| 36 |
build_index()
|
| 37 |
+
st.success("β
Index built successfully")
|
| 38 |
|
| 39 |
st.divider()
|
| 40 |
|
| 41 |
+
# ---------------- QUESTION INPUT ----------------
|
| 42 |
+
question = st.text_input(
|
| 43 |
+
"Ask a question about the document",
|
| 44 |
+
placeholder="e.g. What is the main concept discussed?"
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
# ---------------- ANSWER ----------------
|
| 48 |
+
if question:
|
| 49 |
+
with st.spinner("Searching document..."):
|
| 50 |
+
query_embedding = embedder.encode([question]).astype("float32")
|
| 51 |
+
contexts = retrieve(query_embedding)
|
| 52 |
+
|
| 53 |
+
context_text = "\n\n".join(c["text"] for c in contexts)
|
| 54 |
+
|
| 55 |
+
prompt = f"""
|
| 56 |
+
Answer the question based on the context below.
|
| 57 |
+
|
| 58 |
+
Context:
|
| 59 |
+
{context_text}
|
| 60 |
+
|
| 61 |
+
Question:
|
| 62 |
+
{question}
|
| 63 |
+
|
| 64 |
+
Answer clearly and concisely.
|
| 65 |
+
"""
|
| 66 |
+
|
| 67 |
+
with st.spinner("Thinking..."):
|
| 68 |
+
response = client.chat.completions.create(
|
| 69 |
+
model="llama3-70b-8192",
|
| 70 |
+
messages=[
|
| 71 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
| 72 |
+
{"role": "user", "content": prompt},
|
| 73 |
+
],
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
st.subheader("Answer")
|
| 77 |
+
st.write(response.choices[0].message.content)
|