Update demo.py
Browse files
demo.py
CHANGED
@@ -5,47 +5,7 @@ import os
|
|
5 |
import re
|
6 |
from gradio_client import Client, file
|
7 |
|
8 |
-
|
9 |
-
# Try connecting using environment variables first
|
10 |
-
client_url = "http://127.0.0.1:7860" # Default if no env var
|
11 |
-
try:
|
12 |
-
# Use 'src' if defined (common for HF Spaces)
|
13 |
-
if 'src' in os.environ:
|
14 |
-
client_url = os.environ['src']
|
15 |
-
print(f"Connecting to client URL (src): {client_url}")
|
16 |
-
client = Client(client_url)
|
17 |
-
# Fallback to host/key if src isn't defined but host/key are
|
18 |
-
elif 'host' in os.environ and 'key' in os.environ:
|
19 |
-
client_url = os.environ['host']
|
20 |
-
api_key = os.environ['key']
|
21 |
-
print(f"Connecting to client URL (host): {client_url} using API key.")
|
22 |
-
# Note: gradio_client might expect hf_token for private spaces
|
23 |
-
client = Client(client_url, hf_token=api_key) # Assuming key is hf_token
|
24 |
-
# Fallback to just host if only host is defined
|
25 |
-
elif 'host' in os.environ:
|
26 |
-
client_url = os.environ['host']
|
27 |
-
print(f"Connecting to client URL (host): {client_url} (public/no key)")
|
28 |
-
client = Client(client_url)
|
29 |
-
# Fallback to the hardcoded default if no relevant env vars found
|
30 |
-
else:
|
31 |
-
print(f"No suitable environment variables (src, host/key, host) found.")
|
32 |
-
print(f"Attempting connection to default URL: {client_url}")
|
33 |
-
client = Client(client_url) # Use the default
|
34 |
-
|
35 |
-
print("Gradio Client connected successfully.")
|
36 |
-
# Optional: Check API endpoints
|
37 |
-
# print(client.view_api(print_info=True))
|
38 |
-
except Exception as e:
|
39 |
-
print(f"Error connecting Gradio Client to {client_url}: {e}")
|
40 |
-
print("Please ensure the source Gradio app is running and the URL/credentials are correct.")
|
41 |
-
# Provide a dummy client to prevent crashes
|
42 |
-
class DummyClient:
|
43 |
-
def predict(*args, **kwargs):
|
44 |
-
print("!!! Gradio Client not connected. Prediction will fail. !!!")
|
45 |
-
import numpy as np
|
46 |
-
return (44100, np.zeros(1)) # Sample rate, empty numpy array
|
47 |
-
client = DummyClient()
|
48 |
-
|
49 |
|
50 |
# --- UI Data Loading (Client-Side) ---
|
51 |
BASE_PATH = "Inference"
|
|
|
5 |
import re
|
6 |
from gradio_client import Client, file
|
7 |
|
8 |
+
client = Client(os.environ['src'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
# --- UI Data Loading (Client-Side) ---
|
11 |
BASE_PATH = "Inference"
|