uumerrr684 commited on
Commit
a9cd437
·
verified ·
1 Parent(s): 963d0d8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +156 -52
app.py CHANGED
@@ -6,55 +6,167 @@ import time
6
 
7
  # Page configuration
8
  st.set_page_config(
9
- page_title="AI Assistant 2025",
10
- page_icon="🤖",
11
- layout="wide",
12
- initial_sidebar_state="expanded"
13
  )
14
 
15
- # Simple CSS for chat layout - User right, AI left
16
  st.markdown("""
17
  <style>
18
- /* User messages - Right side */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  .stChatMessage[data-testid*="user"] {
20
  flex-direction: row-reverse !important;
21
- margin-left: 20% !important;
22
  margin-right: 0% !important;
23
  }
24
 
25
  .stChatMessage[data-testid*="user"] .stMarkdown {
26
- background-color: #007bff !important;
27
  color: white !important;
28
  border-radius: 18px 18px 4px 18px !important;
29
  padding: 12px 16px !important;
30
- margin-left: 8px !important;
31
- margin-right: 0px !important;
32
  }
33
 
34
- /* AI messages - Left side (default) */
35
  .stChatMessage[data-testid*="assistant"] {
36
- margin-right: 20% !important;
37
  margin-left: 0% !important;
38
  }
39
 
40
  .stChatMessage[data-testid*="assistant"] .stMarkdown {
41
- background-color: #f1f3f4 !important;
42
- color: #333 !important;
43
  border-radius: 18px 18px 18px 4px !important;
44
  padding: 12px 16px !important;
45
- margin-right: 8px !important;
46
- margin-left: 0px !important;
 
47
  }
48
 
49
- /* Hide avatars for cleaner look */
50
  .stChatMessage img {
51
  display: none !important;
52
  }
53
 
54
- /* Clean chat input */
55
  .stChatInputContainer {
56
- border-top: 1px solid #e0e0e0;
57
- padding-top: 1rem;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  }
59
  </style>
60
  """, unsafe_allow_html=True)
@@ -68,7 +180,6 @@ OPENROUTER_API_KEY = os.environ.get("OPENROUTER_API_KEY")
68
 
69
  @st.cache_data(ttl=300)
70
  def check_api_status():
71
- """Simple API check"""
72
  if not OPENROUTER_API_KEY:
73
  return "No API Key"
74
  try:
@@ -80,9 +191,8 @@ def check_api_status():
80
  return "Error"
81
 
82
  def get_ai_response(messages, model="openai/gpt-3.5-turbo"):
83
- """Get AI response"""
84
  if not OPENROUTER_API_KEY:
85
- return "No API key found. Please add OPENROUTER_API_KEY to environment variables."
86
 
87
  url = "https://openrouter.ai/api/v1/chat/completions"
88
  headers = {
@@ -90,7 +200,7 @@ def get_ai_response(messages, model="openai/gpt-3.5-turbo"):
90
  "Content-Type": "application/json"
91
  }
92
 
93
- api_messages = [{"role": "system", "content": "You are a helpful AI assistant."}]
94
  api_messages.extend(messages)
95
 
96
  data = {
@@ -123,24 +233,24 @@ def get_ai_response(messages, model="openai/gpt-3.5-turbo"):
123
  except json.JSONDecodeError:
124
  continue
125
  except Exception as e:
126
- yield f" Error: {str(e)[:100]}..."
127
 
128
- # Header
129
- st.title("🤖 AI Assistant")
130
- st.caption("Simple chat interface")
131
 
132
- # Simple sidebar
133
  with st.sidebar:
134
  st.header("Settings")
135
 
136
  # API Status
137
  status = check_api_status()
138
  if status == "Connected":
139
- st.success("API Connected")
140
  elif status == "No API Key":
141
- st.error("No API Key")
142
  else:
143
- st.warning("⚠️ Connection Issue")
144
 
145
  st.divider()
146
 
@@ -152,31 +262,29 @@ with st.sidebar:
152
  "google/gemini-pro"
153
  ]
154
 
155
- selected_model = st.selectbox("Model", models)
156
 
157
  st.divider()
158
 
159
  # Controls
160
- col1, col2 = st.columns(2)
161
- with col1:
162
- if st.button("🗑️ Clear", use_container_width=True):
163
- st.session_state.messages = []
164
- st.rerun()
165
-
166
- with col2:
167
- if st.button("🔄 Refresh", use_container_width=True):
168
- st.rerun()
169
-
170
- # Stats
171
- st.metric("Messages", len(st.session_state.messages))
172
 
173
  # Display chat messages
174
  for message in st.session_state.messages:
175
  with st.chat_message(message["role"]):
176
  st.markdown(message["content"])
177
 
178
- # Chat input
179
- if prompt := st.chat_input("Type your message..."):
180
  # Add user message
181
  st.session_state.messages.append({"role": "user", "content": prompt})
182
 
@@ -196,8 +304,4 @@ if prompt := st.chat_input("Type your message..."):
196
  placeholder.markdown(full_response)
197
 
198
  # Add AI response to messages
199
- st.session_state.messages.append({"role": "assistant", "content": full_response})
200
-
201
- # Simple footer
202
- st.markdown("---")
203
- st.markdown("🤖 **AI Assistant 2025** | Built with Streamlit")
 
6
 
7
  # Page configuration
8
  st.set_page_config(
9
+ page_title="AI Assistant",
10
+ page_icon="💬",
11
+ layout="centered",
12
+ initial_sidebar_state="collapsed"
13
  )
14
 
15
+ # Professional styling - ChatGPT inspired
16
  st.markdown("""
17
  <style>
18
+ /* Import clean fonts */
19
+ @import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600&display=swap');
20
+
21
+ /* Global app styling */
22
+ .stApp {
23
+ background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
24
+ font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
25
+ font-weight: 400;
26
+ }
27
+
28
+ /* Hide Streamlit branding and clutter */
29
+ #MainMenu {visibility: hidden;}
30
+ footer {visibility: hidden;}
31
+ header {visibility: hidden;}
32
+ .stDeployButton {display: none;}
33
+
34
+ /* Main container - centered and spacious */
35
+ .main .block-container {
36
+ max-width: 800px;
37
+ padding-top: 2rem;
38
+ padding-bottom: 2rem;
39
+ }
40
+
41
+ /* Professional header styling */
42
+ .main-title {
43
+ text-align: center;
44
+ font-size: 2.5rem;
45
+ font-weight: 600;
46
+ color: #2d3748;
47
+ margin-bottom: 0.5rem;
48
+ letter-spacing: -0.025em;
49
+ }
50
+
51
+ .main-subtitle {
52
+ text-align: center;
53
+ font-size: 1.1rem;
54
+ color: #718096;
55
+ margin-bottom: 3rem;
56
+ font-weight: 400;
57
+ }
58
+
59
+ /* Welcome message - centered */
60
+ .welcome-message {
61
+ text-align: center;
62
+ font-size: 1.2rem;
63
+ color: #4a5568;
64
+ margin: 4rem 0;
65
+ font-weight: 400;
66
+ line-height: 1.6;
67
+ }
68
+
69
+ /* Chat messages styling */
70
+ .stChatMessage {
71
+ border: none !important;
72
+ margin-bottom: 1rem;
73
+ border-radius: 16px !important;
74
+ }
75
+
76
+ /* User messages - Right side, blue */
77
  .stChatMessage[data-testid*="user"] {
78
  flex-direction: row-reverse !important;
79
+ margin-left: 15% !important;
80
  margin-right: 0% !important;
81
  }
82
 
83
  .stChatMessage[data-testid*="user"] .stMarkdown {
84
+ background: #0066cc !important;
85
  color: white !important;
86
  border-radius: 18px 18px 4px 18px !important;
87
  padding: 12px 16px !important;
88
+ margin-left: 12px !important;
89
+ box-shadow: 0 1px 2px rgba(0,0,0,0.1) !important;
90
  }
91
 
92
+ /* AI messages - Left side, light gray */
93
  .stChatMessage[data-testid*="assistant"] {
94
+ margin-right: 15% !important;
95
  margin-left: 0% !important;
96
  }
97
 
98
  .stChatMessage[data-testid*="assistant"] .stMarkdown {
99
+ background: #f8f9fa !important;
100
+ color: #2d3748 !important;
101
  border-radius: 18px 18px 18px 4px !important;
102
  padding: 12px 16px !important;
103
+ margin-right: 12px !important;
104
+ box-shadow: 0 1px 2px rgba(0,0,0,0.1) !important;
105
+ border: 1px solid #e2e8f0 !important;
106
  }
107
 
108
+ /* Hide chat avatars */
109
  .stChatMessage img {
110
  display: none !important;
111
  }
112
 
113
+ /* Beautiful chat input */
114
  .stChatInputContainer {
115
+ border: none !important;
116
+ background: transparent !important;
117
+ padding: 2rem 0 !important;
118
+ }
119
+
120
+ .stChatInputContainer > div {
121
+ border: 1px solid #d1d5db !important;
122
+ border-radius: 24px !important;
123
+ background: white !important;
124
+ box-shadow: 0 2px 8px rgba(0,0,0,0.08) !important;
125
+ padding: 2px !important;
126
+ }
127
+
128
+ .stChatInputContainer textarea {
129
+ border: none !important;
130
+ background: transparent !important;
131
+ padding: 12px 20px !important;
132
+ font-size: 1rem !important;
133
+ color: #2d3748 !important;
134
+ font-family: 'Inter', sans-serif !important;
135
+ resize: none !important;
136
+ }
137
+
138
+ .stChatInputContainer textarea::placeholder {
139
+ color: #9ca3af !important;
140
+ font-weight: 400 !important;
141
+ }
142
+
143
+ /* Sidebar styling */
144
+ .css-1d391kg {
145
+ background: rgba(255,255,255,0.95) !important;
146
+ border-right: 1px solid #e2e8f0 !important;
147
+ }
148
+
149
+ /* Clean buttons */
150
+ .stButton > button {
151
+ border: 1px solid #d1d5db !important;
152
+ border-radius: 8px !important;
153
+ background: white !important;
154
+ color: #374151 !important;
155
+ font-weight: 500 !important;
156
+ transition: all 0.2s !important;
157
+ box-shadow: 0 1px 3px rgba(0,0,0,0.1) !important;
158
+ }
159
+
160
+ .stButton > button:hover {
161
+ background: #f9fafb !important;
162
+ border-color: #9ca3af !important;
163
+ }
164
+
165
+ /* Status indicators */
166
+ .stSuccess, .stError, .stWarning {
167
+ border-radius: 8px !important;
168
+ border: none !important;
169
+ font-weight: 500 !important;
170
  }
171
  </style>
172
  """, unsafe_allow_html=True)
 
180
 
181
  @st.cache_data(ttl=300)
182
  def check_api_status():
 
183
  if not OPENROUTER_API_KEY:
184
  return "No API Key"
185
  try:
 
191
  return "Error"
192
 
193
  def get_ai_response(messages, model="openai/gpt-3.5-turbo"):
 
194
  if not OPENROUTER_API_KEY:
195
+ return "No API key found. Please add OPENROUTER_API_KEY to environment variables."
196
 
197
  url = "https://openrouter.ai/api/v1/chat/completions"
198
  headers = {
 
200
  "Content-Type": "application/json"
201
  }
202
 
203
+ api_messages = [{"role": "system", "content": "You are a helpful AI assistant. Provide clear and helpful responses."}]
204
  api_messages.extend(messages)
205
 
206
  data = {
 
233
  except json.JSONDecodeError:
234
  continue
235
  except Exception as e:
236
+ yield f"Sorry, I encountered an error. Please try again."
237
 
238
+ # Clean, professional header
239
+ st.markdown('<h1 class="main-title">AI Assistant</h1>', unsafe_allow_html=True)
240
+ st.markdown('<p class="main-subtitle">Ask me anything</p>', unsafe_allow_html=True)
241
 
242
+ # Sidebar (collapsed by default)
243
  with st.sidebar:
244
  st.header("Settings")
245
 
246
  # API Status
247
  status = check_api_status()
248
  if status == "Connected":
249
+ st.success("API Connected")
250
  elif status == "No API Key":
251
+ st.error("No API Key")
252
  else:
253
+ st.warning("Connection Issue")
254
 
255
  st.divider()
256
 
 
262
  "google/gemini-pro"
263
  ]
264
 
265
+ selected_model = st.selectbox("Model", models, index=0)
266
 
267
  st.divider()
268
 
269
  # Controls
270
+ if st.button("Clear Chat", use_container_width=True):
271
+ st.session_state.messages = []
272
+ st.rerun()
273
+
274
+ # Show welcome message when no messages
275
+ if not st.session_state.messages:
276
+ st.markdown(
277
+ '<div class="welcome-message">How can I help you today?</div>',
278
+ unsafe_allow_html=True
279
+ )
 
 
280
 
281
  # Display chat messages
282
  for message in st.session_state.messages:
283
  with st.chat_message(message["role"]):
284
  st.markdown(message["content"])
285
 
286
+ # Chat input with inviting placeholder
287
+ if prompt := st.chat_input("Ask anything..."):
288
  # Add user message
289
  st.session_state.messages.append({"role": "user", "content": prompt})
290
 
 
304
  placeholder.markdown(full_response)
305
 
306
  # Add AI response to messages
307
+ st.session_state.messages.append({"role": "assistant", "content": full_response})