File size: 2,694 Bytes
58c5e3e
 
 
 
 
 
 
7404ad7
 
58c5e3e
 
 
 
 
7404ad7
 
 
 
 
58c5e3e
 
 
7404ad7
58c5e3e
 
 
 
7404ad7
 
58c5e3e
 
7404ad7
 
 
 
 
 
 
 
 
 
 
 
 
7f1a08b
7404ad7
 
58c5e3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7f1a08b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from PIL import Image
import google.generativeai as genai
import os

MODEL_ID = "gemini-2.0-flash-exp" 
api_key = os.getenv("GEMINI_API_KEY")
model_id = MODEL_ID
genai.configure(api_key=api_key)

# Initialize chat history
if "messages" not in st.session_state:
    st.session_state.messages = []

if "model" not in st.session_state:
    st.session_state.model = genai.GenerativeModel(MODEL_ID)

model = st.session_state.model

# Function to reset chat history
def reset_chat():
    st.session_state.messages = []
    model.start_chat() 

# Streamlit app
st.title("Gemini Image Chat")

# File uploader with allowed types
uploaded_file = st.file_uploader("Choose an image or PDF...", type=["jpg", "jpeg", "png", "pdf"])

if uploaded_file is not None:
    # Determine file type
    file_type = uploaded_file.type
    if file_type.startswith('image'):
        # Display the uploaded image
        image = Image.open(uploaded_file)
        st.image(image, caption="Uploaded Image.", use_container_width=True)
        mime_type = "image/jpeg"  # Use a consistent MIME type for images
    elif file_type == 'application/pdf':
        # Display a message for PDF upload
        st.write("PDF file uploaded. You can ask questions about its content.")
        mime_type = "application/pdf"
    else:
        st.error("Unsupported file type. Please upload an image or PDF.")
        st.stop()

    # Reset chat history when a new file is uploaded
    reset_chat()

    # Text input for user prompt
    user_input = st.text_input("Enter your prompt:")

    # Send button
    if st.button("Send"):
        if user_input:
            # Add user message to chat history
            st.session_state.messages.append({"role": "user", "content": user_input})

            # Display chat history
            for message in st.session_state.messages:
                with st.chat_message(message["role"]):
                    st.markdown(message["content"])
            
            with st.spinner("Processing..."):
                # Upload the file with the correct MIME type
                file_data = genai.upload_file(uploaded_file, mime_type=mime_type)

                # Send file and prompt to Gemini API
                response = model.generate_content(
                    [
                        user_input, 
                        file_data
                    ]
                )

                # Add Gemini response to chat history
                st.session_state.messages.append({"role": "assistant", "content": response.text})

                # Display Gemini response
                with st.chat_message("assistant"):
                    st.markdown(response.text)