wg25r
commited on
Commit
·
4f43132
1
Parent(s):
ad17ff1
image mod tested
Browse files- __pycache__/mod.cpython-311.pyc +0 -0
- app.py +15 -2
- mod.py +34 -0
__pycache__/mod.cpython-311.pyc
ADDED
Binary file (1.52 kB). View file
|
|
app.py
CHANGED
@@ -7,6 +7,8 @@ from rdkit.Chem import Draw
|
|
7 |
import time
|
8 |
import os
|
9 |
import random
|
|
|
|
|
10 |
|
11 |
@st.dialog("Select From Library")
|
12 |
def lib_modal():
|
@@ -49,10 +51,12 @@ mode = st.radio("Select file source:", ["Upload File", "Choose from Our Demo Lib
|
|
49 |
if mode == "Upload File":
|
50 |
st.session_state.pop("uploaded_file", None)
|
51 |
uploaded_file = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg", "webp", "heic"])
|
52 |
-
col1.checkbox("Contribute To Public Library", value=True, help="If checked, images will be included in the PUBLIC library, and the image will be reviewed by our team and used for model training. When checked, do not upload any sensitive or personal data.")
|
53 |
else:
|
|
|
54 |
uploaded_file = None
|
55 |
-
st.warning("You are accessing a user-uploaded library. These images are unverified
|
|
|
56 |
from_library = st.button("Select from Library")
|
57 |
if from_library:
|
58 |
if st.session_state.show_modal:
|
@@ -88,5 +92,14 @@ if button:
|
|
88 |
pubchem_url = "https://pubchem.ncbi.nlm.nih.gov/compound/{}".format(cid[0].cid)
|
89 |
col.markdown("[PubChem]({})".format(pubchem_url))
|
90 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
st.markdown("---")
|
92 |
st.markdown("Taken {} seconds".format(round(time.time() - start_time, 2)))
|
|
|
7 |
import time
|
8 |
import os
|
9 |
import random
|
10 |
+
from mod import check_img
|
11 |
+
import string
|
12 |
|
13 |
@st.dialog("Select From Library")
|
14 |
def lib_modal():
|
|
|
51 |
if mode == "Upload File":
|
52 |
st.session_state.pop("uploaded_file", None)
|
53 |
uploaded_file = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg", "webp", "heic"])
|
54 |
+
contribute = col1.checkbox("Contribute To Public Library", value=True, help="If checked, images will be included in the PUBLIC library, and the image will be reviewed by our team and used for model training. When checked, do not upload any sensitive or personal data.")
|
55 |
else:
|
56 |
+
contribute = False
|
57 |
uploaded_file = None
|
58 |
+
st.warning("You are accessing a user-uploaded library. These images are unverified and may contain inappropriate content or incorrectly assembled files, despite some moderation. Proceed with caution. For inquiries, contact us.")
|
59 |
+
|
60 |
from_library = st.button("Select from Library")
|
61 |
if from_library:
|
62 |
if st.session_state.show_modal:
|
|
|
92 |
pubchem_url = "https://pubchem.ncbi.nlm.nih.gov/compound/{}".format(cid[0].cid)
|
93 |
col.markdown("[PubChem]({})".format(pubchem_url))
|
94 |
|
95 |
+
if contribute:
|
96 |
+
flagged = check_img(image)
|
97 |
+
if flagged:
|
98 |
+
st.warning("The image is flagged as inappropriate. Please upload a different image.")
|
99 |
+
st.stop()
|
100 |
+
else:
|
101 |
+
filename = "user_upload" + "".join(random.choices(string.ascii_uppercase, k=10)) + ".png"
|
102 |
+
image.save("lib/{}".format(filename))
|
103 |
+
st.success("The image is stored in the library.")
|
104 |
st.markdown("---")
|
105 |
st.markdown("Taken {} seconds".format(round(time.time() - start_time, 2)))
|
mod.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from openai import OpenAI
|
2 |
+
import io
|
3 |
+
import base64
|
4 |
+
from PIL import Image
|
5 |
+
|
6 |
+
client = OpenAI()
|
7 |
+
|
8 |
+
def check_img(PIL_image):
|
9 |
+
buffer = io.BytesIO()
|
10 |
+
PIL_image.save(buffer, format="PNG")
|
11 |
+
buffer.seek(0)
|
12 |
+
img_bytes = buffer.read()
|
13 |
+
base64_bytes = base64.b64encode(img_bytes)
|
14 |
+
base64_string = base64_bytes.decode('utf-8')
|
15 |
+
base64_string = "data:image/png;base64," + base64_string
|
16 |
+
|
17 |
+
response = client.moderations.create(
|
18 |
+
model="omni-moderation-latest",
|
19 |
+
input=[
|
20 |
+
{
|
21 |
+
"type": "image_url",
|
22 |
+
"image_url": {
|
23 |
+
"url": base64_string,
|
24 |
+
}
|
25 |
+
},
|
26 |
+
],
|
27 |
+
)
|
28 |
+
|
29 |
+
return response.results[0].flagged
|
30 |
+
|
31 |
+
if __name__ == "__main__":
|
32 |
+
image = Image.open("lib/11_0M0DY5zO.png")
|
33 |
+
flagged = check_img(image)
|
34 |
+
print("Flagged:", flagged)
|