Update app.py
Browse files
app.py
CHANGED
|
@@ -1,154 +1,97 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
|
|
|
|
|
|
| 3 |
import random
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
if torch.cuda.is_available():
|
| 13 |
-
torch_dtype = torch.float16
|
| 14 |
-
else:
|
| 15 |
-
torch_dtype = torch.float32
|
| 16 |
-
|
| 17 |
-
pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
|
| 18 |
-
pipe = pipe.to(device)
|
| 19 |
-
|
| 20 |
-
MAX_SEED = np.iinfo(np.int32).max
|
| 21 |
-
MAX_IMAGE_SIZE = 1024
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
# @spaces.GPU #[uncomment to use ZeroGPU]
|
| 25 |
-
def infer(
|
| 26 |
-
prompt,
|
| 27 |
-
negative_prompt,
|
| 28 |
-
seed,
|
| 29 |
-
randomize_seed,
|
| 30 |
-
width,
|
| 31 |
-
height,
|
| 32 |
-
guidance_scale,
|
| 33 |
-
num_inference_steps,
|
| 34 |
-
progress=gr.Progress(track_tqdm=True),
|
| 35 |
-
):
|
| 36 |
-
if randomize_seed:
|
| 37 |
-
seed = random.randint(0, MAX_SEED)
|
| 38 |
-
|
| 39 |
-
generator = torch.Generator().manual_seed(seed)
|
| 40 |
-
|
| 41 |
-
image = pipe(
|
| 42 |
-
prompt=prompt,
|
| 43 |
-
negative_prompt=negative_prompt,
|
| 44 |
-
guidance_scale=guidance_scale,
|
| 45 |
-
num_inference_steps=num_inference_steps,
|
| 46 |
-
width=width,
|
| 47 |
-
height=height,
|
| 48 |
-
generator=generator,
|
| 49 |
-
).images[0]
|
| 50 |
-
|
| 51 |
-
return image, seed
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
examples = [
|
| 55 |
-
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
| 56 |
-
"An astronaut riding a green horse",
|
| 57 |
-
"A delicious ceviche cheesecake slice",
|
| 58 |
-
]
|
| 59 |
-
|
| 60 |
-
css = """
|
| 61 |
#col-container {
|
| 62 |
margin: 0 auto;
|
| 63 |
-
max-width:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
}
|
| 65 |
"""
|
| 66 |
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
with gr.Column(elem_id="col-container"):
|
| 69 |
-
gr.Markdown(" #
|
| 70 |
-
|
|
|
|
| 71 |
with gr.Row():
|
| 72 |
-
prompt = gr.Text(
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
)
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
with gr.Accordion("Advanced Settings", open=False):
|
| 85 |
-
negative_prompt = gr.Text(
|
| 86 |
-
label="Negative prompt",
|
| 87 |
-
max_lines=1,
|
| 88 |
-
placeholder="Enter a negative prompt",
|
| 89 |
-
visible=False,
|
| 90 |
-
)
|
| 91 |
-
|
| 92 |
-
seed = gr.Slider(
|
| 93 |
-
label="Seed",
|
| 94 |
-
minimum=0,
|
| 95 |
-
maximum=MAX_SEED,
|
| 96 |
-
step=1,
|
| 97 |
-
value=0,
|
| 98 |
-
)
|
| 99 |
-
|
| 100 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 101 |
-
|
| 102 |
with gr.Row():
|
| 103 |
-
width = gr.Slider(
|
| 104 |
-
|
| 105 |
-
minimum=256,
|
| 106 |
-
maximum=MAX_IMAGE_SIZE,
|
| 107 |
-
step=32,
|
| 108 |
-
value=1024, # Replace with defaults that work for your model
|
| 109 |
-
)
|
| 110 |
-
|
| 111 |
-
height = gr.Slider(
|
| 112 |
-
label="Height",
|
| 113 |
-
minimum=256,
|
| 114 |
-
maximum=MAX_IMAGE_SIZE,
|
| 115 |
-
step=32,
|
| 116 |
-
value=1024, # Replace with defaults that work for your model
|
| 117 |
-
)
|
| 118 |
-
|
| 119 |
-
with gr.Row():
|
| 120 |
-
guidance_scale = gr.Slider(
|
| 121 |
-
label="Guidance scale",
|
| 122 |
-
minimum=0.0,
|
| 123 |
-
maximum=10.0,
|
| 124 |
-
step=0.1,
|
| 125 |
-
value=0.0, # Replace with defaults that work for your model
|
| 126 |
-
)
|
| 127 |
-
|
| 128 |
-
num_inference_steps = gr.Slider(
|
| 129 |
-
label="Number of inference steps",
|
| 130 |
-
minimum=1,
|
| 131 |
-
maximum=50,
|
| 132 |
-
step=1,
|
| 133 |
-
value=2, # Replace with defaults that work for your model
|
| 134 |
-
)
|
| 135 |
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
inputs=[
|
| 141 |
-
prompt,
|
| 142 |
-
negative_prompt,
|
| 143 |
-
seed,
|
| 144 |
-
randomize_seed,
|
| 145 |
-
width,
|
| 146 |
-
height,
|
| 147 |
-
guidance_scale,
|
| 148 |
-
num_inference_steps,
|
| 149 |
-
],
|
| 150 |
-
outputs=[result, seed],
|
| 151 |
)
|
| 152 |
|
| 153 |
if __name__ == "__main__":
|
| 154 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import io
|
| 4 |
+
from PIL import Image
|
| 5 |
import random
|
| 6 |
|
| 7 |
+
# التنسيق البصري الفاخر التابع لـ 7.com.ly (True Black & Cyber Neon)
|
| 8 |
+
custom_css = """
|
| 9 |
+
body, .gradio-container {
|
| 10 |
+
background-color: #000000 !important;
|
| 11 |
+
color: #ffffff !important;
|
| 12 |
+
font-family: 'Inter', sans-serif;
|
| 13 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
#col-container {
|
| 15 |
margin: 0 auto;
|
| 16 |
+
max-width: 800px;
|
| 17 |
+
background: rgba(10, 10, 10, 0.9) !important;
|
| 18 |
+
border: 1px solid #bf00ff !important;
|
| 19 |
+
box-shadow: 0 0 20px rgba(191, 0, 255, 0.15) !important;
|
| 20 |
+
border-radius: 12px !important;
|
| 21 |
+
padding: 30px !important;
|
| 22 |
+
}
|
| 23 |
+
button.primary {
|
| 24 |
+
background: linear-gradient(135deg, #00f3ff, #bf00ff) !important;
|
| 25 |
+
color: #000000 !important;
|
| 26 |
+
font-weight: bold !important;
|
| 27 |
+
border: none !important;
|
| 28 |
+
}
|
| 29 |
+
button.primary:hover {
|
| 30 |
+
box-shadow: 0 0 25px rgba(0, 243, 255, 0.5) !important;
|
| 31 |
+
}
|
| 32 |
+
input, textarea {
|
| 33 |
+
background-color: #050505 !important;
|
| 34 |
+
color: #ffffff !important;
|
| 35 |
+
border: 1px solid #00f3ff !important;
|
| 36 |
}
|
| 37 |
"""
|
| 38 |
|
| 39 |
+
def generate_byok(prompt, negative_prompt, seed, randomize_seed, width, height, hf_token):
|
| 40 |
+
if not hf_token:
|
| 41 |
+
return None, "برجاء إدخال مفتاح الهوية اللحظي (HF Token) لتفعيل المعالجة الفورية."
|
| 42 |
+
|
| 43 |
+
if randomize_seed:
|
| 44 |
+
seed = random.randint(0, 2147483647)
|
| 45 |
+
|
| 46 |
+
# استخدام محرك FLUX.1-schnell فائق السرعة عبر الـ API لزمن استجابة يقارب الصفر
|
| 47 |
+
API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-schnell"
|
| 48 |
+
headers = {"Authorization": f"Bearer {hf_token}"}
|
| 49 |
+
|
| 50 |
+
payload = {
|
| 51 |
+
"inputs": prompt,
|
| 52 |
+
"parameters": {
|
| 53 |
+
"negative_prompt": negative_prompt,
|
| 54 |
+
"width": width,
|
| 55 |
+
"height": height,
|
| 56 |
+
"seed": seed
|
| 57 |
+
}
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
try:
|
| 61 |
+
response = requests.post(API_URL, headers=headers, json=payload, timeout=30)
|
| 62 |
+
if response.status_code == 200:
|
| 63 |
+
return Image.open(io.BytesIO(response.content))
|
| 64 |
+
else:
|
| 65 |
+
return None
|
| 66 |
+
except Exception:
|
| 67 |
+
return None
|
| 68 |
+
|
| 69 |
+
with gr.Blocks(css=custom_css, theme=gr.themes.Base()) as demo:
|
| 70 |
with gr.Column(elem_id="col-container"):
|
| 71 |
+
gr.Markdown("<h1 style='text-align: center; color: #00f3ff; margin-bottom: 0;'>7.com.ly</h1>")
|
| 72 |
+
gr.Markdown("<p style='text-align: center; color: #bf00ff; margin-top: 0;'>Sovereign Sovereign Image Engine (BYOK)</p>")
|
| 73 |
+
|
| 74 |
with gr.Row():
|
| 75 |
+
prompt = gr.Text(label="الوصف البصري (Prompt)", placeholder="أدخل تفاصيل الصورة المطلوبة هنا...", scale=4)
|
| 76 |
+
run_button = gr.Button("توليد فوري", scale=1, variant="primary")
|
| 77 |
+
|
| 78 |
+
result = gr.Image(label="النتيجة السيادية", show_label=False)
|
| 79 |
+
|
| 80 |
+
with gr.Accordion("إعدادات المحرك المتقدمة", open=False):
|
| 81 |
+
hf_token = gr.Text(label="مفتاح العميل اللحظي (Hugging Face Token)", placeholder="hf_...", type="password")
|
| 82 |
+
negative_prompt = gr.Text(label="العناصر المستبعدة (Negative prompt)", placeholder="أشياء لا تريدها في الصورة")
|
| 83 |
+
seed = gr.Slider(label="Seed", minimum=0, maximum=2147483647, step=1, value=0)
|
| 84 |
+
randomize_seed = gr.Checkbox(label="عشوائية الـ Seed", value=True)
|
| 85 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
with gr.Row():
|
| 87 |
+
width = gr.Slider(label="العرض (Width)", minimum=256, maximum=1024, step=32, value=1024)
|
| 88 |
+
height = gr.Slider(label="الارتفاع (Height)", minimum=256, maximum=1024, step=32, value=1024)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
+
run_button.click(
|
| 91 |
+
fn=generate_byok,
|
| 92 |
+
inputs=[prompt, negative_prompt, seed, randomize_seed, width, height, hf_token],
|
| 93 |
+
outputs=[result]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
)
|
| 95 |
|
| 96 |
if __name__ == "__main__":
|
| 97 |
+
demo.launch()
|