Text-to-Image
Diffusers
PyTorch
Safetensors
StableDiffusionPipeline
clip
stable diffusion
comics
text to image
Instructions to use rowenac/vintagechinesecomics with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Diffusers
How to use rowenac/vintagechinesecomics with Diffusers:
pip install -U diffusers transformers accelerate
import torch from diffusers import DiffusionPipeline # switch to "mps" for apple devices pipe = DiffusionPipeline.from_pretrained("rowenac/vintagechinesecomics", dtype=torch.bfloat16, device_map="cuda") prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k" image = pipe(prompt).images[0] - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- Draw Things
- DiffusionBee
Download react app/api/api3.py from rowenac/vintagechinesecomics: direct link, hf CLI and curl.
- Browser
- Download file 2.28 kB
-
https://huggingface.co/rowenac/vintagechinesecomics/resolve/main/react%20app/api/api3.py
- Command line
-
hf download 'hf://rowenac/vintagechinesecomics/react app/api/api3.py'
-
curl -L -o api3.py https://huggingface.co/rowenac/vintagechinesecomics/resolve/main/react%20app/api/api3.py
2.28 kB
| from fastapi import FastAPI, Response | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from diffusers import StableDiffusionPipeline | |
| import torch | |
| from torch import autocast | |
| from io import BytesIO | |
| import base64 | |
| import datetime # Import the datetime module | |
| import uuid # Import the uuid module | |
| import logging | |
| import os | |
| app = FastAPI() | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_credentials=True, | |
| allow_origins=["*"], | |
| allow_methods=["*"], | |
| allow_headers=["*"] | |
| ) | |
| device = "cuda" | |
| model_id = "rowenac/vintagechinesecomics" | |
| pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16) | |
| pipe.to(device) | |
| # Define a folder path for saving images | |
| image_folder = "image_gallery" | |
| # Create the directory if it doesn't exist | |
| os.makedirs(image_folder, exist_ok=True) | |
| def generate(prompt: str): | |
| try: | |
| with autocast(device): | |
| image = pipe(prompt, guidance_scale=8.5).images[0] | |
| # Generate a unique filename for each image using a timestamp and UUID | |
| timestamp = datetime.datetime.now().strftime("%Y%m%d%H%M%S%f") # Format the timestamp | |
| image_uuid = uuid.uuid4() | |
| image_filename = f"{image_folder}/image_{timestamp}_{image_uuid}.png" | |
| # Save the image to the specified folder | |
| image.save(image_filename) | |
| logging.info(f"Image saved to {image_filename}") | |
| buffer = BytesIO() | |
| image.save(buffer, format="PNG") | |
| imgstr = base64.b64encode(buffer.getvalue()).decode('utf-8') | |
| return Response(content=imgstr, media_type="image/png") | |
| except Exception as e: | |
| logging.error(f"Error while generating image: {str(e)}") | |
| return Response(content=str(e), status_code=500) | |
| # New API endpoint to fetch image filenames | |
| def get_images(): | |
| # List all files in the image folder | |
| images = os.listdir(image_folder) | |
| return images | |
| # New API endpoint to serve individual images | |
| def get_image(image_name: str): | |
| # Define the path to the image | |
| image_path = os.path.join(image_folder, image_name) | |
| # Open and serve the image | |
| with open(image_path, "rb") as image_file: | |
| return Response(content=image_file.read(), media_type="image/png") | |