-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatbot.py
More file actions
705 lines (600 loc) · 20 KB
/
Copy pathChatbot.py
File metadata and controls
705 lines (600 loc) · 20 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
import os
import streamlit as st
from dotenv import load_dotenv
from openai import OpenAI
APP_TITLE = "IntelliChat"
DEFAULT_MODEL = "openrouter/free"
OPENROUTER_BASE_URL = "https://openrouter.ai/api/v1"
FREE_MODELS = [
"openrouter/free",
"baidu/cobuddy:free",
"openrouter/owl-alpha",
"nvidia/nemotron-3-nano-omni-30b-a3b-reasoning:free",
"poolside/laguna-xs.2:free",
"poolside/laguna-m.1:free",
"deepseek/deepseek-v4-flash:free",
"google/gemma-4-26b-a4b-it:free",
"google/gemma-4-31b-it:free",
"arcee-ai/trinity-large-thinking:free",
"nvidia/nemotron-3-super-120b-a12b:free",
"minimax/minimax-m2.5:free",
"liquid/lfm-2.5-1.2b-thinking:free",
"liquid/lfm-2.5-1.2b-instruct:free",
"nvidia/nemotron-3-nano-30b-a3b:free",
"nvidia/nemotron-nano-12b-v2-vl:free",
"qwen/qwen3-next-80b-a3b-instruct:free",
"nvidia/nemotron-nano-9b-v2:free",
]
def read_setting(name: str, default: str | None = None) -> str | None:
"""Read a value from Streamlit secrets first, then from environment variables."""
try:
value = st.secrets.get(name)
except Exception:
value = None
if value:
return str(value)
return os.getenv(name, default)
def read_api_key() -> str | None:
api_key = read_setting("OPENROUTER_API_KEY") or read_setting("OPENAI_API_KEY")
if not api_key:
return None
api_key = api_key.strip()
lower_key = api_key.lower()
placeholders = ("your-key", "your-actual-key", "your api key", "paste")
if any(placeholder in lower_key for placeholder in placeholders):
return None
return api_key
def create_client() -> OpenAI | None:
api_key = read_api_key()
if not api_key:
return None
headers = {}
site_url = read_setting("OPENROUTER_SITE_URL")
site_name = read_setting("OPENROUTER_SITE_NAME", APP_TITLE)
if site_url:
headers["HTTP-Referer"] = site_url
if site_name:
headers["X-Title"] = site_name
return OpenAI(
api_key=api_key,
base_url=read_setting("OPENAI_BASE_URL", OPENROUTER_BASE_URL),
default_headers=headers or None,
)
def is_free_model(model: str) -> bool:
return model in FREE_MODELS or model.endswith(":free")
def get_free_model_options() -> list[str]:
configured_model = read_setting("OPENROUTER_MODEL", DEFAULT_MODEL)
models = FREE_MODELS.copy()
if configured_model and is_free_model(configured_model) and configured_model not in models:
models.insert(1, configured_model)
return models
load_dotenv()
# Initialize theme state
if "theme" not in st.session_state:
st.session_state.theme = "dark"
st.set_page_config(
page_title=APP_TITLE,
page_icon="🤖",
layout="centered",
initial_sidebar_state="expanded",
)
# Premium theme definitions
THEMES = {
"dark": {
"bg": "#0a0908",
"bg_secondary": "#13111f",
"panel": "#1a1927",
"panel_soft": "#23212e",
"text": "#f8fafc",
"text_secondary": "#cbd5e1",
"muted": "#94a3b8",
"border": "rgba(248, 250, 252, 0.08)",
"accent_primary": "#06b6d4",
"accent_secondary": "#8b5cf6",
"accent_tertiary": "#ec4899",
"success": "#10b981",
"warning": "#f59e0b",
"error": "#ef4444",
"shadow_sm": "0 2px 8px rgba(0, 0, 0, 0.16)",
"shadow_md": "0 8px 24px rgba(0, 0, 0, 0.24)",
"shadow_lg": "0 16px 48px rgba(0, 0, 0, 0.32)",
},
"light": {
"bg": "#fafbfc",
"bg_secondary": "#f1f5f9",
"panel": "#ffffff",
"panel_soft": "#f8fafc",
"text": "#0f172a",
"text_secondary": "#334155",
"muted": "#94a3b8",
"border": "rgba(15, 23, 42, 0.08)",
"accent_primary": "#0891b2",
"accent_secondary": "#7c3aed",
"accent_tertiary": "#db2777",
"success": "#059669",
"warning": "#d97706",
"error": "#dc2626",
"shadow_sm": "0 2px 8px rgba(0, 0, 0, 0.06)",
"shadow_md": "0 8px 24px rgba(0, 0, 0, 0.08)",
"shadow_lg": "0 16px 48px rgba(0, 0, 0, 0.10)",
}
}
current_theme = THEMES[st.session_state.theme]
st.markdown(
f"""
<style>
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&family=Space+Mono:wght@400;700&display=swap');
:root {{
--bg: {current_theme['bg']};
--bg-secondary: {current_theme['bg_secondary']};
--panel: {current_theme['panel']};
--panel-soft: {current_theme['panel_soft']};
--text: {current_theme['text']};
--text-secondary: {current_theme['text_secondary']};
--muted: {current_theme['muted']};
--border: {current_theme['border']};
--accent-primary: {current_theme['accent_primary']};
--accent-secondary: {current_theme['accent_secondary']};
--accent-tertiary: {current_theme['accent_tertiary']};
--success: {current_theme['success']};
--warning: {current_theme['warning']};
--error: {current_theme['error']};
--shadow-sm: {current_theme['shadow_sm']};
--shadow-md: {current_theme['shadow_md']};
--shadow-lg: {current_theme['shadow_lg']};
}}
* {{
transition: background-color 280ms cubic-bezier(0.4, 0, 0.2, 1),
color 280ms cubic-bezier(0.4, 0, 0.2, 1),
border-color 280ms cubic-bezier(0.4, 0, 0.2, 1);
}}
html, body, [data-testid="stAppViewContainer"] {{
font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}}
.stApp {{
background: linear-gradient(135deg, var(--bg) 0%, var(--bg-secondary) 100%);
color: var(--text);
overflow-x: hidden;
}}
[data-testid="stAppViewContainer"]::before {{
content: '';
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background:
radial-gradient(circle at 20% 50%, rgba(6, 182, 212, 0.08) 0%, transparent 50%),
radial-gradient(circle at 80% 80%, rgba(139, 92, 246, 0.06) 0%, transparent 50%);
pointer-events: none;
z-index: 0;
}}
[data-testid="stHeader"] {{
background: transparent;
border-bottom: 1px solid var(--border);
backdrop-filter: blur(12px);
}}
.block-container {{
max-width: 900px;
padding: 2rem 1.2rem 8rem !important;
position: relative;
z-index: 1;
}}
section[data-testid="stSidebar"] {{
background: var(--panel);
border-right: 1px solid var(--border);
backdrop-filter: blur(8px);
animation: slideInLeft 480ms cubic-bezier(0.34, 1.56, 0.64, 1);
}}
@keyframes slideInLeft {{
from {{
opacity: 0;
transform: translateX(-20px);
}}
to {{
opacity: 1;
transform: translateX(0);
}}
}}
section[data-testid="stSidebar"] h1 {{
color: var(--text);
font-size: 1.35rem;
font-weight: 700;
margin-bottom: 1.5rem;
letter-spacing: -0.02em;
}}
section[data-testid="stSidebar"] .stSelectbox label {{
color: var(--text-secondary);
font-size: 0.875rem;
font-weight: 600;
text-transform: uppercase;
letter-spacing: 0.05em;
margin-bottom: 0.5rem;
}}
.stSelectbox [data-baseweb="select"] > div {{
background: var(--panel-soft);
border: 1.5px solid var(--border);
border-radius: 10px;
color: var(--text);
min-height: 44px;
transition: all 240ms cubic-bezier(0.4, 0, 0.2, 1);
font-weight: 500;
}}
.stSelectbox [data-baseweb="select"] > div:hover {{
border-color: var(--accent-primary);
background: var(--panel);
box-shadow: 0 0 0 3px rgba(6, 182, 212, 0.1);
}}
.stSelectbox [data-baseweb="select"] > div:focus-within {{
border-color: var(--accent-primary);
box-shadow: 0 0 0 3px rgba(6, 182, 212, 0.15);
}}
.stButton button {{
background: linear-gradient(135deg, var(--accent-primary) 0%, var(--accent-secondary) 100%);
border: none;
border-radius: 10px;
color: white;
min-height: 44px;
font-weight: 600;
letter-spacing: 0.02em;
transition: all 240ms cubic-bezier(0.34, 1.56, 0.64, 1);
box-shadow: var(--shadow-md);
position: relative;
overflow: hidden;
}}
.stButton button::before {{
content: '';
position: absolute;
top: 0;
left: -100%;
width: 100%;
height: 100%;
background: rgba(255, 255, 255, 0.2);
transition: left 300ms ease;
}}
.stButton button:hover {{
transform: translateY(-2px);
box-shadow: 0 12px 32px rgba(6, 182, 212, 0.4);
background: linear-gradient(135deg, var(--accent-secondary) 0%, var(--accent-primary) 100%);
}}
.stButton button:hover::before {{
left: 100%;
}}
.stAlert {{
border-radius: 10px;
border: 1.5px solid var(--border);
background: var(--panel);
backdrop-filter: blur(8px);
animation: slideUp 360ms cubic-bezier(0.34, 1.56, 0.64, 1);
}}
@keyframes slideUp {{
from {{
opacity: 0;
transform: translateY(12px);
}}
to {{
opacity: 1;
transform: translateY(0);
}}
}}
.hero-wrap {{
border: 1.5px solid var(--border);
border-radius: 16px;
background: linear-gradient(135deg, var(--panel) 0%, var(--panel-soft) 100%);
backdrop-filter: blur(10px);
box-shadow: var(--shadow-lg);
margin-bottom: 2rem;
padding: 3rem 2rem 2.5rem;
animation: fadeInScale 600ms cubic-bezier(0.34, 1.56, 0.64, 1);
position: relative;
overflow: hidden;
}}
.hero-wrap::before {{
content: '';
position: absolute;
top: -50%;
right: -50%;
width: 200%;
height: 200%;
background: radial-gradient(circle, rgba(6, 182, 212, 0.1) 0%, transparent 70%);
animation: rotate 20s linear infinite;
pointer-events: none;
}}
@keyframes rotate {{
from {{ transform: rotate(0deg); }}
to {{ transform: rotate(360deg); }}
}}
@keyframes fadeInScale {{
from {{
opacity: 0;
transform: scale(0.95);
}}
to {{
opacity: 1;
transform: scale(1);
}}
}}
.status-pill {{
width: fit-content;
margin: 0 auto 1.2rem;
padding: 0.5rem 1.2rem;
border: 1.5px solid var(--accent-primary);
border-radius: 999px;
background: linear-gradient(135deg, rgba(6, 182, 212, 0.12) 0%, rgba(139, 92, 246, 0.08) 100%);
color: var(--accent-primary);
font-size: 0.8rem;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
box-shadow: 0 4px 12px rgba(6, 182, 212, 0.12);
animation: pulse 2s cubic-bezier(0.4, 0, 0.6, 1) infinite;
}}
@keyframes pulse {{
0%, 100% {{ opacity: 1; }}
50% {{ opacity: 0.7; }}
}}
.app-title {{
text-align: center;
color: var(--text);
font-size: clamp(2.5rem, 8vw, 4.5rem);
font-weight: 800;
line-height: 1.1;
letter-spacing: -0.03em;
margin: 0 0 0.6rem;
background: linear-gradient(135deg, var(--text) 0%, var(--accent-primary) 100%);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-clip: text;
}}
.app-subtitle {{
color: var(--text-secondary);
text-align: center;
font-size: 1.1rem;
font-weight: 400;
line-height: 1.6;
margin: 0 auto;
max-width: 600px;
letter-spacing: -0.01em;
}}
[data-testid="stChatMessage"] {{
background: var(--panel-soft);
border: 1.5px solid var(--border);
border-radius: 14px;
box-shadow: var(--shadow-sm);
padding: 1.2rem 1.4rem;
margin: 0.8rem 0;
animation: fadeInUp 400ms cubic-bezier(0.34, 1.56, 0.64, 1);
backdrop-filter: blur(8px);
}}
@keyframes fadeInUp {{
from {{
opacity: 0;
transform: translateY(12px);
}}
to {{
opacity: 1;
transform: translateY(0);
}}
}}
[data-testid="stChatMessage"]:hover {{
border-color: var(--accent-primary);
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
}}
[data-testid="stChatMessage"]:has([data-testid="chatAvatarIcon-user"]) {{
background: linear-gradient(135deg, rgba(6, 182, 212, 0.1) 0%, rgba(139, 92, 246, 0.08) 100%);
border-color: var(--accent-primary);
}}
[data-testid="stChatMessage"]:has([data-testid="chatAvatarIcon-assistant"]) {{
background: linear-gradient(135deg, var(--panel-soft) 0%, rgba(139, 92, 246, 0.05) 100%);
}}
[data-testid="stChatMessage"] p {{
color: var(--text);
line-height: 1.7;
letter-spacing: -0.005em;
}}
[data-testid="chatAvatarIcon-user"] {{
color: var(--accent-primary);
font-size: 1.35rem;
}}
[data-testid="chatAvatarIcon-assistant"] {{
color: var(--accent-secondary);
font-size: 1.35rem;
}}
[data-testid="stChatInput"] {{
gap: 0.75rem;
}}
[data-testid="stChatInput"] textarea {{
background: var(--panel-soft);
border: 1.5px solid var(--border);
border-radius: 12px;
color: var(--text);
min-height: 52px;
font-size: 0.95rem;
font-weight: 400;
padding: 0.75rem 1rem;
resize: vertical;
transition: all 240ms cubic-bezier(0.4, 0, 0.2, 1);
}}
[data-testid="stChatInput"] textarea::placeholder {{
color: var(--muted);
}}
[data-testid="stChatInput"] textarea:focus {{
border-color: var(--accent-primary);
background: var(--panel);
box-shadow: 0 0 0 4px rgba(6, 182, 212, 0.1);
outline: none;
}}
[data-testid="stChatInput"] button {{
border-radius: 10px;
padding: 0.75rem 1.5rem;
font-weight: 600;
letter-spacing: 0.02em;
min-height: 52px;
}}
hr {{
border: none;
height: 1px;
background: var(--border);
margin: 1.5rem 0;
}}
.stCaption {{
color: var(--muted);
font-size: 0.8rem !important;
line-height: 1.5;
margin-top: 1rem;
}}
::-webkit-scrollbar {{
width: 8px;
height: 8px;
}}
::-webkit-scrollbar-track {{
background: var(--panel);
}}
::-webkit-scrollbar-thumb {{
background: var(--accent-primary);
border-radius: 4px;
transition: background 240ms ease;
}}
::-webkit-scrollbar-thumb:hover {{
background: var(--accent-secondary);
}}
@media (max-width: 640px) {{
.block-container {{
padding-left: 0.75rem;
padding-right: 0.75rem;
padding-top: 1.5rem;
}}
.hero-wrap {{
padding: 2rem 1.2rem;
margin-bottom: 1.5rem;
}}
.app-title {{
font-size: 2.25rem;
margin-bottom: 0.5rem;
}}
.app-subtitle {{
font-size: 0.95rem;
}}
[data-testid="stChatMessage"] {{
padding: 1rem 1.1rem;
margin: 0.6rem 0;
}}
[data-testid="stChatInput"] textarea {{
min-height: 48px;
font-size: 0.9rem;
}}
}}
</style>
""",
unsafe_allow_html=True,
)
client = create_client()
# Sidebar with premium styling
with st.sidebar:
st.title("⚙️ Settings")
# Theme toggle
col1, col2 = st.columns(2)
with col1:
if st.button("🌙 Dark", use_container_width=True, key="dark_theme"):
st.session_state.theme = "dark"
st.rerun()
with col2:
if st.button("☀️ Light", use_container_width=True, key="light_theme"):
st.session_state.theme = "light"
st.rerun()
st.divider()
# API Status
if client:
st.success("✓ OpenRouter Connected", icon="🟢")
else:
st.warning("✗ No API Key Configured", icon="🔴")
st.divider()
# Model selection
st.markdown("**AI Model**")
free_models = get_free_model_options()
configured_model = read_setting("OPENROUTER_MODEL", DEFAULT_MODEL)
default_index = (
free_models.index(configured_model)
if configured_model in free_models
else free_models.index(DEFAULT_MODEL)
)
model = st.selectbox("Select model", options=free_models, index=default_index, label_visibility="collapsed")
st.divider()
# Clear chat button
if st.button("🗑️ Clear Chat History", use_container_width=True):
st.session_state.messages = []
st.rerun()
st.divider()
st.caption(
"📝 **Setup Guide**\n\n"
"Set `OPENROUTER_API_KEY` in Streamlit secrets or `.env` file. "
"This app features free AI models only."
)
# Main content with premium hero section
st.markdown(
f"""
<div class="hero-wrap">
<div class="status-pill">✨ Powered by OpenRouter Free Models</div>
<h1 class="app-title">{APP_TITLE}</h1>
<p class="app-subtitle">Experience premium AI conversations with instant, intelligent responses. No signup required.</p>
</div>
""",
unsafe_allow_html=True,
)
# Initialize chat messages
if "messages" not in st.session_state:
st.session_state.messages = [
{
"role": "assistant",
"content": "👋 Welcome to IntelliChat! I'm ready to assist you. What would you like to explore today?",
}
]
# Display chat history
for message in st.session_state.messages:
with st.chat_message(message["role"], avatar="🧠" if message["role"] == "assistant" else "👤"):
st.markdown(message["content"])
# Chat input - FIXED: Removed placeholder parameter for compatibility
prompt = st.chat_input("Type your message here...")
if prompt:
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user", avatar="👤"):
st.markdown(prompt)
with st.chat_message("assistant", avatar="🧠"):
placeholder = st.empty()
if client is None:
response = (
"⚠️ **Configuration Required**\n\n"
"The chatbot is running, but no OpenRouter API key is configured yet. "
"Please add `OPENROUTER_API_KEY` to:\n"
"- Streamlit secrets (in production)\n"
"- A local `.env` file (in development)\n\n"
"Visit [OpenRouter](https://openrouter.ai) to get a free API key."
)
placeholder.markdown(response)
else:
response = ""
try:
stream = client.chat.completions.create(
model=model.strip() or DEFAULT_MODEL,
messages=[
{"role": item["role"], "content": item["content"]}
for item in st.session_state.messages
],
stream=True,
)
for chunk in stream:
token = chunk.choices[0].delta.content or ""
response += token
placeholder.markdown(response + " ▌")
placeholder.markdown(response)
except Exception as exc:
response = (
"❌ **Connection Error**\n\n"
"I couldn't reach the AI service. Please verify:\n"
"- Your OpenRouter API key is valid\n"
"- The selected model is available\n"
"- Your Streamlit secrets are configured\n\n"
f"**Error Details:** `{str(exc)[:100]}...`"
)
placeholder.error(response)
st.session_state.messages.append({"role": "assistant", "content": response})