Download app.py from ProtectDemocracy/threatModel: direct link, hf CLI and curl.
- Browser
- Download file 1.86 kB
-
https://huggingface.co/ProtectDemocracy/threatModel/resolve/main/app.py
- Command line
-
hf download hf://ProtectDemocracy/threatModel/app.py
-
curl -L -o app.py https://huggingface.co/ProtectDemocracy/threatModel/resolve/main/app.py
1.86 kB
| 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() | |