Update app.py
Browse files12345678910111213141516171819202122import gradio as grfrom PIL import Imagefrom transformers import pipeline# Initialize the pipeline with the image captioning modelcaption_pipeline = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")def generate_caption(image):# Convert the PIL Image to the format expected by the modelimage = Image.open(image).convert("RGB")# Use the pipeline to generate a captionresult = caption_pipeline(image)caption = result[0]["generated_text"]return caption# Setup the Gradio interfaceinterface = gr.Interface(fn=generate_caption,inputs=gr.inputs.Image(type="pil", label="Upload an Image"),outputs=gr.outputs.Textbox(label="Generated Caption"))interface.launch()123456789101112131415161718import gradio as grfrom transformers import pipelinefrom PIL import Image# Initialize the pipeline with the image captioning modelcaption_pipeline = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")def generate_caption(image):# The image is received as a PIL Image, so no need for conversionresult = caption_pipeline(image)caption = result[0]["generated_text"]return caption# Setup the Gradio interfaceinterface = gr.Interface(fn=generate_caption,inputs=gr.inputs.Image(label="Upload an Image", type="pil"),outputs=gr.outputs.Textbox(label="Generated Caption"))interface.launch()
app.py+3-7