Spaces:
Runtime error
Runtime error
File size: 1,867 Bytes
032e9d4 c395879 032e9d4 c395879 032e9d4 315a6c8 032e9d4 2633210 032e9d4 2633210 c395879 2633210 c395879 2633210 c395879 2633210 032e9d4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | from PIL import Image
import streamlit as st
from transformers import CLIPProcessor, CLIPModel
# Load CLIP model and processor
model = CLIPModel.from_pretrained("openai/clip-vit-large-patch14")
processor = CLIPProcessor.from_pretrained("openai/clip-vit-large-patch14")
# Function to process the uploaded image
def process_image(image):
# Use the CLIP processor to encode the image
inputs = processor(images=image, return_tensors="pt", padding=True)
# Use a placeholder text prompt for image classification
text_prompt = ["a photo of", "a picture of"]
# Complete the text prompt with the processed image
inputs["input_ids"] = processor.encode_plus(text_prompt, images=image, return_tensors="pt")["input_ids"]
# Use the CLIP model to classify the image
outputs = model(**inputs)
logits_per_image = outputs.logits_per_image
probs = logits_per_image.softmax(dim=1)
return probs
def main():
# Set a simple theme
st.set_page_config(
page_title="Qadoos Image Classifier",
page_icon="🌈",
layout="wide",
initial_sidebar_state="expanded",
)
st.title("Qadoos Image Classifier")
st.subheader("Upload an image")
uploaded_image = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
if uploaded_image is not None:
# Read the image from the uploaded file
image = Image.open(uploaded_image)
# Display the uploaded image
st.image(image, caption='Uploaded Image.', use_column_width=True)
try:
# Preprocess the image and classify
probs = process_image(image)
# Display the result
st.subheader("Result")
st.json(probs.tolist())
except Exception as e:
st.error(f"Error processing image: {e}")
if __name__ == '__main__':
main()
|