ImranzamanML
commited on
Commit
•
ca01849
1
Parent(s):
f9c3ad3
Upload 3 files
Browse files- app.py +99 -0
- image.PNG +0 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import streamlit as st
|
3 |
+
import google.generativeai as genai
|
4 |
+
from PIL import Image
|
5 |
+
import base64
|
6 |
+
|
7 |
+
# Function to set the Google API key
|
8 |
+
def set_api_key(api_key):
|
9 |
+
os.environ["GOOGLE_API_KEY"] = api_key
|
10 |
+
genai.configure(api_key=api_key)
|
11 |
+
|
12 |
+
# Function to get description from the image
|
13 |
+
def get_image_description(image, user_prompt=None):
|
14 |
+
model = genai.GenerativeModel(model_name="gemini-1.5-flash")
|
15 |
+
prompt = user_prompt if user_prompt else "What is in this photo?"
|
16 |
+
response = model.generate_content([prompt], images=[image])
|
17 |
+
return response.text
|
18 |
+
|
19 |
+
# Helper function to convert image to base64
|
20 |
+
def get_base64_of_image(image_path):
|
21 |
+
with open(image_path, "rb") as image_file:
|
22 |
+
base64_str = base64.b64encode(image_file.read()).decode()
|
23 |
+
return base64_str
|
24 |
+
|
25 |
+
def main():
|
26 |
+
st.set_page_config(page_title="Talk to Image", layout="wide")
|
27 |
+
|
28 |
+
st.markdown(
|
29 |
+
f"""
|
30 |
+
<style>
|
31 |
+
.stApp {{
|
32 |
+
background: url(data:image/png;base64,{get_base64_of_image('image.png')});
|
33 |
+
background-size: cover
|
34 |
+
}}
|
35 |
+
</style>
|
36 |
+
""",
|
37 |
+
unsafe_allow_html=True
|
38 |
+
)
|
39 |
+
|
40 |
+
st.title("Image Description using Gemini!")
|
41 |
+
|
42 |
+
st.subheader("Upload your Image File")
|
43 |
+
uploaded_file = st.file_uploader("Upload your image file", type=["png", "jpg", "jpeg"])
|
44 |
+
|
45 |
+
st.sidebar.header("Configuration")
|
46 |
+
api_key = st.sidebar.text_input("Google API Key:", type="password")
|
47 |
+
|
48 |
+
if st.sidebar.button("Set API Key"):
|
49 |
+
if api_key:
|
50 |
+
set_api_key(api_key)
|
51 |
+
st.sidebar.success("API Key set successfully.")
|
52 |
+
else:
|
53 |
+
st.sidebar.error("Please enter a valid API Key.")
|
54 |
+
|
55 |
+
prompt = st.text_input("Enter your prompt (optional):", value="What is in this photo?")
|
56 |
+
|
57 |
+
if st.button("Describe Image") and uploaded_file:
|
58 |
+
if "GOOGLE_API_KEY" in os.environ and os.environ["GOOGLE_API_KEY"]:
|
59 |
+
try:
|
60 |
+
image = Image.open(uploaded_file)
|
61 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
62 |
+
|
63 |
+
with st.spinner("Generating description..."):
|
64 |
+
description = get_image_description(image, prompt)
|
65 |
+
st.write("**Description:** ", description)
|
66 |
+
except Exception as e:
|
67 |
+
st.error(f"An error occurred: {e}")
|
68 |
+
else:
|
69 |
+
st.warning("Please enter your Google API key and click 'Set API Key'.")
|
70 |
+
|
71 |
+
st.markdown("---")
|
72 |
+
st.write("Happy to Connect:")
|
73 |
+
kaggle, linkedin, google_scholar, youtube, github = st.columns(5)
|
74 |
+
|
75 |
+
image_urls = {
|
76 |
+
"kaggle": "https://www.kaggle.com/static/images/site-logo.svg",
|
77 |
+
"linkedin": "https://upload.wikimedia.org/wikipedia/commons/thumb/c/ca/LinkedIn_logo_initials.png/600px-LinkedIn_logo_initials.png",
|
78 |
+
"google_scholar": "https://upload.wikimedia.org/wikipedia/commons/thumb/c/c7/Google_Scholar_logo.svg/768px-Google_Scholar_logo.svg.png",
|
79 |
+
"youtube": "https://upload.wikimedia.org/wikipedia/commons/thumb/7/72/YouTube_social_white_square_%282017%29.svg/640px-YouTube_social_white_square_%282017%29.svg.png",
|
80 |
+
"github": "https://github.githubassets.com/assets/GitHub-Mark-ea2971cee799.png"
|
81 |
+
}
|
82 |
+
|
83 |
+
social_links = {
|
84 |
+
"kaggle": "https://www.kaggle.com/muhammadimran112233",
|
85 |
+
"linkedin": "https://www.linkedin.com/in/muhammad-imran-zaman",
|
86 |
+
"google_scholar": "https://scholar.google.com/citations?user=ulVFpy8AAAAJ&hl=en",
|
87 |
+
"youtube": "https://www.youtube.com/@consolioo",
|
88 |
+
"github": "https://github.com/Imran-ml"
|
89 |
+
}
|
90 |
+
|
91 |
+
kaggle.markdown(f'<a href="{social_links["kaggle"]}"><img src="{image_urls["kaggle"]}" width="50" height="50"></a>', unsafe_allow_html=True)
|
92 |
+
linkedin.markdown(f'<a href="{social_links["linkedin"]}"><img src="{image_urls["linkedin"]}" width="50" height="50"></a>', unsafe_allow_html=True)
|
93 |
+
google_scholar.markdown(f'<a href="{social_links["google_scholar"]}"><img src="{image_urls["google_scholar"]}" width="50" height="50"></a>', unsafe_allow_html=True)
|
94 |
+
youtube.markdown(f'<a href="{social_links["youtube"]}"><img src="{image_urls["youtube"]}" width="50" height="50"></a>', unsafe_allow_html=True)
|
95 |
+
github.markdown(f'<a href="{social_links["github"]}"><img src="{image_urls["github"]}" width="50" height="50"></a>', unsafe_allow_html=True)
|
96 |
+
st.markdown("---")
|
97 |
+
|
98 |
+
if __name__ == "__main__":
|
99 |
+
main()
|
image.PNG
ADDED
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
google-generativeai
|
3 |
+
Pillow
|