|
import openai |
|
import json |
|
import os |
|
|
|
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) |
|
|
|
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) |
|
|
|
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) |