ChandraP12330 commited on
Commit
2edac28
1 Parent(s): 3d4331f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -45
app.py CHANGED
@@ -1,49 +1,16 @@
1
  import streamlit as st
2
- from transformers import CLIPProcessor, CLIPModel
3
- import torch
4
- from PIL import Image
5
- import io
6
 
7
- # Load the CLIP model and processor
8
- clip_model = CLIPModel.from_pretrained("openai/clip-vit-large-patch14")
9
- clip_processor = CLIPProcessor.from_pretrained("openai/clip-vit-large-patch14")
10
 
11
- # Labels for classification
12
- labels = ["dog", "cat", "bird", "flower", "car", "building", "person", "food"]
13
 
14
- def classify_image(image_bytes):
15
- # Convert bytes to PIL Image
16
- image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
17
-
18
- # Preprocess the image
19
- inputs = clip_processor(images=image, return_tensors="pt")
20
-
21
- # Classify the image using the CLIP model
22
- with torch.no_grad():
23
- logits_per_image = clip_model(**inputs)[0]
24
- probs = logits_per_image.softmax(dim=-1)
25
-
26
- # Get the top predicted label
27
- top_prob, top_label = torch.max(probs, dim=-1)
28
- top_label = labels[top_label.item()]
29
-
30
- return top_label
31
-
32
- def main():
33
- st.title("Image Classification")
34
-
35
- # Upload image
36
- uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
37
-
38
- if uploaded_file is not None:
39
- # Display the uploaded image
40
- image = st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)
41
-
42
- # Classify the image
43
- if st.button("Classify Image"):
44
- with st.spinner("Classifying image..."):
45
- top_label = classify_image(uploaded_file.getvalue())
46
- st.success(f"Top Predicted Label: {top_label}")
47
-
48
- if __name__ == "__main__":
49
- main()
 
1
  import streamlit as st
 
 
 
 
2
 
3
+ # Title
4
+ st.title("Image Display App")
 
5
 
6
+ # Input field for URL
7
+ image_url = st.text_input("Enter the URL of the image:")
8
 
9
+ # Display image if a valid URL is provided
10
+ if image_url:
11
+ try:
12
+ st.image(image_url, caption="Uploaded Image")
13
+ except Exception as e:
14
+ st.error(f"Error: {e}")
15
+ else:
16
+ st.warning("Please enter a valid image URL.")