Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,69 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py - Gradio UI for interacting with facebook/opt-125m
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
# Optional toxicity scoring
|
| 7 |
+
try:
|
| 8 |
+
from detoxify import Detoxify
|
| 9 |
+
detox_available = True
|
| 10 |
+
except Exception:
|
| 11 |
+
detox_available = False
|
| 12 |
+
|
| 13 |
+
MODEL_NAME = "facebook/opt-125m"
|
| 14 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 15 |
+
|
| 16 |
+
@st.cache_resource
|
| 17 |
+
def load_models():
|
| 18 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, use_fast=False)
|
| 19 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME)
|
| 20 |
+
model.to(DEVICE)
|
| 21 |
+
model.eval()
|
| 22 |
+
detox = Detoxify('original') if detox_available else None
|
| 23 |
+
return tokenizer, model, detox
|
| 24 |
+
|
| 25 |
+
tokenizer, model, detox = load_models()
|
| 26 |
+
|
| 27 |
+
@torch.inference_mode()
|
| 28 |
+
def generate(prompt, max_new_tokens=150, temperature=0.8, top_p=0.95, return_toxicity=False):
|
| 29 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(DEVICE)
|
| 30 |
+
out = model.generate(
|
| 31 |
+
**inputs,
|
| 32 |
+
do_sample=True,
|
| 33 |
+
max_new_tokens=int(max_new_tokens),
|
| 34 |
+
temperature=float(temperature),
|
| 35 |
+
top_p=float(top_p),
|
| 36 |
+
pad_token_id=tokenizer.eos_token_id
|
| 37 |
+
)
|
| 38 |
+
text = tokenizer.decode(out[0], skip_special_tokens=True)
|
| 39 |
+
# get continuation only
|
| 40 |
+
continuation = text[len(prompt):].strip() if text.startswith(prompt) else text
|
| 41 |
+
toxicity_score = None
|
| 42 |
+
if return_toxicity and detox is not None:
|
| 43 |
+
try:
|
| 44 |
+
toxicity_score = detox.predict(continuation)["toxicity"]
|
| 45 |
+
except Exception:
|
| 46 |
+
toxicity_score = None
|
| 47 |
+
return continuation, toxicity_score
|
| 48 |
+
|
| 49 |
+
with gr.Blocks() as demo:
|
| 50 |
+
gr.Markdown("# OPT-125M Interactive")
|
| 51 |
+
with gr.Row():
|
| 52 |
+
inp = gr.Textbox(label="Prompt", placeholder="Type something to the model...", lines=3)
|
| 53 |
+
with gr.Column():
|
| 54 |
+
max_tokens = gr.Slider(10, 512, value=150, step=10, label="Max new tokens")
|
| 55 |
+
temp = gr.Slider(0.1, 1.5, value=0.8, step=0.05, label="Temperature")
|
| 56 |
+
top_p = gr.Slider(0.1, 1.0, value=0.95, step=0.01, label="Top-p (nucleus)")
|
| 57 |
+
tox_checkbox = gr.Checkbox(value=False, label="Return toxicity score (requires detoxify)")
|
| 58 |
+
run_btn = gr.Button("Generate")
|
| 59 |
+
output_text = gr.Textbox(label="Model output", lines=8)
|
| 60 |
+
tox_out = gr.Textbox(label="Toxicity score (None if unavailable)", lines=1)
|
| 61 |
+
|
| 62 |
+
def on_click(prompt, max_new_tokens, temperature, top_p, tox):
|
| 63 |
+
continuation, tox_score = generate(prompt, max_new_tokens, temperature, top_p, tox)
|
| 64 |
+
return continuation, str(tox_score) if tox_score is not None else "Not available"
|
| 65 |
+
|
| 66 |
+
run_btn.click(on_click, inputs=[inp, max_tokens, temp, top_p, tox_checkbox], outputs=[output_text, tox_out])
|
| 67 |
+
|
| 68 |
+
if __name__ == "__main__":
|
| 69 |
+
demo.launch()
|