boffire commited on
Commit
36e285c
·
verified ·
1 Parent(s): 41a4e04

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -2
app.py CHANGED
@@ -3,6 +3,8 @@ from huggingface_hub import hf_hub_download
3
  import regex
4
  import gradio as gr
5
  import os
 
 
6
 
7
  # Constants
8
  MAX_INPUT_LENGTH = 10000 # Maximum characters allowed
@@ -70,6 +72,19 @@ def predict_language(text, top_k=3, threshold=0.5):
70
 
71
  return "\n\n".join(results)
72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
  # Create Gradio interface
74
  with gr.Blocks(title="OpenLID-v3 Language Identification") as demo:
75
  # Use HTML with target="_blank" to open in new tab
@@ -113,7 +128,7 @@ with gr.Blocks(title="OpenLID-v3 Language Identification") as demo:
113
  gr.Examples(
114
  examples=[
115
  ["Asebter-a yura s wudem awurman d amagrad s tutlayt taqbaylit."],
116
- ["Linterès es dutilizar un sistèma liure, personalizable e en occitan."],
117
  ["Maskinsjefen er oppteken av å løfta fram dei maritime utdanningane."],
118
  ["The quick brown fox jumps over the lazy dog."],
119
  ["Le renard brun rapide saute par-dessus le chien paresseux."],
@@ -148,4 +163,18 @@ with gr.Blocks(title="OpenLID-v3 Language Identification") as demo:
148
  )
149
 
150
  if __name__ == "__main__":
151
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  import regex
4
  import gradio as gr
5
  import os
6
+ import asyncio
7
+ import atexit
8
 
9
  # Constants
10
  MAX_INPUT_LENGTH = 10000 # Maximum characters allowed
 
72
 
73
  return "\n\n".join(results)
74
 
75
+ # Cleanup function to prevent async errors on shutdown
76
+ def cleanup():
77
+ try:
78
+ loop = asyncio.get_event_loop()
79
+ if loop.is_running():
80
+ loop.stop()
81
+ if not loop.is_closed():
82
+ loop.close()
83
+ except Exception:
84
+ pass
85
+
86
+ atexit.register(cleanup)
87
+
88
  # Create Gradio interface
89
  with gr.Blocks(title="OpenLID-v3 Language Identification") as demo:
90
  # Use HTML with target="_blank" to open in new tab
 
128
  gr.Examples(
129
  examples=[
130
  ["Asebter-a yura s wudem awurman d amagrad s tutlayt taqbaylit."],
131
+ ["L'interès es d'utilizar un sistèma liure, personalizable e en occitan."],
132
  ["Maskinsjefen er oppteken av å løfta fram dei maritime utdanningane."],
133
  ["The quick brown fox jumps over the lazy dog."],
134
  ["Le renard brun rapide saute par-dessus le chien paresseux."],
 
163
  )
164
 
165
  if __name__ == "__main__":
166
+ # Get port from environment (HF Spaces sets this)
167
+ port = int(os.environ.get("PORT", 7860))
168
+
169
+ try:
170
+ demo.launch(
171
+ server_name="0.0.0.0",
172
+ server_port=port,
173
+ ssr_mode=False, # Disable experimental SSR to prevent the error
174
+ share=False,
175
+ show_error=True
176
+ )
177
+ except KeyboardInterrupt:
178
+ print("\nShutting down gracefully...")
179
+ finally:
180
+ cleanup()