|
|
import gradio as gr |
|
|
|
|
|
def greet(name): |
|
|
return "Hello " + name + "!!" |
|
|
|
|
|
demo = gr.Interface(fn=greet, inputs="text", outputs="text") |
|
|
demo.launch() |
|
|
from fastapi import FastAPI, HTTPException |
|
|
from pydantic import BaseModel |
|
|
import spaces |
|
|
from diffusers import DiffusionPipeline, EulerAncestralDiscreteScheduler, EulerDiscreteScheduler |
|
|
import torch |
|
|
import os |
|
|
from datetime import datetime |
|
|
from PIL import Image |
|
|
import boto3 |
|
|
from botocore.exceptions import NoCredentialsError |
|
|
from dotenv import load_dotenv |
|
|
|
|
|
|
|
|
load_dotenv() |
|
|
|
|
|
|
|
|
AWS_ACCESS_KEY = os.getenv('AWS_ACCESS_KEY') |
|
|
AWS_SECRET_KEY = os.getenv('AWS_SECRET_KEY') |
|
|
AWS_BUCKET_NAME = os.getenv('AWS_BUCKET_NAME') |
|
|
AWS_REGION = os.getenv('AWS_REGION') |
|
|
HF_TOKEN = os.getenv('HF_TOKEN') |
|
|
|
|
|
|
|
|
s3_client = boto3.client( |
|
|
's3', |
|
|
aws_access_key_id=AWS_ACCESS_KEY, |
|
|
aws_secret_access_key=AWS_SECRET_KEY, |
|
|
region_name=AWS_REGION |
|
|
) |
|
|
|
|
|
|
|
|
character_pipe = DiffusionPipeline.from_pretrained( |
|
|
"cagliostrolab/animagine-xl-3.1", |
|
|
torch_dtype=torch.float16, |
|
|
use_safetensors=True, |
|
|
use_auth_token=HF_TOKEN |
|
|
) |
|
|
character_pipe.scheduler = EulerDiscreteScheduler.from_config(character_pipe.scheduler.config) |
|
|
|
|
|
|
|
|
item_pipe = DiffusionPipeline.from_pretrained( |
|
|
"openart-custom/DynaVisionXL", |
|
|
torch_dtype=torch.float16, |
|
|
use_safetensors=True, |
|
|
use_auth_token=HF_TOKEN |
|
|
) |
|
|
item_pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(item_pipe.scheduler.config) |
|
|
|
|
|
|
|
|
@spaces.GPU(duration=60) |
|
|
def generate_image(model_type, prompt, negative_prompt, width, height, guidance_scale, num_inference_steps): |
|
|
if model_type == "character": |
|
|
pipe = character_pipe |
|
|
default_prompt = "1girl, souji okita, fate series, solo, upper body, bedroom, night, seducing, (sexy clothes)" |
|
|
default_negative_prompt = "lowres, (bad), text, error, fewer, extra, missing, worst quality, jpeg artifacts, low quality, watermark, unfinished, displeasing, oldest, early, chromatic aberration, signature, extra digits, artistic error, username, scan, [abstract]" |
|
|
elif model_type == "item": |
|
|
pipe = item_pipe |
|
|
default_prompt = "great sword, runes on blade, acid on blade, weapon, (((item)))" |
|
|
default_negative_prompt = "1girl, girl, man, boy, 1man, men, girls" |
|
|
else: |
|
|
return "Invalid type. Choose between 'character' or 'item'." |
|
|
|
|
|
|
|
|
final_prompt = prompt if prompt else default_prompt |
|
|
final_negative_prompt = negative_prompt if negative_prompt else default_negative_prompt |
|
|
|
|
|
|
|
|
pipe.to("cuda") |
|
|
|
|
|
|
|
|
image = pipe( |
|
|
prompt=final_prompt, |
|
|
negative_prompt=final_negative_prompt, |
|
|
width=int(width), |
|
|
height=int(height), |
|
|
guidance_scale=float(guidance_scale), |
|
|
num_inference_steps=int(num_inference_steps) |
|
|
).images[0] |
|
|
|
|
|
|
|
|
temp_file = "/tmp/generated_image.png" |
|
|
image.save(temp_file) |
|
|
|
|
|
|
|
|
file_name = datetime.now().strftime("%Y%m%d_%H%M%S") + ".png" |
|
|
try: |
|
|
s3_client.upload_file(temp_file, AWS_BUCKET_NAME, file_name) |
|
|
s3_url = f"https://{AWS_BUCKET_NAME}.s3.{AWS_REGION}.amazonaws.com/{file_name}" |
|
|
return s3_url |
|
|
except NoCredentialsError: |
|
|
return "Credentials not available" |
|
|
|
|
|
|
|
|
app = FastAPI() |
|
|
|
|
|
|
|
|
class PredictRequest(BaseModel): |
|
|
model_type: str |
|
|
prompt: str = "" |
|
|
negative_prompt: str = "" |
|
|
width: int |
|
|
height: int |
|
|
guidance_scale: float |
|
|
num_inference_steps: int |
|
|
|
|
|
|
|
|
@app.get("/") |
|
|
def read_root(): |
|
|
return {"Hello World"} |
|
|
|
|
|
@app.post("/api/predict") |
|
|
async def predict(request: PredictRequest): |
|
|
result = generate_image( |
|
|
model_type=request.model_type, |
|
|
prompt=request.prompt, |
|
|
negative_prompt=request.negative_prompt, |
|
|
width=request.width, |
|
|
height=request.height, |
|
|
guidance_scale=request.guidance_scale, |
|
|
num_inference_steps=request.num_inference_steps |
|
|
) |
|
|
if result is None: |
|
|
raise HTTPException(status_code=400, detail="Invalid input") |
|
|
return {"result": result} |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
import uvicorn |
|
|
uvicorn.run(app, host="0.0.0.0", port=7860) |