Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import google.generativeai as genai
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import numpy as np
|
| 4 |
import PIL.Image
|
|
@@ -7,12 +8,12 @@ import os
|
|
| 7 |
api_key = os.getenv("GEMINI_API_KEY")
|
| 8 |
genai.configure(api_key = api_key)
|
| 9 |
|
| 10 |
-
def
|
| 11 |
"""
|
| 12 |
Writes a short story about the uploaded image.
|
| 13 |
|
| 14 |
Args:
|
| 15 |
-
image: The image
|
| 16 |
|
| 17 |
Returns:
|
| 18 |
A short story about the uploaded image.
|
|
@@ -21,14 +22,13 @@ def ImageStory(image):
|
|
| 21 |
# load model
|
| 22 |
model = genai.GenerativeModel("gemini-1.5-flash")
|
| 23 |
|
| 24 |
-
# check image file and convert to a
|
| 25 |
if isinstance(image, np.ndarray):
|
| 26 |
-
|
| 27 |
-
img = PIL.Image.fromarray(image)
|
| 28 |
else:
|
| 29 |
-
|
| 30 |
|
| 31 |
-
response =
|
| 32 |
|
| 33 |
return response.text
|
| 34 |
|
|
|
|
| 1 |
import google.generativeai as genai
|
| 2 |
+
from typing import Union, IO
|
| 3 |
import gradio as gr
|
| 4 |
import numpy as np
|
| 5 |
import PIL.Image
|
|
|
|
| 8 |
api_key = os.getenv("GEMINI_API_KEY")
|
| 9 |
genai.configure(api_key = api_key)
|
| 10 |
|
| 11 |
+
def ImageChat(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.
|
|
|
|
| 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 = Image.fromarray(image)
|
|
|
|
| 28 |
else:
|
| 29 |
+
img = Image.open(image)
|
| 30 |
|
| 31 |
+
response = model.generate_content(["write a short story about the image", img])
|
| 32 |
|
| 33 |
return response.text
|
| 34 |
|