File size: 1,855 Bytes
b686079
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import re
import gradio as gr
from transformers import pipeline

pipe = pipeline(
    "text-generation",
    model="ProtectDemocracy/threat-detector",
    token=os.getenv("HF_TOKEN"),
)

THREAT_PROMPT: str = """Analyze the following social media post from the user and determine whether or not a threat of violence is present or not. Note that posts that are discussing the news, or simply responding to other threatening posts, are not themselves threatening. Respond with a 0 if there is no violent threat, or a 1 if there is a violent threat."""


# Define the function the interface will use
def generate_text(prompt):
    messages = [
        {"role": "system", "content": THREAT_PROMPT},
        {"role": "user", "content": prompt},
    ]
    full_prompt = pipe.tokenizer.apply_chat_template(
        messages, tokenize=False, add_generation_prompt=True
    )

    # Generate text, restricting the number of new tokens
    output = pipe(full_prompt, max_new_tokens=1, do_sample=False)
    generated_text = output[0]["generated_text"]
    response = generated_text[len(full_prompt) :]
    return response.strip()


# Create the Gradio Interface
iface = gr.Interface(
    fn=generate_text,
    inputs=gr.Textbox(
        lines=5,
        label="Input",
        placeholder="Enter a description of a potential threat...",
    ),
    outputs=gr.Textbox(label="Threat Level (0 or 1)"),
    title="Threat Detector",
    description="This tool analyzes a description of a potential threat and classifies it as threatening (1) or nonthreatening (0)",
    examples=[
        ["Just had the most delicious lunch :) #TacoTuesday"],
        ["I am going to commit an imminent act of unlawful violence!"],
        [
            "Really wish he had hit that shot! Now the Lakers are probably going to lose."
        ],
    ],
)

# Launch
iface.launch()