Spaces:
Sleeping
Sleeping
umm-maybe
commited on
Commit
·
7eaefa4
1
Parent(s):
05140a3
Revisions to app
Browse files
app.py
CHANGED
|
@@ -1,3 +1,82 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
gr.Interface.load("models/manhan/GPT-Tour").launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import json
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
# Load the pipeline
|
| 5 |
+
tourModel = pipeline(model="manhan/GPT-Tour", device=0)
|
| 6 |
+
|
| 7 |
+
def getTour(hh_income,hh_size,age,sex,edu,wrk):
|
| 8 |
+
# person = a dict with person-level and hh-level attributes:
|
| 9 |
+
person = {}
|
| 10 |
+
if hh_income<0:
|
| 11 |
+
_skip = 1 # nvm, bad data
|
| 12 |
+
elif hh_income<4: # $25,000
|
| 13 |
+
person['hh_inc'] = 'poor'
|
| 14 |
+
elif hh_income<6: # $50,000
|
| 15 |
+
person['hh_inc'] = 'low'
|
| 16 |
+
elif hh_income<7: # $75,000
|
| 17 |
+
person['hh_inc'] = 'medium'
|
| 18 |
+
elif hh_income<9: # $125,000
|
| 19 |
+
person['hh_inc'] = 'high'
|
| 20 |
+
else: # over
|
| 21 |
+
person['hh_inc'] = 'affluent'
|
| 22 |
+
if hh_size == 1:
|
| 23 |
+
person['hh_size'] = 'single'
|
| 24 |
+
elif hh_size == 2:
|
| 25 |
+
person['hh_size'] = 'couple'
|
| 26 |
+
elif hh_size <= 4:
|
| 27 |
+
person['hh_size'] = 'small'
|
| 28 |
+
else: # more than four people
|
| 29 |
+
person['hh_size'] = 'large'
|
| 30 |
+
if age < 18:
|
| 31 |
+
person['age_grp'] = 'child'
|
| 32 |
+
elif age < 45:
|
| 33 |
+
person['age_grp'] = 'younger'
|
| 34 |
+
elif age < 65:
|
| 35 |
+
person['age_grp'] = 'older'
|
| 36 |
+
else:
|
| 37 |
+
person['age_grp'] = 'senior'
|
| 38 |
+
person['sex'] = sex
|
| 39 |
+
person['edu'] = edu
|
| 40 |
+
person['wrk'] = wrk
|
| 41 |
+
activity_list = []
|
| 42 |
+
prompt = json.dumps(person)[:-1] + ", pattern: "
|
| 43 |
+
print(person)
|
| 44 |
+
while not activity_list:
|
| 45 |
+
generated = tourModel(prompt, return_full_text=False, max_length=250, temperature=0.9)[0]['generated_text']
|
| 46 |
+
#print(f"{generated}")
|
| 47 |
+
start_pos = generated.find('[')
|
| 48 |
+
end_pos = generated.find(']')+1
|
| 49 |
+
activity_list_str = generated[start_pos:end_pos]
|
| 50 |
+
print(f"Extracted: '{activity_list_str}'")
|
| 51 |
+
# this check doesn't appear to work anyways
|
| 52 |
+
if person['wrk']=='yes' and activity_list_str.find('Work')==-1:
|
| 53 |
+
continue # try again
|
| 54 |
+
if person['wrk']=='no' and activity_list_str.find('Work')>0:
|
| 55 |
+
continue # try again
|
| 56 |
+
if activity_list_str:
|
| 57 |
+
try:
|
| 58 |
+
activity_list = json.loads(activity_list_str)
|
| 59 |
+
if activity_list[-1]!='Home':
|
| 60 |
+
activity_list=[]
|
| 61 |
+
continue
|
| 62 |
+
break
|
| 63 |
+
except Exception as e:
|
| 64 |
+
print("Error parsing activity list")
|
| 65 |
+
print(e)
|
| 66 |
+
else:
|
| 67 |
+
print("Nothing extracted!")
|
| 68 |
+
return activity_list
|
| 69 |
+
|
| 70 |
+
with gr.Interface(fn=getTour, inputs="inputs", outputs="json", title="GPT-Tour", description="Generate a tour for a person", allow_flagging=False, allow_screenshot=False, allow_embedding=False) as iface:
|
| 71 |
+
iface.launch(share=True)
|
| 72 |
+
|
| 73 |
+
inputs = [
|
| 74 |
+
gr.inputs.Textbox(label="Annual Household Income (in dollars)"),
|
| 75 |
+
gr.inputs.Textbox(label="Household Size (number of people)"),
|
| 76 |
+
gr.inputs.Textbox(label="Traveler Age (years)"),
|
| 77 |
+
gr.inputs.Dropdown(["unknown", "male", "female"], label="Gender/sex"),
|
| 78 |
+
gr.inputs.Dropdown(["unknown", "grade school","highschool", "associates", "bachelors", "graduate"], label="Educational attainment level"),
|
| 79 |
+
gr.inputs.Dropdown(["unknown", "yes","no"], label="Worker status")
|
| 80 |
+
]
|
| 81 |
|
| 82 |
gr.Interface.load("models/manhan/GPT-Tour").launch()
|