AnisoraV3 / app_os.py
nyarunyarunya's picture
Update app_os.py
24e33d6 verified
# -*- coding: utf-8 -*-
"""
THis is the main file for the gradio web demo. It uses the CogVideoX-5B model to generate videos gradio web demo.
set environment variable OPENAI_API_KEY to use the OpenAI API to enhance the prompt.
Usage:
OpenAI_API_KEY=your_openai_api_key OPENAI_BASE_URL=https://api.openai.com/v1 python inference/gradio_web_demo.py
"""
import logging
import math
import os
import sys
from fastapi.responses import PlainTextResponse
from PIL import Image
from huggingface_hub.utils.tqdm import progress_bar_states
from numpy import ndarray
current_dir = os.path.abspath(os.path.dirname(__file__))
sys.path.append(os.path.join(current_dir, '../'))
import random
import threading
import time
import cv2
import tempfile
import imageio_ffmpeg
import gradio as gr
from datetime import datetime, timedelta
os.makedirs("./output", exist_ok=True)
os.makedirs("./input", exist_ok=True)
os.makedirs("./gradio_tmp", exist_ok=True)
def delete_old_files():
while True:
now = datetime.now()
cutoff = now - timedelta(minutes=10)
directories = ["./output", "./gradio_tmp"]
for directory in directories:
for filename in os.listdir(directory):
file_path = os.path.join(directory, filename)
if os.path.isfile(file_path):
file_mtime = datetime.fromtimestamp(os.path.getmtime(file_path))
if file_mtime < cutoff:
os.remove(file_path)
time.sleep(600)
threading.Thread(target=delete_old_files, daemon=True).start()
class DEMO:
def __init__(self,generate):
with gr.Blocks() as self.demo:
gr.Markdown("""
<div style="text-align: center; font-size: 32px; font-weight: bold; margin-bottom: 20px;">
AniSora-BilibiliεŠ¨η”»θ§†ι’‘η”Ÿζˆζ¨‘εž‹
</div>
""")
with gr.Row():
with gr.Column():
with gr.Accordion("I2V: Image Input (cannot be used simultaneously with video input)", open=True):
image_input = gr.Image(label="Input Image")
prompt = gr.Textbox(label="Prompt (Less than 200 Words)", placeholder="Enter your prompt here", lines=5)
nf = gr.Slider(label="η§’ζ•°", minimum=3,maximum=5, step=0.5, value=5)
speed = gr.Radio(label="εŠ ι€Ÿζ¨‘εΌ",value='εŠ ι€Ÿη‰ˆ',choices=['εŽŸη‰ˆ','εŠ ι€Ÿη‰ˆ'])
with gr.Group():
with gr.Column():
with gr.Row():
seed_param = gr.Number(
label="Inference Seed (Enter a positive number, -1 for random)", value=233
)
generate_button = gr.Button("🎬 Generate Video")
with gr.Column():
video_output = gr.Video(label="Generated Video")
with gr.Row():
download_video_button = gr.File(label="πŸ“₯ Download Video", visible=False)
seed_text = gr.Number(label="Seed Used for Video Generation", visible=False)
generate_button.click(
generate,
inputs=[prompt, image_input,seed_param,nf,speed],
outputs=[video_output, download_video_button, seed_text],
)
if __name__ == "__main__":
from fastapi import FastAPI
import uvicorn
app = FastAPI()
@app.get('/v2/health/ready')
def health():
return ""
demoo=DEMO()
demo=demoo.demo
demo.queue(max_size=15)
app = gr.mount_gradio_app(app,demo, path="/api/adhoc/ttv/demo")
uvicorn.run(app,host="0.0.0.0",port=26780)#
# demo.launch(server_name="0.0.0.0",server_port=16780)