georad commited on
Commit
4981a6e
·
verified ·
1 Parent(s): e3604d6

Update pages/type_text.py

Browse files
Files changed (1) hide show
  1. pages/type_text.py +2 -123
pages/type_text.py CHANGED
@@ -1,6 +1,5 @@
1
  import streamlit as st
2
  import streamlit.components.v1 as components
3
- from streamlit_shortcuts import button, add_keyboard_shortcuts
4
  import pandas as pd
5
  from io import StringIO
6
  import json
@@ -9,7 +8,6 @@ from transformers import pipeline # AutoTokenizer, AutoModelForCausalLM, AutoMod
9
  from sentence_transformers import SentenceTransformer, util
10
  import time
11
  import os
12
- import base64
13
 
14
  os.getenv("HF_TOKEN")
15
 
@@ -24,122 +22,6 @@ def make_spinner(text = "In progress..."):
24
  with st.spinner(text):
25
  yield
26
 
27
-
28
- def scrollToBottom():
29
- # JavaScript to scroll to bottom
30
- JS_code = """
31
- <script>
32
- // Wrap the code in an Immediately Invoked Function Expression (IIFE)
33
- // to create a private scope and potentially avoid conflicts.
34
- (function() {
35
- // Function to scroll to the bottom of a specific container
36
- function scrollToBottom() {
37
- console.log("Attempting to scroll container to bottom."); // Log when scroll function is called
38
-
39
- // --- Identify the scrollable container ---
40
- // Streamlit often uses a main container div. We'll try to find it.
41
- // YOU NEED TO INSPECT YOUR DEPLOYED APP'S HTML TO FIND THE CORRECT SELECTOR
42
- // FOR THE ELEMENT THAT ACTUALLY HAS THE SCROLLBAR.
43
- //
44
- // Common candidates based on Streamlit versions and structure:
45
- // - '.stAppViewContainer'
46
- // - '.main'
47
- // - '.block-container'
48
- // - Look for divs with class names starting with 'st-emotion-cache-' followed by random characters.
49
- // - A div directly containing your dynamic content (check its class or ID)
50
-
51
- // *** REPLACE THE SELECTOR BELOW with the correct one you found by inspecting your app's HTML ***
52
- // Use document.querySelector('.your-class-name') for a class, or document.getElementById('your-id') for an ID.
53
- const scrollableContainer = document.querySelector('.stMainBlockContainer'); // <--- REPLACE THIS ENTIRE LINE IF USING getElementById
54
- // const scrollableContainer = document.getElementById('stMainBlockContainer'); // <--- REPLACE THIS ENTIRE LINE IF USING querySelector
55
-
56
- if (scrollableContainer) {
57
- console.log("Scrollable container found. Scrolling...");
58
- // Scroll the found container to its bottom
59
- scrollableContainer.scrollTo({
60
- top: scrollableContainer.scrollHeight,
61
- behavior: 'smooth'
62
- });
63
- } else {
64
- console.warn("Scrollable container not found using the specified selector. Please inspect your app's HTML to find the correct class or ID.");
65
- // Fallback to body scroll (less likely to work for Streamlit's main content area)
66
- window.scrollTo({
67
- top: document.body.scrollHeight,
68
- behavior: 'smooth'
69
- });
70
- }
71
- }
72
-
73
- // --- Logic for handling dynamically added content ---
74
-
75
- // Create a MutationObserver instance.
76
- // This observer will watch for changes in the DOM.
77
- const observer = new MutationObserver(function(mutations) {
78
- console.log("MutationObserver detected changes."); // Log when observer fires
79
- // Use requestAnimationFrame to ensure the scroll happens after the browser
80
- // has potentially rendered the new content.
81
- requestAnimationFrame(scrollToBottom);
82
- });
83
-
84
- // Configure the observer to watch for changes to the child list of the body element.
85
- // We keep observing the body because Streamlit adds content there,
86
- // even if a child element is the one that scrolls.
87
- const config = { childList: true, subtree: true };
88
-
89
- console.log("Starting MutationObserver on document.body."); // Log when observer starts
90
- // Start observing the document body for the configured mutations.
91
- observer.observe(document.body, config);
92
-
93
- // --- Initial scroll on page load ---
94
- window.addEventListener('load', function() {
95
- console.log("Window loaded. Initial scroll attempt."); // Log on window load
96
- requestAnimationFrame(scrollToBottom);
97
- });
98
-
99
- })(); // End of IIFE
100
-
101
- </script>
102
- """
103
- # Render the JavaScript code
104
- html = f'<div style="display:none">{JS_code}</div>'
105
- st.components.v1.html(html, height=0, width=0)
106
-
107
-
108
- def auto_scroll_to_bottom():
109
- # JavaScript to scroll to bottom
110
- js_code = """
111
- <script>
112
- // Wait for the page to fully render
113
- const scrollToBottom = () => {
114
- // Get the main Streamlit iframe
115
- const streamlitDoc = window.parent.document;
116
- // Get the app container
117
- const appContainer = streamlitDoc.querySelector('/main');
118
- if (appContainer) {
119
- // Scroll the app container to the bottom
120
- appContainer.scrollTop = appContainer.scrollHeight;
121
- } else {
122
- // Fallback to scrolling the entire page
123
- window.parent.scrollTo(0, streamlitDoc.body.scrollHeight);
124
- }
125
- };
126
-
127
- // Try immediately
128
- scrollToBottom();
129
-
130
- // Also try after a short delay to ensure content is rendered
131
- setTimeout(scrollToBottom, 200);
132
-
133
- // And after a longer delay just to be safe
134
- setTimeout(scrollToBottom, 500);
135
- </script>
136
- """
137
-
138
- # Render the JavaScript code
139
- html = f'<div style="display:none">{js_code}</div>'
140
- st.components.v1.html(html, height=0)
141
-
142
-
143
  #@st.cache
144
  def convert_df(df:pd.DataFrame):
145
  return df.to_csv(index=False).encode('utf-8')
@@ -294,9 +176,7 @@ if INTdesc_input is not None and st.button(":blue[Map to SBS codes]", key="run_s
294
  dfALL = pd.concat([dfALL, pd.DataFrame([dictA])], ignore_index=True)
295
 
296
  st.dataframe(data=dfALL, hide_index=True)
297
- #add_keyboard_shortcuts({'Ctrl+End': 'Map to SBS codes',})
298
- scrollToBottom()
299
- #auto_scroll_to_bottom()
300
 
301
  display_format = "ask REASONING MODEL: Which, if any, of the following SBS descriptions corresponds best to " + INTdesc_input +"? "
302
  #st.write(display_format)
@@ -320,8 +200,7 @@ if INTdesc_input is not None and st.button(":blue[Map to SBS codes]", key="run_s
320
  max_new_tokens=256,
321
  )
322
  st.write(outputs[0]["generated_text"][-1]["content"])
323
- scrollToBottom()
324
- #auto_scroll_to_bottom()
325
 
326
  bs, b1, b2, b3, bLast = st.columns([0.75, 1.5, 1.5, 1.5, 0.75])
327
  with b1:
 
1
  import streamlit as st
2
  import streamlit.components.v1 as components
 
3
  import pandas as pd
4
  from io import StringIO
5
  import json
 
8
  from sentence_transformers import SentenceTransformer, util
9
  import time
10
  import os
 
11
 
12
  os.getenv("HF_TOKEN")
13
 
 
22
  with st.spinner(text):
23
  yield
24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  #@st.cache
26
  def convert_df(df:pd.DataFrame):
27
  return df.to_csv(index=False).encode('utf-8')
 
176
  dfALL = pd.concat([dfALL, pd.DataFrame([dictA])], ignore_index=True)
177
 
178
  st.dataframe(data=dfALL, hide_index=True)
179
+ components.html(scroll_script, height=0, width=0)
 
 
180
 
181
  display_format = "ask REASONING MODEL: Which, if any, of the following SBS descriptions corresponds best to " + INTdesc_input +"? "
182
  #st.write(display_format)
 
200
  max_new_tokens=256,
201
  )
202
  st.write(outputs[0]["generated_text"][-1]["content"])
203
+ #components.html(scroll_script, height=0, width=0)
 
204
 
205
  bs, b1, b2, b3, bLast = st.columns([0.75, 1.5, 1.5, 1.5, 0.75])
206
  with b1: