-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgoogle_gen_ai.py
92 lines (79 loc) · 3.64 KB
/
google_gen_ai.py
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import streamlit as st
from google import genai
from PIL import Image
import io
import pathlib
import os
# Read the Google API key from environment variable
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
# Sidebar for API key input (optional, for local testing)
with st.sidebar:
st.header("API Key")
if not GOOGLE_API_KEY:
GOOGLE_API_KEY = st.text_input("Enter your Google API key:", type="password")
# Add a "Get API Key" link below the API key input field
st.markdown("[Get API Key](https://aistudio.google.com/app/apikey)")
# Main app functionality
if GOOGLE_API_KEY:
client = genai.Client(api_key=GOOGLE_API_KEY)
MODEL_ID = "gemini-2.0-flash-exp"
st.header("Choose Generation Type")
generation_type = st.radio(
"Select the type of generation:",
["Text to Text", "Ask about Image"]
)
# Dynamically change the title based on the selected category
if generation_type == "Text to Text":
st.title("Text Generation...")
elif generation_type == "Ask about Image":
st.title("Ask anything about a Image")
if generation_type == "Text to Text":
st.subheader("Text Generation")
user_input = st.text_area("What do you want to know?'):", height=100)
if st.button("Generate Text"):
if user_input.strip() == "":
st.error("Please enter a text prompt.")
else:
with st.spinner("Generating response..."):
try:
response = client.models.generate_content(
model=MODEL_ID,
contents=user_input
)
st.success("Response generated successfully!")
st.markdown("### Response:")
st.write(response.text)
except Exception as e:
st.error(f"An error occurred: {e}")
elif generation_type == "Ask about Image":
st.subheader("...Anything about IMAGE...")
uploaded_file = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])
user_prompt = st.text_area("Any query about the Image? Tell me.'):", height=100)
if st.button("Generate from Image"):
if not uploaded_file or not user_prompt:
st.error("Please upload an image and provide a prompt.")
else:
with st.spinner("Processing image and generating response..."):
try:
# Save the uploaded image to a temporary file
img_path = pathlib.Path("uploaded_image.png")
img_path.write_bytes(uploaded_file.getvalue())
# Open the image using PIL
image = Image.open(img_path)
image.thumbnail([512, 512]) # Resize for display
st.image(image, caption="Uploaded Image")
# Generate content based on the image and prompt
response = client.models.generate_content(
model=MODEL_ID,
contents=[
image,
user_prompt
]
)
st.success("Response generated successfully!")
st.markdown("### Response:")
st.write(response.text)
except Exception as e:
st.error(f"An error occurred: {e}")
else:
st.warning("Please enter your Google API key in the sidebar or set it as an environment variable.")