Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +45 -14
src/streamlit_app.py
CHANGED
|
@@ -4,6 +4,7 @@ import sys
|
|
| 4 |
import subprocess
|
| 5 |
from pathlib import Path
|
| 6 |
from typing import List
|
|
|
|
| 7 |
|
| 8 |
import streamlit as st
|
| 9 |
from sentence_transformers import SentenceTransformer
|
|
@@ -47,6 +48,27 @@ def get_query_embedding(query: str) -> List[float]:
|
|
| 47 |
return model.encode(query).tolist()
|
| 48 |
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
# ================= CHAT STATE =================
|
| 51 |
def init_chat():
|
| 52 |
if "messages" not in st.session_state:
|
|
@@ -68,7 +90,9 @@ def main():
|
|
| 68 |
with st.sidebar:
|
| 69 |
st.title("βοΈ Settings")
|
| 70 |
top_k = st.slider("Number of results", 1, 10, 5)
|
|
|
|
| 71 |
st.markdown("---")
|
|
|
|
| 72 |
if st.button("ποΈ Clear Chat"):
|
| 73 |
st.session_state.messages = []
|
| 74 |
st.rerun()
|
|
@@ -80,13 +104,16 @@ def main():
|
|
| 80 |
# ================= DISPLAY CHAT =================
|
| 81 |
for msg in st.session_state.messages:
|
| 82 |
with st.chat_message(msg["role"]):
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
# ================= INPUT =================
|
| 86 |
user_input = st.chat_input("Ask a programming question...")
|
| 87 |
|
| 88 |
if user_input:
|
| 89 |
-
#
|
| 90 |
st.session_state.messages.append({
|
| 91 |
"role": "user",
|
| 92 |
"content": user_input
|
|
@@ -103,26 +130,30 @@ def main():
|
|
| 103 |
query_embedding = get_query_embedding(user_input.strip())
|
| 104 |
results = engine.search(query_embedding, top_k=top_k)
|
| 105 |
|
| 106 |
-
#
|
| 107 |
-
|
|
|
|
| 108 |
for i, item in enumerate(results, start=1):
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
|
|
|
|
|
|
| 114 |
|
| 115 |
-
|
|
|
|
| 116 |
|
| 117 |
except Exception as e:
|
| 118 |
-
|
| 119 |
with st.chat_message("assistant"):
|
| 120 |
-
st.error(
|
| 121 |
|
| 122 |
-
#
|
| 123 |
st.session_state.messages.append({
|
| 124 |
"role": "assistant",
|
| 125 |
-
"content":
|
| 126 |
})
|
| 127 |
|
| 128 |
|
|
|
|
| 4 |
import subprocess
|
| 5 |
from pathlib import Path
|
| 6 |
from typing import List
|
| 7 |
+
import re
|
| 8 |
|
| 9 |
import streamlit as st
|
| 10 |
from sentence_transformers import SentenceTransformer
|
|
|
|
| 48 |
return model.encode(query).tolist()
|
| 49 |
|
| 50 |
|
| 51 |
+
# ================= HTML β MARKDOWN RENDER =================
|
| 52 |
+
def render_answer(answer: str):
|
| 53 |
+
"""
|
| 54 |
+
Converts StackOverflow-style HTML into clean Streamlit output
|
| 55 |
+
"""
|
| 56 |
+
|
| 57 |
+
# Split text and code blocks
|
| 58 |
+
parts = re.split(r"<pre><code>|</code></pre>", answer)
|
| 59 |
+
|
| 60 |
+
for i, part in enumerate(parts):
|
| 61 |
+
if i % 2 == 0:
|
| 62 |
+
# Normal text β remove simple HTML tags
|
| 63 |
+
clean_text = re.sub(r"<.*?>", "", part)
|
| 64 |
+
if clean_text.strip():
|
| 65 |
+
st.markdown(clean_text)
|
| 66 |
+
else:
|
| 67 |
+
# Code block
|
| 68 |
+
code = part.strip()
|
| 69 |
+
st.code(code, language="python") # default language
|
| 70 |
+
|
| 71 |
+
|
| 72 |
# ================= CHAT STATE =================
|
| 73 |
def init_chat():
|
| 74 |
if "messages" not in st.session_state:
|
|
|
|
| 90 |
with st.sidebar:
|
| 91 |
st.title("βοΈ Settings")
|
| 92 |
top_k = st.slider("Number of results", 1, 10, 5)
|
| 93 |
+
|
| 94 |
st.markdown("---")
|
| 95 |
+
|
| 96 |
if st.button("ποΈ Clear Chat"):
|
| 97 |
st.session_state.messages = []
|
| 98 |
st.rerun()
|
|
|
|
| 104 |
# ================= DISPLAY CHAT =================
|
| 105 |
for msg in st.session_state.messages:
|
| 106 |
with st.chat_message(msg["role"]):
|
| 107 |
+
if msg["role"] == "assistant":
|
| 108 |
+
render_answer(msg["content"])
|
| 109 |
+
else:
|
| 110 |
+
st.markdown(msg["content"])
|
| 111 |
|
| 112 |
# ================= INPUT =================
|
| 113 |
user_input = st.chat_input("Ask a programming question...")
|
| 114 |
|
| 115 |
if user_input:
|
| 116 |
+
# Store user message
|
| 117 |
st.session_state.messages.append({
|
| 118 |
"role": "user",
|
| 119 |
"content": user_input
|
|
|
|
| 130 |
query_embedding = get_query_embedding(user_input.strip())
|
| 131 |
results = engine.search(query_embedding, top_k=top_k)
|
| 132 |
|
| 133 |
+
# Save raw response for rendering
|
| 134 |
+
full_response = ""
|
| 135 |
+
|
| 136 |
for i, item in enumerate(results, start=1):
|
| 137 |
+
st.markdown(f"### πΉ Result {i}")
|
| 138 |
+
st.markdown(f"**{item['question']}**")
|
| 139 |
+
|
| 140 |
+
render_answer(item["answer"])
|
| 141 |
+
|
| 142 |
+
st.caption(f"Score: {item['score']:.4f}")
|
| 143 |
+
st.divider()
|
| 144 |
|
| 145 |
+
# Save for history (raw)
|
| 146 |
+
full_response += f"{item['question']}\n{item['answer']}\n"
|
| 147 |
|
| 148 |
except Exception as e:
|
| 149 |
+
full_response = f"Error: {e}"
|
| 150 |
with st.chat_message("assistant"):
|
| 151 |
+
st.error(full_response)
|
| 152 |
|
| 153 |
+
# Store assistant message
|
| 154 |
st.session_state.messages.append({
|
| 155 |
"role": "assistant",
|
| 156 |
+
"content": full_response
|
| 157 |
})
|
| 158 |
|
| 159 |
|