Update app.py
Browse files
app.py
CHANGED
|
@@ -1,130 +1,185 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 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 |
-
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 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 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 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 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 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
|
|
|
|
|
|
| 127 |
|
| 128 |
-
|
| 129 |
|
| 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 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
+
if (file_exists==False):
|
| 82 |
+
with open(savePath, "w") as f: #write
|
| 83 |
+
f.write(str("time, message, text\n")) # one time only to get column headers for CSV file
|
| 84 |
+
if name and message:
|
| 85 |
+
writer = csv.DictWriter(f, fieldnames=["time", "message", "name"])
|
| 86 |
+
writer.writerow(
|
| 87 |
+
{"time": str(datetime.now()), "message": message.strip(), "name": name.strip() }
|
| 88 |
+
)
|
| 89 |
+
df = pd.read_csv(savePath)
|
| 90 |
+
df = df.sort_values(df.columns[0],ascending=False)
|
| 91 |
+
else:
|
| 92 |
+
if name and message:
|
| 93 |
+
with open(savePath, "a") as csvfile:
|
| 94 |
+
writer = csv.DictWriter(csvfile, fieldnames=[ "time", "message", "name", ])
|
| 95 |
+
writer.writerow(
|
| 96 |
+
{"time": str(datetime.now()), "message": message.strip(), "name": name.strip() }
|
| 97 |
+
)
|
| 98 |
+
df = pd.read_csv(savePath)
|
| 99 |
+
df = df.sort_values(df.columns[0],ascending=False)
|
| 100 |
+
return df
|
| 101 |
+
|
| 102 |
+
mname = "facebook/blenderbot-400M-distill"
|
| 103 |
+
model = BlenderbotForConditionalGeneration.from_pretrained(mname)
|
| 104 |
+
tokenizer = BlenderbotTokenizer.from_pretrained(mname)
|
| 105 |
+
|
| 106 |
+
def take_last_tokens(inputs, note_history, history):
|
| 107 |
+
if inputs['input_ids'].shape[1] > 128:
|
| 108 |
+
inputs['input_ids'] = torch.tensor([inputs['input_ids'][0][-128:].tolist()])
|
| 109 |
+
inputs['attention_mask'] = torch.tensor([inputs['attention_mask'][0][-128:].tolist()])
|
| 110 |
+
note_history = ['</s> <s>'.join(note_history[0].split('</s> <s>')[2:])]
|
| 111 |
+
history = history[1:]
|
| 112 |
+
return inputs, note_history, history
|
| 113 |
|
| 114 |
+
def add_note_to_history(note, note_history):# good example of non async since we wait around til we know it went okay.
|
| 115 |
+
note_history.append(note)
|
| 116 |
+
note_history = '</s> <s>'.join(note_history)
|
| 117 |
+
return [note_history]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
title = "💬ChatBack🧠💾"
|
| 123 |
+
description = """Chatbot With persistent memory dataset allowing multiagent system AI to access a shared dataset as memory pool with stored interactions.
|
| 124 |
+
Current Best SOTA Chatbot: https://huggingface.co/facebook/blenderbot-400M-distill?text=Hey+my+name+is+ChatBack%21+Are+you+ready+to+rock%3F """
|
| 125 |
+
|
| 126 |
+
def get_base(filename):
|
| 127 |
+
basedir = os.path.dirname(__file__)
|
| 128 |
+
print(basedir)
|
| 129 |
+
#loadPath = basedir + "\\" + filename # works on windows
|
| 130 |
+
loadPath = basedir + filename # works on ubuntu
|
| 131 |
+
print(loadPath)
|
| 132 |
+
return loadPath
|
| 133 |
+
|
| 134 |
+
def chat(message, history):
|
| 135 |
+
history = history or []
|
| 136 |
+
if history:
|
| 137 |
+
history_useful = ['</s> <s>'.join([str(a[0])+'</s> <s>'+str(a[1]) for a in history])]
|
| 138 |
+
else:
|
| 139 |
+
history_useful = []
|
| 140 |
|
| 141 |
+
history_useful = add_note_to_history(message, history_useful)
|
| 142 |
+
inputs = tokenizer(history_useful, return_tensors="pt")
|
| 143 |
+
inputs, history_useful, history = take_last_tokens(inputs, history_useful, history)
|
| 144 |
+
reply_ids = model.generate(**inputs)
|
| 145 |
+
response = tokenizer.batch_decode(reply_ids, skip_special_tokens=True)[0]
|
| 146 |
+
history_useful = add_note_to_history(response, history_useful)
|
| 147 |
+
list_history = history_useful[0].split('</s> <s>')
|
| 148 |
+
history.append((list_history[-2], list_history[-1]))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
+
df=pd.DataFrame()
|
| 151 |
+
|
| 152 |
+
if UseMemory:
|
| 153 |
+
#outputfileName = 'ChatbotMemory.csv'
|
| 154 |
+
outputfileName = 'ChatbotMemory3.csv' # Test first time file create
|
| 155 |
+
df = store_message(message, response, outputfileName) # Save to dataset
|
| 156 |
+
basedir = get_base(outputfileName)
|
| 157 |
+
|
| 158 |
+
|
| 159 |
|
| 160 |
+
return history, df, basedir
|
| 161 |
|
| 162 |
+
with gr.Blocks() as demo:
|
| 163 |
+
gr.Markdown("<h1><center>🍰 AI Google Maps Demonstration🎨</center></h1>")
|
| 164 |
+
|
| 165 |
+
with gr.Row():
|
| 166 |
+
t1 = gr.Textbox(lines=1, default="", label="Chat Text:")
|
| 167 |
+
b1 = gr.Button("Respond and Retrieve Messages")
|
| 168 |
+
b2 = gr.Button("Get Map Information")
|
| 169 |
+
|
| 170 |
+
with gr.Row(): # inputs and buttons
|
| 171 |
+
s1 = gr.State([])
|
| 172 |
+
df1 = gr.Dataframe(wrap=True, max_rows=1000, overflow_row_behaviour= "paginate")
|
| 173 |
+
with gr.Row(): # inputs and buttons
|
| 174 |
+
file = gr.File(label="File")
|
| 175 |
+
s2 = gr.Markdown()
|
| 176 |
+
with gr.Row():
|
| 177 |
+
df21 = gr.Textbox(lines=4, default="", label="Geocode1:")
|
| 178 |
+
df22 = gr.Textbox(lines=4, default="", label="Geocode2:")
|
| 179 |
+
df23 = gr.Textbox(lines=4, default="", label="Geocode3:")
|
| 180 |
+
df3 = gr.Dataframe(wrap=True, max_rows=1000, overflow_row_behaviour= "paginate")
|
| 181 |
+
df4 = gr.Dataframe(wrap=True, max_rows=1000, overflow_row_behaviour= "paginate")
|
| 182 |
+
b1.click(fn=chat, inputs=[t1, s1], outputs=[s1, df1, file])
|
| 183 |
+
b2.click(fn=GetMapInfo, inputs=[t1], outputs=[df21, df22, df23])
|
| 184 |
+
|
| 185 |
+
demo.launch(debug=True, show_error=True)
|