Spaces:
Build error
Build error
Update app.py
Browse files2 unmodified lines34566789101111121314151614151617171819202119222324252624272829302 unmodified linesimport torchfrom PIL import Image
# Load a smaller image captioning model and necessary processors# Load the image-captioning model and necessary processor/tokenizermodel = VisionEncoderDecoderModel.from_pretrained("nlpconnect/vit-gpt2-image-captioning")processor = ViTImageProcessor.from_pretrained("nlpconnect/vit-gpt2-image-captioning")tokenizer = AutoTokenizer.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
# Function to convert image to text (captioning)# Ensure model is in evaluation mode to reduce memory usagemodel.eval()
# Define the function to convert image to textdef image_to_text(image): try: # Preprocess the image with padding pixel_values = processor(images=image, return_tensors="pt", padding=True).pixel_values # Generate caption with padding handled # Preprocess image with padding pixel_values = processor(images=image, return_tensors="pt", padding="max_length", truncation=True).pixel_values
# Generate text caption with padding considered output_ids = model.generate(pixel_values, max_length=16, num_beams=4, early_stopping=True)
# Decode the output ids to text caption = tokenizer.decode(output_ids[0], skip_special_tokens=True) return caption.strip() except Exception as e: # Return any error message to help with troubleshooting # Return any error message for debugging return f"Error: {e}"
# Gradio interface setupapp.py+11-8