Update app.py
Browse files
app.py
CHANGED
|
@@ -23,4 +23,154 @@ directions_result = gmaps.directions("Sydney Town Hall",
|
|
| 23 |
addressvalidation_result = gmaps.addressvalidation(['1600 Amphitheatre Pk'],
|
| 24 |
regionCode='US',
|
| 25 |
locality='Mountain View',
|
| 26 |
-
enableUspsCass=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
addressvalidation_result = gmaps.addressvalidation(['1600 Amphitheatre Pk'],
|
| 24 |
regionCode='US',
|
| 25 |
locality='Mountain View',
|
| 26 |
+
enableUspsCass=True)
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
from transformers import BlenderbotTokenizer, BlenderbotForConditionalGeneration
|
| 32 |
+
import torch
|
| 33 |
+
import gradio as gr
|
| 34 |
+
from datasets import load_dataset
|
| 35 |
+
|
| 36 |
+
# PersistDataset -----
|
| 37 |
+
import os
|
| 38 |
+
import csv
|
| 39 |
+
from gradio import inputs, outputs
|
| 40 |
+
import huggingface_hub
|
| 41 |
+
from huggingface_hub import Repository, hf_hub_download, upload_file
|
| 42 |
+
from datetime import datetime
|
| 43 |
+
|
| 44 |
+
#fastapi is where its at: share your app, share your api
|
| 45 |
+
import fastapi
|
| 46 |
+
|
| 47 |
+
from typing import List, Dict
|
| 48 |
+
import httpx
|
| 49 |
+
import pandas as pd
|
| 50 |
+
import datasets as ds
|
| 51 |
+
|
| 52 |
+
UseMemory=True
|
| 53 |
+
HF_TOKEN=os.environ.get("HF_TOKEN")
|
| 54 |
+
|
| 55 |
+
def SaveResult(text, outputfileName):
|
| 56 |
+
basedir = os.path.dirname(__file__)
|
| 57 |
+
savePath = outputfileName
|
| 58 |
+
print("Saving: " + text + " to " + savePath)
|
| 59 |
+
from os.path import exists
|
| 60 |
+
file_exists = exists(savePath)
|
| 61 |
+
if file_exists:
|
| 62 |
+
with open(outputfileName, "a") as f: #append
|
| 63 |
+
f.write(str(text.replace("\n"," ")))
|
| 64 |
+
f.write('\n')
|
| 65 |
+
else:
|
| 66 |
+
with open(outputfileName, "w") as f: #write
|
| 67 |
+
f.write(str("time, message, text\n")) # one time only to get column headers for CSV file
|
| 68 |
+
f.write(str(text.replace("\n"," ")))
|
| 69 |
+
f.write('\n')
|
| 70 |
+
return
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def store_message(name: str, message: str, outputfileName: str):
|
| 74 |
+
basedir = os.path.dirname(__file__)
|
| 75 |
+
savePath = outputfileName
|
| 76 |
+
|
| 77 |
+
# if file doesnt exist, create it with labels
|
| 78 |
+
from os.path import exists
|
| 79 |
+
file_exists = exists(savePath)
|
| 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 |
+
title = "💬ChatBack🧠💾"
|
| 120 |
+
description = """Chatbot With persistent memory dataset allowing multiagent system AI to access a shared dataset as memory pool with stored interactions.
|
| 121 |
+
Current Best SOTA Chatbot: https://huggingface.co/facebook/blenderbot-400M-distill?text=Hey+my+name+is+ChatBack%21+Are+you+ready+to+rock%3F """
|
| 122 |
+
|
| 123 |
+
def get_base(filename):
|
| 124 |
+
basedir = os.path.dirname(__file__)
|
| 125 |
+
print(basedir)
|
| 126 |
+
#loadPath = basedir + "\\" + filename # works on windows
|
| 127 |
+
loadPath = basedir + filename
|
| 128 |
+
print(loadPath)
|
| 129 |
+
return loadPath
|
| 130 |
+
|
| 131 |
+
def chat(message, history):
|
| 132 |
+
history = history or []
|
| 133 |
+
if history:
|
| 134 |
+
history_useful = ['</s> <s>'.join([str(a[0])+'</s> <s>'+str(a[1]) for a in history])]
|
| 135 |
+
else:
|
| 136 |
+
history_useful = []
|
| 137 |
+
|
| 138 |
+
history_useful = add_note_to_history(message, history_useful)
|
| 139 |
+
inputs = tokenizer(history_useful, return_tensors="pt")
|
| 140 |
+
inputs, history_useful, history = take_last_tokens(inputs, history_useful, history)
|
| 141 |
+
reply_ids = model.generate(**inputs)
|
| 142 |
+
response = tokenizer.batch_decode(reply_ids, skip_special_tokens=True)[0]
|
| 143 |
+
history_useful = add_note_to_history(response, history_useful)
|
| 144 |
+
list_history = history_useful[0].split('</s> <s>')
|
| 145 |
+
history.append((list_history[-2], list_history[-1]))
|
| 146 |
+
|
| 147 |
+
df=pd.DataFrame()
|
| 148 |
+
|
| 149 |
+
if UseMemory:
|
| 150 |
+
#outputfileName = 'ChatbotMemory.csv'
|
| 151 |
+
outputfileName = 'ChatbotMemory3.csv' # Test first time file create
|
| 152 |
+
df = store_message(message, response, outputfileName) # Save to dataset
|
| 153 |
+
basedir = get_base(outputfileName)
|
| 154 |
+
|
| 155 |
+
return history, df, basedir
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
with gr.Blocks() as demo:
|
| 161 |
+
gr.Markdown("<h1><center>🍰 AI Google Maps Demonstration🎨</center></h1>")
|
| 162 |
+
|
| 163 |
+
with gr.Row():
|
| 164 |
+
t1 = gr.Textbox(lines=1, default="", label="Chat Text:")
|
| 165 |
+
b1 = gr.Button("Respond and Retrieve Messages")
|
| 166 |
+
|
| 167 |
+
with gr.Row(): # inputs and buttons
|
| 168 |
+
s1 = gr.State([])
|
| 169 |
+
df1 = gr.Dataframe(wrap=True, max_rows=1000, overflow_row_behaviour= "paginate")
|
| 170 |
+
with gr.Row(): # inputs and buttons
|
| 171 |
+
file = gr.File(label="File")
|
| 172 |
+
s2 = gr.Markdown()
|
| 173 |
+
|
| 174 |
+
b1.click(fn=chat, inputs=[t1, s1], outputs=[s1, df1, file])
|
| 175 |
+
|
| 176 |
+
demo.launch(debug=True, show_error=True)
|