Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,46 +1,57 @@
|
|
| 1 |
-
|
| 2 |
-
from
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
import
|
| 6 |
import os
|
| 7 |
|
| 8 |
-
api_key = os.getenv("GEMINI_API_KEY")
|
| 9 |
-
genai.configure(api_key = api_key)
|
| 10 |
-
|
| 11 |
-
def ImageStory(image: Union[np.ndarray, str, IO]) -> str:
|
| 12 |
-
"""
|
| 13 |
-
Writes a short story about the uploaded image.
|
| 14 |
-
|
| 15 |
-
Args:
|
| 16 |
-
image: The image being uploaded. It can be a NumPy array, file path, or file-like object.
|
| 17 |
-
|
| 18 |
-
Returns:
|
| 19 |
-
A short story about the uploaded image.
|
| 20 |
-
"""
|
| 21 |
-
|
| 22 |
-
# load model
|
| 23 |
-
model = genai.GenerativeModel("gemini-1.5-flash")
|
| 24 |
-
|
| 25 |
-
# check image file and convert to a PIL image
|
| 26 |
-
if isinstance(image, np.ndarray):
|
| 27 |
-
img = PIL.Image.fromarray(image)
|
| 28 |
-
else:
|
| 29 |
-
img = PIL.Image.open(image)
|
| 30 |
-
|
| 31 |
-
response = model.generate_content(["write a short story about the image", img])
|
| 32 |
-
|
| 33 |
-
return response.text
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
app = gr.Interface(ImageStory,
|
| 37 |
-
inputs = gr.Image(label = "Image"),
|
| 38 |
-
outputs = gr.Text(label = "Story"),
|
| 39 |
-
examples = ["rubiks cube.jpg", "giraffe.jpg", "street.jpg"],
|
| 40 |
-
title = "Image To Story",
|
| 41 |
-
theme = "patrickosornio/my_theme1")
|
| 42 |
-
|
| 43 |
-
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
if __name__ == "__main__":
|
| 46 |
app.launch(mcp_server = True)
|
|
|
|
| 1 |
+
from google import genai
|
| 2 |
+
from google.genai import types
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from io import BytesIO
|
| 5 |
+
import base64
|
| 6 |
import os
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
def generate_image(prompt):
|
| 10 |
+
|
| 11 |
+
"""
|
| 12 |
+
Transforms text into an image.
|
| 13 |
+
|
| 14 |
+
Args:
|
| 15 |
+
prompt: A natural language instruction that is used to generate an image.
|
| 16 |
+
|
| 17 |
+
Returns:
|
| 18 |
+
An image.
|
| 19 |
+
"""
|
| 20 |
+
api_key = os.getenv("GEMINI_API_KEY")
|
| 21 |
+
|
| 22 |
+
client = genai.Client(api_key=api_key)
|
| 23 |
+
|
| 24 |
+
prompt = tuple(prompt)
|
| 25 |
+
|
| 26 |
+
response = client.models.generate_content(
|
| 27 |
+
model="gemini-2.0-flash-preview-image-generation",
|
| 28 |
+
contents=prompt,
|
| 29 |
+
config=types
|
| 30 |
+
.GenerateContentConfig(
|
| 31 |
+
response_modalities=['TEXT', 'IMAGE']
|
| 32 |
+
)
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
for part in response.candidates[0].content.parts:
|
| 36 |
+
if part.text is not None:
|
| 37 |
+
pass
|
| 38 |
+
elif part.inline_data is not None:
|
| 39 |
+
image = Image.open(BytesIO((part.inline_data.data)))
|
| 40 |
+
# image.save('gemini-native-image.png')
|
| 41 |
+
# display(image)
|
| 42 |
+
return image
|
| 43 |
+
|
| 44 |
+
# build gradio interface
|
| 45 |
+
app = gr.Interface(fn = generate_image,
|
| 46 |
+
inputs = gr.Text(label="Prompt",placeholder="Type your prompt here. . ."),
|
| 47 |
+
outputs = gr.Image(label="Generated Image"),
|
| 48 |
+
title="Gemini Image Generator",
|
| 49 |
+
examples=["A 3D rendering of a little black girl wearing a colorful dress and smiling broadly at the camera",
|
| 50 |
+
"Disney and Pixar-style playful bunny skipping about in a garden full of carrots and lettuce",
|
| 51 |
+
"A black female superhero with braided hair flying over a over an active volcano with a fiery red sky",
|
| 52 |
+
"A black jazz musician playing his saxophone and surrounded by colorful musical notes."])
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
# launch application
|
| 56 |
if __name__ == "__main__":
|
| 57 |
app.launch(mcp_server = True)
|