Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,18 +1,47 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from diffusers import StableDiffusionImg2ImgPipeline
|
3 |
import torch
|
4 |
-
import
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
8 |
|
9 |
-
def
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
return result
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import torch
|
2 |
+
from diffusers import StableDiffusionImg2ImgPipeline
|
3 |
+
import gradio as gr
|
4 |
+
from PIL import Image
|
5 |
|
6 |
+
# تحميل الموديل المفتوح
|
7 |
+
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
|
8 |
+
"runwayml/stable-diffusion-v1-5"
|
9 |
+
).to("cuda" if torch.cuda.is_available() else "cpu")
|
10 |
|
11 |
+
def generate_image(input_image, prompt, strength=0.7, guidance=7.5):
|
12 |
+
if input_image is None or prompt.strip() == "":
|
13 |
+
return None
|
14 |
+
|
15 |
+
# تحويل الصورة إلى RGB وضبط الحجم
|
16 |
+
init_image = input_image.convert("RGB").resize((512, 512))
|
17 |
+
|
18 |
+
# توليد الصورة
|
19 |
+
result = pipe(
|
20 |
+
prompt=prompt,
|
21 |
+
image=init_image,
|
22 |
+
strength=strength,
|
23 |
+
guidance_scale=guidance
|
24 |
+
).images[0]
|
25 |
+
|
26 |
return result
|
27 |
|
28 |
+
# واجهة Gradio
|
29 |
+
with gr.Blocks() as demo:
|
30 |
+
gr.Markdown("## 🖼️ Stable Diffusion Img2Img - تعديل الصور (ديكور)")
|
31 |
+
with gr.Row():
|
32 |
+
with gr.Column():
|
33 |
+
input_image = gr.Image(type="pil", label="ارفع الصورة")
|
34 |
+
prompt = gr.Textbox(label="الوصف (Prompt)", placeholder="مثال: غرفة مع أثاث عصري وإضاءة دافئة")
|
35 |
+
strength = gr.Slider(0.1, 1.0, value=0.7, step=0.1, label="درجة التغيير (Strength)")
|
36 |
+
guidance = gr.Slider(1, 15, value=7.5, step=0.5, label="جودة التفاصيل (Guidance Scale)")
|
37 |
+
btn = gr.Button("توليد الصورة")
|
38 |
+
with gr.Column():
|
39 |
+
output_image = gr.Image(label="النتيجة")
|
40 |
+
|
41 |
+
btn.click(
|
42 |
+
fn=generate_image,
|
43 |
+
inputs=[input_image, prompt, strength, guidance],
|
44 |
+
outputs=output_image
|
45 |
+
)
|
46 |
+
|
47 |
+
demo.launch()
|