File size: 1,257 Bytes
bef155a
13d3692
3dc74fc
bef155a
 
2c89ce2
bef155a
f5b9df5
2c89ce2
bef155a
e5e3410
4b8b4a1
 
bef155a
1d05c51
13d3692
bef155a
4b8b4a1
 
 
 
 
 
bef155a
13d3692
4b8b4a1
42499f7
4b8b4a1
42499f7
bef155a
13d3692
bef155a
4b8b4a1
 
 
 
b3717b6
 
4b8b4a1
56b9492
463ded1
bef155a
3dc74fc
2bcb597
 
052d7c4
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
import google.generativeai as genai
from typing import Union, IO
import gradio as gr
import numpy as np
import PIL.Image
import os

api_key = os.getenv("GEMINI_API_KEY")
genai.configure(api_key = api_key)

def ImageStory(image: Union[np.ndarray, str, IO]) -> str:
    """
    Writes a short story about the uploaded image.

    Args:
        image: The image being uploaded. It can be a NumPy array, file path, or file-like object.

    Returns:
        A short story about the uploaded image.
    """  
        
    # load model
    model = genai.GenerativeModel("gemini-1.5-flash")

    # check image file and convert to a PIL image
    if isinstance(image, np.ndarray):
        img = PIL.Image.fromarray(image)
    else:
        img = PIL.Image.open(image)

    response = model.generate_content(["write a short story about the image", img])

    return response.text


app = gr.Interface(ImageStory,
                   inputs = gr.Image(label = "Image"),
                   outputs = gr.Text(label = "Story"),
                   examples = ["rubiks cube.jpg", "giraffe.jpg", "street.jpg"],
                   title = "Image To Story",
                   theme = "patrickosornio/my_theme1")



if __name__ == "__main__":
    app.launch(mcp_server = True)