Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from diffusers import StableDiffusionPipeline
|
3 |
+
import torch
|
4 |
+
|
5 |
+
# Tải mô hình Stable Diffusion
|
6 |
+
model_id = "runwayml/stable-diffusion-v1-5"
|
7 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
8 |
+
model_id,
|
9 |
+
torch_dtype=torch.float16,
|
10 |
+
use_auth_token=False # Nếu dùng mô hình riêng, thêm token từ Hugging Face
|
11 |
+
)
|
12 |
+
pipe = pipe.to("cuda") # Chuyển sang GPU (nếu Space có GPU)
|
13 |
+
|
14 |
+
# Hàm tạo hình ảnh
|
15 |
+
def generate_image(prompt, negative_prompt="", num_inference_steps=50, guidance_scale=7.5):
|
16 |
+
image = pipe(
|
17 |
+
prompt,
|
18 |
+
negative_prompt=negative_prompt,
|
19 |
+
num_inference_steps=num_inference_steps,
|
20 |
+
guidance_scale=guidance_scale
|
21 |
+
).images[0]
|
22 |
+
return image
|
23 |
+
|
24 |
+
# Tạo giao diện Gradio
|
25 |
+
interface = gr.Interface(
|
26 |
+
fn=generate_image,
|
27 |
+
inputs=[
|
28 |
+
gr.Textbox(label="Prompt", placeholder="Enter your prompt here, e.g., 'A futuristic city at sunset'"),
|
29 |
+
gr.Textbox(label="Negative Prompt (optional)", placeholder="Things to avoid, e.g., 'blurry, low quality'"),
|
30 |
+
gr.Slider(label="Inference Steps", minimum=10, maximum=100, value=50, step=1),
|
31 |
+
gr.Slider(label="Guidance Scale", minimum=1, maximum=20, value=7.5, step=0.5)
|
32 |
+
],
|
33 |
+
outputs=gr.Image(label="Generated Image"),
|
34 |
+
title="Text-to-Image with Stable Diffusion",
|
35 |
+
description="Enter a prompt to generate an image using Stable Diffusion."
|
36 |
+
)
|
37 |
+
|
38 |
+
# Khởi chạy giao diện
|
39 |
+
interface.launch(server_name="0.0.0.0", server_port=7860)
|