Spaces:
Sleeping
Sleeping
Commit
Β·
7d63c90
1
Parent(s):
1f75213
setup
Browse files- Dockerfile +16 -0
- app.py +38 -0
- dto.py +11 -0
- packges.txt +10 -0
- textclassifier.py +13 -0
Dockerfile
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
|
2 |
+
# you will also find guides on how best to write your Dockerfile
|
3 |
+
|
4 |
+
FROM python:3.9
|
5 |
+
|
6 |
+
RUN useradd -m -u 1000 user
|
7 |
+
USER user
|
8 |
+
ENV PATH="/home/user/.local/bin:$PATH"
|
9 |
+
|
10 |
+
WORKDIR /app
|
11 |
+
|
12 |
+
COPY --chown=user ./packges.txt packges.txt
|
13 |
+
RUN pip install --no-cache-dir --upgrade -r packges.txt
|
14 |
+
|
15 |
+
COPY --chown=user . /app
|
16 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI
|
2 |
+
from fastapi.responses import JSONResponse
|
3 |
+
from fastapi.middleware.cors import CORSMiddleware
|
4 |
+
from textclassifier import TextClassifier
|
5 |
+
from dto import ClassifyRequest
|
6 |
+
|
7 |
+
from transformers import pipeline
|
8 |
+
|
9 |
+
hub_model_id = "NathyB/Hate-Speech-Detection-in-Amharic-Language-mBERT"
|
10 |
+
text_classifier = pipeline("text-classification", model=hub_model_id)
|
11 |
+
|
12 |
+
|
13 |
+
app = FastAPI()
|
14 |
+
|
15 |
+
# Setup cors
|
16 |
+
app.add_middleware(
|
17 |
+
CORSMiddleware,
|
18 |
+
allow_origins=["*"],
|
19 |
+
allow_credentials=True,
|
20 |
+
allow_methods=["*"],
|
21 |
+
allow_headers=["*"],
|
22 |
+
)
|
23 |
+
|
24 |
+
|
25 |
+
|
26 |
+
@app.post("/textclassify")
|
27 |
+
def classifyText(request_body:ClassifyRequest):
|
28 |
+
text = request_body.text
|
29 |
+
summary = TextClassifier(text_classifier).classify(text)
|
30 |
+
return JSONResponse(content=summary, status_code=201)
|
31 |
+
|
32 |
+
|
33 |
+
|
34 |
+
|
35 |
+
# Get route
|
36 |
+
@app.get("/")
|
37 |
+
def home():
|
38 |
+
return {"hello":"world"}
|
dto.py
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from datetime import datetime
|
2 |
+
from typing import List
|
3 |
+
from typing import Optional
|
4 |
+
|
5 |
+
from pydantic import BaseModel
|
6 |
+
from pydantic import Field
|
7 |
+
|
8 |
+
|
9 |
+
class ClassifyRequest(BaseModel):
|
10 |
+
"""Text Summarize request model."""
|
11 |
+
text: str = Field(..., description="The text you want to summarize", examples=["αα£α± α ααα α΅αα«α ααα α¨α¦ααα± ααα₯α΅ α ααα«α ..."])
|
packges.txt
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
nltk
|
2 |
+
numpy
|
3 |
+
fastapi
|
4 |
+
pandas
|
5 |
+
scikit-learn
|
6 |
+
transformers
|
7 |
+
torch
|
8 |
+
torchvision
|
9 |
+
torchaudio
|
10 |
+
uvicorn[standard]
|
textclassifier.py
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
class TextClassifier:
|
2 |
+
|
3 |
+
def __init__(self, model) -> None:
|
4 |
+
self.text_classifier = model
|
5 |
+
|
6 |
+
def classify(self,text):
|
7 |
+
|
8 |
+
data = self.text_classifier(text)[0]
|
9 |
+
print("response from hugging \n", data)
|
10 |
+
|
11 |
+
return data
|
12 |
+
|
13 |
+
|