Spaces:
Runtime error
Runtime error
Delete app.py
Browse files
app.py
DELETED
@@ -1,65 +0,0 @@
|
|
1 |
-
import torch
|
2 |
-
import gradio as gr
|
3 |
-
from PIL import Image
|
4 |
-
import numpy as np
|
5 |
-
|
6 |
-
# بارگذاری مدل
|
7 |
-
model_path = "sapiens_0.3b_render_people_epoch_100_torchscript.pt2"
|
8 |
-
model = torch.jit.load(model_path, map_location=torch.device('cpu'))
|
9 |
-
model.eval()
|
10 |
-
|
11 |
-
def predict(image):
|
12 |
-
try:
|
13 |
-
print("Predict function called")
|
14 |
-
|
15 |
-
# تغییر اندازه تصویر به 768x1024
|
16 |
-
image = image.resize((768, 1024)) # تغییر اندازه به 768x1024
|
17 |
-
|
18 |
-
# پیشپردازش تصویر
|
19 |
-
image = image.convert("RGB")
|
20 |
-
input_tensor = np.array(image)
|
21 |
-
input_tensor = input_tensor.transpose(2, 0, 1) # تبدیل از HWC به CHW
|
22 |
-
input_tensor = input_tensor[np.newaxis, :] # افزودن بعد batch
|
23 |
-
input_tensor = input_tensor / 255.0 # نرمالسازی
|
24 |
-
input_tensor = torch.from_numpy(input_tensor).float()
|
25 |
-
|
26 |
-
print(f"Input tensor shape: {input_tensor.shape}")
|
27 |
-
|
28 |
-
# اجرای مدل
|
29 |
-
with torch.no_grad():
|
30 |
-
output = model(input_tensor)
|
31 |
-
|
32 |
-
print(f"Output tensor shape: {output.shape}")
|
33 |
-
|
34 |
-
# پسپردازش خروجی
|
35 |
-
output_image = output.squeeze().cpu().numpy()
|
36 |
-
|
37 |
-
# اگر خروجی تک کاناله است، به RGB تبدیل میشود
|
38 |
-
if output_image.ndim == 2: # فقط در صورتی که تک کاناله است
|
39 |
-
output_image = np.stack([output_image] * 3, axis=-1)
|
40 |
-
|
41 |
-
elif output_image.shape[0] == 1: # اگر کانال اول 1 است، آن را به RGB تبدیل کنید
|
42 |
-
output_image = np.tile(output_image, (3, 1, 1))
|
43 |
-
output_image = output_image.transpose(1, 2, 0)
|
44 |
-
|
45 |
-
output_image = (output_image * 255).astype(np.uint8)
|
46 |
-
output_image = Image.fromarray(output_image)
|
47 |
-
|
48 |
-
print("Output image generated successfully")
|
49 |
-
return output_image
|
50 |
-
|
51 |
-
except Exception as e:
|
52 |
-
print(f"Error during prediction: {str(e)}")
|
53 |
-
return None
|
54 |
-
|
55 |
-
# تعریف رابط Gradio
|
56 |
-
iface = gr.Interface(
|
57 |
-
fn=predict,
|
58 |
-
inputs=gr.Image(type="pil", label="Input Image"),
|
59 |
-
outputs=gr.Image(type="pil", label="Output Image"),
|
60 |
-
title="Sapiens Model Inference",
|
61 |
-
description="Upload an image to process with the Sapiens model."
|
62 |
-
)
|
63 |
-
|
64 |
-
if __name__ == "__main__":
|
65 |
-
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|