jiandan1998 commited on
Commit
1c95961
·
verified ·
1 Parent(s): 9216555

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +312 -0
app.py CHANGED
@@ -0,0 +1,312 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import requests
3
+ import json
4
+ import time
5
+ import threading
6
+ import uuid
7
+ import shutil
8
+ import base64
9
+ from datetime import datetime
10
+ from pathlib import Path
11
+ from http.server import HTTPServer, SimpleHTTPRequestHandler
12
+ from dotenv import load_dotenv
13
+ import gradio as gr
14
+ import random
15
+ import torch
16
+ from PIL import Image, ImageDraw, ImageFont
17
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
18
+ from functools import lru_cache
19
+
20
+ load_dotenv()
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 = AutoTokenizer.from_pretrained(MODEL_URL)
30
+ model = AutoModelForSequenceClassification.from_pretrained(MODEL_URL)
31
+
32
+ class SessionManager:
33
+ _instances = {}
34
+ _lock = threading.Lock()
35
+
36
+ @classmethod
37
+ def get_session(cls, session_id):
38
+ with cls._lock:
39
+ if session_id not in cls._instances:
40
+ cls._instances[session_id] = {
41
+ 'count': 0,
42
+ 'history': [],
43
+ 'last_active': time.time()
44
+ }
45
+ return cls._instances[session_id]
46
+
47
+ @classmethod
48
+ def cleanup_sessions(cls):
49
+ with cls._lock:
50
+ now = time.time()
51
+ expired = [k for k, v in cls._instances.items() if now - v['last_active'] > 3600]
52
+ for k in expired:
53
+ del cls._instances[k]
54
+
55
+ class RateLimiter:
56
+ def __init__(self):
57
+ self.clients = {}
58
+ self.lock = threading.Lock()
59
+
60
+ def check(self, client_id):
61
+ with self.lock:
62
+ now = time.time()
63
+ if client_id not in self.clients:
64
+ self.clients[client_id] = {'count': 1, 'reset': now + 3600}
65
+ return True
66
+
67
+ if now > self.clients[client_id]['reset']:
68
+ self.clients[client_id] = {'count': 1, 'reset': now + 3600}
69
+ return True
70
+
71
+ if self.clients[client_id]['count'] >= 20:
72
+ return False
73
+
74
+ self.clients[client_id]['count'] += 1
75
+ return True
76
+
77
+ session_manager = SessionManager()
78
+ rate_limiter = RateLimiter()
79
+
80
+ def image_to_base64(file_path):
81
+ try:
82
+ with open(file_path, "rb") as f:
83
+ img_data = f.read()
84
+ if len(img_data) == 0:
85
+ raise ValueError("空文件")
86
+
87
+ # 使用URL安全编码并自动填充
88
+ encoded = base64.urlsafe_b64encode(img_data)
89
+ missing_padding = len(encoded) % 4
90
+ if missing_padding:
91
+ encoded += b'=' * (4 - missing_padding)
92
+
93
+ ext = Path(file_path).suffix.lower()[1:]
94
+ mime_map = {'jpg':'jpeg','jpeg':'jpeg','png':'png','webp':'webp','gif':'gif'}
95
+ mime = mime_map.get(ext, 'jpeg')
96
+ return f"data:image/{mime};base64,{encoded.decode()}"
97
+ except Exception as e:
98
+ raise ValueError(f"Base64 Error: {str(e)}")
99
+
100
+ def create_error_image(message):
101
+ img = Image.new("RGB", (832, 480), "#ffdddd")
102
+ try:
103
+ font = ImageFont.truetype("arial.ttf", 24)
104
+ except:
105
+ font = ImageFont.load_default()
106
+
107
+ draw = ImageDraw.Draw(img)
108
+ text = f"Error: {message[:60]}..." if len(message) > 60 else message
109
+ draw.text((50, 200), text, fill="#ff0000", font=font)
110
+ img.save("error.jpg")
111
+ return "error.jpg"
112
+
113
+ @lru_cache(maxsize=100)
114
+ def classify_prompt(prompt):
115
+ inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=512)
116
+ with torch.no_grad():
117
+ outputs = model(**inputs)
118
+ return torch.argmax(outputs.logits).item()
119
+
120
+ def generate_video(
121
+ image,
122
+ prompt,
123
+ duration,
124
+ enable_safety,
125
+ flow_shift,
126
+ guidance,
127
+ negative_prompt,
128
+ steps,
129
+ seed,
130
+ size,
131
+ session_id
132
+ ):
133
+
134
+ safety_level = classify_prompt(prompt)
135
+ if safety_level != 0:
136
+ error_img = create_error_image(CLASS_NAMES[safety_level])
137
+ yield f"❌ Blocked: {CLASS_NAMES[safety_level]}", error_img
138
+ return
139
+
140
+ if not rate_limiter.check(session_id):
141
+ error_img = create_error_image("Hourly limit exceeded (20 requests)")
142
+ yield "❌ 请求过于频繁,请稍后再试", error_img
143
+ return
144
+
145
+ session = session_manager.get_session(session_id)
146
+ session['last_active'] = time.time()
147
+ session['count'] += 1
148
+
149
+ try:
150
+ api_key = os.getenv("WAVESPEED_API_KEY")
151
+ if not api_key:
152
+ raise ValueError("API key missing")
153
+
154
+ base64_img = image_to_base64(image)
155
+ headers = {
156
+ "Authorization": f"Bearer {api_key}",
157
+ "Content-Type": "application/json"
158
+ }
159
+
160
+ payload = {
161
+ "context_scale": 1,
162
+ "enable_safety_checker": True,
163
+ "flow_shift": flow_shift,
164
+ "guidance_scale": guidance,
165
+ "images": [base64_img],
166
+ "negative_prompt": negative_prompt,
167
+ "num_inference_steps": steps,
168
+ "prompt": prompt,
169
+ "seed": seed if seed != -1 else random.randint(0, 999999),
170
+ "size": "480*832"
171
+ }
172
+
173
+ response = requests.post(
174
+ "https://api.wavespeed.ai/api/v3/wavespeed-ai/wan-2.1-14b-vace",
175
+ headers=headers,
176
+ json=payload
177
+ )
178
+
179
+ if response.status_code != 200:
180
+ raise Exception(f"API Error {response.status_code}: {response.text}")
181
+
182
+ requestId = response.json()["data"]["id"]
183
+ yield f"✅ 任务已提交 (ID: {requestId})", None
184
+
185
+ except Exception as e:
186
+ error_img = create_error_image(str(e))
187
+ yield f"❌ 提交失败: {str(e)}", error_img
188
+ return
189
+
190
+ result_url = f"https://api.wavespeed.ai/api/v3/predictions/{requestId}/result"
191
+ start_time = time.time()
192
+
193
+ while True:
194
+ time.sleep(1)
195
+ try:
196
+ resp = requests.get(result_url, headers=headers)
197
+ if resp.status_code != 200:
198
+ raise Exception(f"状态查询失败: {resp.text}")
199
+
200
+ data = resp.json()["data"]
201
+ status = data["status"]
202
+
203
+ if status == "completed":
204
+ elapsed = time.time() - start_time
205
+ video_url = data["outputs"][0]
206
+ session["history"].append(video_url)
207
+ yield f"🎉 生成成功! 耗时 {elapsed:.1f}s", video_url
208
+ return
209
+
210
+ elif status == "failed":
211
+ raise Exception(data.get("error", "Unknown error"))
212
+
213
+ else:
214
+ yield f"⏳ 当前状态: {status.capitalize()}...", None
215
+
216
+ except Exception as e:
217
+ error_img = create_error_image(str(e))
218
+ yield f"❌ 生成失败: {str(e)}", error_img
219
+ return
220
+
221
+ def cleanup_task():
222
+ while True:
223
+ session_manager.cleanup_sessions()
224
+ time.sleep(3600)
225
+
226
+ with gr.Blocks(
227
+ theme=gr.themes.Soft(),
228
+ css="""
229
+ .video-preview { max-width: 600px !important; }
230
+ .status-box { padding: 10px; border-radius: 5px; margin: 5px; }
231
+ .safe { background: #e8f5e9; border: 1px solid #a5d6a7; }
232
+ .warning { background: #fff3e0; border: 1px solid #ffcc80; }
233
+ .error { background: #ffebee; border: 1px solid #ef9a9a; }
234
+ """
235
+ ) as app:
236
+
237
+ session_id = gr.State(str(uuid.uuid4()))
238
+
239
+ gr.Markdown("# 🌊 Wan-2.1-i2v-480p-Ultra-Fast Run On WaveSpeedAI")
240
+ gr.Markdown("""
241
+ [WaveSpeedAI](https://wavespeed.ai/) is the global pioneer in accelerating AI-powered video and image generation.
242
+ 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.
243
+ """)
244
+
245
+ with gr.Row():
246
+ with gr.Column(scale=1):
247
+ img_input = gr.Image(type="filepath", label="Upload Image")
248
+ prompt = gr.Textbox(label="Prompt", lines=3, placeholder="Prompt...")
249
+ negative_prompt = gr.Textbox(label="Negative Prompt", lines=2)
250
+
251
+ with gr.Row():
252
+ size = gr.Dropdown(["832*480", "480*832"], value="832 * 480", interactive=True, label="Resolution")
253
+ steps = gr.Slider(1, 50, value=30, label="Inference Steps")
254
+ with gr.Row():
255
+ duration = gr.Slider(1, 10, value=5, step=1, label="时长(秒)")
256
+ guidance = gr.Slider(1, 20, value=7, label="Guidance Scale")
257
+ with gr.Row():
258
+ seed = gr.Number(-1, label="Seed")
259
+ random_seed_btn = gr.Button("Random🎲Seed", variant="secondary")
260
+ with gr.Row():
261
+ enable_safety = gr.Checkbox(label="🔒 Enable Safety Checker",value=True, interactive=False)
262
+ flow_shift = gr.Slider(1, 50, value=16, label="flow_shift")
263
+
264
+ with gr.Column(scale=1):
265
+ video_output = gr.Video(label="Generated Video", format="mp4", elem_classes=["video-preview"])
266
+ status_output = gr.Textbox(label="System Status", interactive=False, lines=4)
267
+ generate_btn = gr.Button("Generate Video", variant="primary")
268
+
269
+ gr.Examples(
270
+ examples=[
271
+ ["The elegant lady carefully selects bags in the boutique, and she shows the charm of a mature woman in a black slim dress with a pearl necklace. Holding a vintage-inspired blue leather half-moon handbag, she is carefully observing its craftsmanship and texture. The interior of the store is a haven of sophistication and luxury. Soft, ambient lighting casts a warm glow over the polished wooden floors",
272
+ "https://d2g64w682n9w0w.cloudfront.net/media/ec44bbf6abac4c25998dd2c4af1a46a7/images/1747413751234102420_md9ywspl.png"
273
+ ]
274
+ ],
275
+ inputs=[prompt, img_input],
276
+ label="Example Inputs",
277
+ examples_per_page=3
278
+ )
279
+
280
+ random_seed_btn.click(
281
+ fn=lambda: random.randint(0, 999999),
282
+ outputs=seed
283
+ )
284
+
285
+ generate_btn.click(
286
+ generate_video,
287
+ inputs=[
288
+ img_input,
289
+ prompt,
290
+ duration,
291
+ enable_safety,
292
+ flow_shift,
293
+ guidance,
294
+ negative_prompt,
295
+ steps,
296
+ seed,
297
+ size,
298
+ session_id
299
+ ],
300
+ outputs=[
301
+ status_output,
302
+ video_output
303
+ ]
304
+ )
305
+
306
+ if __name__ == "__main__":
307
+ threading.Thread(target=cleanup_task, daemon=True).start()
308
+ app.queue(max_size=4).launch(
309
+ server_name="0.0.0.0",
310
+ max_threads=16,
311
+ share=False
312
+ )