Spaces:
Running
Running
实现纯官方模型调用 - 移除所有备用方案
Browse files🎯 核心改进:
- 移除 HF Inference API(官方模型不支持)
- 专注于官方 tencent/HunyuanVideo-Foley Gradio Space 调用
- 修复 Gradio Client timeout 参数兼容性问题
- 使用正确的 API 端点 /process_inference
❌ 完全移除:
- 所有备用音频生成方案
- 降级机制和演示音频
- 混乱的多 API 尝试逻辑
✅ 现在专注于:
- 只调用真正的官方模型
- 失败就失败,不提供无意义的替代方案
- 清晰的用户界面说明
用户需要的是真正的 AI 模型结果,不是随便生成的音频!
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <[email protected]>
app.py
CHANGED
@@ -27,16 +27,18 @@ def call_huggingface_inference_api(video_file_path: str, text_prompt: str = "")
|
|
27 |
# Hugging Face API endpoint
|
28 |
API_URL = "https://api-inference.huggingface.co/models/tencent/HunyuanVideo-Foley"
|
29 |
|
30 |
-
#
|
31 |
hf_token = (
|
32 |
os.environ.get('HF_TOKEN') or
|
33 |
os.environ.get('HUGGING_FACE_HUB_TOKEN') or
|
34 |
-
os.environ.get('HUGGINGFACE_TOKEN')
|
|
|
35 |
)
|
36 |
|
37 |
-
# 如果没有 Token,尝试无认证访问(某些公共模型允许)
|
38 |
if not hf_token:
|
39 |
-
logger.
|
|
|
|
|
40 |
|
41 |
# 构建请求头
|
42 |
headers = {"Content-Type": "application/json"}
|
@@ -108,18 +110,28 @@ def call_gradio_client_api(video_file_path: str, text_prompt: str = "") -> Tuple
|
|
108 |
from gradio_client import Client
|
109 |
|
110 |
logger.info("使用 Gradio Client 连接官方 Space...")
|
111 |
-
|
|
|
|
|
|
|
|
|
|
|
112 |
|
113 |
-
|
|
|
|
|
|
|
114 |
result = client.predict(
|
115 |
video_file_path, # video input
|
116 |
text_prompt, # text prompt
|
117 |
4.5, # guidance_scale
|
118 |
50, # inference_steps
|
119 |
1, # sample_nums
|
120 |
-
api_name="/
|
121 |
)
|
122 |
|
|
|
|
|
123 |
if result and len(result) > 0:
|
124 |
# 假设返回的第一个元素是生成的音频文件
|
125 |
audio_file = result[0]
|
@@ -271,34 +283,20 @@ def process_video_with_apis(video_file, text_prompt: str, guidance_scale: float,
|
|
271 |
api_results = []
|
272 |
status_messages = []
|
273 |
|
274 |
-
#
|
275 |
-
logger.info("🔄
|
276 |
-
|
277 |
-
if
|
278 |
-
api_results.append(
|
279 |
-
status_messages.append(f"✅
|
|
|
280 |
else:
|
281 |
-
status_messages.append(f"❌
|
|
|
282 |
|
283 |
-
#
|
284 |
-
if not hf_audio:
|
285 |
-
logger.info("🔄 尝试方法2: Gradio Client API")
|
286 |
-
gc_audio, gc_msg = call_gradio_client_api(video_file_path, text_prompt)
|
287 |
-
if gc_audio:
|
288 |
-
api_results.append(gc_audio)
|
289 |
-
status_messages.append(f"✅ Gradio Client: 成功")
|
290 |
-
else:
|
291 |
-
status_messages.append(f"❌ Gradio Client: {gc_msg}")
|
292 |
-
|
293 |
-
# 方法3: 备用演示(如果所有 API 都失败)
|
294 |
if not api_results:
|
295 |
-
|
296 |
-
fallback_audio = create_fallback_audio(video_file_path, text_prompt)
|
297 |
-
if fallback_audio:
|
298 |
-
api_results.append(fallback_audio)
|
299 |
-
status_messages.append("🎯 备用演示: 生成音频(API 不可用时的演示)")
|
300 |
-
else:
|
301 |
-
status_messages.append("❌ 备用演示: 音频生成失败")
|
302 |
|
303 |
# 构建详细状态消息
|
304 |
final_status = f"""🎵 HunyuanVideo-Foley 处理完成!
|
@@ -366,14 +364,14 @@ def create_api_interface():
|
|
366 |
# API Notice
|
367 |
gr.HTML("""
|
368 |
<div class="api-notice">
|
369 |
-
<strong>🔗
|
370 |
-
<br>•
|
371 |
-
<br>•
|
372 |
-
<br>•
|
373 |
<br><br>
|
374 |
-
<strong>✅
|
375 |
-
<br>•
|
376 |
-
<br>•
|
377 |
<br>• 模型首次加载可能需要 1-2 分钟
|
378 |
</div>
|
379 |
""")
|
@@ -421,7 +419,7 @@ def create_api_interface():
|
|
421 |
)
|
422 |
|
423 |
generate_btn = gr.Button(
|
424 |
-
"🎵
|
425 |
variant="primary"
|
426 |
)
|
427 |
|
@@ -441,12 +439,12 @@ def create_api_interface():
|
|
441 |
# Method info
|
442 |
gr.HTML("""
|
443 |
<div class="method-info">
|
444 |
-
<h3
|
445 |
-
<p><strong
|
446 |
-
<p><strong
|
447 |
-
<p><strong
|
448 |
<br>
|
449 |
-
<p><strong>🚀
|
450 |
</div>
|
451 |
""")
|
452 |
|
@@ -469,8 +467,8 @@ def create_api_interface():
|
|
469 |
# Footer
|
470 |
gr.HTML("""
|
471 |
<div style="text-align: center; padding: 2rem; color: #666; border-top: 1px solid #eee; margin-top: 2rem;">
|
472 |
-
<p><strong
|
473 |
-
<p>✅
|
474 |
<p>📂 模型仓库: <a href="https://huggingface.co/tencent/HunyuanVideo-Foley" target="_blank">tencent/HunyuanVideo-Foley</a></p>
|
475 |
</div>
|
476 |
""")
|
|
|
27 |
# Hugging Face API endpoint
|
28 |
API_URL = "https://api-inference.huggingface.co/models/tencent/HunyuanVideo-Foley"
|
29 |
|
30 |
+
# 在 HuggingFace Spaces 中,Token 通常自动可用
|
31 |
hf_token = (
|
32 |
os.environ.get('HF_TOKEN') or
|
33 |
os.environ.get('HUGGING_FACE_HUB_TOKEN') or
|
34 |
+
os.environ.get('HUGGINGFACE_TOKEN') or
|
35 |
+
os.environ.get('HUGGINGFACE_HUB_TOKEN') # Spaces 环境变量
|
36 |
)
|
37 |
|
|
|
38 |
if not hf_token:
|
39 |
+
logger.warning("未找到 HF Token - 在 HuggingFace Spaces 中这不应该发生")
|
40 |
+
# 对于 Inference API,Token 是必需的
|
41 |
+
return None, "❌ HF Inference API 需要认证 Token,但未找到环境变量"
|
42 |
|
43 |
# 构建请求头
|
44 |
headers = {"Content-Type": "application/json"}
|
|
|
110 |
from gradio_client import Client
|
111 |
|
112 |
logger.info("使用 Gradio Client 连接官方 Space...")
|
113 |
+
# 修复 timeout 参数问题
|
114 |
+
try:
|
115 |
+
client = Client("tencent/HunyuanVideo-Foley")
|
116 |
+
except Exception as e:
|
117 |
+
logger.warning(f"Client 初始化失败: {e}")
|
118 |
+
return None, f"❌ 无法连接到官方 Space: {str(e)}"
|
119 |
|
120 |
+
logger.info(f"使用官方 Space API 处理视频: {os.path.basename(video_file_path)}")
|
121 |
+
logger.info(f"文本提示: '{text_prompt}'")
|
122 |
+
|
123 |
+
# 调用官方 Space 的处理接口
|
124 |
result = client.predict(
|
125 |
video_file_path, # video input
|
126 |
text_prompt, # text prompt
|
127 |
4.5, # guidance_scale
|
128 |
50, # inference_steps
|
129 |
1, # sample_nums
|
130 |
+
api_name="/process_inference" # 使用正确的 API 端点名称
|
131 |
)
|
132 |
|
133 |
+
logger.info(f"API 调用结果: {type(result)}, 内容: {result[:100] if isinstance(result, str) else str(result)[:100]}...")
|
134 |
+
|
135 |
if result and len(result) > 0:
|
136 |
# 假设返回的第一个元素是生成的音频文件
|
137 |
audio_file = result[0]
|
|
|
283 |
api_results = []
|
284 |
status_messages = []
|
285 |
|
286 |
+
# 直接使用官方 Gradio Space API(这是唯一支持的方法)
|
287 |
+
logger.info("🔄 调用官方 tencent/HunyuanVideo-Foley Space")
|
288 |
+
gc_audio, gc_msg = call_gradio_client_api(video_file_path, text_prompt)
|
289 |
+
if gc_audio:
|
290 |
+
api_results.append(gc_audio)
|
291 |
+
status_messages.append(f"✅ 官方 Gradio Space: 成功调用模型")
|
292 |
+
logger.info("✅ 成功从官方模型获得音频结果!")
|
293 |
else:
|
294 |
+
status_messages.append(f"❌ 官方 Gradio Space: {gc_msg}")
|
295 |
+
logger.error(f"❌ 官方模型调用失败: {gc_msg}")
|
296 |
|
297 |
+
# 如果所有 API 都失败,返回错误信息(不再提供备用方案)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
298 |
if not api_results:
|
299 |
+
status_messages.append("❌ 所有 API 调用都失败了,无法生成音频")
|
|
|
|
|
|
|
|
|
|
|
|
|
300 |
|
301 |
# 构建详细状态消息
|
302 |
final_status = f"""🎵 HunyuanVideo-Foley 处理完成!
|
|
|
364 |
# API Notice
|
365 |
gr.HTML("""
|
366 |
<div class="api-notice">
|
367 |
+
<strong>🔗 官方模型调用:</strong>
|
368 |
+
<br>• 直接调用 tencent/HunyuanVideo-Foley 官方 Gradio Space
|
369 |
+
<br>• 使用真正的 AI 模型生成 Foley 音频
|
370 |
+
<br>• 与视频内容完美同步的专业音效
|
371 |
<br><br>
|
372 |
+
<strong>✅ 即插即用:</strong>
|
373 |
+
<br>• 无需任何配置或设置
|
374 |
+
<br>• 上传视频 → 输入描述 → 获得专业音效
|
375 |
<br>• 模型首次加载可能需要 1-2 分钟
|
376 |
</div>
|
377 |
""")
|
|
|
419 |
)
|
420 |
|
421 |
generate_btn = gr.Button(
|
422 |
+
"🎵 调用官方模型生成音频",
|
423 |
variant="primary"
|
424 |
)
|
425 |
|
|
|
439 |
# Method info
|
440 |
gr.HTML("""
|
441 |
<div class="method-info">
|
442 |
+
<h3>🎯 官方模型调用说明</h3>
|
443 |
+
<p><strong>✅ 真实 AI 模型:</strong> 直接调用腾讯混元官方 HunyuanVideo-Foley 模型</p>
|
444 |
+
<p><strong>🎵 专业音效:</strong> 生成与视频内容完美同步的 Foley 音频</p>
|
445 |
+
<p><strong>⚡ 高质量输出:</strong> 48kHz 专业级音频,支持多种音效类型</p>
|
446 |
<br>
|
447 |
+
<p><strong>🚀 使用简单:</strong> 上传视频 + 输入描述 = 获得专业音效</p>
|
448 |
</div>
|
449 |
""")
|
450 |
|
|
|
467 |
# Footer
|
468 |
gr.HTML("""
|
469 |
<div style="text-align: center; padding: 2rem; color: #666; border-top: 1px solid #eee; margin-top: 2rem;">
|
470 |
+
<p><strong>🎵 官方模型调用版本</strong> - 直接调用 tencent/HunyuanVideo-Foley</p>
|
471 |
+
<p>✅ 真实 AI 模型,专业 Foley 音频生成</p>
|
472 |
<p>📂 模型仓库: <a href="https://huggingface.co/tencent/HunyuanVideo-Foley" target="_blank">tencent/HunyuanVideo-Foley</a></p>
|
473 |
</div>
|
474 |
""")
|