Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -17,11 +17,13 @@ results_cache = {}
|
|
17 |
|
18 |
# Download model from Hugging Face
|
19 |
def download_model():
|
|
|
20 |
model_path = hf_hub_download(repo_id="jays009/Restnet50", filename="pytorch_model.bin")
|
21 |
return model_path
|
22 |
|
23 |
# Load the model from Hugging Face
|
24 |
def load_model(model_path):
|
|
|
25 |
model = models.resnet50(pretrained=False)
|
26 |
model.fc = nn.Linear(model.fc.in_features, num_classes)
|
27 |
model.load_state_dict(torch.load(model_path, map_location=torch.device("cpu")))
|
@@ -76,40 +78,24 @@ def predict_from_url(url):
|
|
76 |
print(f"Error during URL processing: {e}")
|
77 |
return {"error": f"Failed to process the URL: {str(e)}"}
|
78 |
|
79 |
-
# Main prediction function
|
80 |
-
def
|
81 |
try:
|
|
|
82 |
if image:
|
83 |
result = predict_from_image(image)
|
84 |
-
|
|
|
85 |
else:
|
86 |
-
|
87 |
-
except Exception as e:
|
88 |
-
print(f"Error in direct upload prediction function: {e}")
|
89 |
-
return {"error": str(e)}
|
90 |
|
91 |
-
#
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
if path.startswith("http://") or path.startswith("https://"):
|
97 |
-
result = predict_from_url(path)
|
98 |
-
elif os.path.isfile(path):
|
99 |
-
image = Image.open(path)
|
100 |
-
result = predict_from_image(image)
|
101 |
-
else:
|
102 |
-
result = {"error": "Invalid path format. Please provide a valid URL or local file path."}
|
103 |
-
|
104 |
-
event_id = id(result)
|
105 |
-
results_cache[event_id] = result
|
106 |
-
|
107 |
-
print(f"Event ID: {event_id}, Result: {result}")
|
108 |
-
return {"event_id": event_id, "result": result}
|
109 |
-
else:
|
110 |
-
return {"error": "No path provided. Please provide a valid path."}
|
111 |
except Exception as e:
|
112 |
-
print(f"Error in prediction function
|
113 |
return {"error": str(e)}
|
114 |
|
115 |
# Function to retrieve result by event_id
|
@@ -144,8 +130,8 @@ with iface:
|
|
144 |
submit_button = gr.Button("Submit")
|
145 |
|
146 |
submit_button.click(
|
147 |
-
fn=
|
148 |
-
inputs=[image_input],
|
149 |
outputs=output
|
150 |
)
|
151 |
|
|
|
17 |
|
18 |
# Download model from Hugging Face
|
19 |
def download_model():
|
20 |
+
print("Downloading model...")
|
21 |
model_path = hf_hub_download(repo_id="jays009/Restnet50", filename="pytorch_model.bin")
|
22 |
return model_path
|
23 |
|
24 |
# Load the model from Hugging Face
|
25 |
def load_model(model_path):
|
26 |
+
print("Loading model...")
|
27 |
model = models.resnet50(pretrained=False)
|
28 |
model.fc = nn.Linear(model.fc.in_features, num_classes)
|
29 |
model.load_state_dict(torch.load(model_path, map_location=torch.device("cpu")))
|
|
|
78 |
print(f"Error during URL processing: {e}")
|
79 |
return {"error": f"Failed to process the URL: {str(e)}"}
|
80 |
|
81 |
+
# Main prediction function with caching
|
82 |
+
def predict(image, url):
|
83 |
try:
|
84 |
+
print("Starting prediction...")
|
85 |
if image:
|
86 |
result = predict_from_image(image)
|
87 |
+
elif url:
|
88 |
+
result = predict_from_url(url)
|
89 |
else:
|
90 |
+
result = {"error": "No input provided. Please upload an image or provide a URL."}
|
|
|
|
|
|
|
91 |
|
92 |
+
event_id = id(result) # Use Python's id() function to generate a unique identifier
|
93 |
+
results_cache[event_id] = result
|
94 |
+
|
95 |
+
print(f"Event ID: {event_id}, Result: {result}")
|
96 |
+
return {"event_id": event_id, "result": result}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
except Exception as e:
|
98 |
+
print(f"Error in prediction function: {e}")
|
99 |
return {"error": str(e)}
|
100 |
|
101 |
# Function to retrieve result by event_id
|
|
|
130 |
submit_button = gr.Button("Submit")
|
131 |
|
132 |
submit_button.click(
|
133 |
+
fn=predict,
|
134 |
+
inputs=[image_input, url_input],
|
135 |
outputs=output
|
136 |
)
|
137 |
|