-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
321 lines (273 loc) · 9.26 KB
/
Copy pathapp.py
File metadata and controls
321 lines (273 loc) · 9.26 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
import streamlit as st
import ollama
import time
# Configure page
st.set_page_config(
page_title="CommonChat AI",
page_icon="🚀",
layout="wide",
initial_sidebar_state="expanded"
)
# Custom CSS for beautiful dark blue theme
st.markdown("""
<style>
/* Import Google Fonts */
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap');
/* Global Styles */
.stApp {
background: linear-gradient(135deg, #0f1419 0%, #1a202c 50%, #2d3748 100%);
font-family: 'Inter', sans-serif;
}
/* Main container */
.main .block-container {
padding-top: 2rem;
padding-bottom: 2rem;
max-width: 800px;
margin: 0 auto;
}
/* Title styling */
.main-title {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
font-size: 3rem;
font-weight: 700;
text-align: center;
margin-bottom: 0.5rem;
text-shadow: 0 4px 8px rgba(102, 126, 234, 0.3);
}
.subtitle {
text-align: center;
color: #a0aec0;
font-size: 1.1rem;
margin-bottom: 2rem;
font-weight: 300;
}
/* Sidebar styling */
.css-1d391kg {
background: linear-gradient(180deg, #1a202c 0%, #2d3748 100%);
border-right: 2px solid #4a5568;
}
.sidebar-title {
color: #667eea;
font-size: 1.5rem;
font-weight: 600;
margin-bottom: 1rem;
text-align: center;
}
/* Custom slider styling */
.stSlider > div > div > div > div {
background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
}
.stSlider > div > div > div > div > div {
background: #667eea;
border: 2px solid #4a5568;
}
/* Selectbox styling */
.stSelectbox > div > div {
background: #2d3748;
border: 1px solid #4a5568;
border-radius: 8px;
}
/* Chat message styling */
.stChatMessage {
background: rgba(45, 55, 72, 0.6);
border: 1px solid #4a5568;
border-radius: 12px;
margin: 0.5rem 0;
backdrop-filter: blur(10px);
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
max-width: 85%;
}
/* User message */
.stChatMessage[data-testid="user-message"] {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
margin-left: auto;
}
/* Assistant message */
.stChatMessage[data-testid="assistant-message"] {
background: rgba(26, 32, 44, 0.8);
border-left: 3px solid #667eea;
margin-right: auto;
}
/* Chat input styling */
.stChatInput > div {
background: #2d3748;
border: 2px solid #4a5568;
border-radius: 25px;
backdrop-filter: blur(10px);
}
.stChatInput input {
background: transparent;
color: #e2e8f0;
font-size: 1rem;
}
/* Button styling */
.stButton > button {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border: none;
border-radius: 25px;
font-weight: 600;
transition: all 0.3s ease;
box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4);
}
.stButton > button:hover {
transform: translateY(-2px);
box-shadow: 0 6px 20px rgba(102, 126, 234, 0.6);
}
/* Info box styling */
.info-box {
background: rgba(26, 32, 44, 0.8);
border: 1px solid #4a5568;
border-radius: 10px;
padding: 1rem;
margin: 1rem 0;
text-align: center;
backdrop-filter: blur(10px);
}
/* Custom metrics */
.metric-card {
background: rgba(45, 55, 72, 0.6);
border: 1px solid #4a5568;
border-radius: 10px;
padding: 1rem;
text-align: center;
backdrop-filter: blur(10px);
}
/* Animated gradient background for chat area */
.chat-container {
background: rgba(26, 32, 44, 0.3);
border-radius: 15px;
padding: 1.5rem;
backdrop-filter: blur(10px);
border: 1px solid rgba(102, 126, 234, 0.2);
max-width: 700px;
margin: 0 auto;
}
/* Hide Streamlit branding */
#MainMenu {visibility: hidden;}
footer {visibility: hidden;}
header {visibility: hidden;}
/* Scrollbar styling */
::-webkit-scrollbar {
width: 8px;
}
::-webkit-scrollbar-track {
background: #1a202c;
}
::-webkit-scrollbar-thumb {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
border-radius: 4px;
}
::-webkit-scrollbar-thumb:hover {
background: linear-gradient(135deg, #764ba2 0%, #667eea 100%);
}
</style>
""", unsafe_allow_html=True)
# Initialize session state for chat history
if "messages" not in st.session_state:
st.session_state.messages = []
# App title with custom styling
st.markdown('<h1 class="main-title">🚀 CommonChat AI</h1>', unsafe_allow_html=True)
st.markdown('<p class="subtitle">Your Private, Intelligent Conversation Partner</p>', unsafe_allow_html=True)
# Enhanced sidebar with clean design
with st.sidebar:
st.markdown('<h2 class="sidebar-title">⚙️ Settings</h2>', unsafe_allow_html=True)
# Temperature control
st.markdown("**Temperature**")
temperature = st.slider(
"",
min_value=0.0,
max_value=1.0,
value=0.7,
step=0.1,
help="Controls creativity"
)
# Model selection
st.markdown("**Model**")
model_name = st.selectbox(
"",
["gemma3", "llama3", "mistral", "codellama"],
index=0,
help="Choose AI model"
)
st.markdown("---")
# Clear chat button
if st.button("🗑️ Clear Chat", use_container_width=True):
st.session_state.messages = []
st.rerun()
st.markdown("---")
# Simple info
st.markdown("""
<div class='info-box'>
<p style='margin: 0; color: #a0aec0; font-size: 0.9rem; text-align: center;'>
🔐 Private & Local<br>
⚡ Fast & Responsive
</p>
</div>
""", unsafe_allow_html=True)
# Main chat area with clean design
st.markdown('<div class="chat-container">', unsafe_allow_html=True)
# Display welcome message if no chat history
if not st.session_state.messages:
st.markdown("""
<div style='text-align: center; padding: 2rem; color: #a0aec0;'>
<h3 style='color: #667eea;'>👋 Welcome to CommonChat AI</h3>
<p>Start a conversation by typing your message below.</p>
</div>
""", unsafe_allow_html=True)
# Display chat messages with clean styling
for i, message in enumerate(st.session_state.messages):
with st.chat_message(message["role"]):
if message["role"] == "user":
st.markdown(message['content'])
else:
st.markdown(message['content'])
st.markdown('</div>', unsafe_allow_html=True)
# Clean chat input
if prompt := st.chat_input("💭 What's on your mind?", key="chat_input"):
# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": prompt})
# Display user message
with st.chat_message("user"):
st.markdown(prompt)
# Display assistant response
with st.chat_message("assistant"):
message_placeholder = st.empty()
full_response = ""
# Add thinking indicator
with st.spinner("Thinking..."):
try:
# Stream the response from Ollama
response = ollama.chat(
model=model_name,
messages=st.session_state.messages,
stream=True,
options={
'temperature': temperature,
'top_p': 0.9,
'top_k': 40
}
)
# Clean streaming
for chunk in response:
if 'message' in chunk and 'content' in chunk['message']:
full_response += chunk['message']['content']
message_placeholder.markdown(full_response + "▊")
# Final message without cursor
message_placeholder.markdown(full_response)
except Exception as e:
error_message = f"Error: {str(e)}"
message_placeholder.markdown(error_message)
full_response = error_message
# Add assistant response to chat history
st.session_state.messages.append({"role": "assistant", "content": full_response})
# Simple footer
st.markdown("---")
st.markdown("""
<div style='text-align: center; color: #718096; font-size: 0.9rem; padding: 1rem;'>
<p><strong>CommonChat AI</strong> - Powered by Ollama</p>
</div>
""", unsafe_allow_html=True)