File size: 2,700 Bytes
6406ec7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import os
from dotenv import load_dotenv
from PIL import Image
import google.generativeai as genai

# Load environment variables from .env.
load_dotenv()

# Configure Google Generative AI
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))

# Function to get response from Gemini text model
def get_gemini_text_response(input_text):
    model = genai.GenerativeModel('gemini-pro')
    response = model.generate_content(input_text)
    return response.text if response else "Please provide a text prompt."

# Function to get response from Gemini vision model
def get_gemini_image_response(image, input_text=None):
    model = genai.GenerativeModel('gemini-pro-vision')
    if input_text:
        response = model.generate_content([input_text, image])
    else:
        response = model.generate_content(image)
    return response.text if response else "Please provide a image."

# Initialize Streamlit app
st.set_page_config(page_title="Gemini Q&A and Image Analysis", page_icon="🤖")

# Sidebar for additional options
st.sidebar.title("About")
st.sidebar.info("This is a Q&A chatbot powered by Google's Gemini AI model.")
st.sidebar.title("Instructions")
st.sidebar.info("1. Choose either 'Q&A Chatbot' or 'Image Analysis' from the options.\n"
                "2. Enter your question/prompt or upload an image.\n"
                "3. Click the 'Ask' button to get the response.\n"
                "4. The response will be displayed below.")

# Main interface
st.title("Gemini Q&A and Image Analysis Chatbot")
st.write("Select an option to either ask a question or analyze an image.")

# User selection
option = st.selectbox("Choose an option:", ("Q&A Chatbot", "Image Analysis"))

# Input fields based on user selection
if option == "Q&A Chatbot":
    input_text = st.text_input("Enter your question or prompt:", key="input_text")
    if st.button("Ask"):
        with st.spinner("Generating response..."):
            response = get_gemini_text_response(input_text)
        st.subheader("Response:")
        st.write(response)

elif option == "Image Analysis":
    uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
    image_input_text = st.text_input("Enter your question or prompt related to the image (optional):", key="image_input_text")
    if uploaded_file is not None:
        image = Image.open(uploaded_file)
        st.image(image, caption="Uploaded Image.", use_column_width=True)
        if st.button("Analyze"):
            with st.spinner("Generating response..."):
                response = get_gemini_image_response(image, input_text=image_input_text)
            st.subheader("Response:")
            st.write(response)