christian-johnson commited on
Commit
b686079
·
verified ·
1 Parent(s): 33ef516

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. README.md +20 -0
  2. app.py +53 -0
  3. requirements.txt +2 -0
README.md ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: ThreatModel
3
+ emoji: 👁
4
+ colorFrom: yellow
5
+ colorTo: blue
6
+ sdk: gradio
7
+ sdk_version: 6.1.0
8
+ app_file: app.py
9
+ pinned: false
10
+ ---
11
+
12
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
13
+
14
+ Update your app here by updating the files, then run:
15
+ ```
16
+ hf upload --token $HF_TOKEN ProtectDemocracy/threatModel ./
17
+ ```
18
+
19
+ Ensure you've set `HF_TOKEN` to a token that has write permissions, and it is in your env.
20
+
app.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import re
3
+ import gradio as gr
4
+ from transformers import pipeline
5
+
6
+ pipe = pipeline(
7
+ "text-generation",
8
+ model="ProtectDemocracy/threat-detector",
9
+ token=os.getenv("HF_TOKEN"),
10
+ )
11
+
12
+ 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."""
13
+
14
+
15
+ # Define the function the interface will use
16
+ def generate_text(prompt):
17
+ messages = [
18
+ {"role": "system", "content": THREAT_PROMPT},
19
+ {"role": "user", "content": prompt},
20
+ ]
21
+ full_prompt = pipe.tokenizer.apply_chat_template(
22
+ messages, tokenize=False, add_generation_prompt=True
23
+ )
24
+
25
+ # Generate text, restricting the number of new tokens
26
+ output = pipe(full_prompt, max_new_tokens=1, do_sample=False)
27
+ generated_text = output[0]["generated_text"]
28
+ response = generated_text[len(full_prompt) :]
29
+ return response.strip()
30
+
31
+
32
+ # Create the Gradio Interface
33
+ iface = gr.Interface(
34
+ fn=generate_text,
35
+ inputs=gr.Textbox(
36
+ lines=5,
37
+ label="Input",
38
+ placeholder="Enter a description of a potential threat...",
39
+ ),
40
+ outputs=gr.Textbox(label="Threat Level (0 or 1)"),
41
+ title="Threat Detector",
42
+ description="This tool analyzes a description of a potential threat and classifies it as threatening (1) or nonthreatening (0)",
43
+ examples=[
44
+ ["Just had the most delicious lunch :) #TacoTuesday"],
45
+ ["I am going to commit an imminent act of unlawful violence!"],
46
+ [
47
+ "Really wish he had hit that shot! Now the Lakers are probably going to lose."
48
+ ],
49
+ ],
50
+ )
51
+
52
+ # Launch
53
+ iface.launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ transformers
2
+ torch