AmitJ82 commited on
Commit
6406ec7
1 Parent(s): 2d56683

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -88
app.py CHANGED
@@ -1,88 +1,67 @@
1
- import streamlit as st
2
- import os
3
- from dotenv import load_dotenv
4
- from PIL import Image
5
- import google.generativeai as genai
6
-
7
- # Load environment variables from .env.
8
- load_dotenv()
9
-
10
- # Configure Google Generative AI
11
- genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
12
-
13
- # Function to get response from Gemini text model
14
- def get_gemini_text_response(input_text):
15
- model = genai.GenerativeModel('gemini-pro')
16
- response = model.generate_content(input_text)
17
- return response.text if response else "Please provide a valid text prompt."
18
-
19
- # Function to get response from Gemini vision model
20
- def get_gemini_image_response(image, input_text=None):
21
- model = genai.GenerativeModel('gemini-pro-vision')
22
- if input_text:
23
- response = model.generate_content([input_text, image])
24
- else:
25
- response = model.generate_content(image)
26
- return response.text if response else "Please provide a valid image."
27
-
28
- # Initialize Streamlit app
29
- st.set_page_config(page_title="Gemini Q&A and Image Analysis", page_icon="🤖")
30
-
31
- # Sidebar for additional options
32
- st.sidebar.title("About")
33
- st.sidebar.info("This is a Q&A chatbot powered by Google's Gemini AI model.")
34
- st.sidebar.title("Instructions")
35
- st.sidebar.info("1. Choose either 'Q&A Chatbot' or 'Image Analysis' from the options.\n"
36
- "2. Enter your question/prompt or upload an image.\n"
37
- "3. Click the 'Ask' button to get the response.\n"
38
- "4. The response will be displayed below.")
39
-
40
- # Main interface
41
- st.title("Gemini Q&A and Image Analysis Chatbot")
42
- st.write("Select an option to either ask a question or analyze an image.")
43
-
44
- # User selection
45
- option = st.selectbox("Choose an option:", ("Q&A Chatbot", "Image Analysis"))
46
-
47
- # Input fields based on user selection
48
- if option == "Q&A Chatbot":
49
- input_text = st.text_input("Enter your question or prompt:", key="input_text")
50
- if st.button("Ask"):
51
- with st.spinner("Generating response..."):
52
- response = get_gemini_text_response(input_text)
53
- st.subheader("Response:")
54
- st.write(response)
55
-
56
- elif option == "Image Analysis":
57
- uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
58
- image_input_text = st.text_input("Enter your question or prompt related to the image (optional):", key="image_input_text")
59
- if uploaded_file is not None:
60
- image = Image.open(uploaded_file)
61
- st.image(image, caption="Uploaded Image.", use_column_width=True)
62
- if st.button("Analyze"):
63
- with st.spinner("Generating response..."):
64
- response = get_gemini_image_response(image, input_text=image_input_text)
65
- st.subheader("Response:")
66
- st.write(response)
67
-
68
- # Footer
69
- st.markdown(
70
- """
71
- <style>
72
- .footer {
73
- position: fixed;
74
- left: 0;
75
- bottom: 0;
76
- width: 100%;
77
- background-color: #f1f1f1;
78
- color: black;
79
- text-align: center;
80
- padding: 10px;
81
- }
82
- </style>
83
- <div class="footer">
84
- <p>Powered by Google's Gemini AI | Made with ❤️ by Amit_J</p>
85
- </div>
86
- """,
87
- unsafe_allow_html=True
88
- )
 
1
+ import streamlit as st
2
+ import os
3
+ from dotenv import load_dotenv
4
+ from PIL import Image
5
+ import google.generativeai as genai
6
+
7
+ # Load environment variables from .env.
8
+ load_dotenv()
9
+
10
+ # Configure Google Generative AI
11
+ genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
12
+
13
+ # Function to get response from Gemini text model
14
+ def get_gemini_text_response(input_text):
15
+ model = genai.GenerativeModel('gemini-pro')
16
+ response = model.generate_content(input_text)
17
+ return response.text if response else "Please provide a text prompt."
18
+
19
+ # Function to get response from Gemini vision model
20
+ def get_gemini_image_response(image, input_text=None):
21
+ model = genai.GenerativeModel('gemini-pro-vision')
22
+ if input_text:
23
+ response = model.generate_content([input_text, image])
24
+ else:
25
+ response = model.generate_content(image)
26
+ return response.text if response else "Please provide a image."
27
+
28
+ # Initialize Streamlit app
29
+ st.set_page_config(page_title="Gemini Q&A and Image Analysis", page_icon="🤖")
30
+
31
+ # Sidebar for additional options
32
+ st.sidebar.title("About")
33
+ st.sidebar.info("This is a Q&A chatbot powered by Google's Gemini AI model.")
34
+ st.sidebar.title("Instructions")
35
+ st.sidebar.info("1. Choose either 'Q&A Chatbot' or 'Image Analysis' from the options.\n"
36
+ "2. Enter your question/prompt or upload an image.\n"
37
+ "3. Click the 'Ask' button to get the response.\n"
38
+ "4. The response will be displayed below.")
39
+
40
+ # Main interface
41
+ st.title("Gemini Q&A and Image Analysis Chatbot")
42
+ st.write("Select an option to either ask a question or analyze an image.")
43
+
44
+ # User selection
45
+ option = st.selectbox("Choose an option:", ("Q&A Chatbot", "Image Analysis"))
46
+
47
+ # Input fields based on user selection
48
+ if option == "Q&A Chatbot":
49
+ input_text = st.text_input("Enter your question or prompt:", key="input_text")
50
+ if st.button("Ask"):
51
+ with st.spinner("Generating response..."):
52
+ response = get_gemini_text_response(input_text)
53
+ st.subheader("Response:")
54
+ st.write(response)
55
+
56
+ elif option == "Image Analysis":
57
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
58
+ image_input_text = st.text_input("Enter your question or prompt related to the image (optional):", key="image_input_text")
59
+ if uploaded_file is not None:
60
+ image = Image.open(uploaded_file)
61
+ st.image(image, caption="Uploaded Image.", use_column_width=True)
62
+ if st.button("Analyze"):
63
+ with st.spinner("Generating response..."):
64
+ response = get_gemini_image_response(image, input_text=image_input_text)
65
+ st.subheader("Response:")
66
+ st.write(response)
67
+