Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
@@ -51,11 +51,13 @@ def load_knn():
|
|
51 |
return np.load(knnpath)
|
52 |
|
53 |
def main():
|
54 |
-
st.title("Percept Human Perception Analyzer")
|
55 |
|
56 |
try:
|
57 |
with st.spinner('Loading CLIP model... This may take a moment.'):
|
58 |
model, preprocess, tokenizer = load_model()
|
|
|
|
|
59 |
except Exception as e:
|
60 |
st.error(f"Error loading model: {str(e)}")
|
61 |
st.info("Please make sure you have enough memory and the correct dependencies installed.")
|
@@ -68,9 +70,22 @@ def main():
|
|
68 |
|
69 |
if file:
|
70 |
try:
|
71 |
-
|
72 |
-
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
except Exception as e:
|
75 |
st.error(f"Error processing image: {str(e)}")
|
76 |
|
|
|
51 |
return np.load(knnpath)
|
52 |
|
53 |
def main():
|
54 |
+
st.title("Percept: Human Perception of Street View Image Analyzer")
|
55 |
|
56 |
try:
|
57 |
with st.spinner('Loading CLIP model... This may take a moment.'):
|
58 |
model, preprocess, tokenizer = load_model()
|
59 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
60 |
+
model = model.to(device)
|
61 |
except Exception as e:
|
62 |
st.error(f"Error loading model: {str(e)}")
|
63 |
st.info("Please make sure you have enough memory and the correct dependencies installed.")
|
|
|
70 |
|
71 |
if file:
|
72 |
try:
|
73 |
+
image = Image.open(file)
|
74 |
+
|
75 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
76 |
+
|
77 |
+
# Process image
|
78 |
+
with st.spinner('Processing image...'):
|
79 |
+
processed_image = process_image(image, preprocess)
|
80 |
+
processed_image = processed_image.to(device)
|
81 |
+
|
82 |
+
# Encode into CLIP vector
|
83 |
+
with torch.no_grad():
|
84 |
+
vec = model.encode_image(processed_image)
|
85 |
+
|
86 |
+
# Normalize vector
|
87 |
+
vec /= vec.norm(dim=-1, keepdim=True)
|
88 |
+
st.write(vec.shape)
|
89 |
except Exception as e:
|
90 |
st.error(f"Error processing image: {str(e)}")
|
91 |
|