Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,49 +2,36 @@ import gradio as gr
|
|
| 2 |
import requests
|
| 3 |
import io
|
| 4 |
import os
|
|
|
|
| 5 |
import random
|
| 6 |
from PIL import Image
|
| 7 |
|
| 8 |
def query(model_name, prompt, is_negative, steps, cfg_scale, seed, strength, width, height):
|
| 9 |
API_URL = f"https://api-inference.huggingface.co/models/{model_name}"
|
| 10 |
-
|
| 11 |
|
| 12 |
-
if not token:
|
| 13 |
-
return "Error: Hugging Face API token is not set."
|
| 14 |
-
|
| 15 |
-
headers = {"Authorization": f"Bearer {token}"}
|
| 16 |
-
|
| 17 |
-
negative_prompt = "low quality, bad anatomy, worst quality, blurry" if is_negative else ""
|
| 18 |
-
|
| 19 |
payload = {
|
| 20 |
"inputs": prompt,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
"parameters": {
|
| 22 |
-
"is_negative": is_negative,
|
| 23 |
-
"num_inference_steps": steps,
|
| 24 |
-
"guidance_scale": cfg_scale,
|
| 25 |
-
"seed": seed if seed != -1 else random.randint(1, 1000000000),
|
| 26 |
-
"strength": strength,
|
| 27 |
"width": width,
|
| 28 |
"height": height
|
| 29 |
}
|
| 30 |
}
|
| 31 |
-
|
| 32 |
response = requests.post(API_URL, headers=headers, json=payload)
|
| 33 |
-
|
| 34 |
if response.status_code != 200:
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
except Exception:
|
| 38 |
-
error_message = response.text
|
| 39 |
-
return f"Error {response.status_code}: {error_message}"
|
| 40 |
-
|
| 41 |
try:
|
| 42 |
image = Image.open(io.BytesIO(response.content))
|
| 43 |
-
if image.format not in ["PNG", "JPEG", "WEBP"]:
|
| 44 |
-
return "Error: Received an invalid image format."
|
| 45 |
return image
|
| 46 |
-
except Exception
|
| 47 |
-
return f"Invalid response: {
|
| 48 |
|
| 49 |
demo = gr.Interface(
|
| 50 |
fn=query,
|
|
@@ -64,4 +51,4 @@ demo = gr.Interface(
|
|
| 64 |
description="テキストプロンプトとオプションを設定して画像を生成します。"
|
| 65 |
)
|
| 66 |
|
| 67 |
-
demo.launch()
|
|
|
|
| 2 |
import requests
|
| 3 |
import io
|
| 4 |
import os
|
| 5 |
+
import json
|
| 6 |
import random
|
| 7 |
from PIL import Image
|
| 8 |
|
| 9 |
def query(model_name, prompt, is_negative, steps, cfg_scale, seed, strength, width, height):
|
| 10 |
API_URL = f"https://api-inference.huggingface.co/models/{model_name}"
|
| 11 |
+
headers = {"Authorization": f"Bearer {os.getenv('token')}"}
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
payload = {
|
| 14 |
"inputs": prompt,
|
| 15 |
+
"is_negative": is_negative,
|
| 16 |
+
"steps": steps,
|
| 17 |
+
"cfg_scale": cfg_scale,
|
| 18 |
+
"seed": seed if seed != -1 else random.randint(1, 1000000000),
|
| 19 |
+
"strength": strength,
|
| 20 |
"parameters": {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
"width": width,
|
| 22 |
"height": height
|
| 23 |
}
|
| 24 |
}
|
|
|
|
| 25 |
response = requests.post(API_URL, headers=headers, json=payload)
|
| 26 |
+
|
| 27 |
if response.status_code != 200:
|
| 28 |
+
return f"Error {response.status_code}: {response.text}"
|
| 29 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
try:
|
| 31 |
image = Image.open(io.BytesIO(response.content))
|
|
|
|
|
|
|
| 32 |
return image
|
| 33 |
+
except Exception:
|
| 34 |
+
return f"Invalid response: {response.text}"
|
| 35 |
|
| 36 |
demo = gr.Interface(
|
| 37 |
fn=query,
|
|
|
|
| 51 |
description="テキストプロンプトとオプションを設定して画像を生成します。"
|
| 52 |
)
|
| 53 |
|
| 54 |
+
demo.launch()
|