test_gradio / app.py
amos1088's picture
test gradio
3aad7ee
raw
history blame
4.01 kB
import gradio as gr
from huggingface_hub import login
import os
import spaces,tempfile
import torch
from diffusers import AnimateDiffSparseControlNetPipeline
from diffusers.models import AutoencoderKL, MotionAdapter, SparseControlNetModel
from diffusers.schedulers import DPMSolverMultistepScheduler
from diffusers.utils import export_to_gif, load_image
from diffusers import AutoPipelineForText2Image
import openai,json
import torch
from diffusers.models import MotionAdapter
from diffusers import AnimateDiffSDXLPipeline, DDIMScheduler
from diffusers.utils import export_to_gif
token = os.getenv("HF_TOKEN")
login(token=token)
openai_token = os.getenv("OPENAI_TOKEN")
openai.api_key = openai_token
openaiclient = openai.OpenAI(api_key=openai.api_key)
def ask_gpt(massage_history,model="gpt-4o-mini",return_str=True,response_format={"type": "json_object"}):
response = openaiclient.chat.completions.create(
model=model,
messages=massage_history,
response_format=response_format,
max_tokens=4000, )
if return_str:
return response.choices[0].message.content
else:
return json.loads(response.choices[0].message.content)
image_pipeline = AutoPipelineForText2Image.from_pretrained("stabilityai/sdxl-turbo", torch_dtype=torch.float16).to("cuda")
image_pipeline.load_ip_adapter("h94/IP-Adapter", subfolder="sdxl_models", weight_name="ip-adapter_sdxl.bin")
@spaces.GPU
def generate_image(prompt, reference_image, controlnet_conditioning_scale):
style_images = [load_image(f.name) for f in reference_image]
image_pipeline.set_ip_adapter_scale(controlnet_conditioning_scale)
image = image_pipeline(
prompt=prompt,
ip_adapter_image=[style_images],
negative_prompt="",
guidance_scale=5,
num_inference_steps=30,
).images[0]
return image
vae_id = "stabilityai/sd-vae-ft-mse"
device = "cuda"
adapter = MotionAdapter.from_pretrained(
"a-r-r-o-w/animatediff-motion-adapter-sdxl-beta", torch_dtype=torch.float16
)
model_id = "stabilityai/sdxl-turbo"
scheduler = DDIMScheduler.from_pretrained(
model_id,
subfolder="scheduler",
clip_sample=False,
timestep_spacing="linspace",
beta_schedule="linear",
steps_offset=1,
)
gif_pipe = AnimateDiffSDXLPipeline.from_pretrained(
model_id,
motion_adapter=adapter,
scheduler=scheduler,
torch_dtype=torch.float16,
variant="fp16",
).to("cuda")
# enable memory savings
gif_pipe.enable_vae_slicing()
gif_pipe.enable_vae_tiling()
gif_pipe.load_ip_adapter("h94/IP-Adapter", subfolder="sdxl_models", weight_name="ip-adapter_sdxl.bin")
@spaces.GPU
def generate_gif(prompt, reference_image, controlnet_conditioning_scale,style_conditioning_scale,num_frames):
image= generate_image(prompt, reference_image, float(style_conditioning_scale))
video = gif_pipe(
prompt=prompt,
ip_adapter_image=[image],
negative_prompt="low quality, worst quality",
num_inference_steps=25,
guidance_scale=8,
num_frames=int(num_frames)
).frames[0]
export_to_gif(video, "output.gif")
yield (conditioning_frames, "output.gif")
# Set up Gradio interface
interface = gr.Interface(
fn=generate_gif,
inputs=[
gr.Textbox(label="Prompt"),
# gr.Image( type= "filepath",label="Reference Image (Style)"),
gr.File(type="filepath",file_count="multiple",label="Reference Image (Style)"),
gr.Slider(label="Control Net Conditioning Scale", minimum=0, maximum=1.0, step=0.1, value=1.0),
gr.Slider(label="Style Scale", minimum=0, maximum=1.0, step=0.1, value=0.6),
gr.Slider(label="Number of frames", minimum=0, maximum=100.0, step=1.0, value=10.0),
],
outputs=["gallery","image"],
title="Image Generation with Stable Diffusion 3 medium and ControlNet",
description="Generates an image based on a text prompt and a reference image using Stable Diffusion 3 medium with ControlNet."
)
interface.launch()