Update app.py
Browse files
app.py
CHANGED
@@ -11,231 +11,179 @@ import base64
|
|
11 |
from dotenv import load_dotenv
|
12 |
import gradio as gr
|
13 |
import random
|
|
|
|
|
|
|
14 |
|
|
|
15 |
load_dotenv()
|
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 |
-
def generate_video(
|
47 |
-
image,
|
48 |
-
prompt,
|
49 |
-
duration,
|
50 |
-
enable_safety,
|
51 |
-
flow_shift,
|
52 |
-
guidance_scale,
|
53 |
-
negative_prompt,
|
54 |
-
inference_steps,
|
55 |
-
seed,
|
56 |
-
size
|
57 |
-
):
|
58 |
-
API_KEY = os.getenv("WAVESPEED_API_KEY")
|
59 |
-
if not API_KEY:
|
60 |
-
yield "❌ Error: Missing API Key", None
|
61 |
-
return
|
62 |
-
|
63 |
try:
|
64 |
-
|
65 |
-
except
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
return
|
68 |
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
"
|
73 |
-
|
74 |
-
|
75 |
-
"negative_prompt": negative_prompt,
|
76 |
-
"num_inference_steps": inference_steps,
|
77 |
-
"prompt": prompt,
|
78 |
-
"seed": seed,
|
79 |
-
"size": size
|
80 |
-
}
|
81 |
-
|
82 |
-
headers = {
|
83 |
-
"Content-Type": "application/json",
|
84 |
-
"Authorization": f"Bearer {API_KEY}",
|
85 |
-
}
|
86 |
|
|
|
87 |
try:
|
88 |
-
|
89 |
-
|
90 |
-
headers=headers,
|
91 |
-
data=json.dumps(payload)
|
92 |
-
)
|
93 |
|
94 |
-
|
95 |
-
|
96 |
-
|
|
|
97 |
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
video_url = None
|
108 |
-
|
109 |
-
while True:
|
110 |
-
time.sleep(1)
|
111 |
-
try:
|
112 |
-
response = requests.get(result_url, headers={"Authorization": f"Bearer {API_KEY}"})
|
113 |
-
if response.status_code != 200:
|
114 |
-
yield f"❌ Polling Error ({response.status_code}): {response.text}", None
|
115 |
return
|
116 |
|
117 |
-
|
118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
yield f"❌ Failed: {data.get('error', 'Unknown error')}", None
|
129 |
-
return
|
130 |
-
|
131 |
-
else:
|
132 |
-
yield f"⏳ Status: {status.capitalize()}...", None
|
133 |
-
|
134 |
-
except Exception as e:
|
135 |
-
yield f"❌ Polling Failed: {str(e)}", None
|
136 |
-
return
|
137 |
-
|
138 |
-
# Gradio UI
|
139 |
-
with gr.Blocks(
|
140 |
-
theme=gr.themes.Soft(),
|
141 |
-
css="""
|
142 |
-
.video-preview {
|
143 |
-
max-width: 600px !important
|
144 |
-
}
|
145 |
-
.example-preview {
|
146 |
-
border: 1px solid #e0e0e0;
|
147 |
-
border-radius: 8px;
|
148 |
-
padding: 10px;
|
149 |
-
margin: 5px;
|
150 |
-
}
|
151 |
-
.example-preview img {
|
152 |
-
max-width: 200px;
|
153 |
-
max-height: 150px;
|
154 |
-
}
|
155 |
-
"""
|
156 |
-
) as app:
|
157 |
-
|
158 |
-
session_id = gr.State(None)
|
159 |
-
|
160 |
-
gr.Markdown("# 🌊 Wan-2.1-i2v-480p-Ultra-Fast Run On WaveSpeedAI")
|
161 |
-
|
162 |
-
gr.Markdown("""
|
163 |
-
[WaveSpeedAI](https://wavespeed.ai/) is the global pioneer in accelerating AI-powered video and image generation.
|
164 |
-
Our in-house inference accelerator provides lossless speedup on image & video generation based on our rich inference optimization software stack, including our in-house inference compiler, CUDA kernel libraries and parallel computing libraries.
|
165 |
-
""")
|
166 |
-
gr.Markdown("""
|
167 |
-
The Wan2.1 14B model is an advanced image-to-video model that offers accelerated inference capabilities, enabling high-res video generation with high visual quality and motion diversity.
|
168 |
-
""")
|
169 |
|
170 |
-
with gr.Row():
|
171 |
-
#
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
img_input = gr.Image(type="filepath", label="Upload Image")
|
176 |
-
prompt = gr.Textbox(label="Prompt", lines=5, placeholder="Describe your scene...")
|
177 |
-
negative_prompt = gr.Textbox(label="Negative Prompt", lines=2)
|
178 |
-
size = gr.Dropdown(["832*480"], value="832*480", label="Resolution")
|
179 |
-
steps = gr.Slider(1, 50, value=30, step=1, label="Inference Steps")
|
180 |
-
duration = gr.Slider(0, 10, value=5, step=5, label="Duration (seconds)")
|
181 |
-
guidance = gr.Slider(1, 30, value=5, step=0.1, label="Guidance Scale")
|
182 |
-
seed = gr.Number(-1, label="Seed")
|
183 |
-
random_seed_btn = gr.Button("🎲random seed", variant="secondary")
|
184 |
-
flow_shift = gr.Number(3, label="Flow Shift",interactive=False)
|
185 |
-
enable_safety = gr.Checkbox(True, label="Safety Checker",interactive=False)
|
186 |
-
# 左侧视频展示区域
|
187 |
-
with gr.Column(scale=1):
|
188 |
-
video_output = gr.Video(label="Generated Video",format="mp4",interactive=False,elem_classes=["video-preview"]
|
189 |
-
)
|
190 |
-
generate_btn = gr.Button("Generate Video", variant="primary")
|
191 |
-
output = gr.Textbox(label="Status", interactive=False, lines=4)
|
192 |
-
gr.Examples(
|
193 |
-
examples=[
|
194 |
-
[
|
195 |
-
"Victorian era, 19th-century gentleman wearing a black top hat and tuxedo, standing on a cobblestone street, dim gaslight lamps, passersby in vintage clothing, gentle breeze moving his coat, slow cinematic pan around him, nostalgic retro film style, realistic textures",
|
196 |
-
"https://d2g64w682n9w0w.cloudfront.net/media/images/1745725874603980753_95mFCAxu.jpg"
|
197 |
-
],
|
198 |
-
[
|
199 |
-
"A cyberpunk female warrior with short silver hair and glowing green eyes, wearing a futuristic armored suit, standing in a neon-lit rainy city street, camera slowly circling around her, raindrops falling in slow motion, neon reflections on wet pavement, cinematic atmosphere, highly detailed, ultra realistic, 4K",
|
200 |
-
"https://d2g64w682n9w0w.cloudfront.net/media/images/1745726299175719855_pFO0WSRM.jpg"
|
201 |
-
],
|
202 |
-
[
|
203 |
-
"Wide shot of a brave medieval female knight in shining silver armor and a red cape, standing on a castle rooftop at sunset, slowly drawing a large ornate sword from its scabbard, seen from a distance with the vast castle and surrounding landscape in the background, golden light bathing the scene, hair and cape flowing gently in the wind, cinematic epic atmosphere, dynamic motion, majestic clouds drifting, ultra realistic, high fantasy world, 4K ultra-detailed",
|
204 |
-
"https://d2g64w682n9w0w.cloudfront.net/media/images/1745727436576834405_rtsokheb.jpg"
|
205 |
-
],
|
206 |
-
[
|
207 |
-
"A girl stands in a lively 17th-century market. She holds a red tomato, looks gently into the camera and smiles briefly. Then, she glances at the tomato in her hand, slowly sets it back into the basket, turns around gracefully, and walks away with her back to the camera. The market around her is rich with colorful vegetables, meats hanging above, and bustling townsfolk. Golden-hour painterly lighting, subtle facial expressions, smooth cinematic motion, ultra-realistic detail, Vermeer-inspired style",
|
208 |
-
"https://d2g64w682n9w0w.cloudfront.net/media/images/1745079024013078406_QT6jKNPZ.png"
|
209 |
-
],
|
210 |
-
[
|
211 |
-
"A calming video explaining diabetes management and prevention tips to reduce anxiety.",
|
212 |
-
"https://d2g64w682n9w0w.cloudfront.net/predictions/517d518c28ef49ed9464610af48528f5/1.jpg"
|
213 |
-
],
|
214 |
-
[
|
215 |
-
"Girl dancing and spinning with friends.",
|
216 |
-
"https://d2g64w682n9w0w.cloudfront.net/media/d45e0d4893d44712b359f3ad0b3c2795/images/1745449961409630099_KISOKGEB.jpg"
|
217 |
-
]
|
218 |
-
],
|
219 |
-
inputs=[prompt, img_input], # 同时绑定到图片和提示输入框
|
220 |
-
label="Example Inputs",
|
221 |
-
examples_per_page=3
|
222 |
-
)
|
223 |
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
|
|
|
|
228 |
|
|
|
|
|
|
|
|
|
229 |
generate_btn.click(
|
230 |
-
|
231 |
-
inputs=[
|
232 |
-
|
233 |
-
outputs=[output, video_output]
|
234 |
)
|
235 |
|
|
|
236 |
if __name__ == "__main__":
|
237 |
-
|
238 |
-
|
239 |
-
max_threads=16,
|
240 |
-
debug=True
|
241 |
-
)
|
|
|
11 |
from dotenv import load_dotenv
|
12 |
import gradio as gr
|
13 |
import random
|
14 |
+
import torch
|
15 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
16 |
+
from functools import lru_cache
|
17 |
|
18 |
+
# 加载环境变量
|
19 |
load_dotenv()
|
20 |
|
21 |
+
# ==== 新增安全检测模块 ====
|
22 |
+
MODEL_URL = "TostAI/nsfw-text-detection-large"
|
23 |
+
CLASS_NAMES = {
|
24 |
+
0: "✅ SAFE",
|
25 |
+
1: "⚠️ QUESTIONABLE",
|
26 |
+
2: "🚫 UNSAFE"
|
27 |
+
}
|
28 |
+
|
29 |
+
# 加载模型和tokenizer
|
30 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_URL)
|
31 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL_URL)
|
32 |
+
|
33 |
+
@lru_cache(maxsize=128)
|
34 |
+
def classify_text(text):
|
35 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
36 |
+
with torch.no_grad():
|
37 |
+
outputs = model(**inputs)
|
38 |
+
return torch.argmax(outputs.logits, dim=1).item()
|
39 |
+
|
40 |
+
# ==== 会话管理模块 ====
|
41 |
+
class SessionManager:
|
42 |
+
_instances = {}
|
43 |
+
_lock = threading.Lock()
|
44 |
+
|
45 |
+
@classmethod
|
46 |
+
def get_session(cls, session_id):
|
47 |
+
with cls._lock:
|
48 |
+
if session_id not in cls._instances:
|
49 |
+
cls._instances[session_id] = {
|
50 |
+
'request_count': 0,
|
51 |
+
'last_request': time.time(),
|
52 |
+
'history': []
|
53 |
+
}
|
54 |
+
return cls._instances[session_id]
|
55 |
+
|
56 |
+
@classmethod
|
57 |
+
def cleanup_sessions(cls):
|
58 |
+
with cls._lock:
|
59 |
+
now = time.time()
|
60 |
+
expired = [k for k, v in cls._instances.items() if now - v['last_request'] > 3600]
|
61 |
+
for k in expired:
|
62 |
+
del cls._instances[k]
|
63 |
+
|
64 |
+
# ==== 频率限制模块 ====
|
65 |
+
class RateLimiter:
|
66 |
+
def __init__(self):
|
67 |
+
self.client_data = {}
|
68 |
+
|
69 |
+
def check_limit(self, client_id):
|
70 |
+
if client_id not in self.client_data:
|
71 |
+
self.client_data[client_id] = {
|
72 |
+
'count': 0,
|
73 |
+
'reset_time': time.time() + 3600
|
74 |
}
|
75 |
+
|
76 |
+
if time.time() > self.client_data[client_id]['reset_time']:
|
77 |
+
self.client_data[client_id] = {
|
78 |
+
'count': 0,
|
79 |
+
'reset_time': time.time() + 3600
|
80 |
+
}
|
81 |
+
|
82 |
+
if self.client_data[client_id]['count'] >= 20:
|
83 |
+
return False
|
84 |
+
self.client_data[client_id]['count'] += 1
|
85 |
+
return True
|
86 |
+
|
87 |
+
# ==== 错误处理模块 ====
|
88 |
+
def create_error_image(message):
|
89 |
+
img = Image.new("RGB", (832, 480), color="#ffdddd")
|
90 |
+
draw = ImageDraw.Draw(img)
|
91 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
try:
|
93 |
+
font = ImageFont.truetype("arial.ttf", 24)
|
94 |
+
except:
|
95 |
+
font = ImageFont.load_default()
|
96 |
+
|
97 |
+
text_width, text_height = draw.textsize(message, font=font)
|
98 |
+
x = (832 - text_width) / 2
|
99 |
+
y = (480 - text_height) / 2
|
100 |
+
|
101 |
+
draw.text((x, y), message, fill="#ff4444", font=font)
|
102 |
+
img.save("error.jpg")
|
103 |
+
return "error.jpg"
|
104 |
+
|
105 |
+
# ==== 核心生成逻辑 ====
|
106 |
+
def generate_video(/* 保持原有参数 */):
|
107 |
+
# 新增安全检测
|
108 |
+
safety_level = classify_text(prompt)
|
109 |
+
if safety_level != 0:
|
110 |
+
error_msg = f"Content blocked: {CLASS_NAMES[safety_level]}"
|
111 |
+
error_img = create_error_image(error_msg)
|
112 |
+
yield f"❌ {error_msg}", error_img
|
113 |
return
|
114 |
|
115 |
+
# 新增频率检查
|
116 |
+
session = SessionManager.get_session(session_id)
|
117 |
+
if session['request_count'] >= 20:
|
118 |
+
yield "❌ Hourly limit exceeded (20 requests)", None
|
119 |
+
return
|
120 |
+
session['request_count'] += 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
|
122 |
+
# 原有生成逻辑保持不变,增加状态跟踪
|
123 |
try:
|
124 |
+
# API调用部分
|
125 |
+
response = requests.post(/* 保持原有参数 */)
|
|
|
|
|
|
|
126 |
|
127 |
+
# 轮询部分增加进度跟踪
|
128 |
+
while True:
|
129 |
+
# 获取状态
|
130 |
+
status = get_status(request_id)
|
131 |
|
132 |
+
# 更新会话最后活动时间
|
133 |
+
session['last_request'] = time.time()
|
134 |
+
|
135 |
+
# 处理不同状态
|
136 |
+
if status == 'processing':
|
137 |
+
yield f"⏳ 生成进度: {progress}%", None
|
138 |
+
elif status == 'completed':
|
139 |
+
session['history'].append(video_url)
|
140 |
+
yield f"✅ 生成完成", video_url
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
return
|
142 |
|
143 |
+
except Exception as e:
|
144 |
+
error_img = create_error_image(str(e))
|
145 |
+
yield f"❌ 生成失败: {str(e)}", error_img
|
146 |
+
|
147 |
+
# ==== 新增定时清理任务 ====
|
148 |
+
def start_cleanup_task():
|
149 |
+
def cleanup():
|
150 |
+
while True:
|
151 |
+
SessionManager.cleanup_sessions()
|
152 |
+
time.sleep(3600)
|
153 |
|
154 |
+
thread = threading.Thread(target=cleanup)
|
155 |
+
thread.daemon = True
|
156 |
+
thread.start()
|
157 |
+
|
158 |
+
# ==== 界面增强 ====
|
159 |
+
with gr.Blocks(/* 保持原有参数 */) as app:
|
160 |
+
# 新增状态组件
|
161 |
+
status_bars = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
162 |
|
163 |
+
with gr.Row():
|
164 |
+
for backend in ["WAN-2.1", "FLUX", "TURBO"]: # 示例后端
|
165 |
+
with gr.Column():
|
166 |
+
gr.Markdown(f"**{backend}**")
|
167 |
+
status_bars[backend] = gr.Textbox(label="状态", value="🟢 空闲")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
168 |
|
169 |
+
# 在生成按钮点击时更新状态
|
170 |
+
generate_btn.click(
|
171 |
+
fn=update_status,
|
172 |
+
inputs=[...],
|
173 |
+
outputs=[status_bars]
|
174 |
+
)
|
175 |
|
176 |
+
# 新增历史记录模块
|
177 |
+
history = gr.Gallery(label="生成历史")
|
178 |
+
|
179 |
+
# 在生成完成后更新历史
|
180 |
generate_btn.click(
|
181 |
+
fn=update_history,
|
182 |
+
inputs=[video_output],
|
183 |
+
outputs=[history]
|
|
|
184 |
)
|
185 |
|
186 |
+
# ==== 启动时初始化 ====
|
187 |
if __name__ == "__main__":
|
188 |
+
start_cleanup_task()
|
189 |
+
app.queue(/* 保持原有参数 */)
|
|
|
|
|
|