Add application file
Browse files121234566 unmodified lines1314151617161718191920212220212223242526272829303126272829303132333435363738394041424344454632import gradio as grfrom gradio import themesfrom transformers import AutoModelForCausalLM, AutoTokenizerfrom PIL import Imageimport gradio as grimport numpy as np
# Load the model and tokenizer6 unmodified lines
def analyze_image_direct(image, question): # Convert PIL Image to the format expected by the model # This is a placeholder transformation; adjust as needed enc_image = np.array(image) # 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 # 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) # 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 a Gradio interfacewith gr.Block() as block: image = gr.inputs.Image(label="Image") question = gr.inputs.Textbox(label="Question") output = gr.outputs.Textbox(label="Answer") gr.Interface(fn=analyze_image_direct, inputs=[image, question], outputs=output).launch()
# 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()block.launch()app.py+15-29