app.py
Browse files
app.py
CHANGED
|
@@ -1,38 +1,22 @@
|
|
| 1 |
# app.py
|
| 2 |
-
import
|
| 3 |
-
import sys
|
| 4 |
-
|
| 5 |
-
# ---------------- 自动安装缺失依赖 ----------------
|
| 6 |
-
def install(package):
|
| 7 |
-
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
|
| 8 |
-
|
| 9 |
-
for pkg in ["torch", "torchvision", "transformers", "gradio", "Pillow"]:
|
| 10 |
-
try:
|
| 11 |
-
__import__(pkg if pkg != "Pillow" else "PIL")
|
| 12 |
-
except ModuleNotFoundError:
|
| 13 |
-
print(f"⚠️ 未找到 {pkg},正在自动安装...")
|
| 14 |
-
install(pkg)
|
| 15 |
-
print(f"✅ {pkg} 安装完成")
|
| 16 |
-
|
| 17 |
-
# ---------------- 导入库 ----------------
|
| 18 |
from transformers import AutoModelForImageClassification, AutoImageProcessor
|
| 19 |
from PIL import Image
|
| 20 |
import torch
|
| 21 |
-
import gradio as gr
|
| 22 |
|
| 23 |
-
# ----------------
|
| 24 |
MODEL_LIST = [
|
|
|
|
| 25 |
"yangy50/garbage-classification",
|
| 26 |
-
"ahmzakif/TrashNet-Classification"
|
| 27 |
-
"harriskr14/trashnet-vit"
|
| 28 |
]
|
| 29 |
|
| 30 |
# ---------------- 加载模型 ----------------
|
| 31 |
models = []
|
| 32 |
processors = []
|
| 33 |
loaded_model_names = []
|
| 34 |
-
|
| 35 |
print("🔹 正在加载模型,请稍等...")
|
|
|
|
| 36 |
for model_name in MODEL_LIST:
|
| 37 |
try:
|
| 38 |
processor = AutoImageProcessor.from_pretrained(model_name)
|
|
@@ -62,7 +46,6 @@ def classify_image(image: Image.Image):
|
|
| 62 |
except Exception as e:
|
| 63 |
results[model_name] = f"❌ 预测失败: {e}"
|
| 64 |
|
| 65 |
-
# 格式化输出
|
| 66 |
results_text = "\n".join([f"{name}: {label}" for name, label in results.items()])
|
| 67 |
return results_text
|
| 68 |
|
|
@@ -71,9 +54,11 @@ iface = gr.Interface(
|
|
| 71 |
fn=classify_image,
|
| 72 |
inputs=gr.Image(type="pil", label="上传图片"),
|
| 73 |
outputs=[gr.Textbox(label="所有模型预测结果")],
|
| 74 |
-
title="
|
| 75 |
-
description="上传图片后,每个模型独立输出预测结果,不做任何人工干预。"
|
|
|
|
| 76 |
)
|
| 77 |
|
|
|
|
| 78 |
if __name__ == "__main__":
|
| 79 |
-
iface.launch()
|
|
|
|
| 1 |
# app.py
|
| 2 |
+
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from transformers import AutoModelForImageClassification, AutoImageProcessor
|
| 4 |
from PIL import Image
|
| 5 |
import torch
|
|
|
|
| 6 |
|
| 7 |
+
# ---------------- 配置模型列表 ----------------
|
| 8 |
MODEL_LIST = [
|
| 9 |
+
"prithivMLmods/Trash-Net",
|
| 10 |
"yangy50/garbage-classification",
|
| 11 |
+
"ahmzakif/TrashNet-Classification"
|
|
|
|
| 12 |
]
|
| 13 |
|
| 14 |
# ---------------- 加载模型 ----------------
|
| 15 |
models = []
|
| 16 |
processors = []
|
| 17 |
loaded_model_names = []
|
|
|
|
| 18 |
print("🔹 正在加载模型,请稍等...")
|
| 19 |
+
|
| 20 |
for model_name in MODEL_LIST:
|
| 21 |
try:
|
| 22 |
processor = AutoImageProcessor.from_pretrained(model_name)
|
|
|
|
| 46 |
except Exception as e:
|
| 47 |
results[model_name] = f"❌ 预测失败: {e}"
|
| 48 |
|
|
|
|
| 49 |
results_text = "\n".join([f"{name}: {label}" for name, label in results.items()])
|
| 50 |
return results_text
|
| 51 |
|
|
|
|
| 54 |
fn=classify_image,
|
| 55 |
inputs=gr.Image(type="pil", label="上传图片"),
|
| 56 |
outputs=[gr.Textbox(label="所有模型预测结果")],
|
| 57 |
+
title="垃圾分类全模型检测",
|
| 58 |
+
description="上传图片后,每个模型独立输出预测结果,不做任何人工干预。",
|
| 59 |
+
allow_flagging="never"
|
| 60 |
)
|
| 61 |
|
| 62 |
+
# ✅ 启用 API 模式(Space 可被外部调用)
|
| 63 |
if __name__ == "__main__":
|
| 64 |
+
iface.launch(server_name="0.0.0.0", server_port=7860, show_api=True)
|