jerdev38282 commited on
Commit
b82e46a
·
verified ·
1 Parent(s): 5827197

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -38
app.py CHANGED
@@ -1,69 +1,78 @@
1
  from fastapi import FastAPI
2
  from pydantic import BaseModel
3
- from transformers import pipeline
4
  import uvicorn
5
 
6
  app = FastAPI()
7
 
8
- # ===== LOAD MODEL (LIGHTWEIGHT MULTILINGUAL TOXICITY) =====
9
- classifier = pipeline(
10
- "text-classification",
11
- model="unitary/multilingual-toxic-xlm-roberta",
12
- top_k=None
13
- )
14
 
15
- # ===== RESPONSE MODEL =====
 
 
16
  class TextIn(BaseModel):
17
  text: str
18
 
19
 
 
 
 
20
  def analyze_text(text: str):
21
- result = classifier(text)[0]
22
-
23
- # model output: list of dicts
24
- # we normalize it
25
- output = []
26
- for r in result:
27
- output.append({
28
- "label": r["label"],
29
- "score": float(r["score"])
30
- })
31
-
32
- # find toxic score (if label exists)
33
- toxic_score = 0.0
34
- for r in output:
35
- if "toxic" in r["label"].lower():
36
- toxic_score = r["score"]
37
-
38
- return {
39
- "input": text,
40
- "toxic_score": toxic_score,
41
- "details": output,
42
- "is_flagged": toxic_score > 0.5
43
- }
44
-
45
-
46
- # ===== GET ENDPOINT (WHAT YOU WANT) =====
47
  @app.get("/analyze")
48
  def analyze_get(text: str):
49
  return analyze_text(text)
50
 
51
 
52
- # ===== OPTIONAL POST ENDPOINT =====
 
 
53
  @app.post("/analyze")
54
  def analyze_post(body: TextIn):
55
  return analyze_text(body.text)
56
 
57
 
58
- # ===== ROOT =====
 
 
59
  @app.get("/")
60
  def root():
61
  return {
62
  "status": "ok",
63
- "usage": "/analyze?text=your_message_here"
64
  }
65
 
66
 
67
- # ===== RUN (for local testing only) =====
 
 
68
  if __name__ == "__main__":
69
  uvicorn.run(app, host="0.0.0.0", port=7860)
 
1
  from fastapi import FastAPI
2
  from pydantic import BaseModel
3
+ from openai import OpenAI
4
  import uvicorn
5
 
6
  app = FastAPI()
7
 
8
+ # =========================
9
+ # OPENAI CLIENT
10
+ # =========================
11
+ client = OpenAI() # uses OPENAI_API_KEY from environment / HF Secrets
 
 
12
 
13
+ # =========================
14
+ # REQUEST BODY
15
+ # =========================
16
  class TextIn(BaseModel):
17
  text: str
18
 
19
 
20
+ # =========================
21
+ # CORE MODERATION FUNCTION
22
+ # =========================
23
  def analyze_text(text: str):
24
+ try:
25
+ response = client.moderations.create(
26
+ model="omni-moderation-latest",
27
+ input=text
28
+ )
29
+
30
+ result = response.results[0]
31
+
32
+ return {
33
+ "input": text,
34
+ "flagged": result.flagged,
35
+ "categories": result.categories.model_dump(),
36
+ "category_scores": result.category_scores.model_dump()
37
+ }
38
+
39
+ except Exception as e:
40
+ return {
41
+ "input": text,
42
+ "error": str(e),
43
+ "flagged": False
44
+ }
45
+
46
+
47
+ # =========================
48
+ # GET ENDPOINT
49
+ # =========================
50
  @app.get("/analyze")
51
  def analyze_get(text: str):
52
  return analyze_text(text)
53
 
54
 
55
+ # =========================
56
+ # POST ENDPOINT
57
+ # =========================
58
  @app.post("/analyze")
59
  def analyze_post(body: TextIn):
60
  return analyze_text(body.text)
61
 
62
 
63
+ # =========================
64
+ # ROOT
65
+ # =========================
66
  @app.get("/")
67
  def root():
68
  return {
69
  "status": "ok",
70
+ "usage": "/analyze?text=your_text_here"
71
  }
72
 
73
 
74
+ # =========================
75
+ # RUN (for local testing only)
76
+ # =========================
77
  if __name__ == "__main__":
78
  uvicorn.run(app, host="0.0.0.0", port=7860)