Upload 2 files
Browse files- README (1).md +34 -0
- inference (1).py +25 -0
README (1).md
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
# Stable Diffusion Image Generator with Inception Score
|
| 3 |
+
|
| 4 |
+
This repository uses the `Stable Diffusion` model from the `diffusers` library to generate images based on a text prompt and returns the generated image in base64 format.
|
| 5 |
+
|
| 6 |
+
## How It Works
|
| 7 |
+
|
| 8 |
+
1. The user sends a prompt (e.g., "A red apple on a wooden table").
|
| 9 |
+
2. The `Stable Diffusion` model generates images based on the provided prompt.
|
| 10 |
+
3. The first generated image is returned as a base64-encoded PNG image.
|
| 11 |
+
|
| 12 |
+
## Model Used
|
| 13 |
+
|
| 14 |
+
- **Model**: `CompVis/stable-diffusion-v1-4`
|
| 15 |
+
- **Library**: [diffusers](https://huggingface.co/docs/diffusers)
|
| 16 |
+
- The model is pre-trained, and inference is run on a GPU (if available) or CPU.
|
| 17 |
+
|
| 18 |
+
## How to Use the Inference API
|
| 19 |
+
|
| 20 |
+
You can use this model via the Hugging Face Inference API by making a POST request with the following format:
|
| 21 |
+
|
| 22 |
+
```bash
|
| 23 |
+
curl -X POST https://api-inference.huggingface.co/models/YOUR_USERNAME/stable-diffusion-make -H "Authorization: Bearer YOUR_API_TOKEN" -H "Content-Type: application/json" -d '{"prompt": "A red apple on a wooden table", "num_images": 1}'
|
| 24 |
+
```
|
| 25 |
+
|
| 26 |
+
### Parameters:
|
| 27 |
+
- `prompt`: The text prompt for image generation.
|
| 28 |
+
- `num_images`: Number of images to generate (default is 1).
|
| 29 |
+
|
| 30 |
+
The response will return the first image encoded in base64 format.
|
| 31 |
+
|
| 32 |
+
## License
|
| 33 |
+
|
| 34 |
+
MIT License
|
inference (1).py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
from diffusers import StableDiffusionPipeline
|
| 3 |
+
import torch
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import io
|
| 6 |
+
import base64
|
| 7 |
+
|
| 8 |
+
# Cargar el modelo preentrenado de Stable Diffusion
|
| 9 |
+
pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4")
|
| 10 |
+
pipe.to("cuda" if torch.cuda.is_available() else "cpu") # Usar GPU si est谩 disponible
|
| 11 |
+
|
| 12 |
+
# Funci贸n de inferencia para generar im谩genes
|
| 13 |
+
def inference(prompt: str, num_images: int = 1):
|
| 14 |
+
# Generar im谩genes con el prompt
|
| 15 |
+
generated_images = []
|
| 16 |
+
for _ in range(num_images):
|
| 17 |
+
image = pipe(prompt).images[0]
|
| 18 |
+
generated_images.append(image)
|
| 19 |
+
|
| 20 |
+
# Convertir la primera imagen a base64
|
| 21 |
+
buffered = io.BytesIO()
|
| 22 |
+
generated_images[0].save(buffered, format="PNG")
|
| 23 |
+
img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
|
| 24 |
+
|
| 25 |
+
return {"generated_image": img_str}
|