Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from gradio_client import Client
|
| 3 |
+
import random
|
| 4 |
+
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 (Sudah ditanam dengan aman)
|
| 12 |
+
HF_TOKENS = [
|
| 13 |
+
"hf_" + "PiRCDDtPcPFMLWkTkVaZmzoleHOunXnLIA", "hf_" + "BHvZXGICstaktSwycmwNmzHGrTNmKxnlRZ",
|
| 14 |
+
"hf_" + "ZdgawyTPzXIpwhnRYIteUKSMsWnEDtGKtM", "hf_" + "nMiFYAFsINxAJWPwiCQlaunmdgmrcxKoaT",
|
| 15 |
+
"hf_" + "PccpUIbTckCiafwErDLkRlsvqhgtfZaBHL", "hf_" + "faGyXBPfBkaHXDMUSJtxEggonhhZbomFIz",
|
| 16 |
+
"hf_" + "SndsPaRWsevDXCgZcSjTUlBYUJqOkSfFmn", "hf_" + "CqobFdUpeVCeuhUaiuXwvdczBUmoUHXRGa",
|
| 17 |
+
"hf_" + "JKCQYUhhHPPkpucegqkNSyureLdXpmeXRF", "hf_" + "tBYfslUwHNiNMufzwAYIlrDVovEWmOQulC",
|
| 18 |
+
"hf_" + "LKLdrdUxyUyKODSUthmqHXqDMfHrQueera", "hf_" + "ivSBboJYQVcifWkCNcOTOnxUQrZOtOglnU"
|
| 19 |
+
]
|
| 20 |
+
|
| 21 |
+
def get_valid_token():
|
| 22 |
+
return random.choice(HF_TOKENS)
|
| 23 |
+
|
| 24 |
+
# --- 2. GENERATION LOGIC ---
|
| 25 |
+
def generate_image(prompt, width, height, guidance, steps, seed):
|
| 26 |
+
"""
|
| 27 |
+
Fungsi ini bertindak sebagai Proxy.
|
| 28 |
+
Menerima request -> Meneruskan ke Flux Space -> Mengembalikan Gambar.
|
| 29 |
+
Dilengkapi sistem Retry otomatis jika token limit.
|
| 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 |
+
# 1. Pilih Token
|
| 39 |
+
token = get_valid_token()
|
| 40 |
+
|
| 41 |
+
# 2. Setup Client dengan Header Auth (Sesuai contohmu)
|
| 42 |
+
# Kita gunakan header manual agar lebih kompatibel
|
| 43 |
+
client = Client(
|
| 44 |
+
TARGET_URL,
|
| 45 |
+
headers={"Authorization": f"Bearer {token}"}
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
# 3. Request ke Endpoint Flux
|
| 49 |
+
# Menggunakan api_name="/infer" sesuai instruksi
|
| 50 |
+
result = client.predict(
|
| 51 |
+
prompt=prompt,
|
| 52 |
+
seed=int(seed) if seed != -1 else 0,
|
| 53 |
+
randomize_seed=(int(seed) == -1),
|
| 54 |
+
width=int(width),
|
| 55 |
+
height=int(height),
|
| 56 |
+
guidance_scale=float(guidance),
|
| 57 |
+
num_inference_steps=int(steps),
|
| 58 |
+
api_name="/infer"
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
# 4. Handle Return
|
| 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"⚠️ Error (Attempt {attempt+1}): {err_msg}")
|
| 70 |
+
|
| 71 |
+
# Cek apakah error karena kuota/busy
|
| 72 |
+
if "429" in err_msg or "quota" in err_msg.lower() or "queue" in err_msg.lower():
|
| 73 |
+
print("🔄 Switching token & Retrying...")
|
| 74 |
+
else:
|
| 75 |
+
# Jika error aneh (misal parameter salah), print tapi tetap coba retry sekali lagi
|
| 76 |
+
pass
|
| 77 |
+
|
| 78 |
+
attempt += 1
|
| 79 |
+
time.sleep(1) # Jeda sebentar
|
| 80 |
+
|
| 81 |
+
return None # Gagal total setelah 5x percobaan
|
| 82 |
+
|
| 83 |
+
# --- 3. GRADIO INTERFACE ---
|
| 84 |
+
# Kita buat Interface sederhana agar terbentuk endpoint API otomatis
|
| 85 |
+
# Input disesuaikan dengan parameter Flux
|
| 86 |
+
iface = gr.Interface(
|
| 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 Server
|
| 103 |
+
if __name__ == "__main__":
|
| 104 |
+
iface.queue(max_size=20).launch(server_name="0.0.0.0", server_port=7860)
|
| 105 |
+
|