Mariam-Elz commited on
Commit
029bab4
·
verified ·
1 Parent(s): ba58eb0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +97 -36
app.py CHANGED
@@ -25,51 +25,112 @@
25
  # )
26
 
27
  # iface.launch()
28
- import os
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  import torch
30
  import gradio as gr
31
- from huggingface_hub import hf_hub_download
32
- from model import CRM # Make sure this matches your model file structure
33
-
34
- # Define model details
35
- REPO_ID = "Mariam-Elz/CRM" # Hugging Face model repo
36
- MODEL_FILES = {
37
- "ccm-diffusion": "ccm-diffusion.pth",
38
- "pixel-diffusion": "pixel-diffusion.pth",
39
- "CRM": "CRM.pth"
 
40
  }
41
- DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
42
 
43
- # Download models from Hugging Face if not already present
44
- MODEL_DIR = "./models"
45
- os.makedirs(MODEL_DIR, exist_ok=True)
46
 
47
- for name, filename in MODEL_FILES.items():
48
- model_path = os.path.join(MODEL_DIR, filename)
49
- if not os.path.exists(model_path):
 
50
  print(f"Downloading {filename}...")
51
- hf_hub_download(repo_id=REPO_ID, filename=filename, local_dir=MODEL_DIR)
52
-
53
- # Load the model
54
- print("Loading CRM Model...")
55
- model = CRM()
56
- model.load_state_dict(torch.load(os.path.join(MODEL_DIR, MODEL_FILES["CRM"]), map_location=DEVICE))
57
- model.to(DEVICE)
58
- model.eval()
59
- print("✅ Model Loaded Successfully!")
60
-
61
- # Define Gradio Interface
62
- def predict(input_image):
 
 
 
 
 
 
63
  with torch.no_grad():
64
- output = model(input_image.to(DEVICE)) # Modify based on model input format
65
- return output.cpu()
 
 
66
 
 
67
  demo = gr.Interface(
68
- fn=predict,
69
- inputs=gr.Image(type="pil"),
70
- outputs=gr.Image(type="pil"),
71
- title="Convolutional Reconstruction Model (CRM)",
72
- description="Upload an image to generate a reconstructed output."
73
  )
74
 
75
  if __name__ == "__main__":
 
25
  # )
26
 
27
  # iface.launch()
28
+
29
+
30
+ #############2nd################3
31
+ # import os
32
+ # import torch
33
+ # import gradio as gr
34
+ # from huggingface_hub import hf_hub_download
35
+ # from model import CRM # Make sure this matches your model file structure
36
+
37
+ # # Define model details
38
+ # REPO_ID = "Mariam-Elz/CRM" # Hugging Face model repo
39
+ # MODEL_FILES = {
40
+ # "ccm-diffusion": "ccm-diffusion.pth",
41
+ # "pixel-diffusion": "pixel-diffusion.pth",
42
+ # "CRM": "CRM.pth"
43
+ # }
44
+ # DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
45
+
46
+ # # Download models from Hugging Face if not already present
47
+ # MODEL_DIR = "./models"
48
+ # os.makedirs(MODEL_DIR, exist_ok=True)
49
+
50
+ # for name, filename in MODEL_FILES.items():
51
+ # model_path = os.path.join(MODEL_DIR, filename)
52
+ # if not os.path.exists(model_path):
53
+ # print(f"Downloading {filename}...")
54
+ # hf_hub_download(repo_id=REPO_ID, filename=filename, local_dir=MODEL_DIR)
55
+
56
+ # # Load the model
57
+ # print("Loading CRM Model...")
58
+ # model = CRM()
59
+ # model.load_state_dict(torch.load(os.path.join(MODEL_DIR, MODEL_FILES["CRM"]), map_location=DEVICE))
60
+ # model.to(DEVICE)
61
+ # model.eval()
62
+ # print("✅ Model Loaded Successfully!")
63
+
64
+ # # Define Gradio Interface
65
+ # def predict(input_image):
66
+ # with torch.no_grad():
67
+ # output = model(input_image.to(DEVICE)) # Modify based on model input format
68
+ # return output.cpu()
69
+
70
+ # demo = gr.Interface(
71
+ # fn=predict,
72
+ # inputs=gr.Image(type="pil"),
73
+ # outputs=gr.Image(type="pil"),
74
+ # title="Convolutional Reconstruction Model (CRM)",
75
+ # description="Upload an image to generate a reconstructed output."
76
+ # )
77
+
78
+ # if __name__ == "__main__":
79
+ # demo.launch()
80
+ ########################3rd######################3
81
+
82
  import torch
83
  import gradio as gr
84
+ import requests
85
+ import os
86
+
87
+ # Download model weights from Hugging Face model repo (if not already present)
88
+ model_repo = "Mariam-Elz/CRM" # Your Hugging Face model repo
89
+
90
+ model_files = {
91
+ "ccm-diffusion.pth": "ccm-diffusion.pth",
92
+ "pixel-diffusion.pth": "pixel-diffusion.pth",
93
+ "CRM.pth": "CRM.pth",
94
  }
 
95
 
96
+ os.makedirs("models", exist_ok=True)
 
 
97
 
98
+ for filename, output_path in model_files.items():
99
+ file_path = f"models/{output_path}"
100
+ if not os.path.exists(file_path):
101
+ url = f"https://huggingface.co/{model_repo}/resolve/main/{filename}"
102
  print(f"Downloading {filename}...")
103
+ response = requests.get(url)
104
+ with open(file_path, "wb") as f:
105
+ f.write(response.content)
106
+
107
+ # Load model (This part depends on how the model is defined)
108
+ device = "cuda" if torch.cuda.is_available() else "cpu"
109
+
110
+ def load_model():
111
+ model_path = "models/CRM.pth"
112
+ model = torch.load(model_path, map_location=device)
113
+ model.eval()
114
+ return model
115
+
116
+ model = load_model()
117
+
118
+ # Define inference function
119
+ def infer(image):
120
+ """Process input image and return a reconstructed image."""
121
  with torch.no_grad():
122
+ # Assuming model expects a tensor input
123
+ image_tensor = torch.tensor(image).to(device)
124
+ output = model(image_tensor)
125
+ return output.cpu().numpy()
126
 
127
+ # Create Gradio UI
128
  demo = gr.Interface(
129
+ fn=infer,
130
+ inputs=gr.Image(type="numpy"),
131
+ outputs=gr.Image(type="numpy"),
132
+ title="Convolutional Reconstruction Model",
133
+ description="Upload an image to get the reconstructed output."
134
  )
135
 
136
  if __name__ == "__main__":