Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -9,23 +9,65 @@ import open_clip
|
|
| 9 |
knnpath = '20241204-ams-no-env-open_clip_ViT-H-14-378-quickgelu.npz'
|
| 10 |
clip_model_name = 'ViT-H-14-378-quickgelu'
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
#model, preprocess = open_clip.create_model_from_pretrained('hf-hub:laion/CLIP-ViT-g-14-laion2B-s12B-b42K')
|
| 13 |
#tokenizer = open_clip.get_tokenizer('hf-hub:laion/CLIP-ViT-g-14-laion2B-s12B-b42K')
|
| 14 |
|
| 15 |
model, preprocess = open_clip.create_model_from_pretrained(clip_model_name)
|
| 16 |
tokenizer = open_clip.get_tokenizer(clip_model_name)
|
| 17 |
|
| 18 |
-
st.
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
st.write(knn['walkability_vecs'].shape)
|
| 25 |
|
| 26 |
-
file
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
if
|
| 29 |
-
|
| 30 |
-
st.write(file)
|
| 31 |
-
st.write(img.size)
|
|
|
|
| 9 |
knnpath = '20241204-ams-no-env-open_clip_ViT-H-14-378-quickgelu.npz'
|
| 10 |
clip_model_name = 'ViT-H-14-378-quickgelu'
|
| 11 |
|
| 12 |
+
|
| 13 |
+
# Set page config
|
| 14 |
+
st.set_page_config(
|
| 15 |
+
page_title="Percept",
|
| 16 |
+
layout="wide"
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
#model, preprocess = open_clip.create_model_from_pretrained('hf-hub:laion/CLIP-ViT-g-14-laion2B-s12B-b42K')
|
| 20 |
#tokenizer = open_clip.get_tokenizer('hf-hub:laion/CLIP-ViT-g-14-laion2B-s12B-b42K')
|
| 21 |
|
| 22 |
model, preprocess = open_clip.create_model_from_pretrained(clip_model_name)
|
| 23 |
tokenizer = open_clip.get_tokenizer(clip_model_name)
|
| 24 |
|
| 25 |
+
@st.cache_resource
|
| 26 |
+
def load_model():
|
| 27 |
+
"""Load the OpenCLIP model and return model and processor"""
|
| 28 |
+
model, _, preprocess = open_clip.create_model_and_transforms(
|
| 29 |
+
'ViT-H-14',
|
| 30 |
+
pretrained='laion2b_s32b_b79k',
|
| 31 |
+
quickgelu=True
|
| 32 |
+
)
|
| 33 |
+
tokenizer = open_clip.get_tokenizer('ViT-H-14')
|
| 34 |
+
return model, preprocess, tokenizer
|
| 35 |
+
|
| 36 |
+
def process_image(image, preprocess):
|
| 37 |
+
"""Process image and return tensor"""
|
| 38 |
+
if isinstance(image, str):
|
| 39 |
+
# If image is a URL
|
| 40 |
+
response = requests.get(image)
|
| 41 |
+
image = Image.open(BytesIO(response.content))
|
| 42 |
+
# Ensure image is in RGB mode
|
| 43 |
+
if image.mode != 'RGB':
|
| 44 |
+
image = image.convert('RGB')
|
| 45 |
+
processed_image = preprocess(image).unsqueeze(0)
|
| 46 |
+
return processed_image
|
| 47 |
+
|
| 48 |
+
def main():
|
| 49 |
+
st.title("OpenCLIP Image Analyzer (ViT-H-14)")
|
| 50 |
+
|
| 51 |
+
try:
|
| 52 |
+
# Load model (uses st.cache_resource)
|
| 53 |
+
with st.spinner('Loading model... This may take a moment.'):
|
| 54 |
+
model, preprocess, tokenizer = load_model()
|
| 55 |
+
except Exception as e:
|
| 56 |
+
st.error(f"Error loading model: {str(e)}")
|
| 57 |
+
st.info("Please make sure you have enough memory and the correct dependencies installed.")
|
| 58 |
|
| 59 |
+
knn = np.load(modelpath)
|
| 60 |
+
st.write(knn['walkability_vecs'].shape)
|
| 61 |
|
| 62 |
+
file = st.file_uploader('Upload An Image')
|
|
|
|
| 63 |
|
| 64 |
+
if file:
|
| 65 |
+
try:
|
| 66 |
+
with Image.open(file) as img:
|
| 67 |
+
st.write(file)
|
| 68 |
+
st.write(img.size)
|
| 69 |
+
except Exception as e:
|
| 70 |
+
st.error(f"Error processing image: {str(e)}")
|
| 71 |
|
| 72 |
+
if __name__ == "__main__":
|
| 73 |
+
main()
|
|
|
|
|
|