Update main.py
Browse files
main.py
CHANGED
@@ -4,7 +4,7 @@ import os
|
|
4 |
import secrets
|
5 |
import string
|
6 |
import time
|
7 |
-
from typing import List, Optional, Union
|
8 |
|
9 |
import httpx
|
10 |
from dotenv import load_dotenv
|
@@ -28,7 +28,6 @@ AVAILABLE_MODELS = [
|
|
28 |
{"id": "gpt-4o", "object": "model", "created": int(time.time()), "owned_by": "system"},
|
29 |
{"id": "gpt-3.5-turbo", "object": "model", "created": int(time.time()), "owned_by": "system"},
|
30 |
{"id": "dall-e-3", "object": "model", "created": int(time.time()), "owned_by": "system"},
|
31 |
-
# Added moderation model to the list
|
32 |
{"id": "text-moderation-stable", "object": "model", "created": int(time.time()), "owned_by": "system"},
|
33 |
]
|
34 |
|
@@ -226,7 +225,7 @@ async def generate_images(request: ImageGenerationRequest):
|
|
226 |
return {"created": int(time.time()), "data": results}
|
227 |
|
228 |
|
229 |
-
# ===
|
230 |
|
231 |
class ModerationRequest(BaseModel):
|
232 |
input: Union[str, List[str]]
|
@@ -236,6 +235,7 @@ class ModerationRequest(BaseModel):
|
|
236 |
async def create_moderation(request: ModerationRequest):
|
237 |
"""
|
238 |
Handles moderation requests, conforming to the OpenAI API specification.
|
|
|
239 |
"""
|
240 |
input_texts = [request.input] if isinstance(request.input, str) else request.input
|
241 |
if not input_texts:
|
@@ -246,9 +246,6 @@ async def create_moderation(request: ModerationRequest):
|
|
246 |
'Content-Type': 'application/json',
|
247 |
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36',
|
248 |
'Referer': 'https://www.chatwithmono.xyz/',
|
249 |
-
'sec-ch-ua-platform': '"Windows"',
|
250 |
-
'sec-ch-ua': '"Not)A;Brand";v="8", "Chromium";v="138", "Google Chrome";v="138"',
|
251 |
-
'sec-ch-ua-mobile': '?0',
|
252 |
}
|
253 |
|
254 |
results = []
|
@@ -261,28 +258,15 @@ async def create_moderation(request: ModerationRequest):
|
|
261 |
upstream_data = resp.json()
|
262 |
|
263 |
# --- Transform upstream response to OpenAI format ---
|
264 |
-
# Based on your example, we assume upstream gives: {"overall_sentiment": "...", "categories": {"hate": ...}}
|
265 |
upstream_categories = upstream_data.get("categories", {})
|
266 |
-
|
267 |
-
# OpenAI has more specific categories; we'll map the ones we can.
|
268 |
openai_categories = {
|
269 |
-
"hate": upstream_categories.get("hate", False),
|
270 |
-
"
|
271 |
-
"
|
272 |
-
"
|
273 |
-
"
|
274 |
-
"self-harm/intent": False, # No data from upstream
|
275 |
-
"self-harm/instructions": False, # No data from upstream
|
276 |
-
"sexual": upstream_categories.get("sexual", False),
|
277 |
-
"sexual/minors": False, # No data from upstream
|
278 |
-
"violence": upstream_categories.get("violence", False),
|
279 |
-
"violence/graphic": False, # No data from upstream
|
280 |
}
|
281 |
-
|
282 |
-
# Generate scores (1.0 for true, 0.0 for false) as upstream doesn't provide them
|
283 |
category_scores = {k: 1.0 if v else 0.0 for k, v in openai_categories.items()}
|
284 |
-
|
285 |
-
# Determine overall 'flagged' status
|
286 |
flagged = upstream_data.get("overall_sentiment") == "flagged"
|
287 |
|
288 |
result_item = {
|
@@ -290,6 +274,13 @@ async def create_moderation(request: ModerationRequest):
|
|
290 |
"categories": openai_categories,
|
291 |
"category_scores": category_scores,
|
292 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
293 |
results.append(result_item)
|
294 |
|
295 |
except httpx.HTTPStatusError as e:
|
@@ -313,7 +304,4 @@ async def create_moderation(request: ModerationRequest):
|
|
313 |
|
314 |
if __name__ == "__main__":
|
315 |
import uvicorn
|
316 |
-
# To run this file:
|
317 |
-
# 1. Make sure you have a .env file with your SNAP key.
|
318 |
-
# 2. Run in your terminal: uvicorn your_script_name:app --reload --port 8000
|
319 |
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
|
4 |
import secrets
|
5 |
import string
|
6 |
import time
|
7 |
+
from typing import List, Optional, Union
|
8 |
|
9 |
import httpx
|
10 |
from dotenv import load_dotenv
|
|
|
28 |
{"id": "gpt-4o", "object": "model", "created": int(time.time()), "owned_by": "system"},
|
29 |
{"id": "gpt-3.5-turbo", "object": "model", "created": int(time.time()), "owned_by": "system"},
|
30 |
{"id": "dall-e-3", "object": "model", "created": int(time.time()), "owned_by": "system"},
|
|
|
31 |
{"id": "text-moderation-stable", "object": "model", "created": int(time.time()), "owned_by": "system"},
|
32 |
]
|
33 |
|
|
|
225 |
return {"created": int(time.time()), "data": results}
|
226 |
|
227 |
|
228 |
+
# === Moderation Endpoint ===
|
229 |
|
230 |
class ModerationRequest(BaseModel):
|
231 |
input: Union[str, List[str]]
|
|
|
235 |
async def create_moderation(request: ModerationRequest):
|
236 |
"""
|
237 |
Handles moderation requests, conforming to the OpenAI API specification.
|
238 |
+
Includes a custom 'reason' field in the result if provided by the upstream API.
|
239 |
"""
|
240 |
input_texts = [request.input] if isinstance(request.input, str) else request.input
|
241 |
if not input_texts:
|
|
|
246 |
'Content-Type': 'application/json',
|
247 |
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36',
|
248 |
'Referer': 'https://www.chatwithmono.xyz/',
|
|
|
|
|
|
|
249 |
}
|
250 |
|
251 |
results = []
|
|
|
258 |
upstream_data = resp.json()
|
259 |
|
260 |
# --- Transform upstream response to OpenAI format ---
|
|
|
261 |
upstream_categories = upstream_data.get("categories", {})
|
|
|
|
|
262 |
openai_categories = {
|
263 |
+
"hate": upstream_categories.get("hate", False), "hate/threatening": False,
|
264 |
+
"harassment": False, "harassment/threatening": False,
|
265 |
+
"self-harm": upstream_categories.get("self-harm", False), "self-harm/intent": False, "self-harm/instructions": False,
|
266 |
+
"sexual": upstream_categories.get("sexual", False), "sexual/minors": False,
|
267 |
+
"violence": upstream_categories.get("violence", False), "violence/graphic": False,
|
|
|
|
|
|
|
|
|
|
|
|
|
268 |
}
|
|
|
|
|
269 |
category_scores = {k: 1.0 if v else 0.0 for k, v in openai_categories.items()}
|
|
|
|
|
270 |
flagged = upstream_data.get("overall_sentiment") == "flagged"
|
271 |
|
272 |
result_item = {
|
|
|
274 |
"categories": openai_categories,
|
275 |
"category_scores": category_scores,
|
276 |
}
|
277 |
+
|
278 |
+
# --- NEW: Conditionally add the 'reason' field ---
|
279 |
+
# This is a custom extension to the OpenAI spec to provide more detail.
|
280 |
+
reason = upstream_data.get("reason")
|
281 |
+
if reason:
|
282 |
+
result_item["reason"] = reason
|
283 |
+
|
284 |
results.append(result_item)
|
285 |
|
286 |
except httpx.HTTPStatusError as e:
|
|
|
304 |
|
305 |
if __name__ == "__main__":
|
306 |
import uvicorn
|
|
|
|
|
|
|
307 |
uvicorn.run(app, host="0.0.0.0", port=8000)
|