Christian
commited on
Commit
·
307e821
1
Parent(s):
475c074
Initial commit
Browse files- app.py +43 -0
- requirements.txt +6 -0
app.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from diffusers import StableDiffusionImg2ImgPipeline
|
| 3 |
+
import torch
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import io
|
| 6 |
+
import base64
|
| 7 |
+
|
| 8 |
+
# Carica il modello Stable Diffusion
|
| 9 |
+
pipe = StableDiffusionImg2ImgPipeline.from_pretrained("stabilityai/stable-diffusion-2-1")
|
| 10 |
+
pipe.to("cuda" if torch.cuda.is_available() else "cpu")
|
| 11 |
+
|
| 12 |
+
def enhance_image(image, prompt, negative_prompt, cfg_scale, denoising_strength):
|
| 13 |
+
"""
|
| 14 |
+
Migliora un'immagine utilizzando Stable Diffusion Img2Img.
|
| 15 |
+
"""
|
| 16 |
+
image = Image.open(image).convert("RGB")
|
| 17 |
+
enhanced_image = pipe(
|
| 18 |
+
prompt=prompt,
|
| 19 |
+
negative_prompt=negative_prompt,
|
| 20 |
+
image=image,
|
| 21 |
+
strength=denoising_strength,
|
| 22 |
+
guidance_scale=cfg_scale
|
| 23 |
+
).images[0]
|
| 24 |
+
|
| 25 |
+
return enhanced_image
|
| 26 |
+
|
| 27 |
+
# Interfaccia utente con Gradio
|
| 28 |
+
demo = gr.Interface(
|
| 29 |
+
fn=enhance_image,
|
| 30 |
+
inputs=[
|
| 31 |
+
gr.Image(type="filepath", label="Carica l'immagine:"),
|
| 32 |
+
gr.Textbox(label="Prompt", value="highly detailed, ultra high resolution"),
|
| 33 |
+
gr.Textbox(label="Negative Prompt", value="low quality, blurry, artifacts"),
|
| 34 |
+
gr.Slider(1, 20, value=7, label="CFG Scale"),
|
| 35 |
+
gr.Slider(0.1, 1.0, value=0.35, label="Denoising Strength")
|
| 36 |
+
],
|
| 37 |
+
outputs=gr.Image(type="pil", label="Immagine migliorata"),
|
| 38 |
+
title="Image Enhancer con Stable Diffusion",
|
| 39 |
+
description="Carica un'immagine e usa Stable Diffusion per migliorarla."
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
if __name__ == "__main__":
|
| 43 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
diffusers
|
| 2 |
+
transformers
|
| 3 |
+
accelerate
|
| 4 |
+
safetensors
|
| 5 |
+
gradio
|
| 6 |
+
torch
|