Spaces:
Runtime error
Runtime error
Commit ·
c395879
1
Parent(s): dceb00c
Update app.py
Browse files12344561 unmodified line8910121112131415161718192021222320 unmodified lines444546404748495051525354555657from PIL import Imageimport streamlit as stfrom transformers import CLIPProcessor, CLIPModelimport torch
# Load CLIP model and processormodel = CLIPModel.from_pretrained("openai/clip-vit-large-patch14")1 unmodified line
# Function to process the uploaded imagedef process_image(image): # Use the CLIP model to process the 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)20 unmodified lines # Display the uploaded image st.image(image, caption='Uploaded Image.', use_column_width=True)
# Preprocess the image 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}")
app.py+12-3