Add application file
Browse files12123678945673 unmodified lines11121319202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091141516171819202122232425262728293031323334from __future__ import annotations
from transformers import AutoModelForCausalLM, AutoTokenizerfrom PIL import Imageimport gradio as grfrom typing import Iterablefrom gradio.themes.base import Basefrom gradio.themes.utils import colors, fonts, sizesimport timeimport numpy as np
# Load the model and tokenizermodel_id = "vikhyatk/moondream2"3 unmodified lines)tokenizer = AutoTokenizer.from_pretrained(model_id, revision=revision)
def analyze_image_direct(name, count): # This is a placeholder function. You need to implement the logic based on your model's capabilities. # For demonstration, it returns a static response. return f"This is a placeholder answer for {name} with count {count}."
class Seafoam(Base): def __init__( self, *, primary_hue: colors.Color | str = colors.emerald, secondary_hue: colors.Color | str = colors.blue, neutral_hue: colors.Color | str = colors.blue, spacing_size: sizes.Size | str = sizes.spacing_md, radius_size: sizes.Size | str = sizes.radius_md, text_size: sizes.Size | str = sizes.text_lg, font: fonts.Font | str | Iterable[fonts.Font | str] = ( fonts.GoogleFont("Quicksand"), "ui-sans-serif", "sans-serif", ), font_mono: fonts.Font | str | Iterable[fonts.Font | str] = ( fonts.GoogleFont("IBM Plex Mono"), "ui-monospace", "monospace", ), ): super().__init__( primary_hue=primary_hue, secondary_hue=secondary_hue, neutral_hue=neutral_hue, spacing_size=spacing_size, radius_size=radius_size, text_size=text_size, font=font, font_mono=font_mono, ) super().set( body_background_fill="repeating-linear-gradient(45deg, *primary_200, *primary_200 10px, *primary_50 10px, *primary_50 20px)", body_background_fill_dark="repeating-linear-gradient(45deg, *primary_800, *primary_800 10px, *primary_900 10px, *primary_900 20px)", button_primary_background_fill="linear-gradient(90deg, *primary_300, *secondary_400)", button_primary_background_fill_hover="linear-gradient(90deg, *primary_200, *secondary_300)", button_primary_text_color="white", button_primary_background_fill_dark="linear-gradient(90deg, *primary_600, *secondary_800)", slider_color="*secondary_300", slider_color_dark="*secondary_600", block_title_text_weight="600", block_border_width="3px", block_shadow="*shadow_drop_lg", button_shadow="*shadow_drop_lg", button_large_padding="32px", )
seafoam = Seafoam()
with gr.Blocks(theme=seafoam) as demo: with gr.Row(): name_input = gr.Textbox(label="Name", placeholder="Enter your name here...") with gr.Row(): count_slider = gr.Slider(label="Count", minimum=0, maximum=100, step=1, value=0) with gr.Row(): submit_button = gr.Button("Submit") clear_button = gr.Button("Clear") output = gr.Textbox(label="Output")
submit_button.click(fn=analyze_image_direct, inputs=[name_input, count_slider], outputs=output) clear_button.click(fn=lambda: ("", 0, ""), inputs=None, outputs=[name_input, count_slider, output])
demo.launch()def analyze_image_direct(image, question): # Convert PIL Image to the format expected by the model # Note: This step depends on the model's expected input format # For demonstration, assuming the model accepts PIL images directly enc_image = model.encode_image(image) # This method might not exist; adjust based on actual model capabilities # Generate an answer to the question based on the encoded image # Note: This step is hypothetical and depends on the model's capabilities answer = model.answer_question(enc_image, question, tokenizer) # Adjust based on actual model capabilities return answer
# Create Gradio interfaceiface = gr.Interface(fn=analyze_image_direct, 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 interfaceiface.launch()app.py+22-79