CoolFace
Apppublic

swarno/Conversational_AI_Image_Chatbot

sourceHugging Facemitupdated 2y agoView on Hugging Face
0likes
app.py100 linesDownload Raw Back to root
1from dotenv import load_dotenv2load_dotenv()  # Load all the environment variables from .env3 4import streamlit as st5import os6from PIL import Image7import google.generativeai as genai8import time9 10genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))11 12# Load Gemini pro vision model13model = genai.GenerativeModel('gemini-1.5-flash')14 15def get_gemini_response(input, image, user_prompt):16    response = model.generate_content([input, image[0], user_prompt])17    return response.text18 19def input_image_details(uploaded_file):20    if uploaded_file is not None:21        # Read the file into bytes22        bytes_data = uploaded_file.getvalue()23        image_parts = [24            {25                "mime_type": uploaded_file.type,  # Get the mime type of the uploaded file26                "data": bytes_data27            }28        ]29        return image_parts30    else:31        raise FileNotFoundError("No file uploaded")32 33# Initialize our Streamlit app34st.set_page_config(page_title="GENTECH Chatbot")35 36st.header("Conversational AI Image Chatbot")37 38# Move the file uploader to the sidebar39uploaded_file = st.sidebar.file_uploader("Choose an image ...", type=["jpg", "jpeg", "png"])40 41if uploaded_file is not None:42    image = Image.open(uploaded_file)43    st.sidebar.image(image, caption="Uploaded Image.", use_column_width=True)44 45# Initialize session state for chat history if not already present46if 'chat_history' not in st.session_state:47    st.session_state.chat_history = []48if 'last_response' not in st.session_state:49    st.session_state.last_response = None50if 'last_question' not in st.session_state:51    st.session_state.last_question = None52if 'last_interaction_time' not in st.session_state:53    st.session_state.last_interaction_time = time.time()54 55# Check for session timeout (2 minutes)56if time.time() - st.session_state.last_interaction_time > 120:57    st.session_state.chat_history = []58    st.session_state.last_response = None59    st.session_state.last_question = None60 61# Define the input prompt for the AI62input_prompt = """63You are a conversational AI image chatbot. Your task is to analyze images provided by the user and answer any questions related to those images. You can remember the context of the conversation and use it to provide accurate and relevant responses. Here are your instructions:64 65Image Analysis: When an image is provided, analyze it to understand its content, objects, and any relevant details.66Answering Questions: Respond to questions related to the image with accurate and detailed information.67Contextual Memory: Remember the context of the conversation, including previous images and questions, to provide coherent and contextually relevant answers.68User Interaction: Engage with the user in a friendly and helpful manner, ensuring that your responses are clear and informative.69"""70 71# Define input text at the bottom72input_text = st.text_input("Input Prompt: ", key="input")73submit = st.button("Ask Solution")74 75# If submit button is clicked76if submit and uploaded_file is not None:77    st.session_state.last_interaction_time = time.time()  # Update last interaction time78    image_data = input_image_details(uploaded_file)79    response = get_gemini_response(input_prompt, image_data, input_text)80 81    # Store the interaction temporarily82    st.session_state.last_response = response83    st.session_state.last_question = input_text84 85# Display the last question and response86if st.session_state.last_question and st.session_state.last_response:87    st.subheader("Last Question and Response:")88    st.write(f"**USER:** {st.session_state.last_question}")89    st.write(f"**JARVIS:** {st.session_state.last_response}")90 91# Only add to chat history if a new question is asked92if submit and uploaded_file is not None:93    st.session_state.chat_history.append({"user": st.session_state.last_question, "ai": st.session_state.last_response})94 95# Display chat history96st.subheader("Chat History")97for interaction in st.session_state.chat_history:98    st.write(f"**USER:** {interaction['user']}")99    st.write(f"**JARVIS:** {interaction['ai']}")100