davanstrien HF Staff commited on
Commit
d55d2ce
·
1 Parent(s): 4d1d929

Refactor server initialization to configure public URL based on environment variable

Browse files
Files changed (1) hide show
  1. serve.py +24 -2
serve.py CHANGED
@@ -1,4 +1,26 @@
1
  from textual_serve.server import Server
 
2
 
3
- server = Server("python dictionary.py")
4
- server.serve(debug=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  from textual_serve.server import Server
2
+ import os
3
 
4
+ # Configure server with host, port, and public_url
5
+ # For local testing, use localhost. For HF Spaces, use the Space URL
6
+ space_host = os.environ.get("SPACE_HOST")
7
+ if space_host:
8
+ # HF Spaces provides the domain without protocol, so add https://
9
+ if not space_host.startswith(("http://", "https://")):
10
+ public_url = f"https://{space_host}"
11
+ else:
12
+ public_url = space_host
13
+ else:
14
+ # Default for local testing
15
+ public_url = "http://localhost:7860"
16
+
17
+ print(f"Using public_url: {public_url}")
18
+
19
+ server = Server(
20
+ "python dictionary.py",
21
+ host="0.0.0.0",
22
+ port=7860,
23
+ public_url=public_url, # This fixes the WebSocket URL issue
24
+ title="Textual Demo",
25
+ )
26
+ server.serve()