Update app.py
Browse files
app.py
CHANGED
|
@@ -5,10 +5,9 @@ import time
|
|
| 5 |
import os
|
| 6 |
|
| 7 |
# --- 1. CONFIGURATION ---
|
| 8 |
-
# Target Space URL
|
| 9 |
TARGET_URL = "https://black-forest-labs-flux-2-dev.hf.space"
|
| 10 |
|
| 11 |
-
# Token List (
|
| 12 |
HF_TOKENS = [
|
| 13 |
"hf_" + "PiRCDDtPcPFMLWkTkVaZmzoleHOunXnLIA", "hf_" + "BHvZXGICstaktSwycmwNmzHGrTNmKxnlRZ",
|
| 14 |
"hf_" + "ZdgawyTPzXIpwhnRYIteUKSMsWnEDtGKtM", "hf_" + "nMiFYAFsINxAJWPwiCQlaunmdgmrcxKoaT",
|
|
@@ -22,84 +21,98 @@ def get_valid_token():
|
|
| 22 |
return random.choice(HF_TOKENS)
|
| 23 |
|
| 24 |
# --- 2. GENERATION LOGIC ---
|
| 25 |
-
def
|
| 26 |
"""
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
| 30 |
"""
|
| 31 |
-
print(f"🚀 Incoming Request: {prompt[:30]}...")
|
| 32 |
-
|
| 33 |
max_retries = 5
|
| 34 |
attempt = 0
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
while attempt < max_retries:
|
| 37 |
try:
|
| 38 |
-
|
| 39 |
-
token
|
| 40 |
|
| 41 |
-
#
|
| 42 |
-
# Kita gunakan header manual agar lebih kompatibel
|
| 43 |
client = Client(
|
| 44 |
TARGET_URL,
|
| 45 |
-
headers={"Authorization": f"Bearer {
|
| 46 |
)
|
| 47 |
|
| 48 |
-
#
|
| 49 |
-
# Menggunakan api_name="/infer" sesuai instruksi
|
| 50 |
result = client.predict(
|
| 51 |
prompt=prompt,
|
| 52 |
-
seed=
|
| 53 |
-
randomize_seed=(
|
| 54 |
-
width=
|
| 55 |
-
height=
|
| 56 |
-
guidance_scale=
|
| 57 |
-
num_inference_steps=
|
| 58 |
api_name="/infer"
|
| 59 |
)
|
| 60 |
|
| 61 |
-
#
|
| 62 |
-
# Result biasanya berupa tuple atau string path gambar
|
| 63 |
if isinstance(result, (list, tuple)):
|
| 64 |
return result[0]
|
| 65 |
return result
|
| 66 |
|
| 67 |
except Exception as e:
|
| 68 |
err_msg = str(e)
|
| 69 |
-
print(f"
|
| 70 |
|
| 71 |
-
#
|
| 72 |
if "429" in err_msg or "quota" in err_msg.lower() or "queue" in err_msg.lower():
|
| 73 |
-
print("🔄
|
| 74 |
else:
|
| 75 |
-
#
|
| 76 |
-
pass
|
| 77 |
|
| 78 |
attempt += 1
|
| 79 |
-
time.sleep(1)
|
|
|
|
|
|
|
| 80 |
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
-
#
|
| 84 |
-
#
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
fn=generate_image,
|
| 88 |
-
inputs=[
|
| 89 |
-
gr.Textbox(label="Prompt"),
|
| 90 |
-
gr.Number(value=1024, label="Width"),
|
| 91 |
-
gr.Number(value=1024, label="Height"),
|
| 92 |
-
gr.Number(value=3.5, label="Guidance Scale"),
|
| 93 |
-
gr.Number(value=28, label="Steps"),
|
| 94 |
-
gr.Number(value=-1, label="Seed (-1 for Random)"),
|
| 95 |
-
],
|
| 96 |
-
outputs=gr.Image(label="Result"),
|
| 97 |
-
title="Flux Load Balancer API",
|
| 98 |
-
description="API Proxy untuk black-forest-labs-flux-2-dev dengan Token Rotation.",
|
| 99 |
-
allow_flagging="never"
|
| 100 |
-
)
|
| 101 |
|
| 102 |
-
# Launch
|
| 103 |
if __name__ == "__main__":
|
| 104 |
-
|
| 105 |
-
|
|
|
|
| 5 |
import os
|
| 6 |
|
| 7 |
# --- 1. CONFIGURATION ---
|
|
|
|
| 8 |
TARGET_URL = "https://black-forest-labs-flux-2-dev.hf.space"
|
| 9 |
|
| 10 |
+
# Token List (Aman dengan trik +)
|
| 11 |
HF_TOKENS = [
|
| 12 |
"hf_" + "PiRCDDtPcPFMLWkTkVaZmzoleHOunXnLIA", "hf_" + "BHvZXGICstaktSwycmwNmzHGrTNmKxnlRZ",
|
| 13 |
"hf_" + "ZdgawyTPzXIpwhnRYIteUKSMsWnEDtGKtM", "hf_" + "nMiFYAFsINxAJWPwiCQlaunmdgmrcxKoaT",
|
|
|
|
| 21 |
return random.choice(HF_TOKENS)
|
| 22 |
|
| 23 |
# --- 2. GENERATION LOGIC ---
|
| 24 |
+
def process_generation(prompt, width, height, guidance, steps, seed):
|
| 25 |
"""
|
| 26 |
+
Logic persis seperti contohmu:
|
| 27 |
+
1. Ambil token
|
| 28 |
+
2. Set Header Authorization
|
| 29 |
+
3. Tembak client.predict dengan api_name='/infer'
|
| 30 |
"""
|
|
|
|
|
|
|
| 31 |
max_retries = 5
|
| 32 |
attempt = 0
|
| 33 |
|
| 34 |
+
# Validasi input (cegah error type)
|
| 35 |
+
width = int(width)
|
| 36 |
+
height = int(height)
|
| 37 |
+
guidance = float(guidance)
|
| 38 |
+
steps = int(steps)
|
| 39 |
+
seed = int(seed)
|
| 40 |
+
|
| 41 |
while attempt < max_retries:
|
| 42 |
try:
|
| 43 |
+
current_token = get_valid_token()
|
| 44 |
+
print(f"🚀 Generating with token ending ...{current_token[-5:]}")
|
| 45 |
|
| 46 |
+
# --- KUNCI: Client dengan Header Manual ---
|
|
|
|
| 47 |
client = Client(
|
| 48 |
TARGET_URL,
|
| 49 |
+
headers={"Authorization": f"Bearer {current_token}"}
|
| 50 |
)
|
| 51 |
|
| 52 |
+
# Request ke Flux
|
|
|
|
| 53 |
result = client.predict(
|
| 54 |
prompt=prompt,
|
| 55 |
+
seed=seed if seed != -1 else 0,
|
| 56 |
+
randomize_seed=(seed == -1),
|
| 57 |
+
width=width,
|
| 58 |
+
height=height,
|
| 59 |
+
guidance_scale=guidance,
|
| 60 |
+
num_inference_steps=steps,
|
| 61 |
api_name="/infer"
|
| 62 |
)
|
| 63 |
|
| 64 |
+
# Handle return tuple/list dari Flux
|
|
|
|
| 65 |
if isinstance(result, (list, tuple)):
|
| 66 |
return result[0]
|
| 67 |
return result
|
| 68 |
|
| 69 |
except Exception as e:
|
| 70 |
err_msg = str(e)
|
| 71 |
+
print(f"❌ Error (Attempt {attempt+1}): {err_msg}")
|
| 72 |
|
| 73 |
+
# Retry jika error kuota/queue
|
| 74 |
if "429" in err_msg or "quota" in err_msg.lower() or "queue" in err_msg.lower():
|
| 75 |
+
print("🔄 Token limit, switching token...")
|
| 76 |
else:
|
| 77 |
+
pass # Tetap retry siapa tau network glitch
|
|
|
|
| 78 |
|
| 79 |
attempt += 1
|
| 80 |
+
time.sleep(1)
|
| 81 |
+
|
| 82 |
+
return None # Gagal total
|
| 83 |
|
| 84 |
+
# --- 3. UI BLOCKS (Solusi Error 'allow_flagging') ---
|
| 85 |
+
# Menggunakan gr.Blocks seperti contohmu, BUKAN gr.Interface
|
| 86 |
+
with gr.Blocks() as app:
|
| 87 |
+
# Invisible inputs untuk API endpoint
|
| 88 |
+
with gr.Row(visible=False):
|
| 89 |
+
prompt = gr.Textbox(label="Prompt")
|
| 90 |
+
width = gr.Number(value=1024, label="Width")
|
| 91 |
+
height = gr.Number(value=1024, label="Height")
|
| 92 |
+
guidance = gr.Number(value=3.5, label="Guidance")
|
| 93 |
+
steps = gr.Number(value=28, label="Steps")
|
| 94 |
+
seed = gr.Number(value=-1, label="Seed")
|
| 95 |
+
|
| 96 |
+
# Output Image
|
| 97 |
+
out_image = gr.Image(label="Result")
|
| 98 |
+
|
| 99 |
+
# Tombol Trigger (Hidden juga gapapa, yg penting ada event click)
|
| 100 |
+
btn_gen = gr.Button("Generate", visible=False)
|
| 101 |
+
|
| 102 |
+
# --- API ENDPOINT DEFINITION ---
|
| 103 |
+
# api_name="generate" membuat endpoint: /api/generate
|
| 104 |
+
btn_gen.click(
|
| 105 |
+
process_generation,
|
| 106 |
+
inputs=[prompt, width, height, guidance, steps, seed],
|
| 107 |
+
outputs=out_image,
|
| 108 |
+
api_name="generate"
|
| 109 |
+
)
|
| 110 |
|
| 111 |
+
# Tambahan: Load event agar bisa testing lewat GUI jika perlu
|
| 112 |
+
# Tapi utamanya ini buat API
|
| 113 |
+
with gr.Row():
|
| 114 |
+
gr.Markdown("## Flux Load Balancer API Ready")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
+
# Launch
|
| 117 |
if __name__ == "__main__":
|
| 118 |
+
app.queue(max_size=20).launch(server_name="0.0.0.0", server_port=7860)
|
|
|