awacke1 commited on
Commit
d90fa0f
·
1 Parent(s): 0d8667e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +118 -169
app.py CHANGED
@@ -1,181 +1,130 @@
1
- import googlemaps
2
- import os
3
- #GM_TOKEN=os.environ.get("GM_TOKEN") # Get Google Maps Token Here: https://console.cloud.google.com/google/maps-apis/
4
-
5
- from datetime import datetime
6
-
7
- gmaps = googlemaps.Client(key='AIzaSyDybq2mxujekZVivmr03Y5-GGHXesn4TLI')
8
-
9
-
10
- def GetMapInfo(inputText):
11
- geocode_result = gmaps.geocode('640 Jackson Street, St. Paul, MN 55101')
12
- geo_address = geocode_result[0]['formatted_address']
13
- geo_directions = geocode_result[0]['geometry']['location']
14
- geo_geocode = geocode_result[0]['geometry']['location_type']
15
-
16
- lat = geo_directions['lat']
17
- lng = geo_directions['lng']
18
-
19
- reverse_geocode_result = gmaps.reverse_geocode((lat, lng))
20
-
21
- now = datetime.now()
22
- directions_result = gmaps.directions("Sydney Town Hall","Parramatta, NSW",mode="transit", departure_time=now)
23
- #addressvalidation_result = gmaps.addressvalidation(['1600 Amphitheatre Pk'], regionCode='US', locality='Mountain View', enableUspsCass=True)
24
-
25
- #return geocode_result, reverse_geocode_result, directions_result, addressvalidation_result
26
- #return geo_address, geo_directions, geo_geocode, reverse_geocode_result, directions_result, addressvalidation_result
27
- return geo_address, geo_directions, geo_geocode
28
-
29
- from transformers import BlenderbotTokenizer, BlenderbotForConditionalGeneration
30
- import torch
31
  import gradio as gr
32
- from datasets import load_dataset
33
-
34
- # PersistDataset -----
35
- import os
36
- import csv
37
- from gradio import inputs, outputs
38
- import huggingface_hub
39
- from huggingface_hub import Repository, hf_hub_download, upload_file
40
- from datetime import datetime
41
-
42
- #fastapi is where its at: share your app, share your api
43
- import fastapi
44
-
45
- from typing import List, Dict
46
- import httpx
47
  import pandas as pd
48
- import datasets as ds
49
-
50
- UseMemory=True
51
- HF_TOKEN=os.environ.get("HF_TOKEN")
52
-
53
- def SaveResult(text, outputfileName):
54
- basedir = os.path.dirname(__file__)
55
- savePath = outputfileName
56
- print("Saving: " + text + " to " + savePath)
57
- from os.path import exists
58
- file_exists = exists(savePath)
59
- if file_exists:
60
- with open(outputfileName, "a") as f: #append
61
- f.write(str(text.replace("\n"," ")))
62
- f.write('\n')
63
- else:
64
- with open(outputfileName, "w") as f: #write
65
- f.write(str("time, message, text\n")) # one time only to get column headers for CSV file
66
- f.write(str(text.replace("\n"," ")))
67
- f.write('\n')
68
- return
69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
- def store_message(name: str, message: str, outputfileName: str):
72
- basedir = os.path.dirname(__file__)
73
- savePath = outputfileName
74
-
75
- # if file doesnt exist, create it with labels
76
- from os.path import exists
77
- file_exists = exists(savePath)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
 
79
- if (file_exists==False):
80
- with open(savePath, "w") as f: #write
81
- f.write(str("time, message, text\n")) # one time only to get column headers for CSV file
82
- if name and message:
83
- writer = csv.DictWriter(f, fieldnames=["time", "message", "name"])
84
- writer.writerow(
85
- {"time": str(datetime.now()), "message": message.strip(), "name": name.strip() }
86
- )
87
- df = pd.read_csv(savePath)
88
- df = df.sort_values(df.columns[0],ascending=False)
89
- else:
90
- if name and message:
91
- with open(savePath, "a") as csvfile:
92
- writer = csv.DictWriter(csvfile, fieldnames=[ "time", "message", "name", ])
93
- writer.writerow(
94
- {"time": str(datetime.now()), "message": message.strip(), "name": name.strip() }
95
- )
96
- df = pd.read_csv(savePath)
97
- df = df.sort_values(df.columns[0],ascending=False)
98
- return df
99
-
100
- mname = "facebook/blenderbot-400M-distill"
101
- model = BlenderbotForConditionalGeneration.from_pretrained(mname)
102
- tokenizer = BlenderbotTokenizer.from_pretrained(mname)
103
-
104
- def take_last_tokens(inputs, note_history, history):
105
- if inputs['input_ids'].shape[1] > 128:
106
- inputs['input_ids'] = torch.tensor([inputs['input_ids'][0][-128:].tolist()])
107
- inputs['attention_mask'] = torch.tensor([inputs['attention_mask'][0][-128:].tolist()])
108
- note_history = ['</s> <s>'.join(note_history[0].split('</s> <s>')[2:])]
109
- history = history[1:]
110
- return inputs, note_history, history
111
 
112
- def add_note_to_history(note, note_history):# good example of non async since we wait around til we know it went okay.
113
- note_history.append(note)
114
- note_history = '</s> <s>'.join(note_history)
115
- return [note_history]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
 
117
- title = "💬ChatBack🧠💾"
118
- description = """Chatbot With persistent memory dataset allowing multiagent system AI to access a shared dataset as memory pool with stored interactions.
119
- Current Best SOTA Chatbot: https://huggingface.co/facebook/blenderbot-400M-distill?text=Hey+my+name+is+ChatBack%21+Are+you+ready+to+rock%3F """
120
 
121
- def get_base(filename):
122
- basedir = os.path.dirname(__file__)
123
- print(basedir)
124
- #loadPath = basedir + "\\" + filename # works on windows
125
- loadPath = basedir + filename # works on ubuntu
126
- print(loadPath)
127
- return loadPath
128
-
129
- def chat(message, history):
130
- history = history or []
131
- if history:
132
- history_useful = ['</s> <s>'.join([str(a[0])+'</s> <s>'+str(a[1]) for a in history])]
133
- else:
134
- history_useful = []
135
 
136
- history_useful = add_note_to_history(message, history_useful)
137
- inputs = tokenizer(history_useful, return_tensors="pt")
138
- inputs, history_useful, history = take_last_tokens(inputs, history_useful, history)
139
- reply_ids = model.generate(**inputs)
140
- response = tokenizer.batch_decode(reply_ids, skip_special_tokens=True)[0]
141
- history_useful = add_note_to_history(response, history_useful)
142
- list_history = history_useful[0].split('</s> <s>')
143
- history.append((list_history[-2], list_history[-1]))
144
-
145
- df=pd.DataFrame()
 
 
 
 
 
 
 
 
 
 
 
 
 
146
 
147
- if UseMemory:
148
- #outputfileName = 'ChatbotMemory.csv'
149
- outputfileName = 'ChatbotMemory3.csv' # Test first time file create
150
- df = store_message(message, response, outputfileName) # Save to dataset
151
- basedir = get_base(outputfileName)
 
 
152
 
153
- return history, df, basedir
154
-
155
-
156
-
157
 
158
- with gr.Blocks() as demo:
159
- gr.Markdown("<h1><center>🍰 AI Google Maps Demonstration🎨</center></h1>")
160
-
161
- with gr.Row():
162
- t1 = gr.Textbox(lines=1, default="", label="Chat Text:")
163
- b1 = gr.Button("Respond and Retrieve Messages")
164
- b2 = gr.Button("Get Map Information")
165
-
166
- with gr.Row(): # inputs and buttons
167
- s1 = gr.State([])
168
- df1 = gr.Dataframe(wrap=True, max_rows=1000, overflow_row_behaviour= "paginate")
169
- with gr.Row(): # inputs and buttons
170
- file = gr.File(label="File")
171
- s2 = gr.Markdown()
172
- with gr.Row():
173
- df21 = gr.Textbox(lines=4, default="", label="Geocode1:")
174
- df22 = gr.Textbox(lines=4, default="", label="Geocode2:")
175
- df23 = gr.Textbox(lines=4, default="", label="Geocode3:")
176
- df3 = gr.Dataframe(wrap=True, max_rows=1000, overflow_row_behaviour= "paginate")
177
- df4 = gr.Dataframe(wrap=True, max_rows=1000, overflow_row_behaviour= "paginate")
178
- b1.click(fn=chat, inputs=[t1, s1], outputs=[s1, df1, file])
179
- b2.click(fn=GetMapInfo, inputs=[t1], outputs=[df21, df22, df23])
180
-
181
- demo.launch(debug=True, show_error=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  import pandas as pd
3
+ import plotly.graph_objects as go
4
+ from datasets import load_dataset
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
+ dataset = load_dataset('text', data_files={'train': ['NPI_2023_01_17-05.10.57.PM.csv'], 'test': 'NPI_2023_01_17-05.10.57.PM.csv'})
7
+ #1.6GB NPI file with MH therapy taxonomy provider codes (NUCC based) with human friendly replacement labels (e.g. Counselor rather than code)
8
+ datasetNYC = load_dataset("gradio/NYC-Airbnb-Open-Data", split="train")
9
+ df = datasetNYC.to_pandas()
10
+
11
+ def MatchText(pddf, name):
12
+ pd.set_option("display.max_rows", None)
13
+ data = pddf
14
+ swith=data.loc[data['text'].str.contains(name, case=False, na=False)]
15
+ return swith
16
+
17
+ def getDatasetFind(findString):
18
+ #finder = dataset.filter(lambda example: example['text'].find(findString))
19
+ finder = dataset['train'].filter(lambda example: example['text'].find(findString))
20
+ finder = finder = finder.to_pandas()
21
+ g1=MatchText(finder, findString)
22
+ return g1
23
+
24
+ def filter_map(min_price, max_price, boroughs):
25
+ filtered_df = df[(df['neighbourhood_group'].isin(boroughs)) & (df['price'] > min_price) & (df['price'] < max_price)]
26
+ names = filtered_df["name"].tolist()
27
+ prices = filtered_df["price"].tolist()
28
+ text_list = [(names[i], prices[i]) for i in range(0, len(names))]
29
 
30
+ fig = go.Figure(go.Scattermapbox(
31
+ customdata=text_list,
32
+ lat=filtered_df['latitude'].tolist(),
33
+ lon=filtered_df['longitude'].tolist(),
34
+ mode='markers',
35
+ marker=go.scattermapbox.Marker(
36
+ size=6
37
+ ),
38
+ hoverinfo="text",
39
+ hovertemplate='Name: %{customdata[0]}Price: $%{customdata[1]}'
40
+ ))
41
+
42
+ fig.update_layout(
43
+ mapbox_style="open-street-map",
44
+ hovermode='closest',
45
+ mapbox=dict(
46
+ bearing=0,
47
+ center=go.layout.mapbox.Center(
48
+ lat=40.67,
49
+ lon=-73.90
50
+ ),
51
+ pitch=0,
52
+ zoom=9
53
+ ),
54
+ )
55
+ return fig
56
+
57
+ def centerMap(min_price, max_price, boroughs):
58
+ filtered_df = df[(df['neighbourhood_group'].isin(boroughs)) & (df['price'] > min_price) & (df['price'] < max_price)]
59
+ names = filtered_df["name"].tolist()
60
+ prices = filtered_df["price"].tolist()
61
+ text_list = [(names[i], prices[i]) for i in range(0, len(names))]
62
 
63
+ latitude = 44.9382
64
+ longitude = -93.6561
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
 
66
+ fig = go.Figure(go.Scattermapbox(
67
+ customdata=text_list,
68
+ lat=filtered_df['latitude'].tolist(),
69
+ lon=filtered_df['longitude'].tolist(), mode='markers',
70
+ marker=go.scattermapbox.Marker(
71
+ size=6
72
+ ),
73
+ hoverinfo="text",
74
+ #hovertemplate='Lat: %{lat} Long:%{lng} City: %{cityNm}'
75
+ ))
76
+
77
+ fig.update_layout(
78
+ mapbox_style="open-street-map",
79
+ hovermode='closest',
80
+ mapbox=dict(
81
+ bearing=0,
82
+ center=go.layout.mapbox.Center(
83
+ lat=latitude,
84
+ lon=longitude
85
+ ),
86
+ pitch=0,
87
+ zoom=9
88
+ ),
89
+ )
90
+ return fig
91
 
 
 
 
92
 
93
+ with gr.Blocks() as demo:
94
+ with gr.Column():
 
 
 
 
 
 
 
 
 
 
 
 
95
 
96
+ # Price/Boroughs/Map/Filter for AirBnB
97
+ with gr.Row():
98
+ min_price = gr.Number(value=250, label="Minimum Price")
99
+ max_price = gr.Number(value=1000, label="Maximum Price")
100
+ boroughs = gr.CheckboxGroup(choices=["Queens", "Brooklyn", "Manhattan", "Bronx", "Staten Island"], value=["Queens", "Brooklyn"], label="Select Boroughs:")
101
+ btn = gr.Button(value="Update Filter")
102
+ map = gr.Plot().style()
103
+
104
+ # Mental Health Provider Finder
105
+ with gr.Row():
106
+ df20 = gr.Textbox(lines=4, default="", label="Find Mental Health Provider e.g. City/State/Name/License:")
107
+ btn2 = gr.Button(value="Find")
108
+ with gr.Row():
109
+ df4 = gr.Dataframe(wrap=True, max_rows=10000, overflow_row_behaviour= "paginate")
110
+
111
+ # City Map
112
+ with gr.Row():
113
+ df2 = gr.Textbox(lines=1, default="Mound", label="Find City:")
114
+ latitudeUI = gr.Textbox(lines=1, default="44.9382", label="Latitude:")
115
+ longitudeUI = gr.Textbox(lines=1, default="-93.6561", label="Longitude:")
116
+ btn3 = gr.Button(value="Lat-Long")
117
+
118
+ demo.load(filter_map, [min_price, max_price, boroughs], map)
119
 
120
+ btn.click(filter_map, [min_price, max_price, boroughs], map)
121
+ btn2.click(getDatasetFind,df20,df4 )
122
+ # Lookup on US once you have city to get lat/long
123
+ # US 55364 Mound Minnesota MN Hennepin 053 44.9382 -93.6561 4
124
+ #latitude = 44.9382
125
+ #longitude = -93.6561
126
+ #btn3.click(centerMap, map)
127
 
128
+ btn3.click(centerMap, [min_price, max_price, boroughs], map)
 
 
 
129
 
130
+ demo.launch()