keneonyeachonam
commited on
Commit
β’
789d96b
1
Parent(s):
d2cea93
Upload 2 files
Browse files- backupapp.py +209 -0
- requirements.txt +6 -0
backupapp.py
ADDED
@@ -0,0 +1,209 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
import json
|
4 |
+
import requests
|
5 |
+
|
6 |
+
#Streaming endpoint
|
7 |
+
API_URL = "https://api.openai.com/v1/chat/completions" #os.getenv("API_URL") + "/generate_stream"
|
8 |
+
OPENAI_API_KEY= os.environ["HF_TOKEN"] # Add a token to this space . Then copy it to the repository secret in this spaces settings panel. os.environ reads from there.
|
9 |
+
# Keys for Open AI ChatGPT API usage are created from here: https://platform.openai.com/account/api-keys
|
10 |
+
|
11 |
+
def predict(inputs, top_p, temperature, chat_counter, chatbot=[], history=[]): #repetition_penalty, top_k
|
12 |
+
|
13 |
+
# 1. Set up a payload
|
14 |
+
payload = {
|
15 |
+
"model": "gpt-3.5-turbo",
|
16 |
+
"messages": [{"role": "user", "content": f"{inputs}"}],
|
17 |
+
"temperature" : 1.0,
|
18 |
+
"top_p":1.0,
|
19 |
+
"n" : 1,
|
20 |
+
"stream": True,
|
21 |
+
"presence_penalty":0,
|
22 |
+
"frequency_penalty":0,
|
23 |
+
}
|
24 |
+
|
25 |
+
# 2. Define your headers and add a key from https://platform.openai.com/account/api-keys
|
26 |
+
headers = {
|
27 |
+
"Content-Type": "application/json",
|
28 |
+
"Authorization": f"Bearer {OPENAI_API_KEY}"
|
29 |
+
}
|
30 |
+
|
31 |
+
# 3. Create a chat counter loop that feeds [Predict next best anything based on last input and attention with memory defined by introspective attention over time]
|
32 |
+
print(f"chat_counter - {chat_counter}")
|
33 |
+
if chat_counter != 0 :
|
34 |
+
messages=[]
|
35 |
+
for data in chatbot:
|
36 |
+
temp1 = {}
|
37 |
+
temp1["role"] = "user"
|
38 |
+
temp1["content"] = data[0]
|
39 |
+
temp2 = {}
|
40 |
+
temp2["role"] = "assistant"
|
41 |
+
temp2["content"] = data[1]
|
42 |
+
messages.append(temp1)
|
43 |
+
messages.append(temp2)
|
44 |
+
temp3 = {}
|
45 |
+
temp3["role"] = "user"
|
46 |
+
temp3["content"] = inputs
|
47 |
+
messages.append(temp3)
|
48 |
+
payload = {
|
49 |
+
"model": "gpt-3.5-turbo",
|
50 |
+
"messages": messages, #[{"role": "user", "content": f"{inputs}"}],
|
51 |
+
"temperature" : temperature, #1.0,
|
52 |
+
"top_p": top_p, #1.0,
|
53 |
+
"n" : 1,
|
54 |
+
"stream": True,
|
55 |
+
"presence_penalty":0,
|
56 |
+
"frequency_penalty":0,
|
57 |
+
}
|
58 |
+
chat_counter+=1
|
59 |
+
|
60 |
+
# 4. POST it to OPENAI API
|
61 |
+
history.append(inputs)
|
62 |
+
print(f"payload is - {payload}")
|
63 |
+
response = requests.post(API_URL, headers=headers, json=payload, stream=True)
|
64 |
+
token_counter = 0
|
65 |
+
partial_words = ""
|
66 |
+
|
67 |
+
# 5. Iterate through response lines and structure readable response
|
68 |
+
counter=0
|
69 |
+
for chunk in response.iter_lines():
|
70 |
+
if counter == 0:
|
71 |
+
counter+=1
|
72 |
+
continue
|
73 |
+
if chunk.decode() :
|
74 |
+
chunk = chunk.decode()
|
75 |
+
if len(chunk) > 12 and "content" in json.loads(chunk[6:])['choices'][0]['delta']:
|
76 |
+
partial_words = partial_words + json.loads(chunk[6:])['choices'][0]["delta"]["content"]
|
77 |
+
if token_counter == 0:
|
78 |
+
history.append(" " + partial_words)
|
79 |
+
else:
|
80 |
+
history[-1] = partial_words
|
81 |
+
chat = [(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2) ] # convert to tuples of list
|
82 |
+
token_counter+=1
|
83 |
+
yield chat, history, chat_counter
|
84 |
+
|
85 |
+
|
86 |
+
def reset_textbox():
|
87 |
+
return gr.update(value='')
|
88 |
+
|
89 |
+
|
90 |
+
|
91 |
+
|
92 |
+
# Episodic and Semantic IO
|
93 |
+
def list_files(file_path):
|
94 |
+
import os
|
95 |
+
icon_csv = "π "
|
96 |
+
icon_txt = "π "
|
97 |
+
current_directory = os.getcwd()
|
98 |
+
file_list = []
|
99 |
+
for filename in os.listdir(current_directory):
|
100 |
+
if filename.endswith(".csv"):
|
101 |
+
file_list.append(icon_csv + filename)
|
102 |
+
elif filename.endswith(".txt"):
|
103 |
+
file_list.append(icon_txt + filename)
|
104 |
+
if file_list:
|
105 |
+
return "\n".join(file_list)
|
106 |
+
else:
|
107 |
+
return "No .csv or .txt files found in the current directory."
|
108 |
+
|
109 |
+
# Function to read a file
|
110 |
+
def read_file(file_path):
|
111 |
+
try:
|
112 |
+
with open(file_path, "r") as file:
|
113 |
+
contents = file.read()
|
114 |
+
return f"{contents}"
|
115 |
+
#return f"Contents of {file_path}:\n{contents}"
|
116 |
+
except FileNotFoundError:
|
117 |
+
return "File not found."
|
118 |
+
|
119 |
+
# Function to delete a file
|
120 |
+
def delete_file(file_path):
|
121 |
+
try:
|
122 |
+
import os
|
123 |
+
os.remove(file_path)
|
124 |
+
return f"{file_path} has been deleted."
|
125 |
+
except FileNotFoundError:
|
126 |
+
return "File not found."
|
127 |
+
|
128 |
+
# Function to write to a file
|
129 |
+
def write_file(file_path, content):
|
130 |
+
try:
|
131 |
+
with open(file_path, "w") as file:
|
132 |
+
file.write(content)
|
133 |
+
return f"Successfully written to {file_path}."
|
134 |
+
except:
|
135 |
+
return "Error occurred while writing to file."
|
136 |
+
|
137 |
+
# Function to append to a file
|
138 |
+
def append_file(file_path, content):
|
139 |
+
try:
|
140 |
+
with open(file_path, "a") as file:
|
141 |
+
file.write(content)
|
142 |
+
return f"Successfully appended to {file_path}."
|
143 |
+
except:
|
144 |
+
return "Error occurred while appending to file."
|
145 |
+
|
146 |
+
|
147 |
+
title = """<h1 align="center">Memory Chat Story Generator ChatGPT</h1>"""
|
148 |
+
description = """
|
149 |
+
## ChatGPT Datasets π
|
150 |
+
- WebText
|
151 |
+
- Common Crawl
|
152 |
+
- BooksCorpus
|
153 |
+
- English Wikipedia
|
154 |
+
- Toronto Books Corpus
|
155 |
+
- OpenWebText
|
156 |
+
## ChatGPT Datasets - Details π
|
157 |
+
- **WebText:** A dataset of web pages crawled from domains on the Alexa top 5,000 list. This dataset was used to pretrain GPT-2.
|
158 |
+
- [WebText: A Large-Scale Unsupervised Text Corpus by Radford et al.](https://paperswithcode.com/dataset/webtext)
|
159 |
+
- **Common Crawl:** A dataset of web pages from a variety of domains, which is updated regularly. This dataset was used to pretrain GPT-3.
|
160 |
+
- [Language Models are Few-Shot Learners](https://paperswithcode.com/dataset/common-crawl) by Brown et al.
|
161 |
+
- **BooksCorpus:** A dataset of over 11,000 books from a variety of genres.
|
162 |
+
- [Scalable Methods for 8 Billion Token Language Modeling](https://paperswithcode.com/dataset/bookcorpus) by Zhu et al.
|
163 |
+
- **English Wikipedia:** A dump of the English-language Wikipedia as of 2018, with articles from 2001-2017.
|
164 |
+
- [Improving Language Understanding by Generative Pre-Training](https://huggingface.co/spaces/awacke1/WikipediaUltimateAISearch?logs=build) Space for Wikipedia Search
|
165 |
+
- **Toronto Books Corpus:** A dataset of over 7,000 books from a variety of genres, collected by the University of Toronto.
|
166 |
+
- [Massively Multilingual Sentence Embeddings for Zero-Shot Cross-Lingual Transfer and Beyond](https://paperswithcode.com/dataset/bookcorpus) by Schwenk and Douze.
|
167 |
+
- **OpenWebText:** A dataset of web pages that were filtered to remove content that was likely to be low-quality or spammy. This dataset was used to pretrain GPT-3.
|
168 |
+
- [Language Models are Few-Shot Learners](https://paperswithcode.com/dataset/openwebtext) by Brown et al.
|
169 |
+
"""
|
170 |
+
|
171 |
+
# 6. Use Gradio to pull it all together
|
172 |
+
with gr.Blocks(css = """#col_container {width: 1400px; margin-left: auto; margin-right: auto;} #chatbot {height: 600px; overflow: auto;}""") as demo:
|
173 |
+
gr.HTML(title)
|
174 |
+
with gr.Column(elem_id = "col_container"):
|
175 |
+
inputs = gr.Textbox(placeholder= "Hi there!", label= "Type an input and press Enter")
|
176 |
+
chatbot = gr.Chatbot(elem_id='chatbot')
|
177 |
+
state = gr.State([])
|
178 |
+
b1 = gr.Button()
|
179 |
+
with gr.Accordion("Parameters", open=False):
|
180 |
+
top_p = gr.Slider( minimum=-0, maximum=1.0, value=1.0, step=0.05, interactive=True, label="Top-p (nucleus sampling)",)
|
181 |
+
temperature = gr.Slider( minimum=-0, maximum=5.0, value=1.0, step=0.1, interactive=True, label="Temperature",)
|
182 |
+
chat_counter = gr.Number(value=0, visible=True, precision=0)
|
183 |
+
|
184 |
+
|
185 |
+
# Episodic/Semantic IO
|
186 |
+
fileName = gr.Textbox(label="Filename")
|
187 |
+
fileContent = gr.TextArea(label="File Content")
|
188 |
+
completedMessage = gr.Textbox(label="Completed")
|
189 |
+
label = gr.Label()
|
190 |
+
with gr.Row():
|
191 |
+
listFiles = gr.Button("π List File(s)")
|
192 |
+
readFile = gr.Button("π Read File")
|
193 |
+
saveFile = gr.Button("πΎ Save File")
|
194 |
+
deleteFile = gr.Button("ποΈ Delete File")
|
195 |
+
appendFile = gr.Button("β Append File")
|
196 |
+
listFiles.click(list_files, inputs=fileName, outputs=fileContent)
|
197 |
+
readFile.click(read_file, inputs=fileName, outputs=fileContent)
|
198 |
+
saveFile.click(write_file, inputs=[fileName, fileContent], outputs=completedMessage)
|
199 |
+
deleteFile.click(delete_file, inputs=fileName, outputs=completedMessage)
|
200 |
+
appendFile.click(append_file, inputs=[fileName, fileContent], outputs=completedMessage )
|
201 |
+
|
202 |
+
|
203 |
+
inputs.submit(predict, [inputs, top_p, temperature,chat_counter, chatbot, state], [chatbot, state, chat_counter])
|
204 |
+
b1.click(predict, [inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter])
|
205 |
+
b1.click(reset_textbox, [], [inputs])
|
206 |
+
inputs.submit(reset_textbox, [], [inputs])
|
207 |
+
gr.Markdown(description)
|
208 |
+
|
209 |
+
demo.queue().launch(debug=True)
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
transformers
|
2 |
+
torch
|
3 |
+
Werkzeug
|
4 |
+
huggingface_hub
|
5 |
+
Pillow
|
6 |
+
datasets
|