Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,62 +1,62 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
from google import genai
|
| 3 |
-
from google.genai import types
|
| 4 |
-
from PIL import Image
|
| 5 |
-
from io import BytesIO
|
| 6 |
-
import tempfile
|
| 7 |
-
import os
|
| 8 |
-
from dotenv import load_dotenv
|
| 9 |
-
load_dotenv()
|
| 10 |
-
GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
|
| 11 |
-
os.environ["GOOGLE_API_KEY"] = GOOGLE_API_KEY
|
| 12 |
-
|
| 13 |
-
client = genai.Client(api_key=GOOGLE_API_KEY)
|
| 14 |
-
|
| 15 |
-
st.set_page_config(page_title="Image Generator", layout="centered")
|
| 16 |
-
st.title("🧠✨ Image Generator")
|
| 17 |
-
st.markdown("Enter a prompt below and generate an AI image with a description!")
|
| 18 |
-
|
| 19 |
-
prompt = st.text_input("Enter your prompt")
|
| 20 |
-
|
| 21 |
-
if st.button("Generate"):
|
| 22 |
-
if prompt:
|
| 23 |
-
with st.spinner("Generating image and description..."):
|
| 24 |
-
try:
|
| 25 |
-
response = client.models.generate_content(
|
| 26 |
-
model="gemini-2.
|
| 27 |
-
contents=prompt,
|
| 28 |
-
config=types.GenerateContentConfig(
|
| 29 |
-
response_modalities=['Text', 'Image']
|
| 30 |
-
)
|
| 31 |
-
)
|
| 32 |
-
|
| 33 |
-
result_text = ""
|
| 34 |
-
result_image = None
|
| 35 |
-
temp_file_path = None
|
| 36 |
-
|
| 37 |
-
for part in response.candidates[0].content.parts:
|
| 38 |
-
if part.text is not None:
|
| 39 |
-
result_text += part.text
|
| 40 |
-
elif part.inline_data is not None:
|
| 41 |
-
result_image = Image.open(BytesIO(part.inline_data.data))
|
| 42 |
-
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
|
| 43 |
-
result_image.save(temp_file.name)
|
| 44 |
-
temp_file_path = temp_file.name
|
| 45 |
-
|
| 46 |
-
st.subheader("Generated Description")
|
| 47 |
-
st.text(result_text)
|
| 48 |
-
|
| 49 |
-
if result_image:
|
| 50 |
-
st.image(result_image, caption="Generated Image", use_column_width=True)
|
| 51 |
-
with open(temp_file_path, "rb") as f:
|
| 52 |
-
st.download_button(
|
| 53 |
-
label="📥 Download Image",
|
| 54 |
-
data=f,
|
| 55 |
-
file_name="generated_image.png",
|
| 56 |
-
mime="image/png"
|
| 57 |
-
)
|
| 58 |
-
|
| 59 |
-
except Exception as e:
|
| 60 |
-
st.error(f"An error occurred: {e}")
|
| 61 |
-
else:
|
| 62 |
-
st.warning("Please enter a prompt before generating.")
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from google import genai
|
| 3 |
+
from google.genai import types
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from io import BytesIO
|
| 6 |
+
import tempfile
|
| 7 |
+
import os
|
| 8 |
+
from dotenv import load_dotenv
|
| 9 |
+
load_dotenv()
|
| 10 |
+
GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
|
| 11 |
+
os.environ["GOOGLE_API_KEY"] = GOOGLE_API_KEY
|
| 12 |
+
|
| 13 |
+
client = genai.Client(api_key=GOOGLE_API_KEY)
|
| 14 |
+
|
| 15 |
+
st.set_page_config(page_title="Image Generator", layout="centered")
|
| 16 |
+
st.title("🧠✨ Image Generator")
|
| 17 |
+
st.markdown("Enter a prompt below and generate an AI image with a description!")
|
| 18 |
+
|
| 19 |
+
prompt = st.text_input("Enter your prompt")
|
| 20 |
+
|
| 21 |
+
if st.button("Generate"):
|
| 22 |
+
if prompt:
|
| 23 |
+
with st.spinner("Generating image and description..."):
|
| 24 |
+
try:
|
| 25 |
+
response = client.models.generate_content(
|
| 26 |
+
model="gemini-2.5-flash-image",
|
| 27 |
+
contents=prompt,
|
| 28 |
+
config=types.GenerateContentConfig(
|
| 29 |
+
response_modalities=['Text', 'Image']
|
| 30 |
+
)
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
result_text = ""
|
| 34 |
+
result_image = None
|
| 35 |
+
temp_file_path = None
|
| 36 |
+
|
| 37 |
+
for part in response.candidates[0].content.parts:
|
| 38 |
+
if part.text is not None:
|
| 39 |
+
result_text += part.text
|
| 40 |
+
elif part.inline_data is not None:
|
| 41 |
+
result_image = Image.open(BytesIO(part.inline_data.data))
|
| 42 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
|
| 43 |
+
result_image.save(temp_file.name)
|
| 44 |
+
temp_file_path = temp_file.name
|
| 45 |
+
|
| 46 |
+
st.subheader("Generated Description")
|
| 47 |
+
st.text(result_text)
|
| 48 |
+
|
| 49 |
+
if result_image:
|
| 50 |
+
st.image(result_image, caption="Generated Image", use_column_width=True)
|
| 51 |
+
with open(temp_file_path, "rb") as f:
|
| 52 |
+
st.download_button(
|
| 53 |
+
label="📥 Download Image",
|
| 54 |
+
data=f,
|
| 55 |
+
file_name="generated_image.png",
|
| 56 |
+
mime="image/png"
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
except Exception as e:
|
| 60 |
+
st.error(f"An error occurred: {e}")
|
| 61 |
+
else:
|
| 62 |
+
st.warning("Please enter a prompt before generating.")
|