Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,6 @@ import gradio as gr # type: ignore
|
|
| 2 |
import requests # type: ignore
|
| 3 |
import io
|
| 4 |
import random
|
| 5 |
-
import os
|
| 6 |
import time
|
| 7 |
from PIL import Image
|
| 8 |
import json
|
|
@@ -41,13 +40,11 @@ def query_with_auto_routing(prompt, model, custom_lora, is_negative=False, steps
|
|
| 41 |
if oauth_token is not None and getattr(oauth_token, "token", None):
|
| 42 |
api_key = oauth_token.token
|
| 43 |
print("Using OAuth token from signed-in user for inference.")
|
| 44 |
-
else:
|
| 45 |
-
api_key = os.getenv("HF_READ_TOKEN")
|
| 46 |
-
if api_key:
|
| 47 |
-
print("Using HF_READ_TOKEN environment variable for inference.")
|
| 48 |
|
| 49 |
if not api_key:
|
| 50 |
-
raise gr.Error(
|
|
|
|
|
|
|
| 51 |
|
| 52 |
# Initialize client with automatic provider selection (default is "auto")
|
| 53 |
client = InferenceClient(token=api_key)
|
|
@@ -76,10 +73,34 @@ def query_with_auto_routing(prompt, model, custom_lora, is_negative=False, steps
|
|
| 76 |
|
| 77 |
print(f'Generation {key} completed with automatic routing!')
|
| 78 |
return image
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
except Exception as e:
|
| 81 |
print(f"Error with automatic routing: {e}")
|
| 82 |
-
raise gr.Error(f"Failed to generate image: {str(e)}")
|
| 83 |
|
| 84 |
def get_model_id_from_name(model_name):
|
| 85 |
"""
|
|
|
|
| 2 |
import requests # type: ignore
|
| 3 |
import io
|
| 4 |
import random
|
|
|
|
| 5 |
import time
|
| 6 |
from PIL import Image
|
| 7 |
import json
|
|
|
|
| 40 |
if oauth_token is not None and getattr(oauth_token, "token", None):
|
| 41 |
api_key = oauth_token.token
|
| 42 |
print("Using OAuth token from signed-in user for inference.")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
if not api_key:
|
| 45 |
+
raise gr.Error(
|
| 46 |
+
"No Hugging Face session detected. Please sign in with your Hugging Face account before running generation."
|
| 47 |
+
)
|
| 48 |
|
| 49 |
# Initialize client with automatic provider selection (default is "auto")
|
| 50 |
client = InferenceClient(token=api_key)
|
|
|
|
| 73 |
|
| 74 |
print(f'Generation {key} completed with automatic routing!')
|
| 75 |
return image
|
| 76 |
+
|
| 77 |
+
except HfHubHTTPError as e:
|
| 78 |
+
status = getattr(e.response, "status_code", None)
|
| 79 |
+
detail_hint = None
|
| 80 |
+
if status == 401:
|
| 81 |
+
detail_hint = (
|
| 82 |
+
"Your Hugging Face session is missing the 'Make calls to Inference Providers' permission. "
|
| 83 |
+
"Generate a fine-grained token with that permission in your Hugging Face settings and "
|
| 84 |
+
"re-authorize the Space so the updated token can be used."
|
| 85 |
+
)
|
| 86 |
+
elif status == 403:
|
| 87 |
+
detail_hint = (
|
| 88 |
+
"The signed-in account did not grant the 'inference-api' scope. Sign out, then sign back in "
|
| 89 |
+
"and approve the requested permissions so your token can access Inference Providers."
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
print(f"Error with automatic routing: {e}")
|
| 93 |
+
if detail_hint:
|
| 94 |
+
raise gr.Error(
|
| 95 |
+
"Failed to generate image: "
|
| 96 |
+
f"{e}.\n\nTroubleshooting tip: {detail_hint}"
|
| 97 |
+
) from e
|
| 98 |
+
|
| 99 |
+
raise gr.Error(f"Failed to generate image: {str(e)}") from e
|
| 100 |
+
|
| 101 |
except Exception as e:
|
| 102 |
print(f"Error with automatic routing: {e}")
|
| 103 |
+
raise gr.Error(f"Failed to generate image: {str(e)}") from e
|
| 104 |
|
| 105 |
def get_model_id_from_name(model_name):
|
| 106 |
"""
|