File size: 2,433 Bytes
f2216e3
 
 
 
 
 
a914a34
 
bc150cd
69c04fb
a914a34
52db929
f2216e3
52db929
 
e8f0e1a
f2216e3
 
 
 
 
 
 
 
 
 
 
a5deb9e
 
f2216e3
a5deb9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b24525
a5deb9e
8970b97
 
 
 
 
 
 
 
c21ac27
8970b97
e40ffb5
8970b97
 
 
 
 
a5deb9e
 
 
f2216e3
 
 
52db929
f2216e3
 
 
 
8d562a8
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import os
os.environ["HF_HOME"] = "/.cache"

import re
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM


model_dir_large = 'edithram23/Redaction_Personal_info_v1'
tokenizer_large = AutoTokenizer.from_pretrained(model_dir_large)
model_large = AutoModelForSeq2SeqLM.from_pretrained(model_dir_large)

def mask_generation(text,model=model_large,tokenizer=tokenizer_large):
    import re
    inputs = ["Mask Generation: " + text+'.']
    inputs = tokenizer(inputs, max_length=512, truncation=True, return_tensors="pt")
    output = model.generate(**inputs, num_beams=8, do_sample=True, max_length=len(text))
    decoded_output = tokenizer.batch_decode(output, skip_special_tokens=True)[0]
    predicted_title = decoded_output.strip()
    pattern = r'\[.*?\]'
    # Replace all occurrences of the pattern with [redacted]
    redacted_text = re.sub(pattern, '[redacted]', predicted_title)
    return redacted_text

from fastapi import FastAPI
import uvicorn

app = FastAPI()
from fastapi import FastAPI, Form
from fastapi.responses import HTMLResponse

app = FastAPI()

# Serve the HTML form
@app.get("/", response_class=HTMLResponse)
async def read_form():
    html_content = """
    <!DOCTYPE html>
    <html>
    <head>
        <title>FastAPI Input Form</title>
    </head>
    <body>
        <h1>Enter Your Message</h1>
        <form action="/submit" method="post">
            <label for="message">Message:</label>
            <input type="text" id="message" name="message">
            <input type="submit" value="Submit">
        </form>
    </body>
    </html>
    """
    return html_content

# Handle the form submission
@app.post("/submit",response_class=HTMLResponse)
async def submit_form(message: str = Form(...)):
    html_content = f"""
    <!DOCTYPE html>
    <html>
    <head>
        <title>FastAPI Input Form</title>
    </head>
    <body>
        <h1>Enter Your Message</h1>
        <form action="/" method="get">
            <p>{mask_generation(message)}</p>
            <input type="submit" value="HOME">
        </form>
    </body>
    </html>
    """
    return html_content
# @app.get("/")
# async def hello():
#     return {"msg" : "Live"}

@app.post("/mask")
async def mask_input(query):
    output = mask_generation(query)
    return {"data" : output}

if __name__ == '__main__':
    os.environ["HF_HOME"] = "/.cache"
    uvicorn.run("main:app", host="0.0.0.0", port=7860, reload=True, workers=1)