AsyncCoder-10 commited on
Commit
0608d7d
·
verified ·
1 Parent(s): baf0f20

Upload 5 files

Browse files
Files changed (5) hide show
  1. README.md +10 -15
  2. app.py +28 -28
  3. app/__init__.py +0 -0
  4. app/main.py +55 -0
  5. requirements.txt +5 -0
README.md CHANGED
@@ -1,15 +1,10 @@
1
- ---
2
- title: Moderation Api
3
- emoji: 🔥
4
- colorFrom: indigo
5
- colorTo: yellow
6
- sdk: gradio
7
- sdk_version: 6.31.0
8
- python_version: '3.12'
9
- app_file: app.py
10
- pinned: false
11
- license: mit
12
- short_description: text moderation
13
- ---
14
-
15
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
+ ---
2
+ title: Multilingual Moderation API
3
+ emoji: 🛡️
4
+ colorFrom: blue
5
+ colorTo: green
6
+ sdk: gradio
7
+ sdk_version: 4.44.1
8
+ app_file: app.py
9
+ pinned: false
10
+ ---
 
 
 
 
 
app.py CHANGED
@@ -1,28 +1,28 @@
1
- import gradio as gr
2
- from app.main import app as fastapi_app
3
-
4
- import spaces
5
-
6
-
7
- @spaces.GPU
8
- def _satisfy_zerogpu_startup_check():
9
- """Dummy function to bypass ZeroGPU startup requirement."""
10
- return None
11
-
12
-
13
- # Create a simple Gradio interface
14
- def moderate_ui(text):
15
- return f"Processing text: {text}"
16
-
17
-
18
- gradio_interface = gr.Interface(
19
- fn=moderate_ui, inputs="text", outputs="text", title="Multilingual Moderation API"
20
- )
21
-
22
- # Mount FastAPI onto Gradio
23
- app = gr.mount_gradio_app(fastapi_app, gradio_interface, path="/")
24
-
25
- if __name__ == "__main__":
26
- import uvicorn
27
-
28
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
1
+ import gradio as gr
2
+ from app.main import app as fastapi_app
3
+
4
+ import spaces
5
+
6
+
7
+ @spaces.GPU
8
+ def _satisfy_zerogpu_startup_check():
9
+ """Dummy function to bypass ZeroGPU startup requirement."""
10
+ return None
11
+
12
+
13
+ # Create a simple Gradio interface
14
+ def moderate_ui(text):
15
+ return f"Processing text: {text}"
16
+
17
+
18
+ gradio_interface = gr.Interface(
19
+ fn=moderate_ui, inputs="text", outputs="text", title="Multilingual Moderation API"
20
+ )
21
+
22
+ # Mount FastAPI onto Gradio
23
+ app = gr.mount_gradio_app(fastapi_app, gradio_interface, path="/")
24
+
25
+ if __name__ == "__main__":
26
+ import uvicorn
27
+
28
+ uvicorn.run(app, host="0.0.0.0", port=7860)
app/__init__.py ADDED
File without changes
app/main.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from pydantic import BaseModel
3
+ from transformers import pipeline
4
+ from glin_profanity import Filter
5
+
6
+ # Initialize your models
7
+ toxicity_pipe = pipeline(
8
+ "text-classification", model="textdetox/twitter-xlmr-toxicity-classifier"
9
+ )
10
+ detox_pipe = pipeline("text2text-generation", model="s-nlp/mt0-xl-detox-orpo")
11
+ profanity_filter = Filter(
12
+ {"languages": ["english", "spanish", "french"], "replace_with": "***"}
13
+ )
14
+
15
+ app = FastAPI(title="Multilingual Moderation API")
16
+
17
+
18
+ class TextRequest(BaseModel):
19
+ text: str
20
+
21
+
22
+ class ModerationResponse(BaseModel):
23
+ original_text: str
24
+ detected_categories: dict
25
+ is_flagged: bool
26
+ rewritten_text: str | None
27
+
28
+
29
+ @app.post("/moderate", response_model=ModerationResponse)
30
+ async def moderate_text(request: TextRequest):
31
+ text = request.text
32
+ profanity_result = profanity_filter.check_profanity(text)
33
+ toxicity_scores = toxicity_pipe(text)[0]
34
+
35
+ is_flagged = (
36
+ profanity_result["contains_profanity"] or toxicity_scores["score"] > 0.5
37
+ )
38
+ rewritten = None
39
+ if is_flagged:
40
+ rewritten = detox_pipe(text, max_length=100)[0]["generated_text"]
41
+
42
+ return ModerationResponse(
43
+ original_text=text,
44
+ detected_categories={
45
+ "profanity": profanity_result["contains_profanity"],
46
+ "toxicity_model": toxicity_scores,
47
+ },
48
+ is_flagged=is_flagged,
49
+ rewritten_text=rewritten,
50
+ )
51
+
52
+
53
+ @app.get("/health")
54
+ async def health_check():
55
+ return {"status": "ok"}
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ fastapi==0.109.0
2
+ uvicorn==0.27.0
3
+ transformers==4.40.0
4
+ glin-profanity==4.1.0
5
+ gradio>=4.0.0