from fastapi import FastAPI from pydantic import BaseModel from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM from diffusers import StableDiffusionPipeline import torch app = FastAPI(title="Custom Content Generator API") TEXT_MODEL = "gpt2" IMAGE_MODEL = "runwayml/stable-diffusion-v1-5" device = "cuda" if torch.cuda.is_available() else "cpu" # Text pipeline tokenizer = AutoTokenizer.from_pretrained(TEXT_MODEL) model = AutoModelForCausalLM.from_pretrained(TEXT_MODEL) text_pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, device_map="auto" if device=="cuda" else None) # Image pipeline (optional) try: sd_pipe = StableDiffusionPipeline.from_pretrained(IMAGE_MODEL, torch_dtype=torch.float16 if device=="cuda" else torch.float32) sd_pipe = sd_pipe.to(device) has_sd = True except Exception as e: sd_pipe = None has_sd = False class TextReq(BaseModel): prompt: str max_len: int = 150 class ImageReq(BaseModel): prompt: str steps: int = 25 @app.post("/generate/text") def gen_text(req: TextReq): out = text_pipe(req.prompt, max_length=req.max_len, do_sample=True) return {"generated": out[0]["generated_text"]} @app.post("/generate/image") def gen_image(req: ImageReq): if not has_sd: return {"error": "Image model not loaded."} img = sd_pipe(req.prompt, num_inference_steps=req.steps).images[0] # save image temporarily and return path or return bytes (for demo keep simple) img.save("out.png") return {"path": "/out.png"}