File size: 2,684 Bytes
e90ad78
b6fc9a7
 
 
e90ad78
b6fc9a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import streamlit as st
import torch
from PIL import Image
from transformers import AutoProcessor, AutoModelForImageTextToText

# Set page configuration
st.set_page_config(page_title="Llama 3.2 Vision Model", page_icon="???")

# Title and description
st.title("Llama 3.2 Vision Model Inference")
st.write("Upload an image and provide a prompt to get model insights!")

# Load model and processor (consider caching to improve performance)
@st.cache_resource
def load_model():
    try:
        processor = AutoProcessor.from_pretrained("meta-llama/Llama-3.2-90B-Vision-Instruct")
        model = AutoModelForImageTextToText.from_pretrained("meta-llama/Llama-3.2-90B-Vision-Instruct")
        return processor, model
    except Exception as e:
        st.error(f"Error loading model: {e}")
        return None, None

# Inference function
def generate_response(image, prompt):
    processor, model = load_model()
    
    if not processor or not model:
        return "Model could not be loaded."
    
    try:
        # Prepare inputs
        inputs = processor(images=image, text=prompt, return_tensors="pt")
        
        # Generate response
        with torch.no_grad():
            outputs = model.generate(**inputs)
        
        # Decode the response
        response = processor.decode(outputs[0], skip_special_tokens=True)
        return response
    
    except Exception as e:
        st.error(f"Error during inference: {e}")
        return "An error occurred during image processing."

# Sidebar for user inputs
st.sidebar.header("Image and Prompt")

# Image uploader
uploaded_file = st.sidebar.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])

# Prompt input
prompt = st.sidebar.text_input("Enter your prompt:", 
                                placeholder="Describe what you want to know about the image")

# Main content area
if uploaded_file is not None:
    # Display uploaded image
    image = Image.open(uploaded_file)
    st.image(image, caption="Uploaded Image", use_column_width=True)
    
    # Generate button
    if st.sidebar.button("Generate Response"):
        if prompt:
            # Show loading spinner
            with st.spinner("Generating response..."):
                response = generate_response(image, prompt)
            
            # Display response
            st.subheader("Model Response")
            st.write(response)
        else:
            st.warning("Please enter a prompt!")
else:
    st.info("Upload an image and enter a prompt to get started!")

# Additional error handling and information
st.sidebar.markdown("---")
st.sidebar.info("Note: Model performance depends on image quality and prompt specificity.")