CodeFlame commited on
Commit
10e464a
·
verified ·
1 Parent(s): b7000a5

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. 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
- from duckduckgo_search import DDGS
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
- # Advanced UI injection to mimic modern AI dashboards (Copilot style)
14
- st.markdown("""
 
 
 
 
 
 
 
 
 
15
  <style>
16
- /* Dark professional canvas background */
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
- /* Elegant Sidebar with Glassmorphism */
24
- [data-testid="stSidebar"] {
25
- background-color: #111318 !important;
26
- border-right: 1px solid #1f242e !important;
27
- }
 
 
28
 
29
- /* Center the chat workspace cleanly and prevent layout shifting */
30
- .block-container {
31
- padding-top: 2rem !important;
32
- padding-bottom: 7rem !important;
33
- max-width: 850px !important;
34
- }
 
35
 
36
- /* Modern Copilot-style Chat Bubble Architecture */
37
- div[data-testid="stChatMessage"] {
38
- padding: 1.25rem !important;
39
- border-radius: 14px !important;
40
- margin-bottom: 1.25rem !important;
41
- border: 1px solid transparent !important;
42
- transition: all 0.2s ease;
43
- }
 
44
 
45
- /* Distinct variations for User vs AI bubbles */
46
- div[data-testid="stChatMessage"]:has([data-testid="stChatMessageAvatar"] img) {
47
- background-color: #1a1d26 !important;
48
- border: 1px solid #282e3d !important;
49
- }
 
 
 
 
50
 
51
- /* Copilot subtle accent highlight for AI responses */
52
- div[data-testid="stChatMessage"]:has([data-testid="stChatMessageAvatar"]):not(:has(img)) {
53
- background-color: #12141c !important;
54
- border-left: 3px solid #4f46e5 !important;
55
- }
56
-
57
- /* Fixed Bottom Chat Container Hack to mimic floating inputs */
58
- div[data-testid="stChatInput"] {
59
- border-radius: 12px !important;
60
- border: 1px solid #2d3748 !important;
61
- background-color: #161920 !important;
62
- box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4) !important;
63
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
 
65
- /* Remove default unnecessary Streamlit elements */
66
- #MainMenu, footer, header {visibility: hidden;}
 
 
 
 
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
- # Engine Parameters
97
- st.sidebar.markdown("---")
98
- st.sidebar.subheader("⚙️ Configurations")
99
- creativity = st.sidebar.slider("Creativity Mode", 0.0, 1.0, 0.4, 0.05)
100
- max_tokens = st.sidebar.slider("Response Limits", 128, 1024, 512, 64)
101
-
102
- # =====================================================================
103
- # 3. COMPUTE MATRIX CODES
104
- # =====================================================================
105
- TEXT_MODEL = "Qwen/Qwen2.5-7B-Instruct"
106
- IMAGE_MODEL = "black-forest-labs/FLUX.1-dev"
107
- client = InferenceClient()
108
-
109
- def query_web_search(text_query):
110
- try:
111
- with DDGS() as ddgs:
112
- results = [r for r in ddgs.text(text_query, max_results=3)]
113
- if results:
114
- return "\n".join([f"[{res['title']}] {res['body']}" for res in results])
115
- except:
116
- return "Search pipeline unavailable."
117
- return "No records returned."
 
 
 
 
 
 
 
 
 
 
 
 
118
 
119
  # =====================================================================
120
- # 4. VIEW RENDERING PLATFORM
121
  # =====================================================================
122
- st.markdown("<h2 style='text-align: center;'>SparkHelix Copilot Workspace</h2>", unsafe_allow_html=True)
123
- st.markdown("<p style='text-align: center; color: #718096; margin-bottom: 2rem;'>Next-gen companion agent connected to web engines and render farms.</p>", unsafe_allow_html=True)
124
-
125
- active_log = st.session_state.all_chats[st.session_state.active_chat]
126
-
127
- # Stream chat logs inside centralized layout block
128
- for message in active_log:
129
- with st.chat_message(message["role"]):
130
- if "content" in message and message["content"]:
131
- st.markdown(message["content"])
132
- if "file_meta" in message:
133
- st.info(message["file_meta"])
134
- if "img_data" in message:
135
- st.image(message["img_data"])
136
- if "vid_placeholder" in message:
137
- st.warning(message["vid_placeholder"])
138
-
139
- # Quick features below main log
140
- st.markdown("---")
141
- mic_input = st.audio_input("🎙️ Voice Dictation Node")
142
- if mic_input:
143
- st.success("✔ Voice segment recorded.")
144
 
145
- prompt_object = st.chat_input("Ask anything, use /draw to generate images...", accept_file=True)
146
 
147
- if prompt_object:
148
- user_string = prompt_object.text if prompt_object.text else ""
149
- attached_file = prompt_object.files[0] if prompt_object.files else None
150
-
151
- with st.chat_message("user"):
152
- if user_string:
153
- st.markdown(user_string)
154
-
155
- log_entry = {"role": "user", "content": user_string}
156
- if attached_file:
157
- log_entry["file_meta"] = f"📁 Attachment Matrix: {attached_file.name}"
158
- active_log.append(log_entry)
159
-
160
- with st.chat_message("assistant"):
161
- try:
162
- if user_string.lower().startswith("/draw"):
163
- generation_prompt = user_string.replace("/draw", "").strip()
164
- with st.spinner("🎨 Generating production layout..."):
165
- img_bytes = client.text_to_image(generation_prompt, model=IMAGE_MODEL)
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()