Spaces:
Sleeping
Sleeping
def verify_moderation(text):
Browse files
app.py
CHANGED
@@ -29,14 +29,18 @@ if is_env_local:
|
|
29 |
GCS_KEY = json.dumps(config["GOOGLE_APPLICATION_CREDENTIALS_JSON"])
|
30 |
CUTOR_OPEN_AI_KEY = config["CUTOR_OPEN_AI_KEY"]
|
31 |
CUTOR_OPEN_AI_ASSISTANT_ID = config["CUTOR_OPEN_AI_ASSISTANT_ID"]
|
|
|
|
|
32 |
else:
|
33 |
OPEN_AI_KEY = os.getenv("OPEN_AI_KEY")
|
34 |
GCS_KEY = os.getenv("GOOGLE_APPLICATION_CREDENTIALS_JSON")
|
35 |
CUTOR_OPEN_AI_KEY = os.getenv("CUTOR_OPEN_AI_KEY")
|
36 |
CUTOR_OPEN_AI_ASSISTANT_ID = os.getenv("CUTOR_OPEN_AI_ASSISTANT_ID")
|
|
|
37 |
|
38 |
OPEN_AI_CLIENT = OpenAI(api_key=OPEN_AI_KEY)
|
39 |
CUTOR_OPEN_AI_CLIENT = OpenAI(api_key=CUTOR_OPEN_AI_KEY)
|
|
|
40 |
|
41 |
# 设置 Google Cloud Storage 客户端
|
42 |
GCS_SERVICE = GoogleCloudStorage(GCS_KEY)
|
@@ -1023,10 +1027,25 @@ def get_chinese_paragraph_practice_log_session_content(file_name):
|
|
1023 |
def verify_string_length(text):
|
1024 |
if len(text) > 2000:
|
1025 |
raise gr.Error("輸入的文字長度過長,請重新輸入!")
|
1026 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1027 |
|
1028 |
def get_chinese_paragraph_evaluate_content(model, sys_content, paragraph, user_generate_paragraph_evaluate_prompt):
|
1029 |
verify_string_length(paragraph)
|
|
|
|
|
1030 |
user_content = f"""
|
1031 |
sys_content: {sys_content}
|
1032 |
---
|
@@ -1060,6 +1079,8 @@ def get_chinese_paragraph_evaluate_content(model, sys_content, paragraph, user_g
|
|
1060 |
def get_chinese_paragraph_refine_evaluate_content(model, sys_content, paragraph_1, paragraph_2, user_refine_paragraph_prompt):
|
1061 |
verify_string_length(paragraph_1)
|
1062 |
verify_string_length(paragraph_2)
|
|
|
|
|
1063 |
user_content = f"""
|
1064 |
sys_content: {sys_content}
|
1065 |
---
|
@@ -1093,6 +1114,8 @@ def get_chinese_paragraph_refine_evaluate_content(model, sys_content, paragraph_
|
|
1093 |
return content_text, gr_update
|
1094 |
|
1095 |
def generate_content_by_open_ai_assistant(user_content, thread_id=None, model_name=None):
|
|
|
|
|
1096 |
client = CUTOR_OPEN_AI_CLIENT
|
1097 |
assistant_id = CUTOR_OPEN_AI_ASSISTANT_ID
|
1098 |
|
@@ -1152,6 +1175,8 @@ def duplicate_element(element):
|
|
1152 |
return element
|
1153 |
|
1154 |
def generate_chinese_essay_idea(model, user_prompt, chinese_essay_title_input):
|
|
|
|
|
1155 |
sys_content = "你是一位老師,正在和我一起練習提高我的寫作技能。 給予的回覆不超過 500字。 用 Markdown 語法回答。"
|
1156 |
user_content = f"""
|
1157 |
{user_prompt}
|
|
|
29 |
GCS_KEY = json.dumps(config["GOOGLE_APPLICATION_CREDENTIALS_JSON"])
|
30 |
CUTOR_OPEN_AI_KEY = config["CUTOR_OPEN_AI_KEY"]
|
31 |
CUTOR_OPEN_AI_ASSISTANT_ID = config["CUTOR_OPEN_AI_ASSISTANT_ID"]
|
32 |
+
OPEN_AI_MODERATION_BOT1 = config["OPEN_AI_MODERATION_BOT1"]
|
33 |
+
|
34 |
else:
|
35 |
OPEN_AI_KEY = os.getenv("OPEN_AI_KEY")
|
36 |
GCS_KEY = os.getenv("GOOGLE_APPLICATION_CREDENTIALS_JSON")
|
37 |
CUTOR_OPEN_AI_KEY = os.getenv("CUTOR_OPEN_AI_KEY")
|
38 |
CUTOR_OPEN_AI_ASSISTANT_ID = os.getenv("CUTOR_OPEN_AI_ASSISTANT_ID")
|
39 |
+
OPEN_AI_MODERATION_BOT1 = os.getenv("OPEN_AI_MODERATION_BOT1", OPEN_AI_KEY)
|
40 |
|
41 |
OPEN_AI_CLIENT = OpenAI(api_key=OPEN_AI_KEY)
|
42 |
CUTOR_OPEN_AI_CLIENT = OpenAI(api_key=CUTOR_OPEN_AI_KEY)
|
43 |
+
OPEN_AI_MODERATION_CLIENT = OpenAI(api_key=OPEN_AI_MODERATION_BOT1)
|
44 |
|
45 |
# 设置 Google Cloud Storage 客户端
|
46 |
GCS_SERVICE = GoogleCloudStorage(GCS_KEY)
|
|
|
1027 |
def verify_string_length(text):
|
1028 |
if len(text) > 2000:
|
1029 |
raise gr.Error("輸入的文字長度過長,請重新輸入!")
|
1030 |
+
|
1031 |
+
def verify_moderation(text):
|
1032 |
+
response = OPEN_AI_MODERATION_CLIENT.moderations.create(input=text)
|
1033 |
+
response_dict = response.model_dump()
|
1034 |
+
is_flagged = response_dict['results'][0]['flagged']
|
1035 |
+
print("========get_chat_moderation==========")
|
1036 |
+
print(f"is_flagged: {is_flagged}")
|
1037 |
+
print(response_dict)
|
1038 |
+
print("========get_chat_moderation==========")
|
1039 |
+
|
1040 |
+
if is_flagged:
|
1041 |
+
raise gr.Error("您的輸入包含不當內容,請重新輸入!")
|
1042 |
+
|
1043 |
+
return is_flagged, response_dict
|
1044 |
|
1045 |
def get_chinese_paragraph_evaluate_content(model, sys_content, paragraph, user_generate_paragraph_evaluate_prompt):
|
1046 |
verify_string_length(paragraph)
|
1047 |
+
verify_moderation(paragraph)
|
1048 |
+
|
1049 |
user_content = f"""
|
1050 |
sys_content: {sys_content}
|
1051 |
---
|
|
|
1079 |
def get_chinese_paragraph_refine_evaluate_content(model, sys_content, paragraph_1, paragraph_2, user_refine_paragraph_prompt):
|
1080 |
verify_string_length(paragraph_1)
|
1081 |
verify_string_length(paragraph_2)
|
1082 |
+
verify_moderation(paragraph_1+paragraph_2)
|
1083 |
+
|
1084 |
user_content = f"""
|
1085 |
sys_content: {sys_content}
|
1086 |
---
|
|
|
1114 |
return content_text, gr_update
|
1115 |
|
1116 |
def generate_content_by_open_ai_assistant(user_content, thread_id=None, model_name=None):
|
1117 |
+
verify_moderation(user_content)
|
1118 |
+
|
1119 |
client = CUTOR_OPEN_AI_CLIENT
|
1120 |
assistant_id = CUTOR_OPEN_AI_ASSISTANT_ID
|
1121 |
|
|
|
1175 |
return element
|
1176 |
|
1177 |
def generate_chinese_essay_idea(model, user_prompt, chinese_essay_title_input):
|
1178 |
+
verify_moderation(chinese_essay_title_input)
|
1179 |
+
|
1180 |
sys_content = "你是一位老師,正在和我一起練習提高我的寫作技能。 給予的回覆不超過 500字。 用 Markdown 語法回答。"
|
1181 |
user_content = f"""
|
1182 |
{user_prompt}
|