Spaces:
Build error
Build error
bhaskarEEN commited on
Commit ·
25852f1
0
Parent(s):
- gemini pro testing
Browse files- .gitignore +1 -0
- app.py +82 -0
- requirements.txt +2 -0
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
.env
|
app.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
App to take in image and output a list of objects in the image
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
import os
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
|
| 8 |
+
import google.generativeai as genai
|
| 9 |
+
import gradio as gr
|
| 10 |
+
from dotenv import load_dotenv
|
| 11 |
+
|
| 12 |
+
load_dotenv() # Load environment variables from .env file
|
| 13 |
+
|
| 14 |
+
genai.configure(api_key=os.environ["GOOGLE_API_KEY"])
|
| 15 |
+
|
| 16 |
+
input_prompt = """
|
| 17 |
+
Extract the objects in the provided image and output them in a list in alphabetical order
|
| 18 |
+
"""
|
| 19 |
+
|
| 20 |
+
# Set up the model
|
| 21 |
+
generation_config = {
|
| 22 |
+
"temperature": 0,
|
| 23 |
+
"top_p": 1,
|
| 24 |
+
"top_k": 32,
|
| 25 |
+
"max_output_tokens": 4096,
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
safety_settings = [
|
| 30 |
+
{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
|
| 31 |
+
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
|
| 32 |
+
{
|
| 33 |
+
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
|
| 34 |
+
"threshold": "BLOCK_MEDIUM_AND_ABOVE",
|
| 35 |
+
},
|
| 36 |
+
{
|
| 37 |
+
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
|
| 38 |
+
"threshold": "BLOCK_MEDIUM_AND_ABOVE",
|
| 39 |
+
},
|
| 40 |
+
]
|
| 41 |
+
|
| 42 |
+
model = genai.GenerativeModel(
|
| 43 |
+
model_name="gemini-pro-vision",
|
| 44 |
+
generation_config=generation_config,
|
| 45 |
+
safety_settings=safety_settings,
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def input_image_setup(file_loc):
|
| 50 |
+
if not (img := Path(file_loc)).exists():
|
| 51 |
+
raise FileNotFoundError(f"Could not find image: {img}")
|
| 52 |
+
|
| 53 |
+
image_parts = [{"mime_type": "image/jpeg", "data": Path(file_loc).read_bytes()}]
|
| 54 |
+
return image_parts
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def generate_gemini_response(input_prompt, image_loc):
|
| 58 |
+
image_prompt = input_image_setup(image_loc)
|
| 59 |
+
prompt_parts = [input_prompt, image_prompt[0]]
|
| 60 |
+
response = model.generate_content(prompt_parts)
|
| 61 |
+
output = "The objects in the image are: \n" + response.text
|
| 62 |
+
# print(response.text)
|
| 63 |
+
return output
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
def upload_file(file_path):
|
| 67 |
+
# print(file_path)
|
| 68 |
+
output = generate_gemini_response(input_prompt, file_path)
|
| 69 |
+
return file_path, output
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
with gr.Blocks() as demo:
|
| 73 |
+
header = gr.Label("Gemini Pro Vision testing")
|
| 74 |
+
image_output = gr.Image()
|
| 75 |
+
submit = gr.UploadButton(label="Click to upload the image to be studied", file_count="single", file_types=["image"])
|
| 76 |
+
output = gr.Textbox(label="Output")
|
| 77 |
+
print("here")
|
| 78 |
+
combined_output = [image_output, output]
|
| 79 |
+
submit.upload(upload_file, submit, combined_output)
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
demo.launch(debug=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==4.31.5
|
| 2 |
+
google-generativeai==0.5.4
|