Add application file
Browse files1234566 unmodified lines13141516171819202122232425262728293031323334353637383940414243444546import gradio as grfrom gradio import themesfrom transformers import AutoModelForCausalLM, AutoTokenizerimport numpy as np# Load the model and tokenizer6 unmodified linesdef 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()1234566 unmodified lines1314151617181920212223242526272829303132from transformers import AutoModelForCausalLM, AutoTokenizerfrom PIL import Imageimport gradio as grimport numpy as np# Load the model and tokenizer6 unmodified linesdef 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 directlyenc_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 capabilitiesanswer = model.answer_question(enc_image, question, tokenizer) # Adjust based on actual model capabilitiesreturn 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()block.launch()
app.py+15-29