Add application file
Browse files12345674 unmodified lines1213141516171819202122232425262728293031323334from transformers import AutoModelForCausalLM, AutoTokenizerfrom PIL import Imageimport gradio as grimport numpy as np# Load the model and tokenizermodel_id = "vikhyatk/moondream2"4 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# 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 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()1234564 unmodified lines11121314151617181920212223242526272829303132333435from transformers import AutoModelForCausalLM, AutoTokenizerfrom PIL import Imageimport gradio as gr# Load the model and tokenizermodel_id = "vikhyatk/moondream2"4 unmodified linestokenizer = AutoTokenizer.from_pretrained(model_id, revision=revision)def analyze_image_direct(image, question):# 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 "This is a placeholder answer."# Define custom CSS to make the interface purplecustom_css = """body { background-color: #800080; }button { background-color: #9932CC; color: white; }textarea { background-color: #DDA0DD; color: black; }"""# Create Gradio interface with custom CSS for a purple themeiface = 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.",theme="dark", # Use the dark theme as a basecss=custom_css) # Apply custom CSS# Launch the interfaceiface.launch()
app.py+14-13