Spaces:
Running on Zero
Running on Zero
Update app/main.py
Browse files- app/main.py +14 -6
app/main.py
CHANGED
|
@@ -1,21 +1,29 @@
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
from pydantic import BaseModel
|
| 3 |
-
from transformers import pipeline, AutoTokenizer
|
| 4 |
from glin_profanity import Filter
|
| 5 |
import re
|
| 6 |
|
| 7 |
-
# 1.
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 19 |
|
| 20 |
class ModerationResponse(BaseModel):
|
| 21 |
original_text: str
|
|
@@ -32,7 +40,7 @@ threat_pattern = re.compile("|".join(THREAT_KEYWORDS), re.IGNORECASE)
|
|
| 32 |
|
| 33 |
@app.post("/moderate", response_model=ModerationResponse)
|
| 34 |
async def moderate_text(request: TextRequest):
|
| 35 |
-
text = request.text
|
| 36 |
|
| 37 |
profanity_result = profanity_filter.check_profanity(text)
|
| 38 |
tox_result = toxicity_pipe(text)[0]
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
from pydantic import BaseModel
|
| 3 |
+
from transformers import pipeline, AutoModelForCausalLM, 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. Phi-3 Model - The FIX is here
|
| 11 |
model_id = "microsoft/Phi-3-mini-4k-instruct"
|
| 12 |
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
|
| 13 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 14 |
+
model_id,
|
| 15 |
+
trust_remote_code=True,
|
| 16 |
+
attn_implementation="eager", # ✅ This resolves the window_size error
|
| 17 |
+
device_map="auto"
|
| 18 |
+
)
|
| 19 |
+
llm_pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
| 20 |
+
|
| 21 |
profanity_filter = Filter({"languages": ["english", "spanish", "french"], "replace_with": "***"})
|
| 22 |
|
| 23 |
app = FastAPI(title="Multilingual Moderation API")
|
| 24 |
|
|
|
|
| 25 |
class TextRequest(BaseModel):
|
| 26 |
+
text: str
|
| 27 |
|
| 28 |
class ModerationResponse(BaseModel):
|
| 29 |
original_text: str
|
|
|
|
| 40 |
|
| 41 |
@app.post("/moderate", response_model=ModerationResponse)
|
| 42 |
async def moderate_text(request: TextRequest):
|
| 43 |
+
text = request.text
|
| 44 |
|
| 45 |
profanity_result = profanity_filter.check_profanity(text)
|
| 46 |
tox_result = toxicity_pipe(text)[0]
|