Skip to content
This repository has been archived by the owner on Feb 22, 2024. It is now read-only.

Commit

Permalink
add pages and sandbox
Browse files Browse the repository at this point in the history
  • Loading branch information
JoaoCarabetta committed Nov 14, 2023
1 parent 97bca0e commit 8d6b159
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 1 deletion.
83 changes: 83 additions & 0 deletions app/model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import os
import requests
import json
import base64
import io
from PIL import Image


def encode_image_to_base64(image):
# if image is str convert to BytesIO
if isinstance(image, str):
with Image.open(image) as img:
buffered = io.BytesIO()
img.save(buffered, format="JPEG")

bytes_data = buffered.getvalue()

else:
bytes_data = image.getvalue()

return base64.b64encode(bytes_data).decode("utf-8")


def vision_ai_classify_image(base64_image):
# read OPENAI_API_KEY env
api_key = os.environ.get("OPENAI_API_KEY")
headers = {"Content-Type": "application/json", "Authorization": f"Bearer {api_key}"}

payload = {
"model": "gpt-4-vision-preview",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": """
You are an expert flooding detector.
You are given a image. You must detect if there is flooding in the image.
the output MUST be a json object with a boolean value for the key "flooding_detected".
If you don't know what to anwser, you can set the key "flooding_detect" as false.
Example:
{
"flooding_detected": true
}
""",
},
{
"type": "image_url",
"image_url": {"url": f"data:image/jpeg;base64,{base64_image}"},
},
],
}
],
"max_tokens": 300,
}

response = requests.post(
"https://api.openai.com/v1/chat/completions", headers=headers, json=payload
)
return response.json()


def get_ai_label(response):
if response.get("error"):
return "Error"
else:
r = response["choices"][0]["message"]["content"]
json_string = r.replace("```json\n", "").replace("\n```", "")
json_object = json.loads(json_string)
return json_object["flooding_detected"]


def run_model(img):
base64_image = encode_image_to_base64(img)

response = vision_ai_classify_image(base64_image)

return get_ai_label(response)
Empty file added app/pages/__init__.py
Empty file.
31 changes: 31 additions & 0 deletions app/pages/🧪 Experimente o Modelo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import streamlit as st
from PIL import Image

from model import run_model


MAX_FILE_SIZE = 5 * 1024 * 1024 # 5MB

st.set_page_config(layout="wide", page_title="Experimente o Modelo")

st.markdown("# Identifique um alagamento por uma imagem")

my_upload = st.file_uploader("Faça upload de uma imagem", type=["png", "jpg", "jpeg"])

if my_upload is not None:
if my_upload.size > MAX_FILE_SIZE:
st.error(
"The uploaded file is too large. Please upload an image smaller than 5MB."
)
else:
res = run_model(my_upload)
else:
my_upload = "./data/imgs/flooded1.jpg"
res = run_model(my_upload)

if res:
st.markdown("### Alagamento Identificado ✅")
else:
st.markdown("### Alagamento Não Identificado ❌")

st.image(my_upload)
2 changes: 1 addition & 1 deletion app/main.py → app/📣 Alagamentos em Tempo Real.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
}

tmp_data_path = Path(__file__).parent.parent / "data" / "cameras_h3_1.csv"
chart_data = pd.read_csv(tmp_data_path)
chart_data = pd.read_csv(tmp_data_path)[:10]
chart_data["icon_data"] = None
for i in chart_data.index:
chart_data["icon_data"][i] = icon_data
Expand Down
Binary file added data/imgs/flooded1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/imgs/flooded2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/imgs/not_flooded1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/imgs/not_flooded2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8d6b159

Please sign in to comment.