Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,60 +1,86 @@
|
|
| 1 |
# app.py
|
| 2 |
-
|
| 3 |
import gradio as gr
|
| 4 |
-
from transformers import AutoModel
|
| 5 |
import torch
|
| 6 |
import numpy as np
|
| 7 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
return model
|
| 15 |
|
| 16 |
def process_image(input_image):
|
|
|
|
|
|
|
|
|
|
| 17 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
# 确保输入图像是PIL Image格式
|
| 19 |
if not isinstance(input_image, Image.Image):
|
| 20 |
-
input_image = Image.fromarray(input_image)
|
| 21 |
-
|
| 22 |
-
# 加载模型
|
| 23 |
-
model = load_model()
|
| 24 |
|
| 25 |
# 图像预处理
|
| 26 |
input_image = input_image.resize((256, 256))
|
| 27 |
|
| 28 |
-
# 转换为tensor
|
| 29 |
-
image_tensor = torch.from_numpy(np.array(input_image)).float()
|
| 30 |
image_tensor = image_tensor.permute(2, 0, 1).unsqueeze(0)
|
| 31 |
|
| 32 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 33 |
-
image_tensor = image_tensor.to(device)
|
| 34 |
-
|
| 35 |
# 模型推理
|
| 36 |
with torch.no_grad():
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
| 41 |
except Exception as e:
|
| 42 |
-
return f"
|
| 43 |
|
| 44 |
# 创建Gradio界面
|
| 45 |
demo = gr.Interface(
|
| 46 |
fn=process_image,
|
| 47 |
inputs=[
|
| 48 |
-
gr.Image(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
],
|
| 50 |
outputs=[
|
| 51 |
-
gr.Model3D(
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
],
|
| 54 |
title="麒迹云台 - 2D转3D模型生成器",
|
| 55 |
description="上传一张图片,AI将自动生成对应的3D模型。支持格式:jpg, png, jpeg",
|
| 56 |
-
theme=gr.themes.Soft()
|
|
|
|
| 57 |
)
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
|
|
|
| 1 |
# app.py
|
|
|
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
import torch
|
| 4 |
import numpy as np
|
| 5 |
from PIL import Image
|
| 6 |
+
from transformers import AutoModel
|
| 7 |
+
import warnings
|
| 8 |
+
warnings.filterwarnings('ignore')
|
| 9 |
|
| 10 |
+
# 全局变量存储模型实例
|
| 11 |
+
model = None
|
| 12 |
+
|
| 13 |
+
def initialize_model():
|
| 14 |
+
global model
|
| 15 |
+
try:
|
| 16 |
+
if model is None:
|
| 17 |
+
model = AutoModel.from_pretrained(
|
| 18 |
+
"jadechoghari/vfusion3d",
|
| 19 |
+
trust_remote_code=True,
|
| 20 |
+
device_map="auto" # 自动处理设备分配
|
| 21 |
+
)
|
| 22 |
+
except Exception as e:
|
| 23 |
+
print(f"模型加载错误: {str(e)}")
|
| 24 |
+
return None
|
| 25 |
return model
|
| 26 |
|
| 27 |
def process_image(input_image):
|
| 28 |
+
if input_image is None:
|
| 29 |
+
return None, "请上传图片"
|
| 30 |
+
|
| 31 |
try:
|
| 32 |
+
# 初始化模型
|
| 33 |
+
model = initialize_model()
|
| 34 |
+
if model is None:
|
| 35 |
+
return None, "模型加载失败"
|
| 36 |
+
|
| 37 |
# 确保输入图像是PIL Image格式
|
| 38 |
if not isinstance(input_image, Image.Image):
|
| 39 |
+
input_image = Image.fromarray(np.uint8(input_image))
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
# 图像预处理
|
| 42 |
input_image = input_image.resize((256, 256))
|
| 43 |
|
| 44 |
+
# 转换为tensor并归一化
|
| 45 |
+
image_tensor = torch.from_numpy(np.array(input_image)).float() / 255.0
|
| 46 |
image_tensor = image_tensor.permute(2, 0, 1).unsqueeze(0)
|
| 47 |
|
|
|
|
|
|
|
|
|
|
| 48 |
# 模型推理
|
| 49 |
with torch.no_grad():
|
| 50 |
+
try:
|
| 51 |
+
output = model(image_tensor)
|
| 52 |
+
return output, "处理成功"
|
| 53 |
+
except Exception as e:
|
| 54 |
+
return None, f"模型推理错误: {str(e)}"
|
| 55 |
+
|
| 56 |
except Exception as e:
|
| 57 |
+
return None, f"处理错误: {str(e)}"
|
| 58 |
|
| 59 |
# 创建Gradio界面
|
| 60 |
demo = gr.Interface(
|
| 61 |
fn=process_image,
|
| 62 |
inputs=[
|
| 63 |
+
gr.Image(
|
| 64 |
+
type="pil",
|
| 65 |
+
label="上传图片",
|
| 66 |
+
tool="select"
|
| 67 |
+
)
|
| 68 |
],
|
| 69 |
outputs=[
|
| 70 |
+
gr.Model3D(
|
| 71 |
+
label="生成的3D模型",
|
| 72 |
+
clear_color=[0.0, 0.0, 0.0, 0.0]
|
| 73 |
+
),
|
| 74 |
+
gr.Textbox(
|
| 75 |
+
label="处理状态",
|
| 76 |
+
placeholder="等待处理..."
|
| 77 |
+
)
|
| 78 |
],
|
| 79 |
title="麒迹云台 - 2D转3D模型生成器",
|
| 80 |
description="上传一张图片,AI将自动生成对应的3D模型。支持格式:jpg, png, jpeg",
|
| 81 |
+
theme=gr.themes.Soft(),
|
| 82 |
+
allow_flagging="never"
|
| 83 |
)
|
| 84 |
|
| 85 |
+
# 启动应用
|
| 86 |
+
demo.launch()
|