Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import base64
|
| 4 |
+
import time
|
| 5 |
+
|
| 6 |
+
# --- 核心配置 ---
|
| 7 |
+
API_KEY = "zz2026022"
|
| 8 |
+
# 根据截图,Base URL 是 http://<IP>:<端口>/flow/v1
|
| 9 |
+
BASE_URL = "http://154.40.59.124:8000/flow/v1"
|
| 10 |
+
|
| 11 |
+
def encode_image(image_path):
|
| 12 |
+
"""将本地图片路径转换为 Base64 字符串"""
|
| 13 |
+
if not image_path:
|
| 14 |
+
return None
|
| 15 |
+
with open(image_path, "rb") as f:
|
| 16 |
+
encoded = base64.b64encode(f.read()).decode('utf-8')
|
| 17 |
+
return f"data:image/png;base64,{encoded}"
|
| 18 |
+
|
| 19 |
+
def generate_video_task(prompt, start_img_path, end_img_path):
|
| 20 |
+
if not start_img_path:
|
| 21 |
+
return None, "错误:请至少上传起始帧图片。"
|
| 22 |
+
|
| 23 |
+
headers = {
|
| 24 |
+
"Authorization": f"Bearer {API_KEY}", #
|
| 25 |
+
"Content-Type": "application/json"
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
# 1. 转换图片为 Base64
|
| 29 |
+
start_b64 = encode_image(start_img_path)
|
| 30 |
+
end_b64 = encode_image(end_img_path) if end_img_path else None
|
| 31 |
+
|
| 32 |
+
# 2. 构造符合 /v1/videos 接口要求的 payload
|
| 33 |
+
input_refs = [start_b64]
|
| 34 |
+
if end_b64:
|
| 35 |
+
input_refs.append(end_b64) # 传入 [起始帧, 结束帧] 列表
|
| 36 |
+
|
| 37 |
+
payload = {
|
| 38 |
+
"prompt": prompt if prompt else "Generate transition video", #
|
| 39 |
+
"model": "veo_3_1", # 使用文档指定的模型名称
|
| 40 |
+
"input_reference": input_refs # 接口参数名为 input_reference
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
try:
|
| 44 |
+
# 3. 提交生成任务 (POST /v1/videos)
|
| 45 |
+
response = requests.post(f"{BASE_URL}/videos", json=payload, headers=headers)
|
| 46 |
+
res_data = response.json()
|
| 47 |
+
|
| 48 |
+
if response.status_code != 200:
|
| 49 |
+
return None, f"任务提交失败: {res_data}"
|
| 50 |
+
|
| 51 |
+
video_id = res_data.get("id")
|
| 52 |
+
if not video_id:
|
| 53 |
+
return None, "未获取到任务 ID,请检查接口返回格式。"
|
| 54 |
+
|
| 55 |
+
# 4. 轮询视频生成状态 (GET /v1/videos/{video_id})
|
| 56 |
+
status_url = f"{BASE_URL}/videos/{video_id}"
|
| 57 |
+
for _ in range(60): # 最多等待 300 秒
|
| 58 |
+
time.sleep(5)
|
| 59 |
+
status_res = requests.get(status_url, headers=headers)
|
| 60 |
+
status_data = status_res.json()
|
| 61 |
+
|
| 62 |
+
# 这里的状态判断逻辑需根据您的 API 实际返回字段名进行适配(如 status 或 state)
|
| 63 |
+
status = status_data.get("status")
|
| 64 |
+
if status == "completed" or status == "succeeded":
|
| 65 |
+
return status_data.get("url"), f"生成成功!ID: {video_id}"
|
| 66 |
+
elif status == "failed":
|
| 67 |
+
return None, f"视频生成失败: {status_data.get('error')}"
|
| 68 |
+
|
| 69 |
+
print(f"正在生成中... 状态: {status}")
|
| 70 |
+
|
| 71 |
+
return None, "任务超时,请稍后重试。"
|
| 72 |
+
|
| 73 |
+
except Exception as e:
|
| 74 |
+
return None, f"程序异常: {str(e)}"
|
| 75 |
+
|
| 76 |
+
# --- Gradio 交互界面 ---
|
| 77 |
+
with gr.Blocks(title="首尾帧视频生成测试") as demo:
|
| 78 |
+
gr.Markdown("## 🎬 视频接口生成测试 (/v1/videos)")
|
| 79 |
+
gr.Markdown("当前使用模型:`veo_3_1`")
|
| 80 |
+
|
| 81 |
+
with gr.Row():
|
| 82 |
+
with gr.Column():
|
| 83 |
+
prompt_input = gr.Textbox(label="视频提示词 (Prompt)", value="画小猫") #
|
| 84 |
+
start_img = gr.Image(label="起始帧 (Start Frame)", type="filepath")
|
| 85 |
+
end_img = gr.Image(label="结束帧 (End Frame)", type="filepath")
|
| 86 |
+
btn = gr.Button("提交视频生成任务", variant="primary")
|
| 87 |
+
|
| 88 |
+
with gr.Column():
|
| 89 |
+
video_out = gr.Video(label="生成结果")
|
| 90 |
+
info_out = gr.Textbox(label="任务状态信息", interactive=False)
|
| 91 |
+
|
| 92 |
+
btn.click(
|
| 93 |
+
fn=generate_video_task,
|
| 94 |
+
inputs=[prompt_input, start_img, end_img],
|
| 95 |
+
outputs=[video_out, info_out]
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
if __name__ == "__main__":
|
| 99 |
+
demo.launch()
|