File size: 1,168 Bytes
c4e381e
 
 
 
 
0276c23
4b1f81a
0276c23
c4e381e
 
 
 
0204b93
c4e381e
 
 
 
 
 
 
 
 
0276c23
4b1f81a
 
 
 
 
 
 
0276c23
 
 
 
 
 
 
 
 
 
 
 
4b1f81a
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
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