File size: 1,422 Bytes
04a7d44
 
 
27103fa
04a7d44
 
 
 
 
27103fa
04a7d44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
from transformers import pipeline
from pysbd import Segmenter

app = FastAPI()
segmenter = Segmenter(language='en', clean=False)
privacy_intent_pipe = pipeline("text-classification",
                               "remzicam/privacy_intent" )

def doc2sent(text:str)-> dict:
    """
    > It takes a string of text and returns a list of sentences
    
    Args:
      text (str): the text to be segmented
    
    Returns:
      A list of sentences
    """
    sentences = segmenter.segment(text)
    return [sentence.replace("\n", "").strip() for sentence in sentences]


def pipe(text:str) -> list[str]:    
    """
    It takes a string of text and returns a dictionary of sentences and their corresponding privacy
    intent labels.
    
    Args:
      text (str): the text to be classified
    
    Returns:
      A dictionary of sentences and their corresponding labels.
    """
    sentences = doc2sent(text)
    preds = [privacy_intent_pipe(sent)[0]["label"] for sent in sentences]
    return dict(zip(sentences, preds))


@app.get("/infer_intents")
def t5(input):
    return {"output": pipe(input)}


app.mount("/", StaticFiles(directory="static", html=True), name="static")


@app.get("/")
def index() -> FileResponse:
    return FileResponse(path="/app/static/index.html", media_type="text/html")