File size: 2,257 Bytes
25852f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""
App to take in image and output a list of objects in the image
"""

import os
from pathlib import Path

import google.generativeai as genai
import gradio as gr
from dotenv import load_dotenv

load_dotenv()  # Load environment variables from .env file

genai.configure(api_key=os.environ["GOOGLE_API_KEY"])

input_prompt = """
Extract the objects in the provided image and output them in a list in alphabetical order
"""

# Set up the model
generation_config = {
    "temperature": 0,
    "top_p": 1,
    "top_k": 32,
    "max_output_tokens": 4096,
}


safety_settings = [
    {"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
    {"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
    {
        "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
        "threshold": "BLOCK_MEDIUM_AND_ABOVE",
    },
    {
        "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
        "threshold": "BLOCK_MEDIUM_AND_ABOVE",
    },
]

model = genai.GenerativeModel(
    model_name="gemini-pro-vision",
    generation_config=generation_config,
    safety_settings=safety_settings,
)


def input_image_setup(file_loc):
    if not (img := Path(file_loc)).exists():
        raise FileNotFoundError(f"Could not find image: {img}")

    image_parts = [{"mime_type": "image/jpeg", "data": Path(file_loc).read_bytes()}]
    return image_parts


def generate_gemini_response(input_prompt, image_loc):
    image_prompt = input_image_setup(image_loc)
    prompt_parts = [input_prompt, image_prompt[0]]
    response = model.generate_content(prompt_parts)
    output = "The objects in the image are: \n" + response.text
    # print(response.text)
    return output


def upload_file(file_path):
    # print(file_path)
    output = generate_gemini_response(input_prompt, file_path)
    return file_path, output


with gr.Blocks() as demo:
    header = gr.Label("Gemini Pro Vision testing")
    image_output = gr.Image()
    submit = gr.UploadButton(label="Click to upload the image to be studied", file_count="single", file_types=["image"])
    output = gr.Textbox(label="Output")
    print("here")
    combined_output = [image_output, output]
    submit.upload(upload_file, submit, combined_output)


demo.launch(debug=True)