Ubuntu
commited on
Commit
·
99870e3
1
Parent(s):
98a187e
UI fixes 5
Browse files
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="
|
11 |
initial_sidebar_state="collapsed"
|
12 |
)
|
13 |
|
14 |
-
#
|
15 |
st.markdown("""
|
16 |
<style>
|
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 |
-
/* 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
|
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 |
-
|
125 |
-
|
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 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
152 |
with col1:
|
153 |
-
|
154 |
with col2:
|
155 |
-
clear_button = st.
|
|
|
|
|
|
|
|
|
|
|
156 |
|
157 |
-
#
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
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 |
-
#
|
183 |
-
st.
|
|
|
|
|
|
|
|
|
184 |
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
|
|
|
|
|
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)
|