File size: 12,060 Bytes
0ab5288 79f81b1 0ab5288 c764f63 0ab5288 c764f63 0ab5288 39a3e39 0ab5288 39a3e39 79f81b1 39a3e39 0ab5288 |
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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
"""
UI界面模块
"""
import gradio as gr
from typing import List, Tuple, Optional
from .components import UIComponents
from ..api import VideoProcessor
from ..config import UI_CONFIG
class Veo3Interface:
"""Veo3界面类"""
def __init__(self):
self.components = UIComponents()
self.video_processor = VideoProcessor()
self.demo = None
def create_interface(self) -> gr.Blocks:
"""创建完整的界面"""
# 读取CSS样式
with open("static/css/styles.css", "r", encoding="utf-8") as f:
css = f.read()
print(f"CSS样式读取成功\n")
with gr.Blocks(css=css, theme=gr.themes.Base()) as demo:
self.demo = demo
# 页面头部
with gr.Column(elem_classes="header-container"):
self.components.create_header()
with gr.Column(elem_classes="main-content"):
# 信息提示框
self.components.create_info_box()
with gr.Row(equal_height=True):
# 左侧 - 输入区域
with gr.Column(scale=1):
# 输入组件(包含图片上传)
api_key, prompt, image_display, file_upload, delete_buttons, aspect_ratio, seeds, random_seed_btn, uploaded_file_state = self.components.create_input_components()
# 生成按钮
generate_btn = self.components.create_generate_button()
# 右侧 - 输出区域
with gr.Column(scale=1):
# 输出区域
output_video, status = self.components.create_output_section()
# 示例
examples = self.components.create_examples()
examples.inputs = [prompt, api_key]
# 设置事件处理器
self._setup_event_handlers(
generate_btn, prompt, api_key, aspect_ratio, seeds, random_seed_btn,
file_upload, image_display, delete_buttons, output_video, status, uploaded_file_state
)
# 添加JavaScript代码
self._add_javascript()
return demo
def _setup_event_handlers(
self,
generate_btn,
prompt,
api_key,
aspect_ratio,
seeds,
random_seed_btn,
file_upload,
image_display,
delete_buttons,
output_video,
status,
uploaded_file_state
):
"""设置事件处理器"""
# 文件上传处理器
self.components.setup_file_upload_handlers(file_upload, image_display, delete_buttons, uploaded_file_state)
# 删除按钮处理器
self.components.setup_delete_handlers(delete_buttons, image_display, uploaded_file_state)
# 随机种子按钮处理器
def generate_random_seed():
"""生成随机种子"""
import random
return random.randint(10000, 99999) # API要求的范围
random_seed_btn.click(
fn=generate_random_seed,
outputs=[seeds]
)
# 生成按钮事件
def prepare_and_generate(prompt, api_key, aspect_ratio, seeds, saved_file_path, progress=gr.Progress()):
"""生成视频的主函数"""
# 处理文件上传参数,如果为None则转换为空列表
file_paths = [saved_file_path] if saved_file_path is not None else []
return self.video_processor.process_veo3_video(
prompt, file_paths, api_key, aspect_ratio, None, seeds, False, progress
)
generate_btn.click(
fn=prepare_and_generate,
inputs=[prompt, api_key, aspect_ratio, seeds, uploaded_file_state],
outputs=[output_video, status]
)
def _add_javascript(self):
"""添加JavaScript代码"""
self.demo.load(None, None, None, js="""
() => {
// 创建全局删除函数
window.deleteImageByIndex = function(index) {
// Gradio的elem_id设置在包装器上,需要找到内部的button
let deleteBtn = null;
// 方法1: 通过ID找到包装器,然后找内部的button
const wrapper = document.getElementById(`delete-btn-${index}`);
if (wrapper) {
deleteBtn = wrapper.querySelector('button');
}
// 方法2: 如果方法1失败,查找所有按钮并通过文本内容匹配
if (!deleteBtn) {
const allButtons = document.querySelectorAll('button');
for (let btn of allButtons) {
if (btn.textContent.includes(`Delete Image ${index + 1}`)) {
deleteBtn = btn;
break;
}
}
}
if (deleteBtn) {
deleteBtn.click();
} else {
// 调试信息
document.querySelectorAll('[id^="delete-btn-"]').forEach(elem => {
console.log(elem.id, elem.tagName, elem.querySelector('button'));
});
}
};
// 美化文件上传区域
function enhanceFileUpload() {
const fileInputs = document.querySelectorAll('.gr-file');
fileInputs.forEach(input => {
// 替换中文文本为英文
const textElements = input.querySelectorAll('.wrap > div');
textElements.forEach((element, index) => {
const text = element.textContent.trim();
// 替换各种可能的中文文本
if (text.includes('将文件拖放到此处') || text.includes('拖放文件到此处')) {
element.textContent = 'Drag and drop files here';
} else if (text.includes('点击上传') || text.includes('点击选择文件')) {
element.textContent = 'or click to upload';
} else if (text.includes('- 或 -') || text.includes('或')) {
element.textContent = '- or -';
}
});
// 如果还有中文文本,直接替换整个内容
const wrap = input.querySelector('.wrap');
if (wrap) {
const allText = wrap.textContent;
if (allText.includes('将文件') || allText.includes('点击上传')) {
wrap.innerHTML = `
<div style="font-size: 0.9rem; font-weight: 500; color: #4a5568; margin: 0.5rem 0;">Drag and drop files here</div>
<div style="font-size: 0.8rem; color: #718096; margin: 0.3rem 0;">- or -</div>
<div style="font-size: 0.8rem; color: #718096; margin: 0.3rem 0;">or click to upload</div>
`;
}
}
// 添加拖拽事件监听
input.addEventListener('dragover', function(e) {
e.preventDefault();
this.classList.add('dragover');
});
input.addEventListener('dragleave', function(e) {
e.preventDefault();
this.classList.remove('dragover');
});
input.addEventListener('drop', function(e) {
e.preventDefault();
this.classList.remove('dragover');
});
// 监听文件选择
const fileInput = input.querySelector('input[type="file"]');
if (fileInput) {
fileInput.addEventListener('change', function() {
if (this.files && this.files.length > 0) {
input.classList.add('has-file');
} else {
input.classList.remove('has-file');
}
});
}
});
}
// 初始化美化
enhanceFileUpload();
// 使用定时器确保文本被替换
const textReplacer = setInterval(function() {
const fileInputs = document.querySelectorAll('.gr-file');
let hasChineseText = false;
fileInputs.forEach(input => {
const wrap = input.querySelector('.wrap');
if (wrap) {
const allText = wrap.textContent;
// 检查所有可能的中文文本
if (allText.includes('将文件') || allText.includes('点击上传') || allText.includes('拖放') ||
allText.includes('或') || allText.includes('此处')) {
hasChineseText = true;
wrap.innerHTML = `
<div style="font-size: 0.9rem; font-weight: 500; color: #4a5568; margin: 0.5rem 0;">Drag and drop files here</div>
<div style="font-size: 0.8rem; color: #718096; margin: 0.3rem 0;">- or -</div>
<div style="font-size: 0.8rem; color: #718096; margin: 0.3rem 0;">or click to upload</div>
`;
}
}
});
// 如果没有中文文本了,停止定时器
if (!hasChineseText) {
clearInterval(textReplacer);
}
}, 50); // 更频繁的检查
// 监听DOM变化,处理动态添加的元素
const observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.type === 'childList') {
mutation.addedNodes.forEach(function(node) {
if (node.nodeType === 1 && node.classList && node.classList.contains('gr-file')) {
enhanceFileUpload();
}
});
}
});
});
observer.observe(document.body, {
childList: true,
subtree: true
});
}
""")
def launch(self, share: bool = False, server_name: str = None, server_port: int = None):
"""启动应用"""
if server_name is None:
server_name = UI_CONFIG["SERVER_HOST"]
if server_port is None:
server_port = UI_CONFIG["SERVER_PORT"]
try:
self.demo.launch(
share=share,
server_name=server_name,
server_port=server_port,
)
except Exception as e:
print(f"❌ Launch failed: {e}")
print("🔄 Trying to restart with default configuration...")
try:
self.demo.launch(
share=False,
server_name="0.0.0.0",
server_port=7860,
show_error=True,
quiet=False
)
except Exception as e2:
print(f"❌ Restart also failed: {e2}")
print("💡 Please check if the port is occupied, or try another port")
raise
|