Spaces:
Running
Running
Improve API calling robustness and error handling
Browse files- Add API endpoint discovery for Gradio Client
- Try multiple possible API endpoints automatically
- Improve HF Inference API error handling and responses
- Add proper token checking and validation
- Better logging and debugging information
- Handle different API response formats properly
app.py
CHANGED
|
@@ -18,26 +18,63 @@ def call_gradio_client_api(video_file, text_prompt, guidance_scale, inference_st
|
|
| 18 |
# 连接到官方Space
|
| 19 |
client = Client("tencent/HunyuanVideo-Foley")
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
logger.info("发送推理请求...")
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
return
|
| 34 |
|
| 35 |
except Exception as e:
|
| 36 |
error_msg = str(e)
|
| 37 |
logger.error(f"Gradio Client API 调用失败: {error_msg}")
|
| 38 |
|
| 39 |
if "not found" in error_msg.lower():
|
| 40 |
-
return None, "❌ 官方Space
|
| 41 |
elif "connection" in error_msg.lower():
|
| 42 |
return None, "❌ 无法连接到官方Space,请检查网络"
|
| 43 |
elif "queue" in error_msg.lower():
|
|
@@ -50,39 +87,66 @@ def call_huggingface_inference_api(video_file, text_prompt):
|
|
| 50 |
try:
|
| 51 |
logger.info("尝试Hugging Face Inference API...")
|
| 52 |
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
|
| 56 |
-
with open(video_file, "rb") as f:
|
| 57 |
-
video_data = f.read()
|
| 58 |
|
| 59 |
-
# 准备请求数据
|
| 60 |
headers = {
|
| 61 |
-
"Authorization": f"Bearer {
|
|
|
|
| 62 |
}
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
# 发送请求
|
| 65 |
response = requests.post(
|
| 66 |
API_URL,
|
| 67 |
headers=headers,
|
| 68 |
-
json=
|
| 69 |
-
timeout=
|
| 70 |
)
|
| 71 |
|
|
|
|
|
|
|
| 72 |
if response.status_code == 200:
|
| 73 |
-
#
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
else:
|
| 80 |
-
logger.error(f"HF API错误: {response.status_code}")
|
| 81 |
-
return None, f"❌
|
| 82 |
|
| 83 |
except Exception as e:
|
| 84 |
logger.error(f"HF API调用失败: {str(e)}")
|
| 85 |
-
return None, f"❌
|
| 86 |
|
| 87 |
def try_alternative_apis(video_file, text_prompt):
|
| 88 |
"""尝试其他可能的API服务"""
|
|
|
|
| 18 |
# 连接到官方Space
|
| 19 |
client = Client("tencent/HunyuanVideo-Foley")
|
| 20 |
|
| 21 |
+
# 首先检查Space的API端点
|
| 22 |
+
logger.info("检查可用的API端点...")
|
| 23 |
+
try:
|
| 24 |
+
# 获取Space的API信息
|
| 25 |
+
api_info = client.view_api()
|
| 26 |
+
logger.info(f"可用的API端点: {api_info}")
|
| 27 |
+
except:
|
| 28 |
+
logger.warning("无法获取API端点信息")
|
| 29 |
+
|
| 30 |
logger.info("发送推理请求...")
|
| 31 |
|
| 32 |
+
# 尝试不同的API端点名称
|
| 33 |
+
possible_endpoints = [
|
| 34 |
+
"/infer_single_video",
|
| 35 |
+
"/predict",
|
| 36 |
+
"/generate",
|
| 37 |
+
None # 使用默认端点
|
| 38 |
+
]
|
| 39 |
+
|
| 40 |
+
for endpoint in possible_endpoints:
|
| 41 |
+
try:
|
| 42 |
+
logger.info(f"尝试端点: {endpoint}")
|
| 43 |
+
|
| 44 |
+
if endpoint:
|
| 45 |
+
result = client.predict(
|
| 46 |
+
video_file,
|
| 47 |
+
text_prompt,
|
| 48 |
+
guidance_scale,
|
| 49 |
+
inference_steps,
|
| 50 |
+
sample_nums,
|
| 51 |
+
api_name=endpoint
|
| 52 |
+
)
|
| 53 |
+
else:
|
| 54 |
+
# 尝试默认调用
|
| 55 |
+
result = client.predict(
|
| 56 |
+
video_file,
|
| 57 |
+
text_prompt,
|
| 58 |
+
guidance_scale,
|
| 59 |
+
inference_steps,
|
| 60 |
+
sample_nums
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
logger.info("API调用成功!")
|
| 64 |
+
return result, "✅ 成功通过官方API生成音频!"
|
| 65 |
+
|
| 66 |
+
except Exception as endpoint_error:
|
| 67 |
+
logger.warning(f"端点 {endpoint} 失败: {str(endpoint_error)}")
|
| 68 |
+
continue
|
| 69 |
|
| 70 |
+
return None, "❌ 所有API端点都调用失败"
|
| 71 |
|
| 72 |
except Exception as e:
|
| 73 |
error_msg = str(e)
|
| 74 |
logger.error(f"Gradio Client API 调用失败: {error_msg}")
|
| 75 |
|
| 76 |
if "not found" in error_msg.lower():
|
| 77 |
+
return None, "❌ 官方Space未找到或不可访问"
|
| 78 |
elif "connection" in error_msg.lower():
|
| 79 |
return None, "❌ 无法连接到官方Space,请检查网络"
|
| 80 |
elif "queue" in error_msg.lower():
|
|
|
|
| 87 |
try:
|
| 88 |
logger.info("尝试Hugging Face Inference API...")
|
| 89 |
|
| 90 |
+
# 检查是否有Token
|
| 91 |
+
hf_token = os.environ.get('HF_TOKEN') or os.environ.get('HUGGING_FACE_HUB_TOKEN')
|
| 92 |
+
if not hf_token:
|
| 93 |
+
return None, "❌ 未配置HF_TOKEN,跳过Inference API"
|
| 94 |
|
| 95 |
+
API_URL = "https://api-inference.huggingface.co/models/tencent/HunyuanVideo-Foley"
|
|
|
|
|
|
|
| 96 |
|
| 97 |
+
# 准备请求数据 - 简化格式
|
| 98 |
headers = {
|
| 99 |
+
"Authorization": f"Bearer {hf_token}",
|
| 100 |
+
"Content-Type": "application/json"
|
| 101 |
}
|
| 102 |
|
| 103 |
+
# 简化的请求数据
|
| 104 |
+
data = {
|
| 105 |
+
"inputs": text_prompt, # 简化输入格式
|
| 106 |
+
"parameters": {
|
| 107 |
+
"guidance_scale": 4.5,
|
| 108 |
+
"num_inference_steps": 50
|
| 109 |
+
}
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
logger.info("发送Inference API请求...")
|
| 113 |
+
|
| 114 |
# 发送请求
|
| 115 |
response = requests.post(
|
| 116 |
API_URL,
|
| 117 |
headers=headers,
|
| 118 |
+
json=data,
|
| 119 |
+
timeout=60 # 缩短超时时间
|
| 120 |
)
|
| 121 |
|
| 122 |
+
logger.info(f"API响应状态码: {response.status_code}")
|
| 123 |
+
|
| 124 |
if response.status_code == 200:
|
| 125 |
+
# 检查响应内容类型
|
| 126 |
+
content_type = response.headers.get('content-type', '')
|
| 127 |
+
if 'audio' in content_type:
|
| 128 |
+
# 保存音频结果
|
| 129 |
+
temp_dir = tempfile.mkdtemp()
|
| 130 |
+
audio_path = os.path.join(temp_dir, "generated_audio.wav")
|
| 131 |
+
with open(audio_path, 'wb') as f:
|
| 132 |
+
f.write(response.content)
|
| 133 |
+
return [audio_path], "✅ 通过Hugging Face API生成成功!"
|
| 134 |
+
else:
|
| 135 |
+
logger.warning(f"响应不是音频格式: {content_type}")
|
| 136 |
+
return None, f"❌ API返回了非音频内容: {content_type}"
|
| 137 |
+
elif response.status_code == 503:
|
| 138 |
+
return None, "⏳ 模型正在加载中,请稍后重试"
|
| 139 |
+
elif response.status_code == 401:
|
| 140 |
+
return None, "❌ HF Token无效或权限不足"
|
| 141 |
+
elif response.status_code == 404:
|
| 142 |
+
return None, "❌ 该模型不支持Inference API"
|
| 143 |
else:
|
| 144 |
+
logger.error(f"HF API错误: {response.status_code} - {response.text}")
|
| 145 |
+
return None, f"❌ HF API错误 {response.status_code}: {response.text[:100]}"
|
| 146 |
|
| 147 |
except Exception as e:
|
| 148 |
logger.error(f"HF API调用失败: {str(e)}")
|
| 149 |
+
return None, f"❌ HF API调用失败: {str(e)}"
|
| 150 |
|
| 151 |
def try_alternative_apis(video_file, text_prompt):
|
| 152 |
"""尝试其他可能的API服务"""
|