File size: 7,563 Bytes
c073aa2 b297061 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
#!/usr/bin/env python
# coding: utf-8
# In[10]:
import pandas as pd
import os
import torch
from transformers import T5Tokenizer, T5ForConditionalGeneration
from transformers.optimization import Adafactor
import time
import warnings
import random
warnings.filterwarnings('ignore')
import re
def strip_html(text):
return re.sub('<[^<]+?>', '', text)
# In[5]:
train_columns = ['round_amount', 'round_date', 'stage', 'investee',
'investee_description', 'investee_country', 'investee_region',
'investee_subregion', 'investee_vertical', 'investee_industry',
'investor_list', 'previous_investors', 'prior_funding']
train = pd.read_csv("train.csv")
# In[6]:
train.publication_timestamp = pd.to_datetime(train.publication_timestamp)
# In[7]:
input_text = train[train_columns].to_dict(orient='records')
train_df = train[['title']].rename(columns={'title':'target_text'})
train_df['input_text'] = input_text
train_df['prefix'] = 'tia'
train_df.input_text = train_df.input_text.astype(str)
# In[8]:
if torch.cuda.is_available():
dev = torch.device("cuda:0")
print("Running on the GPU")
else:
dev = torch.device("cpu")
print("Running on the CPU")
# In[ ]:
tokenizer = T5Tokenizer.from_pretrained('google/t5-v1_1-base')
model = T5ForConditionalGeneration.from_pretrained('t5-v1_1-base_tia/', local_files_only=True)
#moving the model to device(GPU/CPU)
model.to(dev)
# In[12]:
vi_table = train[['investee_industry', 'investee_vertical']].drop_duplicates()
# In[13]:
def update_industry(value):
verticals = list(vi_table[vi_table['investee_industry'] == value]['investee_vertical'].values)
return verticals[0]
def update_vertical(value):
industries = list(vi_table[vi_table['investee_vertical'] == value]['investee_industry'].values)
return industries[0]
# In[ ]:
update_industry('Green')
# In[ ]:
update_vertical('Clean tech')
# In[ ]:
import gradio as gr
# In[ ]:
num_return_sequences = 5
# In[ ]:
def generate_headline(stage, investee_country, investee_subregion, investee_region,
investee_vertical, investee_industry,
round_amount, investee, investee_description, investor_list, previous_investors,
other_values):
full_df = other_values.set_index("key").T
full_df['stage'] = stage
full_df['investee_country'] = investee_country
full_df['investee_subregion'] = investee_subregion
full_df['investee_region'] = investee_region
full_df['investee_vertical'] = investee_vertical
full_df['investee_industry'] = investee_industry
full_df['round_amount'] = str(float(round_amount))
full_df['investee'] = investee
full_df['investee_description'] = investee_description
full_df['investor_list'] = investor_list
full_df['previous_investors'] = previous_investors
random_set =full_df[['round_amount', 'round_date', 'stage', 'investee',
'investee_description', 'investee_country', 'investee_region',
'investee_subregion', 'investee_vertical', 'investee_industry',
'investor_list', 'previous_investors', 'prior_funding']].to_json(orient="records")
# print(random_set)
input_ids = tokenizer.encode(f"tia: {{{random_set}}}", return_tensors="pt") # Batch size 1
input_ids=input_ids.to(dev)
outputs = model.generate(input_ids)
# text_output = tokenizer.decode(outputs[0]) # Single output
text_outputs = model.generate(inputs=input_ids, do_sample=True,
num_beams=2,
num_return_sequences=num_return_sequences,
repetition_penalty=5.0)
outputs = [strip_html(tokenizer.decode(o)) for o in text_outputs]
return "\n".join(outputs)
# In[ ]:
other_columns = ['round_date', 'prior_funding']
# In[ ]:
train.sample(1)[other_columns].T.reset_index().values
# In[ ]:
print(train.query("investee == 'NOSH'")['title'].head(1).T)
train.query("investee == 'NOSH'")[train_columns].head(1).T
# In[ ]:
fake_data = {
"round_amount":1000000.0,
"round_date":"2018-09-26",
"stage":"Pre-series A",
"investee":"NOSH",
"investee_description":"NOSH makes and delivers ready-to-eat meals in Hong Kong.",
"investee_country":"Hong Kong",
"investee_region":"Asia",
"investee_subregion":"Eastern Asia",
"investee_vertical":"Food tech",
"investee_industry":"Restaurants & Food",
"investor_list":["Alibaba Entrepreneurs Fund (阿里巴巴创业者基金)"],
"previous_investors":"",
"prior_funding":1000000.0
}
# In[ ]:
pd.DataFrame([fake_data]).T
# In[ ]:
demo = gr.Blocks()
random_sample = train[train_columns].sample(1)
random_sample = pd.DataFrame([fake_data])
stage = gr.Dropdown(label="stage", choices=list(train[train_columns].stage.unique()))
investee_country = gr.Dropdown(label="investee_country", choices=list(train[train_columns].investee_country.unique()),
value=random_sample.investee_country.values[0])
investee_subregion = gr.Dropdown(label="investee_subregion", choices=list(train[train_columns].investee_subregion.unique()),
value=random_sample.investee_subregion.values[0])
investee_region = gr.Dropdown(label="investee_region", choices=list(train[train_columns].investee_region.unique()),
value=random_sample.investee_region.values[0])
investee_vertical = gr.Dropdown(label="investee_vertical", choices=list(train[train_columns].investee_vertical.unique()),
value=random_sample.investee_vertical.values[0])
investee_industry = gr.Dropdown(label="investee_industry", choices=list(train[train_columns].investee_industry.unique()),
value=random_sample.investee_industry.values[0])
if pd.isnull(random_sample.round_amount.values[0]):
rand_amount = 0
else:
rand_amount = random_sample.round_amount.values[0]
round_amount = gr.Slider(label="round_amount", minimum=100000, maximum=200000000,
value=rand_amount,
step=100000)
investee = gr.Textbox(label="investee", value=random_sample.investee.values[0])
investee_description = gr.Textbox(label="investee_description",
value=random_sample.investee_description.values[0])
investor_list = gr.Textbox(label="investor_list",
value=random_sample.investor_list.values[0])
previous_investors = gr.Textbox(label="previous_investors",
value=random_sample.previous_investors.values[0])
other_values = gr.Dataframe(
headers=['key', 'value'],
value=[['round_date', random_sample.round_date.values[0]],
['prior_funding', random_sample.prior_funding.values[0]]]
)
out = gr.Textbox(max_lines=num_return_sequences)
with demo:
gr.Markdown("Enter funding data to generate news headline.")
inputs=[stage, investee_country, investee_subregion, investee_region,
investee_vertical, investee_industry,
round_amount, investee, investee_description, investor_list, previous_investors,
other_values]
investee_industry.change(fn=update_industry, inputs=investee_industry, outputs=investee_vertical)
investee_vertical.change(fn=update_vertical, inputs=investee_vertical, outputs=investee_industry)
gr.Interface(fn=generate_headline, inputs=inputs, outputs=out, live=True)
description="Enter funding data to generate news headline.",
live=True
demo.launch()
|