forked from fenago/mod
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
40 lines (34 loc) · 1.24 KB
/
Copy pathapp.py
File metadata and controls
40 lines (34 loc) · 1.24 KB
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
import streamlit as st
from openai import OpenAI
import json
# Function to serialize the output
def serialize(obj):
"""Recursively walk object's hierarchy."""
if isinstance(obj, (bool, int, float, str)):
return obj
elif isinstance(obj, dict):
obj = obj.copy()
for key in obj:
obj[key] = serialize(obj[key])
return obj
elif isinstance(obj, list):
return [serialize(item) for item in obj]
elif isinstance(obj, tuple):
return tuple(serialize(item) for item in obj)
elif hasattr(obj, '__dict__'):
return serialize(obj.__dict__)
else:
return repr(obj) # Don't know how to handle, convert to string
# Access the OpenAI API key from Streamlit secrets
api_key = st.secrets["openai_secret"]
# Initialize the OpenAI client with the API key from secrets
client = OpenAI(api_key=api_key)
# Streamlit UI components
st.title('''Wayne's First NLP APP''')
user_input = st.text_area("Enter text to moderate")
if st.button('Moderate'):
response = client.moderations.create(input=user_input)
output = response.results[0]
serialized_output = serialize(output)
json_output = json.dumps(serialized_output, indent=2, ensure_ascii=False)
st.json(json_output)