Modarb-AI / app.py
Ahmed007's picture
Add application file
83e1fb4
raw
history blame
2.52 kB
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
# Load the model and tokenizer
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):
# This is a placeholder; modify based on the actual capabilities of your model.
# Here we assume that the model has methods `encode_image` and `answer_question` which might not exist.
# You need to replace them with the actual methods your model uses to process images and generate answers.
# Convert PIL Image to the format expected by the model
# Example transformation (actual code will depend on model's requirements):
enc_image = np.array(image) # Placeholder transformation; adjust as needed
# Hypothetical method calls (replace with actual methods):
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
# Define a custom theme with purple color scheme
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")
# Create a custom theme instance
purple_theme = PurpleTheme()
# Create Gradio interface with the custom theme
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.")
# Launch the interface
iface.launch()