Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +214 -174
src/streamlit_app.py
CHANGED
|
@@ -1,193 +1,233 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
-
|
| 4 |
-
from PIL import Image
|
| 5 |
-
import datetime
|
| 6 |
-
import io
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
# 1. COPILOT LAYOUT CONFIGURATION & INTERFACE
|
| 10 |
-
# =====================================================================
|
| 11 |
st.set_page_config(page_title="SparkHelix AI", page_icon="🧬", layout="wide")
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
st.markdown(""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
<style>
|
| 16 |
-
|
| 17 |
-
.stApp {
|
| 18 |
-
background-color: #0b0c10 !important;
|
| 19 |
-
color: #e2e8f0 !important;
|
| 20 |
-
font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
|
| 21 |
-
}
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
/*
|
| 30 |
-
.
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
| 35 |
|
| 36 |
-
/*
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
</style>
|
| 68 |
-
""", unsafe_allow_html=True)
|
| 69 |
-
|
| 70 |
-
# =====================================================================
|
| 71 |
-
# 2. SESSION CONTEXT MANAGER
|
| 72 |
-
# =====================================================================
|
| 73 |
-
if "all_chats" not in st.session_state:
|
| 74 |
-
st.session_state.all_chats = {"Default Conversation": []}
|
| 75 |
-
if "active_chat" not in st.session_state:
|
| 76 |
-
st.session_state.active_chat = "Default Conversation"
|
| 77 |
-
|
| 78 |
-
st.sidebar.title("🧬 SparkHelix Pro")
|
| 79 |
-
st.sidebar.markdown("---")
|
| 80 |
-
|
| 81 |
-
st.sidebar.subheader("💬 Chat Sessions")
|
| 82 |
-
if st.sidebar.button("➕ New Chat"):
|
| 83 |
-
timestamp = datetime.datetime.now().strftime("%I:%M %p")
|
| 84 |
-
new_id = f"Chat ({timestamp})"
|
| 85 |
-
st.session_state.all_chats[new_id] = []
|
| 86 |
-
st.session_state.active_chat = new_id
|
| 87 |
-
st.rerun()
|
| 88 |
-
|
| 89 |
-
chosen_chat = st.sidebar.radio(
|
| 90 |
-
"Active Channels:",
|
| 91 |
-
options=list(st.session_state.all_chats.keys()),
|
| 92 |
-
index=list(st.session_state.all_chats.keys()).index(st.session_state.active_chat)
|
| 93 |
-
)
|
| 94 |
-
st.session_state.active_chat = chosen_chat
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
# =====================================================================
|
| 120 |
-
#
|
| 121 |
# =====================================================================
|
| 122 |
-
st.markdown("
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
#
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
st.success("✔ Voice segment recorded.")
|
| 144 |
|
| 145 |
-
|
| 146 |
|
| 147 |
-
if
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
st.image(img_bytes, caption="SparkHelix Artwork Engine")
|
| 167 |
-
active_log.append({
|
| 168 |
-
"role": "assistant",
|
| 169 |
-
"content": f"Image successfully rendered for prompt: '{generation_prompt}'",
|
| 170 |
-
"img_data": img_bytes
|
| 171 |
-
})
|
| 172 |
-
else:
|
| 173 |
-
with st.spinner("Analyzing data streams..."):
|
| 174 |
-
search_context = ""
|
| 175 |
-
if len(user_string) > 10:
|
| 176 |
-
search_context = query_web_search(user_string)
|
| 177 |
-
|
| 178 |
-
engineered_prompt = f"System Instruction: You are SparkHelix AI. Contextual Web Data: {search_context}\nUser Input Parameters: {user_string}"
|
| 179 |
-
|
| 180 |
-
response = client.chat.completions.create(
|
| 181 |
-
model=TEXT_MODEL,
|
| 182 |
-
messages=[{"role": "user", "content": engineered_prompt}],
|
| 183 |
-
max_tokens=max_tokens,
|
| 184 |
-
temperature=creativity
|
| 185 |
-
)
|
| 186 |
-
ai_reply = response.choices[0].message.content
|
| 187 |
-
st.markdown(ai_reply)
|
| 188 |
-
active_log.append({"role": "assistant", "content": ai_reply})
|
| 189 |
-
|
| 190 |
-
st.session_state.all_chats[st.session_state.active_chat] = active_log
|
| 191 |
-
|
| 192 |
-
except Exception as e:
|
| 193 |
-
st.error(f"Error executing command pipeline: {e}")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
+
import json
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# Force wide mode and premium dark base configuration
|
|
|
|
|
|
|
| 6 |
st.set_page_config(page_title="SparkHelix AI", page_icon="🧬", layout="wide")
|
| 7 |
|
| 8 |
+
# Hide standard Streamlit header and footer wrappers
|
| 9 |
+
st.markdown("<style>#MainMenu, footer, header {visibility: hidden;} div.block-container{padding:0;}</style>", unsafe_allow_html=True)
|
| 10 |
+
|
| 11 |
+
# Initialize Session Logs
|
| 12 |
+
if "messages" not in st.session_state:
|
| 13 |
+
st.session_state.messages = [{"role": "assistant", "content": "Hello! I am SparkHelix. How can I help you discover or build today?"}]
|
| 14 |
+
|
| 15 |
+
# =====================================================================
|
| 16 |
+
# 🎨 HIGH-FIDELITY COPILOT CORE INTERFACE (CSS/HTML INJECTION)
|
| 17 |
+
# =====================================================================
|
| 18 |
+
copilot_html = f"""
|
| 19 |
<style>
|
| 20 |
+
@import url('https://fonts.googleapis.com/css2?family=Segoe+UI:wght@400;500;600&display=swap');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
body {{
|
| 23 |
+
background-color: #030508 !important;
|
| 24 |
+
font-family: 'Segoe UI', system-ui, sans-serif;
|
| 25 |
+
color: #f3f4f6;
|
| 26 |
+
margin: 0;
|
| 27 |
+
padding: 0;
|
| 28 |
+
}}
|
| 29 |
|
| 30 |
+
/* Layout Framework */
|
| 31 |
+
.copilot-container {{
|
| 32 |
+
display: flex;
|
| 33 |
+
height: 100vh;
|
| 34 |
+
width: 100vw;
|
| 35 |
+
background-color: #0b0d12;
|
| 36 |
+
}}
|
| 37 |
|
| 38 |
+
/* Sleek Left Sidebar */
|
| 39 |
+
.copilot-sidebar {{
|
| 40 |
+
width: 260px;
|
| 41 |
+
background-color: #0d1017;
|
| 42 |
+
border-right: 1px solid #1e2530;
|
| 43 |
+
display: flex;
|
| 44 |
+
flex-direction: column;
|
| 45 |
+
padding: 20px;
|
| 46 |
+
}}
|
| 47 |
|
| 48 |
+
.brand-title {{
|
| 49 |
+
font-size: 18px;
|
| 50 |
+
font-weight: 600;
|
| 51 |
+
display: flex;
|
| 52 |
+
align-items: center;
|
| 53 |
+
gap: 10px;
|
| 54 |
+
margin-bottom: 30px;
|
| 55 |
+
color: #ffffff;
|
| 56 |
+
}}
|
| 57 |
|
| 58 |
+
.new-chat-btn {{
|
| 59 |
+
background: linear-gradient(90deg, #4f46e5 0%, #7c3aed 100%);
|
| 60 |
+
border: none;
|
| 61 |
+
color: white;
|
| 62 |
+
padding: 12px;
|
| 63 |
+
border-radius: 10px;
|
| 64 |
+
font-weight: 500;
|
| 65 |
+
cursor: pointer;
|
| 66 |
+
text-align: center;
|
| 67 |
+
margin-bottom: 20px;
|
| 68 |
+
box-shadow: 0 4px 12px rgba(79, 70, 229, 0.2);
|
| 69 |
+
}}
|
| 70 |
+
|
| 71 |
+
/* Center Chat Stream */
|
| 72 |
+
.chat-workspace {{
|
| 73 |
+
flex: 1;
|
| 74 |
+
display: flex;
|
| 75 |
+
flex-direction: column;
|
| 76 |
+
height: 100%;
|
| 77 |
+
background-color: #0b0d12;
|
| 78 |
+
position: relative;
|
| 79 |
+
}}
|
| 80 |
+
|
| 81 |
+
.chat-header {{
|
| 82 |
+
padding: 20px 40px;
|
| 83 |
+
border-bottom: 1px solid #1e2530;
|
| 84 |
+
font-size: 16px;
|
| 85 |
+
font-weight: 500;
|
| 86 |
+
color: #9ca3af;
|
| 87 |
+
}}
|
| 88 |
+
|
| 89 |
+
.message-stream {{
|
| 90 |
+
flex: 1;
|
| 91 |
+
overflow-y: auto;
|
| 92 |
+
padding: 40px 10% 120px 10%;
|
| 93 |
+
display: flex;
|
| 94 |
+
flex-direction: column;
|
| 95 |
+
gap: 24px;
|
| 96 |
+
}}
|
| 97 |
+
|
| 98 |
+
/* Chat Bubble Speccing */
|
| 99 |
+
.chat-row {{
|
| 100 |
+
display: flex;
|
| 101 |
+
gap: 16px;
|
| 102 |
+
max-width: 800px;
|
| 103 |
+
width: 100%;
|
| 104 |
+
align-self: center;
|
| 105 |
+
}}
|
| 106 |
+
|
| 107 |
+
.chat-row.user-row {{
|
| 108 |
+
justify-content: flex-end;
|
| 109 |
+
}}
|
| 110 |
+
|
| 111 |
+
.avatar {{
|
| 112 |
+
width: 32px;
|
| 113 |
+
height: 32px;
|
| 114 |
+
border-radius: 50%;
|
| 115 |
+
display: flex;
|
| 116 |
+
align-items: center;
|
| 117 |
+
justify-content: center;
|
| 118 |
+
font-weight: 600;
|
| 119 |
+
font-size: 14px;
|
| 120 |
+
}}
|
| 121 |
+
|
| 122 |
+
.avatar.ai-avatar {{
|
| 123 |
+
background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
|
| 124 |
+
color: white;
|
| 125 |
+
}}
|
| 126 |
+
|
| 127 |
+
.avatar.user-avatar {{
|
| 128 |
+
background-color: #374151;
|
| 129 |
+
color: #f3f4f6;
|
| 130 |
+
}}
|
| 131 |
+
|
| 132 |
+
.bubble {{
|
| 133 |
+
padding: 14px 18px;
|
| 134 |
+
border-radius: 16px;
|
| 135 |
+
font-size: 15px;
|
| 136 |
+
line-height: 1.5;
|
| 137 |
+
max-width: 85%;
|
| 138 |
+
}}
|
| 139 |
+
|
| 140 |
+
.ai-bubble {{
|
| 141 |
+
background-color: transparent;
|
| 142 |
+
color: #e5e7eb;
|
| 143 |
+
}}
|
| 144 |
|
| 145 |
+
.user-bubble {{
|
| 146 |
+
background-color: #1e2533;
|
| 147 |
+
color: #ffffff;
|
| 148 |
+
border: 1px solid #2d3748;
|
| 149 |
+
border-radius: 16px 16px 4px 16px;
|
| 150 |
+
}}
|
| 151 |
</style>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
|
| 153 |
+
<div class="copilot-container">
|
| 154 |
+
<div class="copilot-sidebar">
|
| 155 |
+
<div class="brand-title">🧬 SparkHelix Copilot</div>
|
| 156 |
+
<div class="new-chat-btn">➕ New Topic</div>
|
| 157 |
+
<div style="color: #4b5563; font-size: 12px; margin-top: auto;">System Status: Connected</div>
|
| 158 |
+
</div>
|
| 159 |
+
|
| 160 |
+
<div class="chat-workspace">
|
| 161 |
+
<div class="chat-header">SparkHelix Workspace // Chat Context</div>
|
| 162 |
+
<div class="message-stream">
|
| 163 |
+
"""
|
| 164 |
+
|
| 165 |
+
# Dynamically print bubbles inside our custom HTML shell
|
| 166 |
+
for msg in st.session_state.messages:
|
| 167 |
+
if msg["role"] == "assistant":
|
| 168 |
+
copilot_html += f"""
|
| 169 |
+
<div class="chat-row">
|
| 170 |
+
<div class="avatar ai-avatar">🧬</div>
|
| 171 |
+
<div class="bubble ai-bubble">{msg['content']}</div>
|
| 172 |
+
</div>
|
| 173 |
+
"""
|
| 174 |
+
else:
|
| 175 |
+
copilot_html += f"""
|
| 176 |
+
<div class="chat-row user-row">
|
| 177 |
+
<div class="bubble user-bubble">{msg['content']}</div>
|
| 178 |
+
<div class="avatar user-avatar">U</div>
|
| 179 |
+
</div>
|
| 180 |
+
"""
|
| 181 |
+
|
| 182 |
+
# Close the wrapper div
|
| 183 |
+
copilot_html += "</div></div></div>"
|
| 184 |
+
|
| 185 |
+
# Render the layout instantly
|
| 186 |
+
st.markdown(copilot_html, unsafe_allow_html=True)
|
| 187 |
|
| 188 |
# =====================================================================
|
| 189 |
+
# 📥 FLOATING BOTTOM INPUT CONTROLLER (HACKED INTO POSITION)
|
| 190 |
# =====================================================================
|
| 191 |
+
st.markdown("""
|
| 192 |
+
<style>
|
| 193 |
+
/* Pin the interactive chat input directly over the UI backdrop */
|
| 194 |
+
div[data-testid="stChatInput"] {
|
| 195 |
+
position: fixed !important;
|
| 196 |
+
bottom: 30px !important;
|
| 197 |
+
left: 53% !important;
|
| 198 |
+
transform: translateX(-50%) !important;
|
| 199 |
+
width: 55% !important;
|
| 200 |
+
max-width: 750px !important;
|
| 201 |
+
z-index: 999999 !important;
|
| 202 |
+
background-color: #151b26 !important;
|
| 203 |
+
border: 1px solid #2a3447 !important;
|
| 204 |
+
border-radius: 24px !important;
|
| 205 |
+
box-shadow: 0 10px 30px rgba(0,0,0,0.5) !important;
|
| 206 |
+
}
|
| 207 |
+
textarea {
|
| 208 |
+
color: #ffffff !important;
|
| 209 |
+
}
|
| 210 |
+
</style>
|
| 211 |
+
""", unsafe_allow_html=True)
|
|
|
|
| 212 |
|
| 213 |
+
user_input = st.chat_input("Ask SparkHelix anything...")
|
| 214 |
|
| 215 |
+
if user_input:
|
| 216 |
+
# Save input log
|
| 217 |
+
st.session_state.messages.append({"role": "user", "content": user_input})
|
| 218 |
+
|
| 219 |
+
# Process AI request using the correct conversational wrapper
|
| 220 |
+
try:
|
| 221 |
+
client = InferenceClient()
|
| 222 |
+
response = client.chat.completions.create(
|
| 223 |
+
model="Qwen/Qwen2.5-7B-Instruct",
|
| 224 |
+
messages=[{"role": "user", "content": user_input}],
|
| 225 |
+
max_tokens=512,
|
| 226 |
+
temperature=0.4
|
| 227 |
+
)
|
| 228 |
+
ai_reply = response.choices[0].message.content
|
| 229 |
+
st.session_state.messages.append({"role": "assistant", "content": ai_reply})
|
| 230 |
+
except Exception as e:
|
| 231 |
+
st.session_state.messages.append({"role": "assistant", "content": f"System Alert: Connection route congested. Details: {e}"})
|
| 232 |
+
|
| 233 |
+
st.rerun()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|