What-can-go-wrong / utils.py
dipta007's picture
added 2 passes
3b827fa
raw
history blame
1.17 kB
from openai import OpenAI
import streamlit as st
from PIL import Image
from io import BytesIO
import base64
import json
import re
# Convert Image to Base64
def im_2_b64(image):
image = Image.open(image)
image = image.convert("RGB")
buff = BytesIO()
image.save(buff, format="JPEG")
img_str = base64.b64encode(buff.getvalue())
return img_str
RANDOM_SEED = 42
client = OpenAI(api_key=st.secrets["OPENAI_KEY"])
def get_str_to_json(st):
st = re.sub(r"```python", "", st)
st = re.sub(r"```", "", st)
st = re.sub(r"'(.*)': '(.*)'", r'"\1": "\2"', st)
st = re.sub(r"'(.*)': (.*)", r'"\1": \2', st)
st = re.sub(r"(.*): '(.*)'", r'\1: "\2"', st)
st = st.replace("\\'", "'")
st = st.replace('\\"', '"')
st = st.replace("True", "true")
st = st.replace("False", "false")
st = st.replace("None", "null")
st = st.replace("nan", "null")
st = st.replace("inf", "null")
st = st.replace("-inf", "null")
st = st.replace(",,", ",null,")
st = st.replace(",]", ",null]")
st = st.replace(",}", ",null}")
st = st.replace(",,", ",null,")
st = st.strip()
st = json.loads(st)
return st