File size: 10,358 Bytes
773061f ee55c42 773061f 622813c 57eebfd 773061f |
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 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
import openai
import json
import os
# os.system('pip install gradio==2.8.12')
import gradio as gr
openai.api_key = os.environ.get('OPENAI')
def callAPI(input_prompt, engine_type="text-davinciplus-001", temp=0.2):
try:
output = openai.Completion.create(
engine=engine_type,
prompt=input_prompt,
max_tokens=250,
temperature=temp,
n=1,
stop="\n---"
)
output = json.dumps(output)
output = json.loads(output)['choices'][0]
for ind in output:
if (ind == 'text'):
output = output[ind]
return output
else:
print('Error: failed to find text in output')
raise RuntimeError
except:
print('Error: failed to make successful OpenAI API call')
print(output)
raise RuntimeError
def makeValues(text_array):
print(text_array)
try:
output = {}
for text in text_array:
if text == '':
pass
else:
try:
split_text = text.split(": ", 1)
if split_text[1] == "none" or split_text[1] == "None" or split_text[1] == '':
output[str(split_text[0])] = ""
else:
output[str(split_text[0])] = split_text[1].strip()
except:
split_text = text.split(":", 1)
if split_text[1] == "none" or split_text[1] == "None" or split_text[1] == '':
output[str(split_text[0])] = ""
else:
output[str(split_text[0])] = split_text[1].strip()
return output
except:
print("error converting array values to dict")
print(output)
print(text)
raise RuntimeError
def company(model, input=None):
if not input:
return "Error: Please supply a company's name"
else:
company_text="Fill in the company facts table truthfully. If unsure list none.\n---\nFord\nstock_symbol: F\nstock_market: NYSE\nceo: Jim Farley\nheadquarters: Dearborn, MI\nindustry: automotive, manufacturing, electric vehicle\nfounded_date: 1903\nfounders: Henry Ford\nlegal_name: Ford Motor Company\ncompany_website: https://ford.com\nsummary: Ford Motor Company is an American multinational automobile manufacturer headquartered in Dearborn, Michigan, United States.\ncompetitors: Chevrolet, Toyota, Honda\nwikipedia_url: https://en.wikipedia.org/wiki/Ford_Motor_Company\ncrunchbase_url: https://www.crunchbase.com/organization/ford\ninstagram_url: https://www.instagram.com/ford\ntwitter_url: https://twitter.com/Ford\nyoutube_url: https://www.youtube.com/user/ford\n---\n"
loaded_prompt=company_text+input
engine_used=model
result=callAPI(loaded_prompt, engine_used, 0.2)
result = result.split('\n')
formatted_result=makeValues(result)
try:
final={}
final['triples']={}
final['metadata']={}
final['subject']=input
final['type']='company'
final['triples']['stock_symbol'] = formatted_result['stock_symbol']
final['triples']['stock_market'] = formatted_result['stock_market']
final['triples']['ceo'] = formatted_result['ceo']
final['triples']['industry'] = formatted_result['industry'].split(',')
final['triples']['headquarters'] = formatted_result['headquarters']
final['triples']['founded_date'] = formatted_result['founded_date']
final['triples']['founders'] = formatted_result['founders'].split(',')
final['triples']['legal_name'] = formatted_result['legal_name']
final['triples']['company_website'] = formatted_result['company_website']
final['triples']['wikipedia_url'] = formatted_result['wikipedia_url']
final['triples']['summary'] = formatted_result['summary']
final['metadata']['producer']='MIDAS v0.01'
final['metadata']['nlp_model']=engine_used
print(final)
#json_result = json.dumps(final)
except:
print('Error: could not form JSON from key:values')
print(formatted_result)
return '{"Error": "could not form JSON from key:values"}'
return final
def person(model, input=None):
if not input:
return "Error: Please supply a company's name"
else:
person_text="Fill in the persons fact table truthfully. If unsure list none.\n---\nJim Farley\ncommon_name: Jim Farley\nborn: 1956\nlegal_name: Jim Farley\nsummary: Jim Farley is the CEO of Ford Motor Company.\nwikipedia_url: https://en.wikipedia.org/wiki/Jim_Farley\ninstagram_url:\ntwitter_url:\nyoutube_url:\n---\n"
loaded_prompt=person_text+input
engine_used=model
result=callAPI(loaded_prompt, engine_used, 0)
result = result.split('\n')
formatted_result=makeValues(result)
try:
final={}
final['triples']={}
final['metadata']={}
final['subject']=input
final['type']='person'
final['triples']['common_name'] = formatted_result['common_name']
final['triples']['born'] = formatted_result['born']
final['triples']['legal_name'] = formatted_result['legal_name']
final['triples']['instagram_url'] = formatted_result['instagram_url']
final['triples']['twitter_url'] = formatted_result['twitter_url']
final['triples']['wikipedia_url'] = formatted_result['wikipedia_url']
final['triples']['summary'] = formatted_result['summary']
final['metadata']['producer']='MIDAS v0.01'
final['metadata']['nlp_model']=engine_used
print(final)
#json_result = json.dumps(final)
except:
print('Error: could not form JSON from key:values')
print(formatted_result)
return '{"Error": "could not form JSON from key:values"}'
return final
model_options = {
"Curie Instruct (OpenAI)": "text-curie-001",
"GPT-J": "gpt-j",
}
def start(types=None, text=None):
model = "Curie Instruct (OpenAI)"
if not text or not types or not model:
return "Error: Please supply an input"
else:
if model_options[model] == 'gpt-j':
return "GPT-J coming soon"
else:
if types == "Company":
return company(model_options[model], text)
if types == "Person":
return person(model_options[model], text)
custom_css = '''
@import url('https://use.typekit.net/kmj7hxn.css');
.gradio-bg {
font-family: 'Roc Grotesk', sans-serif;
}
.gradio-bg .gradio-page {
display: flex;
width: 100vw;
min-height: 50vh;
flex-direction: column;
justify-content: center;
align-items: center;
margin: 0px;
max-width: 100vw;
background: transparent;
}
.gradio-bg .gradio-page .content {
padding: 0px;
margin: 0px;
}
.gradio-interface {
width: 100vw;
max-width: 1400px;
}
.gradio-interface .panel:nth-child(2) .component:nth-child(3) {
display:none
}
.gradio-interface[theme=default] .panel-header {
letter-spacing: 0.1em;
font-weight: 500;
margin-left: 5px;
margin-bottom: 4px;
}
.gradio-bg .panel-buttons {
justify-content: flex-end;
background: #D8CBFE;
margin: 0;
border: none;
padding: 15px;
padding-top: 0px;
border-radius: 0px 0px 15px 15px;
}
.panel-button:nth-child(1){
display:none;
}
.gradio-bg .panel-button {
flex: 0 0 0;
min-width: 150px;
}
.gradio-bg .gradio-interface .panel-button.submit {
background: #7131FA;
border-radius: 50px;
color: #FFFFFF;
font-size: 18px;
min-width: 150px;
letter-spacing: 0.06em;
line-height: 100%;
flex: 0 0 0;
transition: all ease-in-out 240ms;
}
.gradio-bg .gradio-interface .panel-button.submit:hover {
background-color: #9f72ff;
box-shadow: 1px 1px 15px rgba(0, 0, 0, 0.25);
}
.input_text:focus {
border-color: #FA7880;
}
.input-text[theme=default] input,
.input-text[theme=default] textarea {
line-height: 110%;
color: #7131FA;
border-radius: 5px;
padding: 15px;
border: none;
background: #FFFFFF;
}
.input-text[theme=default] {
font-weight: 500;
font-size: 28px;
padding: 17px;
border-radius: 8px;
}
.input-text textarea:focus-visible {
outline: none;
}
.input-dropdown[theme=default] .selector {
font-weight: 500;
background: rgba(255,255,255,0.5);
padding: 7px 12px 7px 12px;
}
.input-dropdown[theme=default] .dropdown-item {
font-weight: 500;
}
.input-dropdown[theme=default] .dropdown-item:hover {
background: #FFFFFF;
color: #7131FA;
font-weight: 500;
}
.gradio-bg .gradio-interface .input-radio .radio-item.selected {
background-color: #7131FA;
}
.gradio-bg .gradio-interface .input-radio .selected .radio-circle {
border-color: #4365c4;
}
.gradio-bg .gradio-interface .output-json {
background: #333;
padding: 25px;
border-radius: 10px;
font-size: 16px;
color: #eee;
}
.text-green-500 {
color: #cabdff;
}
.panel:nth-child(1) {
margin-left: 50px;
margin-right: 10px;
margin-bottom: 80px;
max-width: 575px;
}
.panel {
background: transparent;
}
.gradio-bg .gradio-interface[theme=default] .component-set {
background: #D8CBFE;
border: none;
box-shadow: none;
border-radius: 15px 15px 0px 0px;
padding: 20px;
padding-bottom: 12px;
}
.panel:nth-child(2) .gradio-interface[theme=default] .panel-header {
display: none;
}
.labels {
height: 20px;
width: auto;
}
@media (max-width: 1000px){
.panel:nth-child(1) {
margin-left: 0px;
margin-right: 0px;
}
.gradio-bg .gradio-interface .output-json {
height: auto;
}
}
.footer {
display: none !important;
}
'''
iface = gr.Interface(
fn=start,
inputs=[
gr.inputs.Dropdown(["Company", "Person"], label="Entity Type"),
gr.inputs.Textbox(lines=1, label="Subject")
],
outputs=[
gr.outputs.JSON(label="JSON Triples")
],
css=custom_css,
theme="default",
allow_flagging='never',
allow_screenshot=False,
)
iface.launch(enable_queue=True) |