Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Gemini Pro demo.
|
| 3 |
+
This example demonstrates how to use the Gemini protocol over a StreamLit app, and
|
| 4 |
+
how to use the Gemini Pro API to generate text from images.
|
| 5 |
+
|
| 6 |
+
About Developer:
|
| 7 |
+
LinkedIn: @pydev
|
| 8 |
+
Email: [email protected]
|
| 9 |
+
Github: @pydevpk
|
| 10 |
+
Twitter: @mrraosahab
|
| 11 |
+
Website: https://pydevpk.github.io
|
| 12 |
+
"""
|
| 13 |
+
|
| 14 |
+
from dotenv import load_dotenv
|
| 15 |
+
|
| 16 |
+
# load environment variables
|
| 17 |
+
load_dotenv()
|
| 18 |
+
|
| 19 |
+
import os
|
| 20 |
+
import streamlit as st
|
| 21 |
+
import google.generativeai as genai
|
| 22 |
+
from PIL import Image
|
| 23 |
+
|
| 24 |
+
genai.configure(api_key=os.getenv('API_KEY_GEMINI'))
|
| 25 |
+
|
| 26 |
+
model = genai.GenerativeModel('gemini-pro-vision')
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def get_gemini_response(input, image):
|
| 30 |
+
if input != "":
|
| 31 |
+
response = model.generate_content([input, image])
|
| 32 |
+
return response
|
| 33 |
+
else:
|
| 34 |
+
response = model.generate_content(image)
|
| 35 |
+
return response
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
# setting webpage title
|
| 39 |
+
st.title("Gemini Pro")
|
| 40 |
+
# setting webpage header
|
| 41 |
+
st.subheader("A generative AI that can create text from images")
|
| 42 |
+
# take an input
|
| 43 |
+
input = st.text_input("Enter a text prompt", "")
|
| 44 |
+
# take an image
|
| 45 |
+
uploader = st.file_uploader("Upload an image", type=['png', 'jpg', 'jpeg'])
|
| 46 |
+
image = ""
|
| 47 |
+
if not uploader is None:
|
| 48 |
+
image = uploader.read()
|
| 49 |
+
image = Image.open(uploader)
|
| 50 |
+
st.image(image, caption='Uploaded Image.', use_column_width=True)
|
| 51 |
+
|
| 52 |
+
# creating button on webpage
|
| 53 |
+
submit = st.button("know this image")
|
| 54 |
+
if submit:
|
| 55 |
+
loader_text = st.empty()
|
| 56 |
+
loader_text.write('Classifying...')
|
| 57 |
+
# st.write("Classifying...")
|
| 58 |
+
# Classify the image
|
| 59 |
+
response = get_gemini_response(input, image)
|
| 60 |
+
# Display the results
|
| 61 |
+
st.write(response.text)
|
| 62 |
+
# replace loader_text
|
| 63 |
+
loader_text.write('Response from Gemini Pro')
|