yassonee commited on
Commit
13ecec7
·
verified ·
1 Parent(s): edcb3d3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -36
app.py CHANGED
@@ -3,33 +3,52 @@ from transformers import pipeline
3
  from PIL import Image, ImageDraw
4
  import numpy as np
5
  import colorsys
6
- import os
7
 
8
- # Configuration des en-têtes pour WebSocket
9
- os.environ['STREAMLIT_SERVER_WEBSOCKET_HEADERS'] = '{"Access-Control-Allow-Origin": "*"}'
10
- os.environ['STREAMLIT_SERVER_ENABLE_CORS'] = 'true'
11
-
12
- # Configuration de la page
13
  st.set_page_config(
14
  page_title="Fraktur Detektion",
15
  layout="wide",
16
  initial_sidebar_state="collapsed"
17
  )
18
 
19
- # Script pour gérer les WebSockets sur Edge
20
- st.markdown("""
21
- <script>
22
- if (window.WebSocket && navigator.userAgent.indexOf("Edge") > -1) {
23
- const originalWebSocket = window.WebSocket;
24
- window.WebSocket = function(url, protocols) {
25
- if (url.includes('_stcore/stream')) {
26
- url = url.replace('wss://', 'ws://');
27
- }
28
- return new originalWebSocket(url, protocols);
29
- };
30
- }
31
- </script>
32
- """, unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
  st.markdown("""
35
  <style>
@@ -118,27 +137,28 @@ st.markdown("""
118
  display: none !important;
119
  }
120
 
121
- /* Fix for WebSocket connection issues */
122
  iframe {
123
- visibility: hidden;
 
 
 
 
 
 
 
 
 
 
124
  }
125
- </style>
126
 
127
- <script>
128
- // Fix for WebSocket connection
129
- window.addEventListener('load', function() {
130
- setTimeout(function() {
131
- const frames = document.getElementsByTagName('iframe');
132
- for (let frame of frames) {
133
- frame.style.visibility = 'visible';
134
- }
135
- }, 1000);
136
- });
137
- </script>
138
  """, unsafe_allow_html=True)
139
 
140
- # Configuration du cache pour les modèles
141
- @st.cache_resource(show_spinner=False)
142
  def load_models():
143
  return {
144
  "KnochenAuge": pipeline("object-detection", model="D3STRON/bone-fracture-detr"),
 
3
  from PIL import Image, ImageDraw
4
  import numpy as np
5
  import colorsys
 
6
 
7
+ # Configuration pour HuggingFace Spaces
 
 
 
 
8
  st.set_page_config(
9
  page_title="Fraktur Detektion",
10
  layout="wide",
11
  initial_sidebar_state="collapsed"
12
  )
13
 
14
+ # Script pour gérer les WebSockets
15
+ st.components.v1.html("""
16
+ <script>
17
+ // Fonction pour corriger la connexion WebSocket
18
+ function fixWebSocketConnection() {
19
+ const originalWebSocket = window.WebSocket;
20
+ window.WebSocket = function(url, protocols) {
21
+ if (url.includes('_stcore/stream')) {
22
+ const newUrl = new URL(url);
23
+ newUrl.pathname = '/_stcore/stream';
24
+ url = newUrl.toString();
25
+ }
26
+ return new originalWebSocket(url, protocols);
27
+ };
28
+ }
29
+
30
+ // Configuration du WebSocket pour Edge et autres navigateurs
31
+ if (window.WebSocket) {
32
+ fixWebSocketConnection();
33
+
34
+ // Gérer la reconnexion en cas d'erreur
35
+ window.addEventListener('load', function() {
36
+ const maxRetries = 5;
37
+ let retryCount = 0;
38
+
39
+ function tryConnection() {
40
+ if (retryCount < maxRetries) {
41
+ fixWebSocketConnection();
42
+ retryCount++;
43
+ setTimeout(tryConnection, 2000);
44
+ }
45
+ }
46
+
47
+ tryConnection();
48
+ });
49
+ }
50
+ </script>
51
+ """, height=0)
52
 
53
  st.markdown("""
54
  <style>
 
137
  display: none !important;
138
  }
139
 
140
+ /* Fix pour les iframes et WebSocket */
141
  iframe {
142
+ display: block !important;
143
+ visibility: visible !important;
144
+ opacity: 1 !important;
145
+ }
146
+
147
+ .st-emotion-cache-1yiq2ps {
148
+ overflow: visible !important;
149
+ }
150
+
151
+ .st-emotion-cache-1dp5vir {
152
+ display: none !important;
153
  }
 
154
 
155
+ .st-emotion-cache-r421ms {
156
+ z-index: 999999 !important;
157
+ }
158
+ </style>
 
 
 
 
 
 
 
159
  """, unsafe_allow_html=True)
160
 
161
+ @st.cache_resource
 
162
  def load_models():
163
  return {
164
  "KnochenAuge": pipeline("object-detection", model="D3STRON/bone-fracture-detr"),