Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -5,11 +5,11 @@
|
|
| 5 |
# - https://huggingface.co/EnglishVoice/t5-base-keywords-to-headline?text=diabetic+diet+plan
|
| 6 |
# - Apache 2.0
|
| 7 |
|
|
|
|
|
|
|
| 8 |
|
| 9 |
import torch
|
| 10 |
from transformers import T5ForConditionalGeneration,T5Tokenizer
|
| 11 |
-
import gradio as gr
|
| 12 |
-
|
| 13 |
|
| 14 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 15 |
|
|
@@ -17,26 +17,36 @@ model = T5ForConditionalGeneration.from_pretrained("EnglishVoice/t5-base-keyword
|
|
| 17 |
tokenizer = T5Tokenizer.from_pretrained("EnglishVoice/t5-base-keywords-to-headline", clean_up_tokenization_spaces=True, legacy=False)
|
| 18 |
model = model.to(device)
|
| 19 |
|
| 20 |
-
def title_gen(keywords):
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
text = "headline: " + keywords
|
| 23 |
encoding = tokenizer.encode_plus(text, return_tensors = "pt")
|
| 24 |
input_ids = encoding["input_ids"].to(device)
|
| 25 |
attention_masks = encoding["attention_mask"].to(device)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
beam_outputs = model.generate(
|
| 27 |
input_ids = input_ids,
|
| 28 |
attention_mask = attention_masks,
|
| 29 |
max_new_tokens = 30,
|
| 30 |
do_sample = True,
|
| 31 |
num_return_sequences = 5,
|
| 32 |
-
temperature =
|
| 33 |
-
#num_beams = 20,
|
| 34 |
-
#num_beam_groups = 20,
|
| 35 |
-
#diversity_penalty=0.8,
|
| 36 |
-
no_repeat_ngram_size = 3,
|
| 37 |
-
penalty_alpha = 0.8,
|
| 38 |
-
#early_stopping = True,
|
| 39 |
top_k = 15,
|
|
|
|
| 40 |
#top_p = 0.60,
|
| 41 |
)
|
| 42 |
|
|
@@ -44,24 +54,78 @@ def title_gen(keywords):
|
|
| 44 |
|
| 45 |
for i in range(len(beam_outputs)):
|
| 46 |
result = tokenizer.decode(beam_outputs[i], skip_special_tokens=True)
|
| 47 |
-
titles += f"<
|
| 48 |
|
| 49 |
return titles
|
| 50 |
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
iface = gr.Interface(fn=title_gen,
|
| 54 |
-
inputs=[gr.Textbox(label="Paste
|
| 55 |
-
outputs=[gr.HTML(label="
|
| 56 |
title="AI Keywords to Title Generator",
|
| 57 |
-
description="Turn keywords into creative suggestions",
|
| 58 |
-
article="<div align=left><h1>AI Creative Title Generator</h1><li>With just keywords, generate a list of creative titles.</li><li>Click on Submit to generate more
|
| 59 |
flagging_mode='never'
|
| 60 |
)
|
| 61 |
|
| 62 |
iface.launch()
|
| 63 |
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
|
|
|
| 5 |
# - https://huggingface.co/EnglishVoice/t5-base-keywords-to-headline?text=diabetic+diet+plan
|
| 6 |
# - Apache 2.0
|
| 7 |
|
| 8 |
+
# In[7]:
|
| 9 |
+
|
| 10 |
|
| 11 |
import torch
|
| 12 |
from transformers import T5ForConditionalGeneration,T5Tokenizer
|
|
|
|
|
|
|
| 13 |
|
| 14 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 15 |
|
|
|
|
| 17 |
tokenizer = T5Tokenizer.from_pretrained("EnglishVoice/t5-base-keywords-to-headline", clean_up_tokenization_spaces=True, legacy=False)
|
| 18 |
model = model.to(device)
|
| 19 |
|
|
|
|
| 20 |
|
| 21 |
+
|
| 22 |
+
# In[37]:
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def title_gen(keywords, diversity, temp):
|
| 26 |
+
|
| 27 |
+
if keywords!= "":
|
| 28 |
text = "headline: " + keywords
|
| 29 |
encoding = tokenizer.encode_plus(text, return_tensors = "pt")
|
| 30 |
input_ids = encoding["input_ids"].to(device)
|
| 31 |
attention_masks = encoding["attention_mask"].to(device)
|
| 32 |
+
|
| 33 |
+
if diversity:
|
| 34 |
+
num_beams = 20,
|
| 35 |
+
num_beam_groups = 20,
|
| 36 |
+
diversity_penalty=0.8,
|
| 37 |
+
early_stopping = True,
|
| 38 |
+
else:
|
| 39 |
+
penalty_alpha = 0.8,
|
| 40 |
+
|
| 41 |
beam_outputs = model.generate(
|
| 42 |
input_ids = input_ids,
|
| 43 |
attention_mask = attention_masks,
|
| 44 |
max_new_tokens = 30,
|
| 45 |
do_sample = True,
|
| 46 |
num_return_sequences = 5,
|
| 47 |
+
temperature = temp,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
top_k = 15,
|
| 49 |
+
no_repeat_ngram_size = 3,
|
| 50 |
#top_p = 0.60,
|
| 51 |
)
|
| 52 |
|
|
|
|
| 54 |
|
| 55 |
for i in range(len(beam_outputs)):
|
| 56 |
result = tokenizer.decode(beam_outputs[i], skip_special_tokens=True)
|
| 57 |
+
titles += f"<p align=center><b>{result}</b></p>" #Create string with titles and <br> tag for html reading in gradio html
|
| 58 |
|
| 59 |
return titles
|
| 60 |
|
| 61 |
|
| 62 |
+
# In[8]:
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
import gradio as gr
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
# In[40]:
|
| 69 |
+
|
| 70 |
|
| 71 |
iface = gr.Interface(fn=title_gen,
|
| 72 |
+
inputs=[gr.Textbox(label="Paste one or more keywords searated by a comma and hit 'Submit'.", lines=1), "checkbox", gr.Slider(0.1, 1.9, 1.2)],
|
| 73 |
+
outputs=[gr.HTML(label="Title suggestions:")],
|
| 74 |
title="AI Keywords to Title Generator",
|
| 75 |
+
#description="Turn keywords into creative suggestions",
|
| 76 |
+
article="<div align=left><h1>AI Creative Title Generator</h1><li>With just keywords, generate a list of creative titles.</li><li>Click on Submit to generate more title options.</li><li>Tweak slider for less or more creative titles</li><li>Check 'diversity' to turn on diversity beam search</li><p>AI Model:<br><li>T5 Model trained on a dataset of titles and related keywords</li><li>Original model id: EnglishVoice/t5-base-keywords-to-headline by English Voice AI Labs</li></p><p>Default parameter details:<br><li>Temperature = 1.2, no_repeat_ngram_size=3, top_k = 15, penalty_alpha = 0.8, max_new_tokens = 30</li><p>Diversity beam search params:<br><li>num_beams=20, diversity_penalty=0.8, num_beam_groups=20</li></div>",
|
| 77 |
flagging_mode='never'
|
| 78 |
)
|
| 79 |
|
| 80 |
iface.launch()
|
| 81 |
|
| 82 |
|
| 83 |
+
# In[ ]:
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
'''
|
| 87 |
+
#Create a four button panel for changing parameters with one click
|
| 88 |
+
|
| 89 |
+
def fn(text):
|
| 90 |
+
return ("Hello gradio!")
|
| 91 |
+
|
| 92 |
+
with gr.Blocks () as demo:
|
| 93 |
+
|
| 94 |
+
with gr.Row(variant='compact') as PanelRow1: #first row: top
|
| 95 |
+
|
| 96 |
+
with gr.Column(scale=0, min_width=180) as PanelCol5:
|
| 97 |
+
gr.HTML("")
|
| 98 |
+
with gr.Column(scale=0) as PanelCol4:
|
| 99 |
+
submit = gr.Button("Temp++", scale=0)
|
| 100 |
+
with gr.Column(scale=1) as PanelCol5:
|
| 101 |
+
gr.HTML("")
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
with gr.Row(variant='compact') as PanelRow2: #2nd row: left, right, middle
|
| 105 |
+
|
| 106 |
+
with gr.Column(min_width=100) as PanelCol1:
|
| 107 |
+
submit = gr.Button("Contrastive")
|
| 108 |
+
with gr.Column(min_width=100) as PanelCol2:
|
| 109 |
+
submit = gr.Button("Re-generate")
|
| 110 |
+
with gr.Column(min_width=100) as PanelCol3:
|
| 111 |
+
submit = gr.Button("Diversity Beam")
|
| 112 |
+
|
| 113 |
+
with gr.Column(min_width=100) as PanelCol5:
|
| 114 |
+
gr.HTML("")
|
| 115 |
+
with gr.Column(min_width=100) as PanelCol5:
|
| 116 |
+
gr.HTML("")
|
| 117 |
+
with gr.Column(scale=0) as PanelCol5:
|
| 118 |
+
gr.HTML("")
|
| 119 |
+
|
| 120 |
+
with gr.Row(variant='compact') as PanelRow3: #last row: down
|
| 121 |
+
with gr.Column(scale=0, min_width=180) as PanelCol7:
|
| 122 |
+
gr.HTML("")
|
| 123 |
+
with gr.Column(scale=1) as PanelCol6:
|
| 124 |
+
submit = gr.Button("Temp--", scale=0)
|
| 125 |
+
|
| 126 |
+
with gr.Column(scale=0) as PanelCol5:
|
| 127 |
+
gr.HTML("")
|
| 128 |
+
|
| 129 |
+
demo.launch()
|
| 130 |
+
'''
|
| 131 |
|