Add application file
Browse files
app.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from model import MoondreamModel
|
3 |
+
import torch
|
4 |
+
|
5 |
+
# Load the model and tokenizer once at startup
|
6 |
+
model = MoondreamModel.load_model()
|
7 |
+
tokenizer = MoondreamModel.load_tokenizer()
|
8 |
+
|
9 |
+
# Define the default question
|
10 |
+
default_question = "Describe the image."
|
11 |
+
|
12 |
+
# Function to handle image and return generated caption
|
13 |
+
def generate_caption_with_default(image):
|
14 |
+
# Preprocess the image
|
15 |
+
preprocessed_image = MoondreamModel.preprocess_image(image)
|
16 |
+
|
17 |
+
# Generate caption
|
18 |
+
caption = MoondreamModel.generate_caption(model, preprocessed_image, tokenizer)
|
19 |
+
|
20 |
+
return caption
|
21 |
+
|
22 |
+
# Create Gradio interface
|
23 |
+
interface = gr.Interface(
|
24 |
+
fn=generate_caption_with_default,
|
25 |
+
inputs=gr.inputs.Image(type="pil", label="Upload an Image"),
|
26 |
+
outputs="text",
|
27 |
+
title="Image Caption Generator",
|
28 |
+
description=f"The default question is: '{default_question}'. Upload an image to generate a description."
|
29 |
+
)
|
30 |
+
|
31 |
+
# Launch the interface
|
32 |
+
interface.launch()
|