Spaces:
Running
on
Zero
Running
on
Zero
feat: 添加 HuggingFace Space API 调用测试脚本
Browse files- hf_space_api_predict_test.py +32 -0
hf_space_api_predict_test.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from gradio_client import Client, handle_file
|
| 2 |
+
import numpy as np
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import io
|
| 5 |
+
|
| 6 |
+
def test_gradio_api():
|
| 7 |
+
client = Client("Hakureirm/NailongKiller")
|
| 8 |
+
|
| 9 |
+
result = client.predict(
|
| 10 |
+
img=handle_file("example1.jpg"),
|
| 11 |
+
api_name="/predict"
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
# 如果结果是临时文件路径
|
| 15 |
+
if isinstance(result, str):
|
| 16 |
+
# 读取图像数据并直接转换为 PIL Image
|
| 17 |
+
img = Image.open(result)
|
| 18 |
+
# 保存到指定位置
|
| 19 |
+
img.save("detection_result.jpg")
|
| 20 |
+
# 删除临时文件
|
| 21 |
+
import os
|
| 22 |
+
os.remove(result)
|
| 23 |
+
print("检测结果已保存为 detection_result.jpg")
|
| 24 |
+
|
| 25 |
+
return "已完成检测并保存结果"
|
| 26 |
+
|
| 27 |
+
if __name__ == "__main__":
|
| 28 |
+
try:
|
| 29 |
+
message = test_gradio_api()
|
| 30 |
+
print(message)
|
| 31 |
+
except Exception as e:
|
| 32 |
+
print(f"错误: {str(e)}")
|