samidh commited on
Commit
828659a
·
verified ·
1 Parent(s): a4e96f1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -27
app.py CHANGED
@@ -1,4 +1,4 @@
1
- # This is a project of Chakra Lab LLC. All rights reserved.
2
 
3
  import gradio as gr
4
  import os
@@ -10,19 +10,11 @@ from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
10
 
11
  device = 'cuda' if torch.cuda.is_available() else 'cpu'
12
 
13
- #base_model_name = "google/gemma-7b"
14
  base_model_name = "google/gemma-2-9b"
15
- #adapter_model_name = "samidh/cope-g7bq-2c-hs.s1.5fpc.9-sx.s1.5.9o-VL.s1.5.9-HR.s5-SH.s5-l5e5-e3-d25-r8"
16
- #adapter_model_name = "cope-project/cope-g2.9b-2c-hs.s1.5fpc.9-sx.s1.5.9o-vl.s1.5.9-hr.s5-sh.s5.l5e5-e3-d0-r8"
17
-
18
- #adapter_model_name = "samidh/cope-g2.9b-2c-hs.at-sx.at-vl.s5-sh.s5-hr.s5-e2"
19
  adapter_model_name = "samidh/cope.a.r09.2"
20
 
21
  bnb_config = BitsAndBytesConfig(
22
  load_in_8bit=True,
23
- #bnb_4bit_quant_type="nf4",
24
- #bnb_4bit_compute_dtype=torch.bfloat16,
25
- #bnb_4bit_use_double_quant=True
26
  )
27
 
28
  model = AutoModelForCausalLM.from_pretrained(base_model_name,
@@ -120,25 +112,14 @@ def predict(content, policy):
120
  # Decode the predicted token
121
  decoded_output = tokenizer.decode([predicted_token_id])
122
 
123
- # Get the probability of the predicted token
124
- predicted_prob = probabilities[0, predicted_token_id].item()
125
-
126
- # Function to get probability for a specific token
127
- def get_token_probability(token):
128
- token_id = tokenizer.encode(token, add_special_tokens=False)[0]
129
- return probabilities[0, token_id].item()
130
-
131
- predicted_prob_0 = get_token_probability('0')
132
- predicted_prob_1 = get_token_probability('1')
133
-
134
  if decoded_output == '1':
135
- return f'VIOLATING\n(P: {predicted_prob_1:.2f})'
136
  else:
137
- return f'NON-Violating\n(P: {predicted_prob_0:.2f})'
138
 
139
  with gr.Blocks() as iface:
140
- gr.Markdown("# CoPE Alpha Preview")
141
- gr.Markdown("See if the given content violates your given policy.")
142
 
143
  with gr.Row():
144
  content_input = gr.Textbox(label="Content", lines=2, value=DEFAULT_CONTENT)
@@ -150,15 +131,13 @@ with gr.Blocks() as iface:
150
  gr.Markdown("""
151
  ## About CoPE
152
 
153
- CoPE (the COntent Policy Evaluation engine) is a small language model capable of accurate content policy labeling. This is a **preview** of our alpha release and is strictly for **research** purposes. This should **NOT** be used for any production use cases.
154
 
155
  ## How to Use
156
 
157
  1. Enter your content in the "Content" box.
158
  2. Specify your policy in the "Policy" box.
159
  3. Click "Submit" to see the results.
160
-
161
- **Note**: Inference times are **slow** (1-2 seconds) since this is built on dev infra and not yet optimized for live systems. Please be patient!
162
 
163
  ## More Info
164
 
 
1
+ # This is a project of Zentropi Inc. All rights reserved.
2
 
3
  import gradio as gr
4
  import os
 
10
 
11
  device = 'cuda' if torch.cuda.is_available() else 'cpu'
12
 
 
13
  base_model_name = "google/gemma-2-9b"
 
 
 
 
14
  adapter_model_name = "samidh/cope.a.r09.2"
15
 
16
  bnb_config = BitsAndBytesConfig(
17
  load_in_8bit=True,
 
 
 
18
  )
19
 
20
  model = AutoModelForCausalLM.from_pretrained(base_model_name,
 
112
  # Decode the predicted token
113
  decoded_output = tokenizer.decode([predicted_token_id])
114
 
 
 
 
 
 
 
 
 
 
 
 
115
  if decoded_output == '1':
116
+ return f'MATCHES (i.e., violating)'
117
  else:
118
+ return f'NO MATCHES (i.e., non-violating)'
119
 
120
  with gr.Blocks() as iface:
121
+ gr.Markdown("# Zentropi CoPE Demo")
122
+ gr.Markdown("See if the given content matches any of your given policy labels.")
123
 
124
  with gr.Row():
125
  content_input = gr.Textbox(label="Content", lines=2, value=DEFAULT_CONTENT)
 
131
  gr.Markdown("""
132
  ## About CoPE
133
 
134
+ CoPE (the COntent Policy Evaluation engine) is a small language model capable of accurate content policy labeling. This is a **demo* of our initial release and should **NOT** be used for any production use cases.
135
 
136
  ## How to Use
137
 
138
  1. Enter your content in the "Content" box.
139
  2. Specify your policy in the "Policy" box.
140
  3. Click "Submit" to see the results.
 
 
141
 
142
  ## More Info
143