Spaces:
Build error
Build error
Update app.py
Browse files2 unmodified lines34567891011121314151617181920212223242526272 unmodified linesimport torchfrom PIL import Image# Load a smaller image captioning model and necessary processorsmodel = 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)def image_to_text(image):try:# Preprocess the image with paddingpixel_values = processor(images=image, return_tensors="pt", padding=True).pixel_values# Generate caption with padding handledoutput_ids = model.generate(pixel_values, max_length=16, num_beams=4, early_stopping=True)# Decode the output ids to textcaption = tokenizer.decode(output_ids[0], skip_special_tokens=True)return caption.strip()except Exception as e:# Return any error message to help with troubleshootingreturn f"Error: {e}"# Gradio interface setup2 unmodified lines34567891011121314151617181920212223242526272829302 unmodified linesimport torchfrom PIL import Image# 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")# 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 image with paddingpixel_values = processor(images=image, return_tensors="pt", padding="max_length", truncation=True).pixel_values# Generate text caption with padding consideredoutput_ids = model.generate(pixel_values, max_length=16, num_beams=4, early_stopping=True)# Decode the output ids to textcaption = tokenizer.decode(output_ids[0], skip_special_tokens=True)return caption.strip()except Exception as e:# Return any error message for debuggingreturn f"Error: {e}"# Gradio interface setup
app.py+11-8