|
from __future__ import annotations |
|
from typing import Iterable |
|
import gradio as gr |
|
from gradio.themes.base import Base |
|
from gradio.themes.utils import colors, fonts, sizes |
|
from transformers import AutoModelForCausalLM, AutoTokenizer |
|
from PIL import Image |
|
import numpy as np |
|
|
|
|
|
model_id = "vikhyatk/moondream2" |
|
revision = "2024-05-20" |
|
model = AutoModelForCausalLM.from_pretrained( |
|
model_id, trust_remote_code=True, revision=revision |
|
) |
|
tokenizer = AutoTokenizer.from_pretrained(model_id, revision=revision) |
|
|
|
def analyze_image_direct(image, question): |
|
|
|
|
|
|
|
|
|
|
|
|
|
enc_image = np.array(image) |
|
|
|
|
|
inputs = tokenizer.encode(question, return_tensors='pt') |
|
outputs = model.generate(inputs, max_length=50) |
|
answer = tokenizer.decode(outputs[0], skip_special_tokens=True) |
|
|
|
return answer |
|
|
|
|
|
class PurpleTheme(Base): |
|
def __init__(self): |
|
super().__init__() |
|
self.primary_hue = colors.Purple |
|
self.secondary_hue = colors.Purple |
|
self.neutral_hue = colors.Gray |
|
self.text_size = sizes.text_lg |
|
self.text_color = colors.white |
|
self.background_color = colors.purple_900 |
|
self.primary_text_color = colors.white |
|
self.secondary_background_color = colors.purple_700 |
|
self.secondary_text_color = colors.white |
|
self.font = fonts.GoogleFont("Arial") |
|
|
|
|
|
purple_theme = PurpleTheme() |
|
|
|
|
|
iface = gr.Interface(fn=analyze_image_direct, |
|
theme=purple_theme, |
|
inputs=[gr.Image(type="pil"), gr.Textbox(lines=2, placeholder="Enter your question here...")], |
|
outputs='text', |
|
title="Direct Image Question Answering", |
|
description="Upload an image and ask a question about it directly using the model.") |
|
|
|
|
|
iface.launch() |
|
|