Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,10 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
from huggingface_hub.utils import RepositoryNotFoundError, HfHubHTTPError
|
| 5 |
|
| 6 |
# Get the secret model ID
|
|
@@ -8,19 +12,33 @@ MODEL_ID = os.getenv("HF_MODEL_ID")
|
|
| 8 |
if not MODEL_ID:
|
| 9 |
raise RuntimeError("Secret HF_MODEL_ID not set in environment!")
|
| 10 |
|
| 11 |
-
# Create API instance
|
| 12 |
-
api = HfApi()
|
| 13 |
-
|
| 14 |
# Function to generate random number using the model
|
| 15 |
def generate_random_number():
|
| 16 |
try:
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
except (RepositoryNotFoundError, HfHubHTTPError) as e:
|
| 25 |
return f"Error accessing model: {str(e)}"
|
| 26 |
except Exception as e:
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
import random
|
| 4 |
+
import tempfile
|
| 5 |
+
import importlib.util
|
| 6 |
+
import sys
|
| 7 |
+
from huggingface_hub import snapshot_download, HfApi
|
| 8 |
from huggingface_hub.utils import RepositoryNotFoundError, HfHubHTTPError
|
| 9 |
|
| 10 |
# Get the secret model ID
|
|
|
|
| 12 |
if not MODEL_ID:
|
| 13 |
raise RuntimeError("Secret HF_MODEL_ID not set in environment!")
|
| 14 |
|
|
|
|
|
|
|
|
|
|
| 15 |
# Function to generate random number using the model
|
| 16 |
def generate_random_number():
|
| 17 |
try:
|
| 18 |
+
# Download the model files to a temporary directory
|
| 19 |
+
with tempfile.TemporaryDirectory() as temp_dir:
|
| 20 |
+
# Download the private repo (requires authentication)
|
| 21 |
+
model_path = snapshot_download(
|
| 22 |
+
repo_id=MODEL_ID,
|
| 23 |
+
token=os.getenv("HF_TOKEN"), # You'll need to add this secret too
|
| 24 |
+
local_dir=temp_dir
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
# Dynamically import the model module
|
| 28 |
+
sys.path.insert(0, model_path)
|
| 29 |
+
|
| 30 |
+
# Now we can import from the downloaded model
|
| 31 |
+
from model import RandomNumberGenerator
|
| 32 |
+
|
| 33 |
+
# Use the model to generate a random number
|
| 34 |
+
generator = RandomNumberGenerator()
|
| 35 |
+
number = generator.generate()
|
| 36 |
+
|
| 37 |
+
# Clean up path
|
| 38 |
+
sys.path.remove(model_path)
|
| 39 |
+
|
| 40 |
+
return f"Generated random number: {number}"
|
| 41 |
+
|
| 42 |
except (RepositoryNotFoundError, HfHubHTTPError) as e:
|
| 43 |
return f"Error accessing model: {str(e)}"
|
| 44 |
except Exception as e:
|