jiandan1998 commited on
Commit
a86ae77
·
verified ·
1 Parent(s): 9b22478

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -18
app.py CHANGED
@@ -80,7 +80,7 @@ def create_error_image(message):
80
 
81
  def classify_prompt(prompt):
82
  inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=512)
83
- with torch.no_grad():
84
  outputs = model(**inputs)
85
  return torch.argmax(outputs.logits).item()
86
 
@@ -96,6 +96,21 @@ def image_to_base64(file_path):
96
  except Exception as e:
97
  raise ValueError(f"Base64编码失败: {str(e)}")
98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
  def generate_video(
100
  context_scale,
101
  enable_safety_checker,
@@ -128,21 +143,18 @@ def generate_video(
128
  session['last_active'] = time.time()
129
  session['count'] += 1
130
 
131
- API_KEY = os.getenv("WAVESPEED_API_KEY")
132
  if not API_KEY:
133
  error_img = create_error_image("API key not found")
134
  yield "❌ Error: Missing API Key", error_img
135
  return
136
 
137
  try:
138
- if not images or len(images) < 2:
139
- raise ValueError("must provide at least 2 images")
140
-
141
  base64_images = []
142
- for img_path in images[:2]:
143
- base64_img = image_to_base64(img_path)
144
- base64_images.append(base64_img)
145
-
146
  except Exception as e:
147
  error_img = create_error_image(str(e))
148
  yield f"❌failed to upload images: {str(e)}", error_img
@@ -155,6 +167,15 @@ def generate_video(
155
  else:
156
  video_payload = video
157
 
 
 
 
 
 
 
 
 
 
158
  payload = {
159
  "context_scale": context_scale,
160
  "enable_fast_mode": enable_fast_mode,
@@ -283,23 +304,23 @@ with gr.Blocks(
283
  random_seed_btn = gr.Button("Random🎲Seed", variant="secondary", elem_id="centered_button")
284
  with gr.Row():
285
  enable_safety_checker = gr.Checkbox(True, label="Enable Safety Checker", interactive=True)
286
- enable_fast_mode = gr.Checkbox(False, label="To enable the fast mode, please visit Wave Speed AI", interactive=False)
287
  with gr.Column(scale=1):
288
  video_output = gr.Video(label="Video Output", format="mp4", interactive=False, elem_classes=["video-preview"])
289
  generate_btn = gr.Button("Generate", variant="primary")
290
  status_output = gr.Textbox(label="status", interactive=False, lines=4)
291
  gr.Examples(
292
- examples=[
 
 
293
  [
294
- "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, as well as her pretty face. 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",
295
- [
296
- "https://d2g64w682n9w0w.cloudfront.net/media/ec44bbf6abac4c25998dd2c4af1a46a7/images/1747413751234102420_md9ywspl.png",
297
- "https://d2g64w682n9w0w.cloudfront.net/media/ec44bbf6abac4c25998dd2c4af1a46a7/images/1747413586520964413_7bkgc9ol.png"
298
- ]
299
  ]
 
300
  ],
301
- inputs=[prompt, images],
302
- )
303
 
304
  random_seed_btn.click(
305
  fn=lambda: random.randint(0, 999999),
@@ -344,5 +365,6 @@ if __name__ == "__main__":
344
  app.queue(max_size=4).launch(
345
  server_name="0.0.0.0",
346
  max_threads=16,
 
347
  share=False
348
  )
 
80
 
81
  def classify_prompt(prompt):
82
  inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=512)
83
+ with torch.no_grad():
84
  outputs = model(**inputs)
85
  return torch.argmax(outputs.logits).item()
86
 
 
96
  except Exception as e:
97
  raise ValueError(f"Base64编码失败: {str(e)}")
98
 
99
+ def video_to_base64(file_path):
100
+ """
101
+ 将视频文件转换为Base64格式
102
+ """
103
+ try:
104
+ with open(file_path, "rb") as video_file:
105
+ raw_data = video_file.read()
106
+ encoded = base64.b64encode(raw_data)
107
+ missing_padding = len(encoded) % 4
108
+ if missing_padding:
109
+ encoded += b'=' * (4 - missing_padding)
110
+ return encoded.decode('utf-8')
111
+ except Exception as e:
112
+ raise ValueError(f"Base64编码失败: {str(e)}")
113
+
114
  def generate_video(
115
  context_scale,
116
  enable_safety_checker,
 
143
  session['last_active'] = time.time()
144
  session['count'] += 1
145
 
146
+ API_KEY = "30a09de38569400bcdab9cec1c9a660b1924a2b5f54aa386eeb87f96a112fb93"
147
  if not API_KEY:
148
  error_img = create_error_image("API key not found")
149
  yield "❌ Error: Missing API Key", error_img
150
  return
151
 
152
  try:
 
 
 
153
  base64_images = []
154
+ if images is not None: # 检查 images 是否为 None
155
+ for img_path in images:
156
+ base64_img = image_to_base64(img_path)
157
+ base64_images.append(base64_img)
158
  except Exception as e:
159
  error_img = create_error_image(str(e))
160
  yield f"❌failed to upload images: {str(e)}", error_img
 
167
  else:
168
  video_payload = video
169
 
170
+ # 将视频文件转换为Base64格式
171
+ try:
172
+ base64_video = video_to_base64(video_payload)
173
+ video_payload = base64_video
174
+ except Exception as e:
175
+ error_img = create_error_image(str(e))
176
+ yield f"❌ Failed to encode video: {str(e)}", error_img
177
+ return
178
+
179
  payload = {
180
  "context_scale": context_scale,
181
  "enable_fast_mode": enable_fast_mode,
 
304
  random_seed_btn = gr.Button("Random🎲Seed", variant="secondary", elem_id="centered_button")
305
  with gr.Row():
306
  enable_safety_checker = gr.Checkbox(True, label="Enable Safety Checker", interactive=True)
307
+ enable_fast_mode = gr.Checkbox(True, label="To enable the fast mode, please visit Wave Speed AI", interactive=False)
308
  with gr.Column(scale=1):
309
  video_output = gr.Video(label="Video Output", format="mp4", interactive=False, elem_classes=["video-preview"])
310
  generate_btn = gr.Button("Generate", variant="primary")
311
  status_output = gr.Textbox(label="status", interactive=False, lines=4)
312
  gr.Examples(
313
+ examples=[
314
+ [
315
+ "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, as well as her pretty face. 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",
316
  [
317
+ "https://d2g64w682n9w0w.cloudfront.net/media/ec44bbf6abac4c25998dd2c4af1a46a7/images/1747413751234102420_md9ywspl.png",
318
+ "https://d2g64w682n9w0w.cloudfront.net/media/ec44bbf6abac4c25998dd2c4af1a46a7/images/1747413586520964413_7bkgc9ol.png"
 
 
 
319
  ]
320
+ ]
321
  ],
322
+ inputs=[prompt, images],
323
+ )
324
 
325
  random_seed_btn.click(
326
  fn=lambda: random.randint(0, 999999),
 
365
  app.queue(max_size=4).launch(
366
  server_name="0.0.0.0",
367
  max_threads=16,
368
+ server_port=8009,
369
  share=False
370
  )