Add application file
Browse files121452374565 unmodified lines1213141920212215242516171827282919202122232425263536373839404142434445464748495027282930313233343536373854394041424344456146from __future__ import annotationsfrom typing import Iterableimport gradio as grfrom gradio.themes.base import Basefrom gradio.themes.utils import colors, fonts, sizesfrom gradio import themesfrom 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 # This is a placeholder transformation; adjust as needed enc_image = np.array(image) # Hypothetical method calls (replace with actual methods): inputs = tokenizer.encode(question, return_tensors='pt') outputs = model.generate(inputs, max_length=50) # Example of processing text input with the model inputs = 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(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 instancepurple_theme = PurpleTheme()class 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=purple_theme, 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()iface.launch()app.py+17-32