Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import base64
|
| 2 |
+
import io
|
| 3 |
+
import os
|
| 4 |
+
import requests
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import gradio as gr
|
| 7 |
+
|
| 8 |
+
BACKEND_URL = os.getenv("BACKEND_URL", "http://backend:8000") # docker-compose network name
|
| 9 |
+
|
| 10 |
+
def call_redact(text):
|
| 11 |
+
payload = {"text": text, "options": {"temperature": 0.0, "max_tokens": 256}}
|
| 12 |
+
r = requests.post(f"{BACKEND_URL}/redact", json=payload, timeout=120)
|
| 13 |
+
r.raise_for_status()
|
| 14 |
+
data = r.json()
|
| 15 |
+
|
| 16 |
+
img = None
|
| 17 |
+
if data.get("plot_png_b64"):
|
| 18 |
+
img_bytes = base64.b64decode(data["plot_png_b64"])
|
| 19 |
+
img = Image.open(io.BytesIO(img_bytes))
|
| 20 |
+
|
| 21 |
+
return data["normalized"], img, f"{data['latency_ms']} ms"
|
| 22 |
+
|
| 23 |
+
with gr.Blocks(title="PII Redaction UI") as demo:
|
| 24 |
+
gr.Markdown("### PII Redaction (FastAPI backend + Gradio frontend)")
|
| 25 |
+
|
| 26 |
+
inp = gr.Textbox(lines=4, label="Enter text")
|
| 27 |
+
out_text = gr.Textbox(label="Normalized redaction")
|
| 28 |
+
out_plot = gr.Image(label="Top-K probabilities", type="pil")
|
| 29 |
+
out_lat = gr.Textbox(label="Latency")
|
| 30 |
+
|
| 31 |
+
btn = gr.Button("Redact")
|
| 32 |
+
btn.click(fn=call_redact, inputs=inp, outputs=[out_text, out_plot, out_lat])
|
| 33 |
+
|
| 34 |
+
if __name__ == "__main__":
|
| 35 |
+
# run on 0.0.0.0:7860 for docker
|
| 36 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|