|
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_dotenv() |
|
|
|
|
|
openai.api_key = os.getenv('api_key') |
|
|
|
|
|
if "image_details" not in st.session_state: |
|
st.session_state.image_details = "" |
|
|
|
st.title("Image to Text Response with RAG") |
|
|
|
|
|
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 |
|
|
|
|
|
st.session_state.messages = [] |
|
|
|
|
|
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() |
|
|
|
|
|
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 = [] |
|
|
|
|
|
for message in st.session_state.messages: |
|
with st.chat_message(message["role"]): |
|
st.write(message["content"]) |
|
|
|
|
|
prompt = st.chat_input("Ask something about the image") |
|
if prompt: |
|
|
|
st.session_state.messages.append({"role": "user", "content": prompt}) |
|
|
|
|
|
response = generate_response([st.session_state.image_details], prompt) |
|
st.session_state.messages.append({"role": "assistant", "content": response}) |
|
|
|
|
|
with st.chat_message("user"): |
|
st.write(prompt) |
|
|
|
|
|
with st.chat_message("assistant"): |
|
st.write(response) |
|
|