WhiteWolf21 commited on
Commit
d8a9cf1
·
1 Parent(s): 69e68df

Update: Live Video

Browse files
Files changed (1) hide show
  1. app.py +194 -11
app.py CHANGED
@@ -7,8 +7,8 @@ from openai import OpenAI
7
  import copy
8
  import time
9
  import io
10
-
11
- client = OpenAI(api_key='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')
12
 
13
  st.set_page_config(
14
  page_title="Image to Analyze",
@@ -17,6 +17,194 @@ st.set_page_config(
17
  initial_sidebar_state="expanded",
18
  )
19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  col1, col2 = st.columns(2)
21
 
22
  wl1, wl2 = st.columns(2)
@@ -106,9 +294,8 @@ def generate_description(image_base64):
106
  "role": "user",
107
  "content": [
108
  {"type": "text", "text": """Please answer at the following format:
109
- Wafer Defection Type: <put the defect type here only>
110
- Description: <Analyze how the defect type occur>
111
- Solution: <Suggest solution to solve this in the future>"""},
112
  {
113
  "type": "image_url",
114
  "image_url": {
@@ -133,11 +320,7 @@ def update_df():
133
  #Creating title for Streamlit app
134
  st.title("Wafer Defect Detection with LLM Classification and Analyze")
135
 
136
- with col1:
137
- st.components.v1.iframe("http://localhost:7456/", width=None, height=500, scrolling=False)
138
-
139
- with col2:
140
- st.components.v1.iframe("http://localhost:7457/", width=None, height=500, scrolling=False)
141
 
142
  default_img = ""
143
  st.session_state.buttondo = False
@@ -148,7 +331,7 @@ with st.sidebar:
148
 
149
  genre = st.radio(
150
  "Baseline",
151
- ["Propose Solution", "Baseline 1", "Baseline 0.2", "Baseline 0.1"])
152
 
153
  Center = st.checkbox('Center')
154
  if Center:
 
7
  import copy
8
  import time
9
  import io
10
+ import uuid
11
+ import html
12
 
13
  st.set_page_config(
14
  page_title="Image to Analyze",
 
17
  initial_sidebar_state="expanded",
18
  )
19
 
20
+ def inject_js_code(source: str) -> None:
21
+ div_id = uuid.uuid4()
22
+
23
+ st.markdown(
24
+ f"""
25
+ <div style="display:none" id="{div_id}">
26
+ <iframe src="javascript: \
27
+ var script = document.createElement('script'); \
28
+ script.type = 'text/javascript'; \
29
+ script.text = {html.escape(repr(source))}; \
30
+ var div = window.parent.document.getElementById('{div_id}'); \
31
+ div.appendChild(script); \
32
+ div.parentElement.parentElement.parentElement.style.display = 'none'; \
33
+ "/>
34
+ </div>
35
+ """,
36
+ unsafe_allow_html=True,
37
+ )
38
+
39
+ def screenshot_window() -> None:
40
+ # JS Code to be executed
41
+ source = """
42
+ // Function to detect if the current browser is Chrome
43
+ const isChrome = () => /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);
44
+
45
+ /*
46
+ const button = document.getElementById('reportButton');
47
+ button.addEventListener('click', function() {
48
+ // Alert and exit if the browser is Chrome
49
+ if (isChrome()) {
50
+ //alert("Currently this function is available only on Firefox!");
51
+ //button.style.display = 'none'; // Hides the button
52
+ //return;
53
+ }
54
+
55
+ // Load a script dynamically and execute a callback after loading
56
+ const loadScript = (url, isLoaded, callback) => {
57
+ if (!isLoaded()) {
58
+ const script = document.createElement('script');
59
+ script.type = 'text/javascript';
60
+ script.onload = callback;
61
+ script.src = url;
62
+ document.head.appendChild(script);
63
+ } else {
64
+ callback();
65
+ }
66
+ };
67
+
68
+ // Check if html2canvas library is loaded
69
+ const isHtml2CanvasLoaded = () => typeof html2canvas !== 'undefined';
70
+
71
+ // Capture an individual iframe and call a callback with the result
72
+ const captureIframe = (iframe, callback) => {
73
+ try {
74
+ const iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
75
+ console.log(iframeDoc)
76
+ html2canvas(iframeDoc.body, {
77
+ scale: 1,
78
+ logging: true,
79
+ useCORS: true,
80
+ allowTaint: true
81
+ }).then(canvas => {
82
+ callback(canvas ? canvas : null);
83
+ }).catch(error => {
84
+ console.error('Could not capture iframe:', error);
85
+ callback(null);
86
+ });
87
+ } catch (error) {
88
+ console.error('Could not access iframe:', error);
89
+ callback(null);
90
+ }
91
+ };
92
+
93
+ // Main function to capture all windows
94
+ const captureAllWindows = () => {
95
+ const streamlitDoc = window.parent.document;
96
+ const stApp = streamlitDoc.querySelector('.main > .block-container');
97
+ const iframes = Array.from(stApp.querySelectorAll('iframe'));
98
+ let capturedImages = [];
99
+
100
+ // Process each iframe sequentially
101
+ const processIframes = (index = 0) => {
102
+ if (index < iframes.length) {
103
+ captureIframe(iframes[index], function(canvas) {
104
+ if (canvas) {
105
+ const img = document.createElement('img');
106
+ img.src = canvas.toDataURL('image/png');
107
+ capturedImages.push({iframe: iframes[index], img: img});
108
+ } else {
109
+ console.error('Skipping an iframe due to capture failure.');
110
+ }
111
+ processIframes(index + 1);
112
+ });
113
+ } else {
114
+ // Capture the main app window after processing all iframes
115
+ html2canvas(stApp, {
116
+ onclone: function(clonedDocument) {
117
+ const clonedIframes = Array.from(clonedDocument.querySelectorAll('iframe'));
118
+ capturedImages.forEach(({img}, index) => {
119
+ if (index < clonedIframes.length) {
120
+ const clonedIframe = clonedIframes[index];
121
+ clonedIframe.parentNode.replaceChild(img, clonedIframe);
122
+ }
123
+ });
124
+ },
125
+ scale: 1,
126
+ logging: true,
127
+ useCORS: true,
128
+ allowTaint: true,
129
+ ignoreElements: () => {}
130
+ }).then(finalCanvas => {
131
+ // Create a download link for the captured image
132
+ finalCanvas.toBlob(blob => {
133
+ const url = window.URL.createObjectURL(blob);
134
+ var link = document.createElement('a');
135
+ link.style.display = 'none';
136
+ link.href = url;
137
+ link.download = 'screenshot.png';
138
+ document.body.appendChild(link);
139
+ link.click();
140
+ document.body.removeChild(link);
141
+ window.URL.revokeObjectURL(url);
142
+ });
143
+ }).catch(error => {
144
+ console.error('Screenshot capture failed:', error);
145
+ });
146
+ }
147
+ };
148
+
149
+ processIframes();
150
+ };
151
+
152
+ loadScript(
153
+ 'https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.3.2/html2canvas.min.js',
154
+ isHtml2CanvasLoaded,
155
+ captureAllWindows
156
+ );
157
+
158
+ });
159
+ */
160
+ """
161
+
162
+ inject_js_code(source=source)
163
+
164
+ def add_reportgen_button():
165
+ st.markdown(
166
+ """
167
+ <!-- <button id="reportButton" class="st-style-button">Generate Page Report</button> -->
168
+
169
+ <style>
170
+ .st-style-button {
171
+ display: inline-flex;
172
+ -webkit-box-align: center;
173
+ align-items: center;
174
+ -webkit-box-pack: center;
175
+ justify-content: center;
176
+ font-weight: 400;
177
+ padding: 0.25rem 0.75rem;
178
+ border-radius: 0.5rem;
179
+ min-height: 38.4px;
180
+ margin: 0px;
181
+ line-height: 1.6;
182
+ color: inherit;
183
+ width: auto;
184
+ user-select: none;
185
+ background-color: white; /* Set a white background */
186
+ border: 1px solid rgba(49, 51, 63, 0.2);
187
+ outline: none; !important
188
+ box-shadow: none !important;
189
+ }
190
+
191
+ /* Change background on mouse-over */
192
+ .st-style-button:hover {
193
+ background-color: white;
194
+ color: #0A04D2;
195
+ border: 1px solid #0A04D2;
196
+ }
197
+
198
+ </style>
199
+ """,
200
+ unsafe_allow_html=True,
201
+ )
202
+ screenshot_window()
203
+
204
+ add_reportgen_button()
205
+
206
+ client = OpenAI(api_key='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')
207
+
208
  col1, col2 = st.columns(2)
209
 
210
  wl1, wl2 = st.columns(2)
 
294
  "role": "user",
295
  "content": [
296
  {"type": "text", "text": """Please answer at the following format:
297
+ Defect
298
+ Description: <Analyze how the wafer defect type in the image>"""},
 
299
  {
300
  "type": "image_url",
301
  "image_url": {
 
320
  #Creating title for Streamlit app
321
  st.title("Wafer Defect Detection with LLM Classification and Analyze")
322
 
323
+ st.components.v1.iframe("https://whitewolf21.github.io/live/", width=None, height=750, scrolling=False)
 
 
 
 
324
 
325
  default_img = ""
326
  st.session_state.buttondo = False
 
331
 
332
  genre = st.radio(
333
  "Baseline",
334
+ ["Propose Solution", "Baseline 1", "Baseline 2", "Baseline 3"])
335
 
336
  Center = st.checkbox('Center')
337
  if Center: