Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
def create_prompt(input_text: str, tokenizer):
|
| 6 |
+
return f"{input_text}{tokenizer.bos_token}"
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
MODELS = [
|
| 10 |
+
"gpt2-sarcasm-defuser",
|
| 11 |
+
"gpt2-medium-sarcasm-defuser",
|
| 12 |
+
"bart-base-sarcasm-defuser",
|
| 13 |
+
]
|
| 14 |
+
MODEL_TASKS = {
|
| 15 |
+
"gpt2-sarcasm-defuser": "text-generation",
|
| 16 |
+
"gpt2-medium-sarcasm-defuser": "text-generation",
|
| 17 |
+
"bart-base-sarcasm-defuser": "text2text-generation",
|
| 18 |
+
}
|
| 19 |
+
model_pipe = {}
|
| 20 |
+
for m in MODELS:
|
| 21 |
+
model_pipe[m] = pipeline(MODEL_TASKS[m], f"maxmarcon/{m}")
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def sarcasm_defuser(
|
| 25 |
+
text: str, model: str, max_new_tokens: int, greedy: bool, temperature: float
|
| 26 |
+
):
|
| 27 |
+
|
| 28 |
+
pipe = model_pipe[model]
|
| 29 |
+
|
| 30 |
+
text = (
|
| 31 |
+
create_prompt(text, pipe.tokenizer)
|
| 32 |
+
if MODEL_TASKS[model] == "text-generation"
|
| 33 |
+
else text
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
model_specific_args = (
|
| 37 |
+
{"return_full_text": False} if MODEL_TASKS[model] == "text-generation" else {}
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
output = pipe(
|
| 41 |
+
text,
|
| 42 |
+
max_new_tokens=max_new_tokens,
|
| 43 |
+
do_sample=not greedy,
|
| 44 |
+
temperature=float(temperature),
|
| 45 |
+
**model_specific_args,
|
| 46 |
+
)
|
| 47 |
+
return output[0]["generated_text"]
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
gradio_app = gr.Interface(
|
| 51 |
+
fn=sarcasm_defuser,
|
| 52 |
+
inputs=[
|
| 53 |
+
gr.Textbox(),
|
| 54 |
+
gr.Radio(MODELS, value=MODELS[0], label="Model"),
|
| 55 |
+
gr.Number(50, label="Max Tokens", precision=0),
|
| 56 |
+
gr.Checkbox(False, label="Greedy"),
|
| 57 |
+
gr.Number(1.0, label="Temperature", precision=1, step=0.1),
|
| 58 |
+
],
|
| 59 |
+
flagging_mode="never",
|
| 60 |
+
outputs=["text"],
|
| 61 |
+
title="Sarcasm Defuser",
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
if __name__ == "__main__":
|
| 65 |
+
gradio_app.launch()
|