Use HF Inference Providers
Browse files
app.py
CHANGED
|
@@ -17,6 +17,11 @@ from urllib.request import Request, urlopen
|
|
| 17 |
import gradio as gr
|
| 18 |
from PIL import Image, ImageDraw, ImageEnhance, ImageFilter, ImageFont
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
STARTER_HTML = """<!DOCTYPE html>
|
| 22 |
<html>
|
|
@@ -99,9 +104,8 @@ class StylePlan:
|
|
| 99 |
|
| 100 |
HF_TOKEN = os.environ.get("HF_TOKEN", "")
|
| 101 |
HF_IMAGE_MODEL = os.environ.get("HF_IMAGE_MODEL", "black-forest-labs/FLUX.1-schnell")
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
HF_PROMPT_ENDPOINT = f"https://api-inference.huggingface.co/models/{HF_PROMPT_MODEL}"
|
| 105 |
|
| 106 |
|
| 107 |
def slugify(value: str) -> str:
|
|
@@ -277,16 +281,16 @@ def hf_prompt_json(html_code: str, role_lines: list[tuple[str, str]], style_hint
|
|
| 277 |
f"Asset roles:\n{role_block}\n\n"
|
| 278 |
"Return JSON only."
|
| 279 |
)
|
| 280 |
-
prompt = f"<|im_start|>system\n{instruction}<|im_end|>\n<|im_start|>user\n{user_text}<|im_end|>\n<|im_start|>assistant\n"
|
| 281 |
payload = {
|
| 282 |
-
"
|
| 283 |
-
"
|
| 284 |
-
"
|
| 285 |
-
"
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
"
|
|
|
|
| 290 |
}
|
| 291 |
request = Request(
|
| 292 |
HF_PROMPT_ENDPOINT,
|
|
@@ -301,7 +305,10 @@ def hf_prompt_json(html_code: str, role_lines: list[tuple[str, str]], style_hint
|
|
| 301 |
with urlopen(request, timeout=90) as response:
|
| 302 |
raw = response.read().decode("utf-8", errors="replace")
|
| 303 |
parsed = json.loads(raw)
|
| 304 |
-
if isinstance(parsed,
|
|
|
|
|
|
|
|
|
|
| 305 |
text = parsed[0].get("generated_text", "") if isinstance(parsed[0], dict) else str(parsed[0])
|
| 306 |
elif isinstance(parsed, dict):
|
| 307 |
text = parsed.get("generated_text", parsed.get("text", ""))
|
|
@@ -974,36 +981,23 @@ def placeholder_png_bytes(role: str, width: int, height: int) -> bytes:
|
|
| 974 |
def hf_image_png(spec: AssetSpec, index: int, run_id: int) -> tuple[bytes | None, str | None]:
|
| 975 |
if not HF_TOKEN:
|
| 976 |
return None, "HF_TOKEN is not visible to the Space runtime"
|
|
|
|
|
|
|
| 977 |
|
| 978 |
-
payload = {
|
| 979 |
-
"inputs": spec.prompt,
|
| 980 |
-
"parameters": {
|
| 981 |
-
"width": spec.width,
|
| 982 |
-
"height": spec.height,
|
| 983 |
-
"num_inference_steps": 4,
|
| 984 |
-
"guidance_scale": 0.0,
|
| 985 |
-
"seed": abs(hash(f"{spec.role}|{spec.prompt}|{index}|{run_id}")) % 2147483647,
|
| 986 |
-
},
|
| 987 |
-
"options": {"wait_for_model": True},
|
| 988 |
-
}
|
| 989 |
-
request = Request(
|
| 990 |
-
HF_IMAGE_ENDPOINT,
|
| 991 |
-
data=json.dumps(payload).encode("utf-8"),
|
| 992 |
-
headers={
|
| 993 |
-
"Authorization": f"Bearer {HF_TOKEN}",
|
| 994 |
-
"Content-Type": "application/json",
|
| 995 |
-
"Accept": "image/png",
|
| 996 |
-
},
|
| 997 |
-
method="POST",
|
| 998 |
-
)
|
| 999 |
try:
|
| 1000 |
-
|
| 1001 |
-
|
| 1002 |
-
|
| 1003 |
-
|
| 1004 |
-
|
| 1005 |
-
|
| 1006 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1007 |
except Exception as exc:
|
| 1008 |
return None, short_error(exc)
|
| 1009 |
|
|
|
|
| 17 |
import gradio as gr
|
| 18 |
from PIL import Image, ImageDraw, ImageEnhance, ImageFilter, ImageFont
|
| 19 |
|
| 20 |
+
try:
|
| 21 |
+
from huggingface_hub import InferenceClient
|
| 22 |
+
except Exception:
|
| 23 |
+
InferenceClient = None
|
| 24 |
+
|
| 25 |
|
| 26 |
STARTER_HTML = """<!DOCTYPE html>
|
| 27 |
<html>
|
|
|
|
| 104 |
|
| 105 |
HF_TOKEN = os.environ.get("HF_TOKEN", "")
|
| 106 |
HF_IMAGE_MODEL = os.environ.get("HF_IMAGE_MODEL", "black-forest-labs/FLUX.1-schnell")
|
| 107 |
+
HF_PROMPT_MODEL = os.environ.get("HF_PROMPT_MODEL", "Qwen/Qwen2.5-Coder-7B-Instruct:fastest")
|
| 108 |
+
HF_PROMPT_ENDPOINT = os.environ.get("HF_PROMPT_ENDPOINT", "https://router.huggingface.co/v1/chat/completions")
|
|
|
|
| 109 |
|
| 110 |
|
| 111 |
def slugify(value: str) -> str:
|
|
|
|
| 281 |
f"Asset roles:\n{role_block}\n\n"
|
| 282 |
"Return JSON only."
|
| 283 |
)
|
|
|
|
| 284 |
payload = {
|
| 285 |
+
"model": HF_PROMPT_MODEL,
|
| 286 |
+
"messages": [
|
| 287 |
+
{"role": "system", "content": instruction},
|
| 288 |
+
{"role": "user", "content": user_text},
|
| 289 |
+
],
|
| 290 |
+
"max_tokens": 900,
|
| 291 |
+
"temperature": 0.55,
|
| 292 |
+
"top_p": 0.9,
|
| 293 |
+
"stream": False,
|
| 294 |
}
|
| 295 |
request = Request(
|
| 296 |
HF_PROMPT_ENDPOINT,
|
|
|
|
| 305 |
with urlopen(request, timeout=90) as response:
|
| 306 |
raw = response.read().decode("utf-8", errors="replace")
|
| 307 |
parsed = json.loads(raw)
|
| 308 |
+
if isinstance(parsed, dict) and parsed.get("choices"):
|
| 309 |
+
message = parsed["choices"][0].get("message", {})
|
| 310 |
+
text = message.get("content", "") if isinstance(message, dict) else str(message)
|
| 311 |
+
elif isinstance(parsed, list) and parsed:
|
| 312 |
text = parsed[0].get("generated_text", "") if isinstance(parsed[0], dict) else str(parsed[0])
|
| 313 |
elif isinstance(parsed, dict):
|
| 314 |
text = parsed.get("generated_text", parsed.get("text", ""))
|
|
|
|
| 981 |
def hf_image_png(spec: AssetSpec, index: int, run_id: int) -> tuple[bytes | None, str | None]:
|
| 982 |
if not HF_TOKEN:
|
| 983 |
return None, "HF_TOKEN is not visible to the Space runtime"
|
| 984 |
+
if InferenceClient is None:
|
| 985 |
+
return None, "huggingface_hub is not installed in this Space"
|
| 986 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 987 |
try:
|
| 988 |
+
client = InferenceClient(api_key=HF_TOKEN)
|
| 989 |
+
image = client.text_to_image(
|
| 990 |
+
prompt=spec.prompt,
|
| 991 |
+
model=HF_IMAGE_MODEL,
|
| 992 |
+
width=spec.width,
|
| 993 |
+
height=spec.height,
|
| 994 |
+
num_inference_steps=4,
|
| 995 |
+
guidance_scale=0.0,
|
| 996 |
+
seed=abs(hash(f"{spec.role}|{spec.prompt}|{index}|{run_id}")) % 2147483647,
|
| 997 |
+
)
|
| 998 |
+
out = io.BytesIO()
|
| 999 |
+
image.save(out, format="PNG")
|
| 1000 |
+
return image_to_png_bytes(out.getvalue(), spec.width, spec.height), None
|
| 1001 |
except Exception as exc:
|
| 1002 |
return None, short_error(exc)
|
| 1003 |
|