AsyncCoder-10 commited on
Commit
4027cdd
·
verified ·
1 Parent(s): d1839f7

Update app/main.py

Browse files
Files changed (1) hide show
  1. app/main.py +5 -16
app/main.py CHANGED
@@ -4,27 +4,18 @@ from transformers import pipeline, AutoTokenizer
4
  from glin_profanity import Filter
5
  import re
6
 
7
- # 1. Toxicity classifier
8
  toxicity_pipe = pipeline("text-classification", model="textdetox/twitter-xlmr-toxicity-classifier")
9
-
10
- # 2. UPGRADED: Use a highly capable Chat Model (Phi-3)
11
  model_id = "microsoft/Phi-3-mini-4k-instruct"
12
-
13
- # --- ADD trust_remote_code=True HERE ---
14
  tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
15
- llm_pipe = pipeline(
16
- "text-generation",
17
- model=model_id,
18
- tokenizer=tokenizer,
19
- trust_remote_code=True # <--- THE FIX
20
- )
21
-
22
  profanity_filter = Filter({"languages": ["english", "spanish", "french"], "replace_with": "***"})
23
 
24
  app = FastAPI(title="Multilingual Moderation API")
25
 
 
26
  class TextRequest(BaseModel):
27
- text: str
28
 
29
  class ModerationResponse(BaseModel):
30
  original_text: str
@@ -41,7 +32,7 @@ threat_pattern = re.compile("|".join(THREAT_KEYWORDS), re.IGNORECASE)
41
 
42
  @app.post("/moderate", response_model=ModerationResponse)
43
  async def moderate_text(request: TextRequest):
44
- text = request.text
45
 
46
  profanity_result = profanity_filter.check_profanity(text)
47
  tox_result = toxicity_pipe(text)[0]
@@ -64,7 +55,6 @@ async def moderate_text(request: TextRequest):
64
 
65
  prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
66
  outputs = llm_pipe(prompt, max_new_tokens=200, do_sample=True, temperature=0.7)
67
-
68
  generated_text = outputs[0]['generated_text'][len(prompt):].strip()
69
 
70
  lines = generated_text.split('\n')
@@ -78,7 +68,6 @@ async def moderate_text(request: TextRequest):
78
  rewritten.append(clean_line)
79
 
80
  rewritten = list(dict.fromkeys(rewritten))[:5]
81
-
82
  if not rewritten:
83
  rewritten = ["[AI could not generate a rewrite, please revise manually]"]
84
 
 
4
  from glin_profanity import Filter
5
  import re
6
 
7
+ # 1. Models
8
  toxicity_pipe = pipeline("text-classification", model="textdetox/twitter-xlmr-toxicity-classifier")
 
 
9
  model_id = "microsoft/Phi-3-mini-4k-instruct"
 
 
10
  tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
11
+ llm_pipe = pipeline("text-generation", model=model_id, tokenizer=tokenizer, trust_remote_code=True)
 
 
 
 
 
 
12
  profanity_filter = Filter({"languages": ["english", "spanish", "french"], "replace_with": "***"})
13
 
14
  app = FastAPI(title="Multilingual Moderation API")
15
 
16
+ # 2. Request Model - THIS IS THE CRITICAL PART
17
  class TextRequest(BaseModel):
18
+ text: str # <--- It MUST be 'text', not 'data'
19
 
20
  class ModerationResponse(BaseModel):
21
  original_text: str
 
32
 
33
  @app.post("/moderate", response_model=ModerationResponse)
34
  async def moderate_text(request: TextRequest):
35
+ text = request.text # Using 'text' correctly
36
 
37
  profanity_result = profanity_filter.check_profanity(text)
38
  tox_result = toxicity_pipe(text)[0]
 
55
 
56
  prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
57
  outputs = llm_pipe(prompt, max_new_tokens=200, do_sample=True, temperature=0.7)
 
58
  generated_text = outputs[0]['generated_text'][len(prompt):].strip()
59
 
60
  lines = generated_text.split('\n')
 
68
  rewritten.append(clean_line)
69
 
70
  rewritten = list(dict.fromkeys(rewritten))[:5]
 
71
  if not rewritten:
72
  rewritten = ["[AI could not generate a rewrite, please revise manually]"]
73