Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# app.py (النسخة النهائية -
|
| 2 |
|
| 3 |
import gradio as gr
|
| 4 |
import numpy as np
|
|
@@ -11,12 +11,10 @@ import time
|
|
| 11 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 12 |
DTYPE = torch.float16 if torch.cuda.is_available() else torch.float32
|
| 13 |
|
| 14 |
-
# IMPORTANT: Replace "YourUsername/Takween-v1" with your model's name on Hugging Face
|
| 15 |
MODEL_ID = "YourUsername/Takween-v1"
|
| 16 |
BASE_MODEL_ID = "runwayml/stable-diffusion-v1-5"
|
| 17 |
MAX_SEED = np.iinfo(np.int32).max
|
| 18 |
|
| 19 |
-
# Project Logo (Embedded SVG)
|
| 20 |
LOGO_SVG = """
|
| 21 |
<svg xmlns="http://www.w3.org/2000/svg" width="48" height="48" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
| 22 |
<path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2z"></path>
|
|
@@ -37,7 +35,7 @@ pipe = pipe.to(DEVICE)
|
|
| 37 |
|
| 38 |
# --- 3. Professional Theme (Golden Version) ---
|
| 39 |
theme = gr.themes.Base(
|
| 40 |
-
primary_hue=gr.themes.colors.amber,
|
| 41 |
secondary_hue=gr.themes.colors.neutral,
|
| 42 |
font=[gr.themes.GoogleFont("IBM Plex Sans"), "system-ui", "sans-serif"],
|
| 43 |
).set(
|
|
@@ -49,21 +47,24 @@ theme = gr.themes.Base(
|
|
| 49 |
button_primary_background_fill_hover="*primary_600",
|
| 50 |
)
|
| 51 |
|
| 52 |
-
# --- 4. Inference Function with UI Updates ---
|
| 53 |
def infer(prompt, negative_prompt, guidance_scale, num_inference_steps, seed, randomize_seed):
|
| 54 |
if randomize_seed:
|
| 55 |
seed = random.randint(0, MAX_SEED)
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
-
# Update UI to show loading state
|
| 61 |
yield {
|
| 62 |
output_image: gr.update(value=None, interactive=False, visible=True),
|
| 63 |
run_button: gr.update(interactive=False, value="Generating..."),
|
| 64 |
}
|
| 65 |
|
| 66 |
-
# Generate the image
|
| 67 |
image = pipe(
|
| 68 |
prompt=prompt,
|
| 69 |
negative_prompt=negative_prompt,
|
|
@@ -72,7 +73,6 @@ def infer(prompt, negative_prompt, guidance_scale, num_inference_steps, seed, ra
|
|
| 72 |
generator=generator,
|
| 73 |
).images[0]
|
| 74 |
|
| 75 |
-
# Update UI with the final result
|
| 76 |
yield {
|
| 77 |
output_image: gr.update(value=image, interactive=True),
|
| 78 |
output_seed: gr.update(value=seed),
|
|
@@ -81,44 +81,25 @@ def infer(prompt, negative_prompt, guidance_scale, num_inference_steps, seed, ra
|
|
| 81 |
|
| 82 |
# --- 5. Professional UI Layout ---
|
| 83 |
with gr.Blocks(theme=theme, css="#footer {text-align: center;}") as demo:
|
| 84 |
-
# Header
|
| 85 |
with gr.Row():
|
| 86 |
gr.HTML(f"<div style='display: flex; align-items: center; gap: 12px;'>{LOGO_SVG}<h1>Takween Project</h1></div>")
|
| 87 |
-
|
| 88 |
gr.Markdown("#### A specialized model for generating precise geometric images from text descriptions.")
|
| 89 |
gr.HTML("<hr>")
|
| 90 |
-
|
| 91 |
-
# Main Layout (2 columns)
|
| 92 |
with gr.Row():
|
| 93 |
-
# Left Column: Controls
|
| 94 |
with gr.Column(scale=1):
|
| 95 |
prompt = gr.Textbox(label="Prompt", placeholder="A red circle with thick black borders...", lines=3)
|
| 96 |
negative_prompt = gr.Textbox(label="Negative Prompt", placeholder="Low quality, blurry, distorted...")
|
| 97 |
-
|
| 98 |
with gr.Accordion("Advanced Settings", open=False):
|
| 99 |
guidance_scale = gr.Slider(label="Guidance Scale", minimum=1.0, maximum=20.0, value=7.5, step=0.1)
|
| 100 |
num_inference_steps = gr.Slider(label="Number of Steps", minimum=10, maximum=100, value=30, step=1)
|
| 101 |
with gr.Row():
|
| 102 |
seed = gr.Number(label="Seed", value=0, precision=0)
|
| 103 |
randomize_seed = gr.Checkbox(label="Randomize", value=True)
|
| 104 |
-
|
| 105 |
run_button = gr.Button("Generate Image", variant="primary")
|
| 106 |
-
|
| 107 |
-
gr.Examples(
|
| 108 |
-
examples=[
|
| 109 |
-
"A filled red circle with a thick black border",
|
| 110 |
-
"An outline blue triangle positioned to the left of a yellow square",
|
| 111 |
-
"A green star overlapping a purple rectangle",
|
| 112 |
-
],
|
| 113 |
-
inputs=[prompt]
|
| 114 |
-
)
|
| 115 |
-
|
| 116 |
-
# Right Column: Results
|
| 117 |
with gr.Column(scale=2):
|
| 118 |
output_image = gr.Image(label="Generated Image", interactive=False, height=512)
|
| 119 |
output_seed = gr.Textbox(label="Seed Used", interactive=False)
|
| 120 |
-
|
| 121 |
-
# Footer
|
| 122 |
gr.HTML("<hr>")
|
| 123 |
with gr.Accordion("Team and Acknowledgments", open=False):
|
| 124 |
gr.Markdown("""
|
|
@@ -138,8 +119,7 @@ with gr.Blocks(theme=theme, css="#footer {text-align: center;}") as demo:
|
|
| 138 |
</div>
|
| 139 |
""")
|
| 140 |
gr.Markdown("<p id='footer'>© 2025 Takween Project. Developed by Osama Saeed & Tareq Al-Omari. All rights reserved.</p>")
|
| 141 |
-
|
| 142 |
-
# Event Listeners
|
| 143 |
run_button.click(
|
| 144 |
fn=infer,
|
| 145 |
inputs=[prompt, negative_prompt, guidance_scale, num_inference_steps, seed, randomize_seed],
|
|
|
|
| 1 |
+
# app.py (النسخة النهائية - مع تصحيح SyntaxError الثاني)
|
| 2 |
|
| 3 |
import gradio as gr
|
| 4 |
import numpy as np
|
|
|
|
| 11 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 12 |
DTYPE = torch.float16 if torch.cuda.is_available() else torch.float32
|
| 13 |
|
|
|
|
| 14 |
MODEL_ID = "YourUsername/Takween-v1"
|
| 15 |
BASE_MODEL_ID = "runwayml/stable-diffusion-v1-5"
|
| 16 |
MAX_SEED = np.iinfo(np.int32).max
|
| 17 |
|
|
|
|
| 18 |
LOGO_SVG = """
|
| 19 |
<svg xmlns="http://www.w3.org/2000/svg" width="48" height="48" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
| 20 |
<path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2z"></path>
|
|
|
|
| 35 |
|
| 36 |
# --- 3. Professional Theme (Golden Version) ---
|
| 37 |
theme = gr.themes.Base(
|
| 38 |
+
primary_hue=gr.themes.colors.amber,
|
| 39 |
secondary_hue=gr.themes.colors.neutral,
|
| 40 |
font=[gr.themes.GoogleFont("IBM Plex Sans"), "system-ui", "sans-serif"],
|
| 41 |
).set(
|
|
|
|
| 47 |
button_primary_background_fill_hover="*primary_600",
|
| 48 |
)
|
| 49 |
|
| 50 |
+
# --- 4. Inference Function with UI Updates (Corrected) ---
|
| 51 |
def infer(prompt, negative_prompt, guidance_scale, num_inference_steps, seed, randomize_seed):
|
| 52 |
if randomize_seed:
|
| 53 |
seed = random.randint(0, MAX_SEED)
|
| 54 |
|
| 55 |
+
# =======================================================
|
| 56 |
+
# <<< تم تعديل هذا الجزء لحل مشكلة SyntaxError >>>
|
| 57 |
+
# الخطوة 1: إنشاء المولد على الجهاز الصحيح
|
| 58 |
+
generator = torch.Generator(device=DEVICE)
|
| 59 |
+
# الخطوة 2: تحديد البذرة للمولد
|
| 60 |
+
generator.manual_seed(seed)
|
| 61 |
+
# =======================================================
|
| 62 |
|
|
|
|
| 63 |
yield {
|
| 64 |
output_image: gr.update(value=None, interactive=False, visible=True),
|
| 65 |
run_button: gr.update(interactive=False, value="Generating..."),
|
| 66 |
}
|
| 67 |
|
|
|
|
| 68 |
image = pipe(
|
| 69 |
prompt=prompt,
|
| 70 |
negative_prompt=negative_prompt,
|
|
|
|
| 73 |
generator=generator,
|
| 74 |
).images[0]
|
| 75 |
|
|
|
|
| 76 |
yield {
|
| 77 |
output_image: gr.update(value=image, interactive=True),
|
| 78 |
output_seed: gr.update(value=seed),
|
|
|
|
| 81 |
|
| 82 |
# --- 5. Professional UI Layout ---
|
| 83 |
with gr.Blocks(theme=theme, css="#footer {text-align: center;}") as demo:
|
|
|
|
| 84 |
with gr.Row():
|
| 85 |
gr.HTML(f"<div style='display: flex; align-items: center; gap: 12px;'>{LOGO_SVG}<h1>Takween Project</h1></div>")
|
|
|
|
| 86 |
gr.Markdown("#### A specialized model for generating precise geometric images from text descriptions.")
|
| 87 |
gr.HTML("<hr>")
|
|
|
|
|
|
|
| 88 |
with gr.Row():
|
|
|
|
| 89 |
with gr.Column(scale=1):
|
| 90 |
prompt = gr.Textbox(label="Prompt", placeholder="A red circle with thick black borders...", lines=3)
|
| 91 |
negative_prompt = gr.Textbox(label="Negative Prompt", placeholder="Low quality, blurry, distorted...")
|
|
|
|
| 92 |
with gr.Accordion("Advanced Settings", open=False):
|
| 93 |
guidance_scale = gr.Slider(label="Guidance Scale", minimum=1.0, maximum=20.0, value=7.5, step=0.1)
|
| 94 |
num_inference_steps = gr.Slider(label="Number of Steps", minimum=10, maximum=100, value=30, step=1)
|
| 95 |
with gr.Row():
|
| 96 |
seed = gr.Number(label="Seed", value=0, precision=0)
|
| 97 |
randomize_seed = gr.Checkbox(label="Randomize", value=True)
|
|
|
|
| 98 |
run_button = gr.Button("Generate Image", variant="primary")
|
| 99 |
+
gr.Examples(examples=["A filled red circle with a thick black border", "An outline blue triangle positioned to the left of a yellow square", "A green star overlapping a purple rectangle"], inputs=[prompt])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
with gr.Column(scale=2):
|
| 101 |
output_image = gr.Image(label="Generated Image", interactive=False, height=512)
|
| 102 |
output_seed = gr.Textbox(label="Seed Used", interactive=False)
|
|
|
|
|
|
|
| 103 |
gr.HTML("<hr>")
|
| 104 |
with gr.Accordion("Team and Acknowledgments", open=False):
|
| 105 |
gr.Markdown("""
|
|
|
|
| 119 |
</div>
|
| 120 |
""")
|
| 121 |
gr.Markdown("<p id='footer'>© 2025 Takween Project. Developed by Osama Saeed & Tareq Al-Omari. All rights reserved.</p>")
|
| 122 |
+
|
|
|
|
| 123 |
run_button.click(
|
| 124 |
fn=infer,
|
| 125 |
inputs=[prompt, negative_prompt, guidance_scale, num_inference_steps, seed, randomize_seed],
|