jalal07555 commited on
Commit
eb04cf7
·
verified ·
1 Parent(s): f5f2c2a

Create app.py

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