File size: 4,176 Bytes
ba58eb0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
029bab4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5c9ed90
ba58eb0
029bab4
 
 
 
 
 
 
 
 
 
ba58eb0
 
029bab4
ba58eb0
029bab4
 
 
 
ba58eb0
029bab4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ba58eb0
029bab4
 
 
 
ba58eb0
029bab4
ba58eb0
029bab4
 
 
 
 
5c9ed90
 
ba58eb0
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# import gradio as gr
# import torch
# from PIL import Image
# from model import CRM
# from inference import generate3d
# import numpy as np

# # Load model
# crm_path = "CRM.pth"  # Make sure the model is uploaded to the Space
# model = CRM(torch.load(crm_path, map_location="cpu"))
# model = model.to("cuda:0" if torch.cuda.is_available() else "cpu")

# def generate_3d(image_path, seed=1234, scale=5.5, step=30):
#     image = Image.open(image_path).convert("RGB")
#     np_img = np.array(image)
#     glb_path = generate3d(model, np_img, np_img, "cuda:0" if torch.cuda.is_available() else "cpu")
#     return glb_path

# iface = gr.Interface(
#     fn=generate_3d,
#     inputs=gr.Image(type="filepath"),
#     outputs=gr.Model3D(),
#     title="Convolutional Reconstruction Model (CRM)",
#     description="Upload an image to generate a 3D model."
# )

# iface.launch()


#############2nd################3
# import os
# import torch
# import gradio as gr
# from huggingface_hub import hf_hub_download
# from model import CRM  # Make sure this matches your model file structure

# # Define model details
# REPO_ID = "Mariam-Elz/CRM"  # Hugging Face model repo
# MODEL_FILES = {
#     "ccm-diffusion": "ccm-diffusion.pth",
#     "pixel-diffusion": "pixel-diffusion.pth",
#     "CRM": "CRM.pth"
# }
# DEVICE = "cuda" if torch.cuda.is_available() else "cpu"

# # Download models from Hugging Face if not already present
# MODEL_DIR = "./models"
# os.makedirs(MODEL_DIR, exist_ok=True)

# for name, filename in MODEL_FILES.items():
#     model_path = os.path.join(MODEL_DIR, filename)
#     if not os.path.exists(model_path):
#         print(f"Downloading {filename}...")
#         hf_hub_download(repo_id=REPO_ID, filename=filename, local_dir=MODEL_DIR)

# # Load the model
# print("Loading CRM Model...")
# model = CRM()
# model.load_state_dict(torch.load(os.path.join(MODEL_DIR, MODEL_FILES["CRM"]), map_location=DEVICE))
# model.to(DEVICE)
# model.eval()
# print("✅ Model Loaded Successfully!")

# # Define Gradio Interface
# def predict(input_image):
#     with torch.no_grad():
#         output = model(input_image.to(DEVICE))  # Modify based on model input format
#     return output.cpu()

# demo = gr.Interface(
#     fn=predict,
#     inputs=gr.Image(type="pil"),
#     outputs=gr.Image(type="pil"),
#     title="Convolutional Reconstruction Model (CRM)",
#     description="Upload an image to generate a reconstructed output."
# )

# if __name__ == "__main__":
#     demo.launch()
########################3rd######################3

import torch
import gradio as gr
import requests
import os

# Download model weights from Hugging Face model repo (if not already present)
model_repo = "Mariam-Elz/CRM"  # Your Hugging Face model repo

model_files = {
    "ccm-diffusion.pth": "ccm-diffusion.pth",
    "pixel-diffusion.pth": "pixel-diffusion.pth",
    "CRM.pth": "CRM.pth",
}

os.makedirs("models", exist_ok=True)

for filename, output_path in model_files.items():
    file_path = f"models/{output_path}"
    if not os.path.exists(file_path):
        url = f"https://huggingface.co/{model_repo}/resolve/main/{filename}"
        print(f"Downloading {filename}...")
        response = requests.get(url)
        with open(file_path, "wb") as f:
            f.write(response.content)

# Load model (This part depends on how the model is defined)
device = "cuda" if torch.cuda.is_available() else "cpu"

def load_model():
    model_path = "models/CRM.pth"
    model = torch.load(model_path, map_location=device)
    model.eval()
    return model

model = load_model()

# Define inference function
def infer(image):
    """Process input image and return a reconstructed image."""
    with torch.no_grad():
        # Assuming model expects a tensor input
        image_tensor = torch.tensor(image).to(device)
        output = model(image_tensor)
        return output.cpu().numpy()

# Create Gradio UI
demo = gr.Interface(
    fn=infer,
    inputs=gr.Image(type="numpy"),
    outputs=gr.Image(type="numpy"),
    title="Convolutional Reconstruction Model",
    description="Upload an image to get the reconstructed output."
)

if __name__ == "__main__":
    demo.launch()