Ubuntu commited on
Commit
99870e3
·
1 Parent(s): 98a187e

UI fixes 5

Browse files
Files changed (1) hide show
  1. app.py +120 -163
app.py CHANGED
@@ -1,188 +1,145 @@
1
  import streamlit as st
2
  import torch
3
  from model_handler import load_model_and_tokenizer, generate_response
4
- import time
5
 
6
  # Page configuration
7
  st.set_page_config(
8
  page_title="Phi-2 Fine-tuned Assistant",
9
  page_icon="🤖",
10
- layout="centered",
11
  initial_sidebar_state="collapsed"
12
  )
13
 
14
- # Custom CSS for chat interface - fixed scrolling issues
15
  st.markdown("""
16
  <style>
17
- /* Reset Streamlit default spacing */
18
- .main .block-container {
19
- padding-top: 1rem;
20
- padding-bottom: 1rem;
21
- max-width: 800px;
22
- }
23
-
24
- /* Fix header spacing */
25
- header {
26
- visibility: hidden;
27
- }
28
-
29
- /* Hide main menu and footer */
30
- #MainMenu {visibility: hidden;}
31
- footer {visibility: hidden;}
32
-
33
- /* Chat bubbles */
34
- .user-bubble {
35
- background-color: #e6f7ff;
36
- border-radius: 15px;
37
- padding: 10px 15px;
38
- margin: 5px 0;
39
- max-width: 80%;
40
- margin-left: auto;
41
- margin-right: 10px;
42
- color: #000000;
43
- }
44
- .assistant-bubble {
45
- background-color: #f0f0f0;
46
- border-radius: 15px;
47
- padding: 10px 15px;
48
- margin: 5px 0;
49
- max-width: 80%;
50
- margin-left: 10px;
51
- color: #000000;
52
- }
53
-
54
- /* Chat container - fixed height with single scrollbar */
55
- .chat-container {
56
- display: flex;
57
- flex-direction: column;
58
- height: 60vh;
59
- overflow-y: auto;
60
- padding: 10px;
61
- margin-bottom: 10px;
62
- border-radius: 10px;
63
- background-color: rgba(255, 255, 255, 0.05);
64
- }
65
-
66
- /* Input styling */
67
- .stTextInput>div>div>input {
68
- border-radius: 20px;
69
- }
70
- .stButton>button {
71
- border-radius: 20px;
72
- width: 100%;
73
- }
74
-
75
- /* Ensure dark mode compatibility */
76
- .stApp {
77
- background-color: #121212;
78
- }
79
-
80
- /* Title styling */
81
- h2 {
82
- margin-top: 0 !important;
83
- margin-bottom: 1rem !important;
84
- text-align: center;
85
- }
86
-
87
- /* Loading indicator */
88
- .loading-container {
89
- display: flex;
90
- justify-content: center;
91
- align-items: center;
92
- height: 60vh;
93
- }
94
  </style>
95
  """, unsafe_allow_html=True)
96
 
97
- # Initialize session state for chat history
98
  if "messages" not in st.session_state:
99
  st.session_state.messages = []
100
-
101
- if "model_loaded" not in st.session_state:
102
- st.session_state.model_loaded = False
103
-
104
- # App title
105
- st.markdown("<h2>Phi-2 Fine-tuned Assistant</h2>", unsafe_allow_html=True)
106
-
107
- # Load model (only once)
108
- if not st.session_state.model_loaded:
109
- # Show loading indicator in the chat area
110
- st.markdown('<div class="loading-container"><div>Loading the fine-tuned model... This may take a minute.</div></div>', unsafe_allow_html=True)
111
-
112
- # Load model in the background
113
- model, tokenizer = load_model_and_tokenizer()
114
- st.session_state.model = model
115
- st.session_state.tokenizer = tokenizer
116
- st.session_state.model_loaded = True
117
- st.rerun() # Rerun to refresh UI after loading
118
-
119
- # Chat interface (only show when model is loaded)
120
- if st.session_state.model_loaded:
121
- # Display chat messages
122
- st.markdown('<div class="chat-container" id="chat-container">', unsafe_allow_html=True)
123
 
124
- # Display existing messages
125
- for message in st.session_state.messages:
126
- role = message["role"]
127
- content = message["content"]
128
-
129
- if role == "user":
130
- st.markdown(f'<div class="user-bubble">{content}</div>', unsafe_allow_html=True)
131
- else:
132
- st.markdown(f'<div class="assistant-bubble">{content}</div>', unsafe_allow_html=True)
133
-
134
- st.markdown('</div>', unsafe_allow_html=True)
135
 
136
- # Auto-scroll to bottom using JavaScript
137
- if st.session_state.messages:
138
- st.markdown("""
139
- <script>
140
- function scrollToBottom() {
141
- var chatContainer = document.getElementById('chat-container');
142
- if (chatContainer) {
143
- chatContainer.scrollTop = chatContainer.scrollHeight;
144
- }
145
- }
146
- scrollToBottom();
147
- </script>
148
- """, unsafe_allow_html=True)
149
-
150
- # Chat input
151
- col1, col2 = st.columns([5, 1])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152
  with col1:
153
- user_input = st.text_input("", placeholder="Type your message here...")
154
  with col2:
155
- clear_button = st.button("Clear")
 
 
 
 
 
156
 
157
- # Process input when user submits a message
158
- if user_input:
159
- # Add user message to chat history
160
- st.session_state.messages.append({"role": "user", "content": user_input})
161
-
162
- # Generate response
163
- try:
164
- # Show typing indicator
165
- temp_messages = st.session_state.messages.copy()
166
- temp_messages.append({"role": "assistant", "content": "Assistant is typing..."})
167
-
168
- # Generate full response first (no streaming in this version to avoid UI issues)
169
- full_response = ""
170
- for token in generate_response(
171
- st.session_state.model,
172
- st.session_state.tokenizer,
173
- user_input
174
- ):
175
- full_response += token
176
-
177
- # Add assistant's response to chat history
178
- st.session_state.messages.append({"role": "assistant", "content": full_response})
179
- except Exception as e:
180
- st.session_state.messages.append({"role": "assistant", "content": f"I'm sorry, I encountered an error: {str(e)}"})
181
 
182
- # Rerun to update the UI
183
- st.rerun()
 
 
 
 
184
 
185
- # Clear chat when button is pressed
186
- if clear_button:
187
- st.session_state.messages = []
188
- st.rerun()
 
 
 
1
  import streamlit as st
2
  import torch
3
  from model_handler import load_model_and_tokenizer, generate_response
 
4
 
5
  # Page configuration
6
  st.set_page_config(
7
  page_title="Phi-2 Fine-tuned Assistant",
8
  page_icon="🤖",
9
+ layout="wide",
10
  initial_sidebar_state="collapsed"
11
  )
12
 
13
+ # Clean, minimal CSS
14
  st.markdown("""
15
  <style>
16
+ /* Remove all default padding and margins */
17
+ .block-container {
18
+ padding: 0 !important;
19
+ max-width: 100% !important;
20
+ }
21
+
22
+ /* Hide header and footer */
23
+ header, footer, #MainMenu {
24
+ visibility: hidden;
25
+ }
26
+
27
+ /* Main container */
28
+ .main-container {
29
+ max-width: 800px;
30
+ margin: 0 auto;
31
+ padding: 20px;
32
+ }
33
+
34
+ /* Title */
35
+ .title {
36
+ text-align: center;
37
+ margin-bottom: 20px;
38
+ }
39
+
40
+ /* Chat messages */
41
+ .user-message {
42
+ background-color: #e6f7ff;
43
+ border-radius: 15px;
44
+ padding: 10px 15px;
45
+ margin: 5px 0;
46
+ max-width: 80%;
47
+ margin-left: auto;
48
+ margin-right: 10px;
49
+ color: #000;
50
+ }
51
+
52
+ .assistant-message {
53
+ background-color: #f0f0f0;
54
+ border-radius: 15px;
55
+ padding: 10px 15px;
56
+ margin: 5px 0;
57
+ max-width: 80%;
58
+ margin-left: 10px;
59
+ color: #000;
60
+ }
61
+
62
+ /* Input area */
63
+ .input-area {
64
+ display: flex;
65
+ margin-top: 20px;
66
+ }
67
+
68
+ /* Loading message */
69
+ .loading {
70
+ text-align: center;
71
+ padding: 20px;
72
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
  </style>
74
  """, unsafe_allow_html=True)
75
 
76
+ # Initialize session state
77
  if "messages" not in st.session_state:
78
  st.session_state.messages = []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
 
80
+ if "model" not in st.session_state:
81
+ st.session_state.model = None
 
 
 
 
 
 
 
 
 
82
 
83
+ if "tokenizer" not in st.session_state:
84
+ st.session_state.tokenizer = None
85
+
86
+ # Main app container
87
+ st.markdown('<div class="main-container">', unsafe_allow_html=True)
88
+ st.markdown('<h1 class="title">Phi-2 Fine-tuned Assistant</h1>', unsafe_allow_html=True)
89
+
90
+ # Load model if not already loaded
91
+ if st.session_state.model is None:
92
+ st.markdown('<div class="loading">Loading model... This may take a minute.</div>', unsafe_allow_html=True)
93
+ try:
94
+ model, tokenizer = load_model_and_tokenizer()
95
+ st.session_state.model = model
96
+ st.session_state.tokenizer = tokenizer
97
+ st.rerun()
98
+ except Exception as e:
99
+ st.error(f"Error loading model: {str(e)}")
100
+ st.stop()
101
+
102
+ # Display chat messages
103
+ for message in st.session_state.messages:
104
+ if message["role"] == "user":
105
+ st.markdown(f'<div class="user-message">{message["content"]}</div>', unsafe_allow_html=True)
106
+ else:
107
+ st.markdown(f'<div class="assistant-message">{message["content"]}</div>', unsafe_allow_html=True)
108
+
109
+ # Input form with submit button
110
+ with st.form(key="message_form", clear_on_submit=True):
111
+ user_input = st.text_input("Message:", key="user_input")
112
+ col1, col2 = st.columns([4, 1])
113
  with col1:
114
+ submit_button = st.form_submit_button("Send")
115
  with col2:
116
+ clear_button = st.form_submit_button("Clear Chat")
117
+
118
+ # Handle form submission
119
+ if submit_button and user_input:
120
+ # Add user message
121
+ st.session_state.messages.append({"role": "user", "content": user_input})
122
 
123
+ # Generate response
124
+ try:
125
+ full_response = ""
126
+ for token in generate_response(
127
+ st.session_state.model,
128
+ st.session_state.tokenizer,
129
+ user_input
130
+ ):
131
+ full_response += token
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132
 
133
+ # Add assistant response
134
+ st.session_state.messages.append({"role": "assistant", "content": full_response})
135
+ except Exception as e:
136
+ st.session_state.messages.append({"role": "assistant", "content": f"Error: {str(e)}"})
137
+
138
+ st.rerun()
139
 
140
+ # Handle clear button
141
+ if clear_button:
142
+ st.session_state.messages = []
143
+ st.rerun()
144
+
145
+ st.markdown('</div>', unsafe_allow_html=True)