awacke1 commited on
Commit
df192ae
·
1 Parent(s): 91dd3e2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +117 -0
app.py ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import BlenderbotTokenizer, BlenderbotForConditionalGeneration
2
+ import torch
3
+ import gradio as gr
4
+
5
+ import os
6
+ import csv
7
+ from gradio import inputs, outputs
8
+ from datetime import datetime
9
+ import fastapi
10
+ from typing import List, Dict
11
+ import httpx
12
+ import pandas as pd
13
+ import datasets as ds
14
+ UseMemory=True
15
+
16
+ HF_TOKEN=os.environ.get("HF_TOKEN")
17
+
18
+ def SaveResult(text, outputfileName):
19
+ basedir = os.path.dirname(__file__)
20
+ savePath = outputfileName
21
+ print("Saving: " + text + " to " + savePath)
22
+ from os.path import exists
23
+ file_exists = exists(savePath)
24
+ if file_exists:
25
+ with open(outputfileName, "a") as f: #append
26
+ f.write(str(text.replace("\n"," ")))
27
+ f.write('\n')
28
+ else:
29
+ with open(outputfileName, "w") as f: #write
30
+ f.write(str(text.replace("\n"," ")))
31
+ f.write('\n')
32
+ return
33
+
34
+ if UseMemory:
35
+ try:
36
+ # Retrieve File
37
+ except:
38
+ print("file not found")
39
+
40
+ def store_message(name: str, message: str, outputfileName: str):
41
+ basedir = os.path.dirname(__file__)
42
+ savePath = outputfileName
43
+ if name and message:
44
+ print("Saving: " + text + " to " + savePath)
45
+ with open(savePath, "a") as csvfile:
46
+ writer = csv.DictWriter(csvfile, fieldnames=[ "time", "message", "name", ])
47
+ writer.writerow(
48
+ {"time": str(datetime.now()), "message": message.strip(), "name": name.strip() }
49
+ )
50
+ df = pd.read_csv(savePath)
51
+ return df
52
+
53
+ mname = "facebook/blenderbot-400M-distill"
54
+ model = BlenderbotForConditionalGeneration.from_pretrained(mname)
55
+ tokenizer = BlenderbotTokenizer.from_pretrained(mname)
56
+
57
+ def take_last_tokens(inputs, note_history, history):
58
+ if inputs['input_ids'].shape[1] > 128:
59
+ inputs['input_ids'] = torch.tensor([inputs['input_ids'][0][-128:].tolist()])
60
+ inputs['attention_mask'] = torch.tensor([inputs['attention_mask'][0][-128:].tolist()])
61
+ note_history = ['</s> <s>'.join(note_history[0].split('</s> <s>')[2:])]
62
+ history = history[1:]
63
+ return inputs, note_history, history
64
+
65
+ def add_note_to_history(note, note_history):# good example of non async since we wait around til we know it went okay.
66
+ note_history.append(note)
67
+ note_history = '</s> <s>'.join(note_history)
68
+ return [note_history]
69
+
70
+ title = "💬ChatBack🧠💾"
71
+ description = """Chatbot With persistent memory dataset allowing multiagent system AI to access a shared dataset as memory pool with stored interactions.
72
+ Current Best SOTA Chatbot: https://huggingface.co/facebook/blenderbot-400M-distill?text=Hey+my+name+is+ChatBack%21+Are+you+ready+to+rock%3F """
73
+
74
+ def chat(message, history):
75
+ history = history or []
76
+ if history:
77
+ history_useful = ['</s> <s>'.join([str(a[0])+'</s> <s>'+str(a[1]) for a in history])]
78
+ else:
79
+ history_useful = []
80
+ history_useful = add_note_to_history(message, history_useful)
81
+ inputs = tokenizer(history_useful, return_tensors="pt")
82
+ inputs, history_useful, history = take_last_tokens(inputs, history_useful, history)
83
+ reply_ids = model.generate(**inputs)
84
+ response = tokenizer.batch_decode(reply_ids, skip_special_tokens=True)[0]
85
+ history_useful = add_note_to_history(response, history_useful)
86
+ list_history = history_useful[0].split('</s> <s>')
87
+ history.append((list_history[-2], list_history[-1]))
88
+
89
+ df=pd.DataFrame()
90
+
91
+ if UseMemory:
92
+ outputfileName = 'File.csv'
93
+ df = store_message(message, response, outputfileName) # Save to dataset
94
+ basedir = os.path.dirname(__file__)
95
+ savePath = outputfileName
96
+
97
+ return history, df, outputfileName
98
+
99
+ with gr.Blocks() as demo:
100
+ gr.Markdown("<h1><center>🍰Gradio chatbot backed by memory in a dataset repository.🎨</center></h1>")
101
+ #gr.Markdown("The memory dataset for saves is [{DATASET_REPO_URL}]({DATASET_REPO_URL}) And here: https://huggingface.co/spaces/awacke1/DatasetAnalyzer Code and datasets on chat are here hf tk: https://paperswithcode.com/datasets?q=chat&v=lst&o=newest")
102
+
103
+ with gr.Row():
104
+ t1 = gr.Textbox(lines=1, default="", label="Chat Text:")
105
+ b1 = gr.Button("Send Message")
106
+
107
+ with gr.Row(): # inputs and buttons
108
+ s1 = gr.State([])
109
+ s2 = gr.Markdown()
110
+ with gr.Row():
111
+ file = gr.File(label="File"),
112
+ df1 = gr.Dataframe(wrap=True, max_rows=1000, overflow_row_behaviour= "paginate")
113
+
114
+
115
+ b1.click(fn=chat, inputs=[t1, s1], outputs=[s1, df1, file])
116
+
117
+ demo.launch(debug=True, show_error=True)