File size: 2,142 Bytes
28e78a6
 
 
 
 
 
 
70ece7b
 
28e78a6
 
 
 
 
3f3396d
28e78a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os

import openai
import streamlit as st
from dotenv import load_dotenv
from PIL import Image

from blip_model import extract_image_details
from gpt_model import generate_response

# Load environment variables from .env file
load_dotenv()

# Set OpenAI API key
openai.api_key = os.getenv('api_key')

# Initialize session state for image details
if "image_details" not in st.session_state:
    st.session_state.image_details = ""

st.title("Image to Text Response with RAG")

# File uploader for image upload
uploaded_file = st.file_uploader("Upload an Image", type=["jpg", "jpeg", "png"])

if uploaded_file is not None:
    if "uploaded_file" not in st.session_state or uploaded_file != st.session_state.uploaded_file:
        st.session_state.uploaded_file = uploaded_file

        # Clear previous messages
        st.session_state.messages = []

        # Show loading status while processing the image
        with st.status("Processing image...", state="running"):
            image = Image.open(uploaded_file)
            st.session_state.image_details = extract_image_details(image)

        st.success("Image processed successfully.")
else:
    st.stop()

# Chat interface
if st.session_state.image_details:
    st.write("### Ask a question about the image")
    
    if "messages" not in st.session_state:
        st.session_state.messages = []

    # Display chat messages
    for message in st.session_state.messages:
        with st.chat_message(message["role"]):
            st.write(message["content"])

    # Chat input
    prompt = st.chat_input("Ask something about the image")
    if prompt:
        # Add user message to session state
        st.session_state.messages.append({"role": "user", "content": prompt})

        # Generate response
        response = generate_response([st.session_state.image_details], prompt)
        st.session_state.messages.append({"role": "assistant", "content": response})

        # Display user message
        with st.chat_message("user"):
            st.write(prompt)

        # Display assistant message
        with st.chat_message("assistant"):
            st.write(response)