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.Popen(
|
| 6 |
+
["uvicorn", "backend.main:app", "--host", "0.0.0.0", "--port", "8000"]
|
| 7 |
+
)
|
| 8 |
+
|
| 9 |
+
# small delay so backend boots
|
| 10 |
+
time.sleep(2)
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
import streamlit as st
|
| 16 |
+
import requests
|
| 17 |
+
import time
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
API_URL = "http://0.0.0.0:8000"
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
st.set_page_config(page_title="GenAI RAG App (Docs + Code)", layout="wide")
|
| 25 |
+
st.title("🧠 GenAI RAG App (Docs + Code)")
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
# ---------------- Chat Memory ----------------
|
| 29 |
+
if "history" not in st.session_state:
|
| 30 |
+
st.session_state["history"] = []
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
# ---------------- Sidebar Settings ----------------
|
| 34 |
+
with st.sidebar:
|
| 35 |
+
st.header("Settings")
|
| 36 |
+
|
| 37 |
+
domain = st.radio("Knowledge Base:", ["doc", "code"])
|
| 38 |
+
|
| 39 |
+
if st.button("Clear Chat"):
|
| 40 |
+
st.session_state["history"] = []
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
# ---------------- Tabs ----------------
|
| 44 |
+
upload_tab, query_tab = st.tabs(["⬆ Upload", "💬 Chat"])
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
# ---------------- Upload Tab ----------------
|
| 48 |
+
with upload_tab:
|
| 49 |
+
st.subheader("Upload File")
|
| 50 |
+
|
| 51 |
+
file = st.file_uploader("Upload PDF / TXT / Python code", type=["pdf", "txt", "md", "py", "js", "ts"])
|
| 52 |
+
|
| 53 |
+
if file:
|
| 54 |
+
ext = file.name.split(".")[-1]
|
| 55 |
+
|
| 56 |
+
if ext in ["pdf", "txt", "md"]:
|
| 57 |
+
endpoint = "/upload/doc"
|
| 58 |
+
else:
|
| 59 |
+
endpoint = "/upload/code"
|
| 60 |
+
|
| 61 |
+
files = {"file": (file.name, file.getvalue())}
|
| 62 |
+
|
| 63 |
+
with st.spinner("Indexing file..."):
|
| 64 |
+
r = requests.post(API_URL + endpoint, files=files)
|
| 65 |
+
|
| 66 |
+
if r.status_code == 200:
|
| 67 |
+
st.success("File Indexed!")
|
| 68 |
+
st.json(r.json())
|
| 69 |
+
else:
|
| 70 |
+
st.error("Upload Failed! Check FastAPI backend.")
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
# ---------------- Helper: Streaming Output ----------------
|
| 75 |
+
def stream_text(text, delay=0.025):
|
| 76 |
+
"""Yield tokens to simulate streaming."""
|
| 77 |
+
for token in text.split():
|
| 78 |
+
yield token + " "
|
| 79 |
+
time.sleep(delay)
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
# ---------------- Query / Chat Tab ----------------
|
| 84 |
+
with query_tab:
|
| 85 |
+
st.subheader("Ask a Question")
|
| 86 |
+
|
| 87 |
+
# Show chat history
|
| 88 |
+
for msg in st.session_state["history"]:
|
| 89 |
+
role = "You" if msg["role"] == "user" else "AI"
|
| 90 |
+
st.markdown(f"**{role}:** {msg['text']}")
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
# Mode selection based on domain
|
| 94 |
+
if domain == "code":
|
| 95 |
+
mode = st.selectbox("Mode:", [
|
| 96 |
+
"Explain",
|
| 97 |
+
"Debug",
|
| 98 |
+
"Fix",
|
| 99 |
+
"Refactor",
|
| 100 |
+
"Add Comments",
|
| 101 |
+
"Complexity"
|
| 102 |
+
])
|
| 103 |
+
else:
|
| 104 |
+
mode = st.selectbox("Mode:", [
|
| 105 |
+
"Answer",
|
| 106 |
+
"Summarize",
|
| 107 |
+
"Bullet Points",
|
| 108 |
+
"Keywords"
|
| 109 |
+
])
|
| 110 |
+
|
| 111 |
+
question = st.text_input("Enter your question")
|
| 112 |
+
|
| 113 |
+
if st.button("Ask"):
|
| 114 |
+
if not question.strip():
|
| 115 |
+
st.warning("Enter a question before submitting.")
|
| 116 |
+
else:
|
| 117 |
+
payload = {"question": question, "domain": domain, "mode": mode}
|
| 118 |
+
|
| 119 |
+
with st.spinner("Thinking..."):
|
| 120 |
+
r = requests.post(API_URL + "/query", json=payload)
|
| 121 |
+
|
| 122 |
+
if r.status_code == 200:
|
| 123 |
+
data = r.json()
|
| 124 |
+
answer = data["answer"]
|
| 125 |
+
context = data["context"]
|
| 126 |
+
|
| 127 |
+
# Save history
|
| 128 |
+
st.session_state["history"].append({"role": "user", "text": question})
|
| 129 |
+
st.session_state["history"].append({"role": "assistant", "text": answer})
|
| 130 |
+
|
| 131 |
+
# Stream output
|
| 132 |
+
st.success("AI:")
|
| 133 |
+
placeholder = st.empty()
|
| 134 |
+
stream = stream_text(answer)
|
| 135 |
+
|
| 136 |
+
full = ""
|
| 137 |
+
for token in stream:
|
| 138 |
+
full += token
|
| 139 |
+
placeholder.markdown(full + "▌")
|
| 140 |
+
|
| 141 |
+
# RAG Context section
|
| 142 |
+
with st.expander("🔍 Retrieved Context (RAG)"):
|
| 143 |
+
if domain == "code":
|
| 144 |
+
st.code(context)
|
| 145 |
+
else:
|
| 146 |
+
st.text(context)
|
| 147 |
+
|
| 148 |
+
else:
|
| 149 |
+
st.error("Query Failed! Check backend or logs.")
|