Spaces:
Runtime error

AbdulQadoos's picture
Update app.py
c395879
Raw History Blame Contribute Delete
1.87 kB
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()