Spaces:
Runtime error
Runtime error
File size: 1,544 Bytes
57913ca 9a4add7 57913ca 9a4add7 57913ca 9a4add7 57913ca 9a4add7 57913ca 9a4add7 57913ca 9a4add7 57913ca 9a4add7 57913ca 9a4add7 57913ca | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | 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"}
|