Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,31 +7,41 @@ import os
|
|
| 7 |
api_key = os.getenv("GEMINI_API_KEY")
|
| 8 |
genai.configure(api_key = api_key)
|
| 9 |
|
| 10 |
-
def
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
|
|
|
| 23 |
|
| 24 |
-
|
|
|
|
|
|
|
| 25 |
|
|
|
|
| 26 |
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
| 28 |
inputs = gr.Image(label = "Image"),
|
| 29 |
outputs = gr.Text(label = "Story"),
|
| 30 |
-
examples = ["rubiks cube.jpg","giraffe.jpg","street.jpg"],
|
| 31 |
title = "Image-To-Story",
|
| 32 |
theme = "patrickosornio/my_theme1")
|
| 33 |
|
| 34 |
|
| 35 |
-
app.launch()
|
| 36 |
|
| 37 |
|
|
|
|
| 7 |
api_key = os.getenv("GEMINI_API_KEY")
|
| 8 |
genai.configure(api_key = api_key)
|
| 9 |
|
| 10 |
+
def ImageStory(image):
|
| 11 |
|
| 12 |
+
"""
|
| 13 |
+
Writes a short story about the uploaded image.
|
| 14 |
|
| 15 |
+
Arg:
|
| 16 |
+
image: The image that is being uploaded
|
| 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 Numpy array
|
| 26 |
+
if isinstance(image, np.ndarray):
|
| 27 |
|
| 28 |
+
img = PIL.Image.fromarray(image)
|
| 29 |
+
else:
|
| 30 |
+
img = PIL.Image.open(image)
|
| 31 |
|
| 32 |
+
response = model.generate_content(["write a short story about the image", img])
|
| 33 |
|
| 34 |
+
return response.text
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
app = gr.Interface(ImageStory,
|
| 38 |
inputs = gr.Image(label = "Image"),
|
| 39 |
outputs = gr.Text(label = "Story"),
|
| 40 |
+
examples = ["rubiks cube.jpg", "giraffe.jpg", "street.jpg"],
|
| 41 |
title = "Image-To-Story",
|
| 42 |
theme = "patrickosornio/my_theme1")
|
| 43 |
|
| 44 |
|
| 45 |
+
app.launch(mcp_server = True)
|
| 46 |
|
| 47 |
|