Add application file
Browse files123456789105 unmodified lines16171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061from __future__ import annotationsfrom typing import Iterableimport gradio as grfrom gradio.themes.base import Basefrom gradio.themes.utils import colors, fonts, sizesfrom transformers import AutoModelForCausalLM, AutoTokenizerfrom PIL import Imageimport numpy as np# Load the model and tokenizer5 unmodified linestokenizer = 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 schemeclass PurpleTheme(Base):def __init__(self):super().__init__()self.primary_hue = colors.purpleself.secondary_hue = colors.purpleself.neutral_hue = colors.grayself.text_size = sizes.text_lgself.text_color = colors.whiteself.background_color = colors.purple_900self.primary_text_color = colors.whiteself.secondary_background_color = colors.purple_700self.secondary_text_color = colors.whiteself.font = fonts.GoogleFont("Arial")# Create a custom theme instancepurple_theme = PurpleTheme()# Create Gradio interface with the custom themeiface = 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 interfaceiface.launch()1234565 unmodified lines1213141516171819202122232425262728293031323334353637383940414243444546import gradio as grfrom gradio import themesfrom transformers import AutoModelForCausalLM, AutoTokenizerimport numpy as np# Load the model and tokenizer5 unmodified linestokenizer = AutoTokenizer.from_pretrained(model_id, revision=revision)def analyze_image_direct(image, question):# Convert PIL Image to the format expected by the model# This is a placeholder transformation; adjust as neededenc_image = np.array(image)# Example of processing text input with the modelinputs = tokenizer(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 schemeclass PurpleTheme(themes.Theme):base = "light"font = "Arial"colors = {"primary": "#9b59b6","text": "#FFFFFF","background": "#5B2C6F","secondary_background": "#7D3C98",}# Create Gradio interface with the custom themeiface = gr.Interface(fn=analyze_image_direct,theme=PurpleTheme(),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+17-32