andreska commited on
Commit
1af7545
·
1 Parent(s): 01e1fc7

CODEFIX: Added Adrega styling to fonts and improved breaking after bulletpoints etc

Browse files
Files changed (2) hide show
  1. app.py +48 -14
  2. styles.css +4 -0
app.py CHANGED
@@ -1,6 +1,8 @@
1
  import streamlit as st
2
  import streamlit.components.v1 as components
3
  import os
 
 
4
  from datasets import load_dataset
5
  from langchain_ollama import OllamaLLM
6
  from langchain_huggingface import HuggingFaceEmbeddings
@@ -108,7 +110,14 @@ retriever = st.session_state.db.as_retriever(search_kwargs={"k": 5})
108
 
109
  # 🔹 UI Styling
110
  st.markdown("""<style>
111
- .scrollable-div { max-height: 400px; overflow-y: auto; padding: 10px; border: 1px solid #ccc; background-color: #f9f9f9; }
 
 
 
 
 
 
 
112
  input[data-testid="stTextInput"] { cursor: pointer; }
113
  input[data-testid="stTextInput"]:focus { cursor: text; }
114
  </style>
@@ -149,9 +158,11 @@ def build_context(history, max_chars=10000):
149
  return "\n".join(context)
150
 
151
  def render_chat():
152
- html = """
 
153
  <style>
154
  .scrollable-div {
 
155
  max-height: 400px;
156
  overflow-y: auto;
157
  padding: 10px;
@@ -165,41 +176,64 @@ def render_chat():
165
  for msg in st.session_state.chat_history:
166
  content = msg["content"]
167
 
 
 
 
168
  # Convert markdown to HTML
169
  content = content.replace('\n', '<br>') # Line breaks
170
  content = content.replace('**', '<b>', 1).replace('**', '</b>', 1)
171
  while '**' in content:
172
  content = content.replace('**', '<b>', 1).replace('**', '</b>', 1)
173
 
174
- # Convert markdown lists to HTML
175
  lines = content.split('<br>')
176
  processed_lines = []
177
- in_list = False
 
178
 
179
  for line in lines:
180
  stripped = line.strip()
181
- if stripped.startswith('- ') or stripped.startswith('* '):
182
- if not in_list:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
183
  processed_lines.append('<ul>')
184
- in_list = True
185
  processed_lines.append(f'<li>{stripped[2:]}</li>')
186
  else:
187
- if in_list:
188
  processed_lines.append('</ul>')
189
- in_list = False
 
 
 
190
  processed_lines.append(line)
191
 
192
- if in_list:
193
  processed_lines.append('</ul>')
 
 
194
 
195
  content = ''.join(processed_lines)
196
 
197
  if msg["role"] == "user":
198
- html += f'<div style="background-color:#e0e0e0; padding:8px; border-radius:5px; margin-bottom:6px;"><b>You:</b> {content}</div>'
199
  else:
200
- html += f'<div style="padding:8px; margin-bottom:6px;"><b>Adrega AI:</b> {content}</div>'
201
 
202
- html += """
203
  </div>
204
  <script>
205
  const chatBox = document.getElementById("chat-box");
@@ -209,7 +243,7 @@ def render_chat():
209
  </script>
210
  """
211
 
212
- components.html(html, height=420, scrolling=False)
213
 
214
  # 🔹 Handle user input
215
  def handle_submit():
 
1
  import streamlit as st
2
  import streamlit.components.v1 as components
3
  import os
4
+ import html
5
+ import re
6
  from datasets import load_dataset
7
  from langchain_ollama import OllamaLLM
8
  from langchain_huggingface import HuggingFaceEmbeddings
 
110
 
111
  # 🔹 UI Styling
112
  st.markdown("""<style>
113
+ .scrollable-div {
114
+ font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif !important;
115
+ max-height: 400px;
116
+ overflow-y: auto;
117
+ padding: 10px;
118
+ border: 1px solid #ccc;
119
+ background-color: #f9f9f9;
120
+ }
121
  input[data-testid="stTextInput"] { cursor: pointer; }
122
  input[data-testid="stTextInput"]:focus { cursor: text; }
123
  </style>
 
158
  return "\n".join(context)
159
 
160
  def render_chat():
161
+
162
+ html_content = """
163
  <style>
164
  .scrollable-div {
165
+ font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif !important;
166
  max-height: 400px;
167
  overflow-y: auto;
168
  padding: 10px;
 
176
  for msg in st.session_state.chat_history:
177
  content = msg["content"]
178
 
179
+ # Decode HTML entities first
180
+ content = html.unescape(content)
181
+
182
  # Convert markdown to HTML
183
  content = content.replace('\n', '<br>') # Line breaks
184
  content = content.replace('**', '<b>', 1).replace('**', '</b>', 1)
185
  while '**' in content:
186
  content = content.replace('**', '<b>', 1).replace('**', '</b>', 1)
187
 
188
+ # Convert lists to HTML
189
  lines = content.split('<br>')
190
  processed_lines = []
191
+ in_ul_list = False
192
+ in_ol_list = False
193
 
194
  for line in lines:
195
  stripped = line.strip()
196
+ # Check for numbered lists (1. 2. etc.)
197
+ if re.match(r'^\d+\.\s+', stripped):
198
+ if in_ul_list:
199
+ processed_lines.append('</ul>')
200
+ in_ul_list = False
201
+ if not in_ol_list:
202
+ processed_lines.append('<ol>')
203
+ in_ol_list = True
204
+ item_text = re.sub(r'^\d+\.\s+', '', stripped)
205
+ processed_lines.append(f'<li>{item_text}</li>')
206
+ # Check for bullet lists
207
+ elif stripped.startswith('- ') or stripped.startswith('* '):
208
+ if in_ol_list:
209
+ processed_lines.append('</ol>')
210
+ in_ol_list = False
211
+ if not in_ul_list:
212
  processed_lines.append('<ul>')
213
+ in_ul_list = True
214
  processed_lines.append(f'<li>{stripped[2:]}</li>')
215
  else:
216
+ if in_ul_list:
217
  processed_lines.append('</ul>')
218
+ in_ul_list = False
219
+ if in_ol_list:
220
+ processed_lines.append('</ol>')
221
+ in_ol_list = False
222
  processed_lines.append(line)
223
 
224
+ if in_ul_list:
225
  processed_lines.append('</ul>')
226
+ if in_ol_list:
227
+ processed_lines.append('</ol>')
228
 
229
  content = ''.join(processed_lines)
230
 
231
  if msg["role"] == "user":
232
+ html_content += f'<div style="background-color:#e0e0e0; padding:8px; border-radius:5px; margin-bottom:6px;"><b>You:</b> {content}</div>'
233
  else:
234
+ html_content += f'<div style="padding:8px; margin-bottom:6px;"><b>Adrega AI:</b> {content}</div>'
235
 
236
+ html_content += """
237
  </div>
238
  <script>
239
  const chatBox = document.getElementById("chat-box");
 
243
  </script>
244
  """
245
 
246
+ components.html(html_content, height=420, scrolling=False)
247
 
248
  # 🔹 Handle user input
249
  def handle_submit():
styles.css CHANGED
@@ -1,3 +1,7 @@
 
 
 
 
1
  .st-emotion-cache-1w723zb {
2
  padding: 4rem !important; /* Adjust the padding value as needed */
3
  }
 
1
+ * {
2
+ font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif !important;
3
+ }
4
+
5
  .st-emotion-cache-1w723zb {
6
  padding: 4rem !important; /* Adjust the padding value as needed */
7
  }