File size: 3,840 Bytes
4f7b5ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24e33d6
 
 
 
 
 
 
 
 
 
 
 
 
 
4f7b5ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# -*- 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)