-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
1265 lines (1105 loc) · 50.1 KB
/
app.py
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
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import streamlit as st
import os
from langchain.chains import ConversationChain
from langchain.memory import ConversationBufferWindowMemory
from langchain_groq import ChatGroq
from langchain.prompts import PromptTemplate
from dotenv import load_dotenv
import time
import json
from datetime import datetime
import pandas as pd
import plotly.express as px
from PIL import Image
import io
import requests
from stability_sdk import client
import stability_sdk.interfaces.gooseai.generation.generation_pb2 as generation
import uuid
from typing import Optional, Dict, List, Union
import base64
# Load environment variables
load_dotenv()
groq_api_key = os.getenv("GROQ_API_KEY")
stability_api_key = os.getenv("STABILITY_API_KEY")
# Constants
DEFAULT_MODEL = "llama-3.1-8b-instant"
DEFAULT_TEMPERATURE = 0.7
DEFAULT_MAX_TOKENS = 1000
DEFAULT_MEMORY_LENGTH = 5
DEFAULT_CONVERSATION_MODE = "General"
# List of supported models with detailed information
MODELS = {
"llama-3.1-8b-instant": {
"description": "Fast, efficient model for quick responses",
"category": "General",
"token_limit": 16384,
"strengths": "Speed, efficiency",
"best_for": "Quick conversations, basic tasks",
"icon": "⚡"
},
"deepseek-r1-distill-llama-70b": {
"description": "Advanced distilled model with excellent performance",
"category": "Advanced",
"token_limit": 32768,
"strengths": "Knowledge, reasoning",
"best_for": "Complex reasoning, detailed explanations",
"icon": "🧠"
},
"qwen-2.5-32b": {
"description": "High-quality model for detailed responses",
"category": "Advanced",
"token_limit": 32768,
"strengths": "Quality, context handling",
"best_for": "Longer conversations, nuanced responses",
"icon": "🎯"
},
"llama-3.3-70b-specdec": {
"description": "Speculative decoding-enhanced Llama 3 for faster responses",
"category": "Specialized",
"token_limit": 16384,
"strengths": "Speed, English tasks, technical content",
"best_for": "Fast English generation, coding assistance",
"icon": "🚀"
},
"qwen-2.5-coder-32b": {
"description": "Model optimized for coding tasks",
"category": "Specialized",
"token_limit": 32768,
"strengths": "Code generation, technical knowledge",
"best_for": "Programming assistance, technical documentation",
"icon": "💻"
},
"stable-diffusion-xl": {
"description": "Stability AI's advanced image generation model",
"category": "Image Generation",
"token_limit": 0, # Not applicable for image models
"strengths": "High-quality image generation, creative visuals",
"best_for": "Generating images from text prompts",
"icon": "🎨"
}
}
# Model categories for organization
MODEL_CATEGORIES = {
"General": ["llama-3.1-8b-instant"],
"Advanced": ["deepseek-r1-distill-llama-70b", "qwen-2.5-32b"],
"Specialized": ["llama-3.3-70b-specdec", "qwen-2.5-coder-32b"],
"Image Generation": ["stable-diffusion-xl"]
}
# System prompts for different conversation modes
SYSTEM_PROMPTS = {
"General": "You are a helpful, harmless, and honest AI assistant.",
"Creative": "You are a creative AI assistant that helps with brainstorming, storytelling, and creative writing. Be imaginative and inspirational in your responses.",
"Technical": "You are a technical AI assistant that specializes in programming, data analysis, and technical topics. Provide detailed and accurate technical information.",
"Concise": "You are a concise AI assistant. Provide brief, to-the-point responses that get straight to the answer without unnecessary elaboration.",
"Multimodal": "You are a multimodal AI assistant that can handle both text and image generation requests. When asked to create images, provide detailed prompts for the image generation model."
}
# Image generation style presets
STYLE_PRESETS = [
None, "3d-model", "analog-film", "anime", "cinematic", "comic-book",
"digital-art", "enhance", "fantasy-art", "isometric", "line-art",
"low-poly", "modeling-compound", "neon-punk", "origami",
"photographic", "pixel-art", "tile-texture"
]
# Sampler methods
SAMPLER_METHODS = [
"K_DPMPP_2M", "K_DPMPP_2S_ANCESTRAL", "K_DPM_2",
"K_DPM_2_ANCESTRAL", "K_EULER", "K_EULER_ANCESTRAL"
]
def initialize_session_state():
"""Initialize all session state variables with type hints"""
if 'chat_history' not in st.session_state:
st.session_state.chat_history: List[Dict] = []
if 'conversation' not in st.session_state:
st.session_state.conversation: Optional[ConversationChain] = None
if 'model' not in st.session_state:
st.session_state.model: str = DEFAULT_MODEL
if 'memory_length' not in st.session_state:
st.session_state.memory_length: int = DEFAULT_MEMORY_LENGTH
if 'system_prompt' not in st.session_state:
st.session_state.system_prompt: str = SYSTEM_PROMPTS[DEFAULT_CONVERSATION_MODE]
if 'conversation_mode' not in st.session_state:
st.session_state.conversation_mode: str = DEFAULT_CONVERSATION_MODE
if 'temperature' not in st.session_state:
st.session_state.temperature: float = DEFAULT_TEMPERATURE
if 'max_tokens' not in st.session_state:
st.session_state.max_tokens: int = DEFAULT_MAX_TOKENS
if 'session_start_time' not in st.session_state:
st.session_state.session_start_time: datetime = datetime.now()
if 'usage_stats' not in st.session_state:
st.session_state.usage_stats: Dict = {
"messages_sent": 0,
"tokens_used": 0,
"models_used": {},
"response_speeds": [],
"images_generated": 0
}
if 'saved_chats' not in st.session_state:
st.session_state.saved_chats: List[Dict] = []
if 'current_chat_id' not in st.session_state:
st.session_state.current_chat_id: str = str(uuid.uuid4())
if 'current_chat_title' not in st.session_state:
st.session_state.current_chat_title: str = f"Chat {len(st.session_state.saved_chats) + 1}"
if 'image_generation_params' not in st.session_state:
st.session_state.image_generation_params: Dict = {
"width": 1024,
"height": 1024,
"steps": 30,
"cfg_scale": 7.0,
"sampler": "K_DPMPP_2M",
"style_preset": None,
"seed": None,
"negative_prompt": None
}
if 'generated_images' not in st.session_state:
st.session_state.generated_images: List[Dict] = []
if 'active_tab' not in st.session_state:
st.session_state.active_tab: str = "💬 Chat"
def create_conversation(model: str, memory_length: int, system_prompt: str,
temperature: float, max_tokens: int) -> Optional[ConversationChain]:
"""Create a new conversation with the specified parameters"""
memory = ConversationBufferWindowMemory(
memory_key="history",
input_key="input",
k=memory_length
)
# Preload existing chat history into memory
for message in st.session_state.chat_history:
if 'human' in message and 'AI' in message: # Only load valid messages
memory.save_context({'input': message['human']}, {'output': message['AI']})
# Skip LLM initialization for image generation model
if model == "stable-diffusion-xl":
return None
# Initialize Groq chat model with parameters
try:
groq_chat = ChatGroq(
groq_api_key=groq_api_key,
model_name=model,
temperature=temperature,
max_tokens=max_tokens
)
# Create a proper prompt template with the system prompt
template = f"{system_prompt}\n\nCurrent conversation:\n{{history}}\nHuman: {{input}}\nAI: "
prompt_template = PromptTemplate(
input_variables=["history", "input"],
template=template
)
# Create conversation chain with system prompt
return ConversationChain(
llm=groq_chat,
memory=memory,
prompt=prompt_template
)
except Exception as e:
st.error(f"Error initializing conversation: {str(e)}")
return None
def calculate_response_speed(response_text: str, response_time: float) -> float:
"""Calculate the response speed in tokens/second"""
# Estimate tokens (improved estimate - approximately 4 chars per token)
estimated_tokens = len(response_text) / 4
# Calculate speed (tokens per second)
if response_time > 0:
return estimated_tokens / response_time
return 0
def generate_image(prompt: str, negative_prompt: Optional[str] = None) -> Optional[Image.Image]:
"""Generate image using Stability AI's API"""
if not stability_api_key:
st.error("Stability API key is missing. Please add your API key to the .env file.")
return None
try:
stability_api = client.StabilityInference(
key=stability_api_key,
verbose=True,
engine="stable-diffusion-xl-1024-v1-0"
)
params = st.session_state.image_generation_params
# Prepare the request
request = {
"prompt": prompt,
"width": params["width"],
"height": params["height"],
"steps": params["steps"],
"cfg_scale": params["cfg_scale"],
"sampler": getattr(generation, f"SAMPLER_{params['sampler']}"),
}
# Add optional parameters if they exist
if params["style_preset"]:
request["style_preset"] = params["style_preset"]
if params["seed"]:
request["seed"] = params["seed"]
if negative_prompt:
request["negative_prompt"] = negative_prompt
answers = stability_api.generate(**request)
for resp in answers:
for artifact in resp.artifacts:
if artifact.finish_reason == generation.FILTER:
st.warning("Your request activated the API's safety filters and could not be processed.")
return None
if artifact.type == generation.ARTIFACT_IMAGE:
img = Image.open(io.BytesIO(artifact.binary))
return img
return None
except Exception as e:
st.error(f"Error generating image: {str(e)}")
return None
def handle_user_input(user_question: str):
"""Process user input and generate response with enhanced error handling and analytics"""
if not user_question.strip():
st.warning("Please enter a valid message.")
return
# Display user message
st.chat_message("human").write(user_question)
# Handle image generation requests
if st.session_state.model == "stable-diffusion-xl":
with st.chat_message("assistant"):
with st.spinner("Generating image..."):
start_time = time.time()
# Generate the image
negative_prompt = st.session_state.image_generation_params.get("negative_prompt")
generated_image = generate_image(user_question, negative_prompt)
if generated_image:
response_time = time.time() - start_time
# Save the image to session state
img_bytes = io.BytesIO()
generated_image.save(img_bytes, format='PNG')
img_bytes.seek(0)
# Store image data
image_data = {
"id": str(uuid.uuid4()),
"prompt": user_question,
"image": img_bytes.getvalue(),
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"params": st.session_state.image_generation_params.copy()
}
st.session_state.generated_images.append(image_data)
# Update usage stats
st.session_state.usage_stats["messages_sent"] += 1
st.session_state.usage_stats["images_generated"] += 1
# Display the image
st.image(generated_image, caption=f"Generated from: '{user_question}'")
# Save to chat history with metadata
message = {
'human': user_question,
'AI': "[IMAGE GENERATED]",
'timestamp': datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
'model': st.session_state.model,
'response_time': response_time,
'image_data': image_data
}
st.session_state.chat_history.append(message)
st.rerun()
return
# Handle text responses
with st.chat_message("assistant"):
thinking_placeholder = st.empty()
message_container = st.container()
with thinking_placeholder:
st.info("Generating response...")
try:
start_time = time.time()
# Get response from model
response = st.session_state.conversation.invoke({'input': user_question})
chatbot_reply = response['response']
# Calculate response time
response_time = time.time() - start_time
# Calculate response speed (tokens/second)
response_speed = calculate_response_speed(chatbot_reply, response_time)
# Update usage statistics (estimated tokens)
st.session_state.usage_stats["messages_sent"] += 1
estimated_tokens = len(user_question.split()) + len(chatbot_reply.split())
st.session_state.usage_stats["tokens_used"] += estimated_tokens
# Track response speed for analytics
st.session_state.usage_stats["response_speeds"].append({
"model": st.session_state.model,
"time": response_time,
"speed": response_speed,
"timestamp": datetime.now()
})
# Track model usage
if st.session_state.model in st.session_state.usage_stats["models_used"]:
st.session_state.usage_stats["models_used"][st.session_state.model] += 1
else:
st.session_state.usage_stats["models_used"][st.session_state.model] = 1
# Save to chat history with metadata
message = {
'human': user_question,
'AI': chatbot_reply,
'timestamp': datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
'model': st.session_state.model,
'response_time': response_time,
'response_speed': response_speed
}
st.session_state.chat_history.append(message)
# Remove thinking indicator and display response
thinking_placeholder.empty()
with message_container:
# Apply markdown formatting to improve readability
st.markdown(chatbot_reply)
# Display response metadata in cleaner format
st.caption(f"**{st.session_state.model}** • {response_time:.2f}s • {response_speed:.1f} tokens/sec")
# Auto-scroll to bottom after new message
st.rerun()
except Exception as e:
thinking_placeholder.empty()
with message_container:
error_message = str(e)
if "api_key" in error_message.lower():
error_message = "API key error. Please verify your Groq API key is valid."
elif "timeout" in error_message.lower():
error_message = "Request timed out. The model may be experiencing high traffic."
elif "rate limit" in error_message.lower():
error_message = "Rate limit exceeded. Please wait a moment before trying again."
st.error(f"⚠️ Error: {error_message}")
st.button("Try Again", on_click=lambda: handle_user_input(user_question), key=f"retry_{len(st.session_state.chat_history)}")
def reset_conversation():
"""Clear conversation history and reset the chat"""
st.session_state.chat_history = []
st.session_state.generated_images = []
st.session_state.current_chat_id = str(uuid.uuid4())
st.session_state.current_chat_title = f"Chat {len(st.session_state.saved_chats) + 1}"
st.session_state.conversation = create_conversation(
st.session_state.model,
st.session_state.memory_length,
st.session_state.system_prompt,
st.session_state.temperature,
st.session_state.max_tokens
)
st.rerun()
def handle_settings_change():
"""Handle changes to any settings and recreate conversation"""
st.session_state.conversation = create_conversation(
st.session_state.model,
st.session_state.memory_length,
st.session_state.system_prompt,
st.session_state.temperature,
st.session_state.max_tokens
)
def save_current_chat() -> bool:
"""Save current chat session"""
if not st.session_state.chat_history:
st.warning("Cannot save an empty chat.")
return False
saved_chat = {
"id": st.session_state.current_chat_id,
"title": st.session_state.current_chat_title,
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"model": st.session_state.model,
"history": st.session_state.chat_history,
"system_prompt": st.session_state.system_prompt,
"images": st.session_state.generated_images.copy() if st.session_state.model == "stable-diffusion-xl" else []
}
# Check if this chat already exists in saved chats
existing_index = next((i for i, chat in enumerate(st.session_state.saved_chats)
if chat["id"] == st.session_state.current_chat_id), None)
if existing_index is not None:
# Update existing chat
st.session_state.saved_chats[existing_index] = saved_chat
else:
# Add new chat
st.session_state.saved_chats.append(saved_chat)
st.success(f"Chat '{saved_chat['title']}' saved successfully!")
return True
def load_saved_chat(chat_index: int):
"""Load a previously saved chat"""
if chat_index < 0 or chat_index >= len(st.session_state.saved_chats):
st.error("Invalid chat selection.")
return
saved_chat = st.session_state.saved_chats[chat_index]
st.session_state.chat_history = saved_chat["history"]
st.session_state.model = saved_chat["model"]
st.session_state.system_prompt = saved_chat["system_prompt"]
st.session_state.current_chat_title = saved_chat["title"]
st.session_state.current_chat_id = saved_chat["id"]
if saved_chat["model"] == "stable-diffusion-xl" and "images" in saved_chat:
st.session_state.generated_images = saved_chat["images"].copy()
# Recreate conversation with loaded parameters
st.session_state.conversation = create_conversation(
st.session_state.model,
st.session_state.memory_length,
st.session_state.system_prompt,
st.session_state.temperature,
st.session_state.max_tokens
)
st.success(f"Loaded chat: {saved_chat['title']}")
st.session_state.active_tab = "💬 Chat"
st.rerun()
def export_chat_history(format: str = "json") -> Optional[str]:
"""Export chat history in various formats"""
if not st.session_state.chat_history:
st.warning("No chat history to export.")
return None
if format == "json":
chat_data = {
"title": st.session_state.current_chat_title,
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"model": st.session_state.model,
"system_prompt": st.session_state.system_prompt,
"messages": [{
"role": "human" if i % 2 == 0 else "assistant",
"content": msg["human"] if i % 2 == 0 else msg["AI"],
"timestamp": msg.get("timestamp", ""),
"response_time": msg.get("response_time", ""),
"response_speed": msg.get("response_speed", "")
} for i, msg in enumerate(st.session_state.chat_history)]
}
# Add image data if available
if st.session_state.model == "stable-diffusion-xl" and st.session_state.generated_images:
chat_data["images"] = [{
"prompt": img["prompt"],
"timestamp": img["timestamp"],
"params": img["params"]
} for img in st.session_state.generated_images]
return json.dumps(chat_data, indent=2)
elif format == "markdown":
md_content = f"# {st.session_state.current_chat_title}\n\n"
md_content += f"*Exported on: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}*\n\n"
md_content += f"**Model:** {st.session_state.model}\n\n"
md_content += f"**System Prompt:** {st.session_state.system_prompt}\n\n"
md_content += "---\n\n"
for msg in st.session_state.chat_history:
md_content += f"## Human\n\n{msg['human']}\n\n"
md_content += f"## Assistant\n\n{msg['AI']}\n\n"
if 'response_time' in msg and 'response_speed' in msg:
md_content += f"*Response time: {msg['response_time']:.2f}s • Speed: {msg['response_speed']:.1f} tokens/sec*\n\n"
md_content += "---\n\n"
# Add image section if available
if st.session_state.model == "stable-diffusion-xl" and st.session_state.generated_images:
md_content += "## Generated Images\n\n"
for img in st.session_state.generated_images:
md_content += f"### Image: {img['prompt']}\n\n"
md_content += f"*Generated at: {img['timestamp']}*\n\n"
md_content += "Parameters:\n```json\n"
md_content += json.dumps(img['params'], indent=2)
md_content += "\n```\n\n---\n\n"
return md_content
elif format == "html":
html_content = f"""
<!DOCTYPE html>
<html>
<head>
<title>{st.session_state.current_chat_title}</title>
<style>
body {{ font-family: Arial, sans-serif; line-height: 1.6; max-width: 800px; margin: 0 auto; padding: 20px; }}
h1 {{ color: #2c3e50; border-bottom: 1px solid #eee; padding-bottom: 10px; }}
.message {{ margin-bottom: 20px; padding: 10px; border-radius: 5px; }}
.human {{ background-color: #f5f5f5; }}
.assistant {{ background-color: #e8f4f8; }}
.meta {{ font-size: 0.8em; color: #666; margin-top: 5px; }}
.image-container {{ margin: 15px 0; }}
.image-container img {{ max-width: 100%; border-radius: 5px; }}
.image-prompt {{ font-style: italic; color: #555; }}
</style>
</head>
<body>
<h1>{st.session_state.current_chat_title}</h1>
<p><strong>Model:</strong> {st.session_state.model}</p>
<p><strong>System Prompt:</strong> {st.session_state.system_prompt}</p>
<p><em>Exported on: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}</em></p>
<hr>
"""
for msg in st.session_state.chat_history:
if 'image_data' in msg:
# Handle image messages
img_base64 = base64.b64encode(msg['image_data']['image']).decode('utf-8')
html_content += f"""
<div class="message assistant">
<div class="image-container">
<img src="data:image/png;base64,{img_base64}" alt="Generated image">
<p class="image-prompt">Generated from: {msg['human']}</p>
</div>
<div class="meta">
{msg.get('timestamp', '')} • {msg.get('model', '')} • Response time: {msg.get('response_time', 0):.2f}s
</div>
</div>
"""
else:
# Handle text messages
role_class = "human" if 'human' in msg else "assistant"
content = msg['human'] if 'human' in msg else msg['AI']
html_content += f"""
<div class="message {role_class}">
<h3>{'You' if role_class == 'human' else 'Assistant'}</h3>
<p>{content}</p>
<div class="meta">
{msg.get('timestamp', '')} • {msg.get('model', '')} • Response time: {msg.get('response_time', 0):.2f}s • Speed: {msg.get('response_speed', 0):.1f} tokens/sec
</div>
</div>
"""
html_content += "</body></html>"
return html_content
return None
def display_chat_analytics():
"""Display analytics about chat usage including response speed metrics"""
st.subheader("📊 Chat Analytics")
col1, col2 = st.columns(2)
with col1:
st.metric("Total Messages", st.session_state.usage_stats["messages_sent"])
st.metric("Estimated Tokens Used", st.session_state.usage_stats["tokens_used"])
if st.session_state.model == "stable-diffusion-xl":
st.metric("Images Generated", st.session_state.usage_stats["images_generated"])
# Session duration
current_time = datetime.now()
session_duration = current_time - st.session_state.session_start_time
hours, remainder = divmod(session_duration.seconds, 3600)
minutes, seconds = divmod(remainder, 60)
st.metric("Session Duration", f"{hours}h {minutes}m {seconds}s")
# Calculate average response speed
if st.session_state.usage_stats["response_speeds"]:
avg_speed = sum(item["speed"] for item in st.session_state.usage_stats["response_speeds"]) / len(st.session_state.usage_stats["response_speeds"])
st.metric("Avg Response Speed", f"{avg_speed:.1f} tokens/sec")
with col2:
# Model usage chart
if st.session_state.usage_stats["models_used"]:
model_usage_data = pd.DataFrame({
'Model': list(st.session_state.usage_stats["models_used"].keys()),
'Usage Count': list(st.session_state.usage_stats["models_used"].values())
})
fig = px.pie(model_usage_data, values='Usage Count', names='Model',
title='Model Usage Distribution', hole=0.3)
st.plotly_chart(fig, use_container_width=True)
# Add speed comparison chart
if st.session_state.usage_stats["response_speeds"]:
st.subheader("⏱️ Response Speed Analysis")
# Convert the response speeds list to a DataFrame
speeds_df = pd.DataFrame(st.session_state.usage_stats["response_speeds"])
# Model speed comparison
model_speeds = speeds_df.groupby('model')['speed'].mean().reset_index()
fig_speed = px.bar(
model_speeds,
x='model',
y='speed',
title='Average Response Speed by Model (tokens/sec)',
labels={'speed': 'Speed (tokens/sec)', 'model': 'Model'},
color='model'
)
st.plotly_chart(fig_speed, use_container_width=True)
# Speed over time
if len(speeds_df) > 1:
fig_time = px.line(
speeds_df,
x='timestamp',
y='speed',
color='model',
title='Response Speed Over Time',
labels={'speed': 'Speed (tokens/sec)', 'timestamp': 'Time'},
line_shape="spline"
)
st.plotly_chart(fig_time, use_container_width=True)
def display_image_generation_settings():
"""Display settings for image generation"""
st.subheader("🖼️ Image Generation Settings")
col1, col2 = st.columns(2)
with col1:
st.session_state.image_generation_params["width"] = st.slider(
"Width", 512, 2048, st.session_state.image_generation_params["width"], 64,
help="Width of the generated image"
)
st.session_state.image_generation_params["height"] = st.slider(
"Height", 512, 2048, st.session_state.image_generation_params["height"], 64,
help="Height of the generated image"
)
st.session_state.image_generation_params["steps"] = st.slider(
"Steps", 10, 150, st.session_state.image_generation_params["steps"], 5,
help="Number of diffusion steps (more steps = higher quality but slower)"
)
with col2:
st.session_state.image_generation_params["cfg_scale"] = st.slider(
"CFG Scale", 1.0, 20.0, st.session_state.image_generation_params["cfg_scale"], 0.5,
help="How closely to follow the prompt (higher = more strict)"
)
st.session_state.image_generation_params["sampler"] = st.selectbox(
"Sampler",
SAMPLER_METHODS,
index=SAMPLER_METHODS.index(st.session_state.image_generation_params["sampler"]),
help="Diffusion sampler method"
)
st.session_state.image_generation_params["style_preset"] = st.selectbox(
"Style Preset (optional)",
STYLE_PRESETS,
index=STYLE_PRESETS.index(st.session_state.image_generation_params["style_preset"]),
help="Predefined style to apply to the image"
)
# Negative prompt
st.session_state.image_generation_params["negative_prompt"] = st.text_area(
"Negative Prompt (optional)",
value=st.session_state.image_generation_params.get("negative_prompt", ""),
help="What you don't want to see in the generated image"
)
# Seed controls
seed_col1, seed_col2 = st.columns([3, 1])
with seed_col1:
seed_input = st.number_input(
"Seed (optional)",
min_value=0,
max_value=2147483647,
value=st.session_state.image_generation_params["seed"] or 0,
help="Random seed for reproducibility (0 = random)"
)
st.session_state.image_generation_params["seed"] = seed_input if seed_input != 0 else None
with seed_col2:
if st.button("🎲 Random Seed", use_container_width=True):
st.session_state.image_generation_params["seed"] = None
st.rerun()
def display_model_card(model_name: str):
"""Display detailed information about a model"""
model_info = MODELS[model_name]
st.markdown(f"""
<div style="background-color: #f8f9fa; border-radius: 10px; padding: 15px; margin-bottom: 20px;">
<h3>{model_info['icon']} {model_name}</h3>
<p>{model_info['description']}</p>
<div style="display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 10px;">
<div>
<strong>Category:</strong><br>
{model_info['category']}
</div>
<div>
<strong>Token Limit:</strong><br>
{model_info['token_limit']:,}
</div>
<div>
<strong>Strengths:</strong><br>
{model_info['strengths']}
</div>
<div>
<strong>Best For:</strong><br>
{model_info['best_for']}
</div>
</div>
</div>
""", unsafe_allow_html=True)
def display_chat_history():
"""Display the chat history with improved formatting"""
# Create a container for chat history with scroll
chat_history_container = st.container()
with chat_history_container:
# Improved empty state with better styling
if not st.session_state.chat_history:
st.markdown("""
<div style="text-align: center; padding: 80px 20px; color: #666; background-color: #f9f9f9; border-radius: 10px; margin: 20px 0;">
<h3>Start a New Conversation</h3>
<p>Select a model from the sidebar and type your message below to begin chatting.</p>
</div>
""", unsafe_allow_html=True)
# Display chat history with improved styling
for i, message in enumerate(st.session_state.chat_history):
# User message
if 'human' in message:
with st.chat_message("human"):
st.markdown(message['human'])
# Assistant message with improved formatting
if 'AI' in message:
with st.chat_message("assistant"):
# Handle image responses
if 'image_data' in message:
st.image(message['image_data']['image'], caption=f"Generated from: '{message['human']}'")
st.caption(f"**{message.get('model', st.session_state.model)}** • Response time: {message.get('response_time', 0):.2f}s")
else:
# Apply better formatting for text responses
st.markdown(message['AI'])
# Add metadata in a cleaner format
if 'response_time' in message:
speed_info = f"| Speed: {message.get('response_speed', 0):.1f} tokens/sec" if 'response_speed' in message else ""
st.caption(f"**{message.get('model', st.session_state.model)}** | Response time: {message.get('response_time', 0):.2f}s {speed_info}")
def display_gallery():
"""Display the image gallery"""
if st.session_state.model == "stable-diffusion-xl":
st.title("🖼️ Generated Images Gallery")
if st.session_state.generated_images:
st.markdown(f"**{len(st.session_state.generated_images)} images generated in this session**")
# Display images in a responsive grid
cols = st.columns(2)
col_index = 0
for img_data in reversed(st.session_state.generated_images):
with cols[col_index]:
# Display image with caption
st.image(img_data['image'], use_column_width=True, caption=f"Prompt: {img_data['prompt']}")
# Show metadata in a container (not an expander)
with st.container():
st.caption(f"**Generated at:** {img_data['timestamp']}")
# Button to show parameters
if st.button("Show Generation Parameters", key=f"params_{img_data['id']}"):
st.json(img_data['params'])
# Alternate between columns
col_index = 1 - col_index
else:
st.info("No images generated yet. Use the Stable Diffusion XL model to generate images.")
else:
st.info("Image gallery is only available when using the Stable Diffusion XL model. Switch models in the sidebar.")
def display_export_options():
"""Display export options for the chat"""
st.title("📤 Export Conversation")
col1, col2 = st.columns(2)
with col1:
export_format = st.radio(
"Export format:",
["JSON", "Markdown", "HTML"],
horizontal=True,
index=0
)
with col2:
st.write("") # Spacing
if st.button("📥 Generate Export", use_container_width=True):
format_key = export_format.lower()
exported_content = export_chat_history(format_key)
if exported_content:
# Offer download
st.download_button(
label=f"💾 Download as {export_format}",
data=exported_content,
file_name=f"{st.session_state.current_chat_title.replace(' ', '_')}_{datetime.now().strftime('%Y%m%d')}.{format_key}",
mime={
"json": "application/json",
"markdown": "text/markdown",
"html": "text/html"
}[format_key],
use_container_width=True
)
# Show preview in expandable section
with st.expander("Preview Export Content", expanded=True):
if format_key == "html":
st.components.v1.html(exported_content, height=500, scrolling=True)
else:
st.text_area("", exported_content, height=300)
else:
st.warning("Nothing to export. Start a conversation first.")
def main():
# Page configuration
st.set_page_config(
page_title="Multimodal Chat Assistant",
page_icon="💬",
layout="wide",
initial_sidebar_state="expanded"
)
# Add custom CSS for improved UI
st.markdown("""
<style>
.main .block-container {
padding-top: 1rem;
padding-bottom: 6rem;
}
.stTabs [data-baseweb="tab-panel"] {
padding-top: 0.5rem;
}
.stSidebar .block-container {
padding-top: 1rem;
}
.stChatMessage {
padding: 1rem;
border-radius: 8px;
margin-bottom: 1rem;
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}
.stChatMessage.user {
background-color: rgba(240, 242, 246, 0.5);
border-left: 4px solid #4e79a7;
}
.stChatMessage.assistant {
background-color: rgba(240, 246, 240, 0.5);
border-left: 4px solid #59a14f;
}
.chat-input-container {
position: fixed;
bottom: 0;
left: 25%;
right: 0;
background: white;
z-index: 100;
padding: 1rem;
box-shadow: 0 -4px 15px rgba(0,0,0,0.08);
border-top: 1px solid #e0e0e0;
width: 75%;
}
@media (max-width: 992px) {
.chat-input-container {
left: 0;
width: 100%;
}
}
.chat-history {
max-height: calc(100vh - 250px);
overflow-y: auto;
padding-bottom: 100px;
}
.stButton>button {
border-radius: 4px;
transition: all 0.2s;
}
.stButton>button:hover {
transform: translateY(-1px);
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
}
.chat-title {
font-size: 1.2rem;
font-weight: bold;
margin-bottom: 0.5rem;
}
.scroll-to-bottom {
max-height: 0;
overflow-anchor: none;
}
.image-gallery {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
gap: 1rem;
margin-top: 1rem;
}
.image-card {
border: 1px solid #e0e0e0;
border-radius: 8px;
padding: 0.5rem;
transition: transform 0.2s;
}
.image-card:hover {
transform: translateY(-2px);
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
}
.image-card img {
width: 100%;
border-radius: 4px;
}
.image-prompt {
font-size: 0.9rem;
margin-top: 0.5rem;
color: #555;
}
.model-card {
background-color: #f8f9fa;
border-radius: 10px;
padding: 15px;
margin-bottom: 15px;
transition: all 0.2s;
}
.model-card:hover {
background-color: #e9ecef;
}
.model-card h4 {
margin-top: 0;
margin-bottom: 10px;
}
.model-card p {
margin-bottom: 10px;