File size: 1,352 Bytes
7800904
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import gradio as gr
import traceback
from inference import advanced_generate, get_image_features
 
def caption_image(img, method):
    try:
        if img is None:
            return "Please upload an image."
        
        encoder_output = get_image_features(img)

        if method=='Beam':
            return advanced_generate(encoder_output, method="beam", beam_width=3, temperature=1.0)
        elif method=='Sample':
            captions = []
            for i in range(5):
                caption = advanced_generate(
                    encoder_output,
                    method="sample",
                    temperature=1.1
                )
                captions.append(f"{i+1}. {caption}")
            return "\n".join(captions)
        
    except Exception as e:
        traceback.print_exc()
        return f"Error: {str(e)}"

demo = gr.Interface(
    fn=caption_image,
    inputs=[
        gr.Image(type="numpy", label="Upload an image"),
        gr.Radio(
            choices=["Beam", "Sample"],
            value="beam",
            label="Captioning Method"
        )
    ],
    outputs=gr.Textbox(label="Generated Caption", lines=5),
    title="Image Captioning Model",
    description="Upload an image and choose a decoding method to generate a caption."
)

demo.launch(share=True)