Spaces:
Runtime error
Runtime error
File size: 10,322 Bytes
432a7d3 4b223c8 e8bac0f 04e9db1 432a7d3 b89f79c 5355a5a eb296c1 a523f20 31b572b c06a9b5 42b376b c399698 35f31dc cf6167a a523f20 e8bac0f cf6167a a523f20 cf6167a a523f20 432a7d3 216c05e 0866905 432a7d3 0866905 432a7d3 0866905 7a71ed0 432a7d3 0866905 432a7d3 f62fb29 eb296c1 432a7d3 f667345 432a7d3 c399698 0866905 02e550e d98e648 a4a30c8 c06a9b5 a4a30c8 c06a9b5 a4a30c8 0866905 432a7d3 c06a9b5 0866905 02e550e 7a327e7 fa5952c c06a9b5 f667345 0866905 02e550e fa5952c 02e550e fa5952c 02e550e dcb78ae 682535d 5843541 c399698 17a97e6 c399698 17a97e6 1c96884 5843541 42b376b c399698 3cc66f4 f667345 3cc66f4 eb00a3f c399698 eb00a3f 3cc66f4 f667345 eb00a3f 3cc66f4 eb00a3f 3cc66f4 c399698 |
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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
import gradio as gr
from gradio_client import Client
import os
import logging
import json
from datetime import datetime
import tempfile
import numpy as np
from PIL import Image
import shutil
import httpx
import time
import base64
from gradio_client import Client, handle_file
import cv2
from moviepy.editor import VideoFileClip
# λ‘κΉ
μ€μ
logging.basicConfig(level=logging.INFO)
# νμμμ μ€μ μ 30μ΄λ‘ λλ¦Ό
httpx_client = httpx.Client(timeout=30.0)
max_retries = 3
retry_delay = 5 # 5μ΄ λκΈ°
for attempt in range(max_retries):
try:
api_client = Client("http://211.233.58.202:7960/")
api_client.httpx_client = httpx_client # httpx ν΄λΌμ΄μΈνΈ μ€μ
break # μ±κ³΅νλ©΄ 루ν μ’
λ£
except httpx.ReadTimeout:
if attempt < max_retries - 1: # λ§μ§λ§ μλκ° μλλ©΄
print(f"Connection timed out. Retrying in {retry_delay} seconds...")
time.sleep(retry_delay)
else:
print("Failed to connect after multiple attempts.")
raise # λͺ¨λ μλ μ€ν¨ μ μμΈ λ°μ
# κ°€λ¬λ¦¬ μ μ₯ λλ ν 리 μ€μ
GALLERY_DIR = "gallery"
GALLERY_JSON = "gallery.json"
# κ°€λ¬λ¦¬ λλ ν 리 μμ±
os.makedirs(GALLERY_DIR, exist_ok=True)
def save_to_gallery(video_path, prompt):
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
new_video_path = os.path.join(GALLERY_DIR, f"{timestamp}.mp4")
# λΉλμ€ νμΌ λ³΅μ¬
shutil.copy2(video_path, new_video_path)
# κ°€λ¬λ¦¬ μ 보 μ μ₯
gallery_info = {
"video": new_video_path,
"prompt": prompt,
"timestamp": timestamp
}
if os.path.exists(GALLERY_JSON):
with open(GALLERY_JSON, "r") as f:
gallery = json.load(f)
else:
gallery = []
gallery.append(gallery_info)
with open(GALLERY_JSON, "w") as f:
json.dump(gallery, f, indent=2)
return new_video_path
def load_gallery():
if os.path.exists(GALLERY_JSON):
with open(GALLERY_JSON, "r") as f:
gallery = json.load(f)
return [{"image": item["video"], "caption": item["prompt"]} for item in reversed(gallery)]
return []
def update_gallery():
gallery_items = load_gallery()
return [
{"video": item['image'], "caption": item['caption']}
for item in gallery_items
]
def respond(image, prompt, steps, cfg_scale, eta, fs, seed, video_length):
logging.info(f"Received prompt: {prompt}, steps: {steps}, cfg_scale: {cfg_scale}, "
f"eta: {eta}, fs: {fs}, seed: {seed}, video_length: {video_length}")
try:
# μ΄λ―Έμ§ νμΌ μ²λ¦¬
if image is not None:
image_file = handle_file(image)
else:
image_file = None
# λΉλμ€ μμ± μμ²
result = api_client.predict(
image=image_file,
prompt=prompt,
steps=steps,
cfg_scale=cfg_scale,
eta=eta,
fs=fs,
seed=seed,
video_length=video_length,
api_name="/infer"
)
logging.info("API response received: %s", result)
if isinstance(result, dict) and 'video' in result:
saved_video_path = save_to_gallery(result['video'], prompt)
return saved_video_path
else:
raise ValueError("μμμΉ λͺ»ν API μλ΅ νμ")
except Exception as e:
logging.error("API μμ² μ€ μ€λ₯ λ°μ: %s", str(e))
return "μ€λ₯λ‘ μΈν΄ λΉλμ€ μμ±μ μ€ν¨νμ΅λλ€."
css = """
footer {
visibility: hidden;
}
"""
examples = [
["A glamorous young woman with long, wavy blonde hair and smokey eye makeup, posing in a luxury hotel room. She's wearing a sparkly gold cocktail dress and holding up a white card with 'openfree.ai' written on it in elegant calligraphy. Soft, warm lighting creates a luxurious atmosphere. ", "q1.webp"],
["A fit male fitness influencer with short dark hair and stubble, standing shirtless in a modern gym. He has defined abs and arm muscles, and is holding a protein shake in one hand and a card that says 'openfree.ai' in the other. Bright, clean lighting highlights his physique.", "q2.webp"],
["A bohemian-style female travel blogger with sun-kissed skin and messy beach waves, sitting on a tropical beach at sunset. She's wearing a flowy white sundress and holding up a weathered postcard with 'openfree.ai' scrawled on it. Golden hour lighting bathes the scene in warm tones. ", "q3.webp"],
["A trendy male fashion influencer with perfectly styled hair and designer stubble, posing on a city street. He's wearing a tailored suit and holding up a sleek black business card with 'openfree.ai' printed in minimalist white font. The background shows blurred city lights, creating a chic urban atmosphere.", "q4.webp"],
["A fresh-faced young female beauty guru with freckles and natural makeup, sitting at a vanity covered in cosmetics. She's wearing a pastel pink robe and holding up a makeup palette with 'openfree.ai' written on it in lipstick. Soft, flattering lighting enhances her radiant complexion. ", "q5.webp"],
["A stylish young woman with long, wavy ombre hair and winged eyeliner, posing in front of a neon-lit city skyline at night. She's wearing a sleek black leather jacket over a sparkly crop top and holding up a holographic business card that says 'openfree.ai' in futuristic font. The card reflects the colorful neon lights, creating a cyberpunk aesthetic.", "q6.webp"],
["Create a surreal advertisement poster for a fictional time travel agency. The background should depict a swirling vortex of clock faces and historical landmarks from different eras. In the foreground, place large, bold text that reads "AI TOURS: YOUR PAST IS OUR FUTURE" in a retro-futuristic font. The text should appear to be partially disintegrating into particles that are being sucked into the time vortex. Include smaller text at the bottom with fictional pricing and the slogan "History is just a ticket away!"", "q7.webp"],
["Photo realistic scene inspired by LOTR: [A tiny red dragon sleeps curled up in a nest on a medieval wizard's table]. Shot with a macro lens (f/2.8, 50mm) and a Canon EOSR5, the soft focus captures [the cozy morning light filtering through a near by window]. The pastel colors and whimsical steam shapes enhance the serene atmosphere, evoking a DnD RPG setting. The image is rendered in 16K and 8K, highlighting [the intricate details and medieval charm].", "q8.webp"],
["μ¬κΈλΌμ€λ₯Ό μ°©μ©ν κ·μ¬μ΄ κ°μμ§κ° 'Openfree.ai'λΌκ³ μ°μ¬μ§ νμ§νμ λ€κ³ μλ€.", "q9.webp"],
["λ―Έλ μ μΈ λμμ ν΄μ§λ νκ²½", "q10.webp"],
["λΉ¨κ°μ λ°μ€μ 'openfree.ai'λΌκ³ κΈμ¨κ° μ°μ¬μκ³ , κ·Έμμ κ°μμ§μ κ³ μμ΄κ° μμμλ€.", "q11.webp"],
["λ‘λ΄μ΄ 곡μ¬μ₯μμ μμ
μ€μ΄λ€. λ°°κ²½μ 'Coming soon~'λΌλ κΈμκ° μ°μ¬μλ€.", "q20.webp"],
["A serene landscape with mountains in the background and a clear lake in the foreground.", "q12.webp"],
["A street scene from Tokyo at night, vibrant and full of lights.", "q13.webp"],
["An astronaut riding a horse on Mars.", "q14.webp"],
["A surreal painting of a tree growing books.", "q15.webp"],
["A cottage in a snowy forest, lit by warm lights.", "q16.webp"],
["A still life of various fruits and a wine glass on a table.", "q17.webp"],
["A digital artwork of a neon-lit alley in a cyberpunk city.", "q18.webp"],
["A fantasy map of a fictional world, with detailed terrain and cities.", "q19.webp"]
]
def use_prompt_and_image(prompt, image):
return prompt, image
def show_video(evt: gr.SelectData):
return evt.value["video"] if isinstance(evt.value, dict) and "video" in evt.value else None
with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css) as demo:
with gr.Tab("Generate"):
with gr.Row():
input_image = gr.Image(label="Upload an image", type="filepath")
input_text = gr.Textbox(label="Enter your prompt for video generation")
output_video = gr.Video(label="Generated Video")
with gr.Row():
steps = gr.Slider(minimum=1, maximum=100, step=1, label="Steps", value=30)
cfg_scale = gr.Slider(minimum=1, maximum=15, step=0.1, label="CFG Scale", value=3.5)
eta = gr.Slider(minimum=0, maximum=1, step=0.1, label="ETA", value=1)
fs = gr.Slider(minimum=1, maximum=30, step=1, label="FPS", value=8)
seed = gr.Slider(minimum=0, maximum=1000000, step=1, label="Seed", value=123)
video_length = gr.Slider(minimum=1, maximum=10, step=1, label="Video Length (seconds)", value=2)
with gr.Row():
for prompt, image_file in examples:
with gr.Column():
gr.Image(image_file, label=prompt[:50] + "...")
gr.Button("Use this example").click(
fn=use_prompt_and_image,
inputs=[],
outputs=[input_text, input_image],
api_name=False
).then(
lambda p=prompt, i=image_file: (p, i),
inputs=[],
outputs=[input_text, input_image]
)
with gr.Tab("Gallery"):
gallery = gr.Gallery(
label="Generated Videos",
show_label=False,
elem_id="gallery",
columns=[5],
rows=[3],
object_fit="contain",
height="auto",
preview=True
)
selected_video = gr.Video(label="Selected Video")
refresh_btn = gr.Button("Refresh Gallery")
refresh_btn.click(fn=update_gallery, inputs=None, outputs=gallery)
demo.load(fn=update_gallery, inputs=None, outputs=gallery)
gallery.select(show_video, None, selected_video)
input_text.submit(
fn=respond,
inputs=[input_image, input_text, steps, cfg_scale, eta, fs, seed, video_length],
outputs=output_video
).then(
fn=update_gallery,
inputs=None,
outputs=gallery
)
if __name__ == "__main__":
demo.launch() |