awacke1 commited on
Commit
daeed14
Β·
1 Parent(s): 32d5367

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -30
app.py CHANGED
@@ -45,7 +45,6 @@ def predict(inputs, top_p, temperature, chat_counter, chatbot=[], history=[]):
45
  temp3["role"] = "user"
46
  temp3["content"] = inputs
47
  messages.append(temp3)
48
- #messages
49
  payload = {
50
  "model": "gpt-3.5-turbo",
51
  "messages": messages, #[{"role": "user", "content": f"{inputs}"}],
@@ -61,28 +60,19 @@ def predict(inputs, top_p, temperature, chat_counter, chatbot=[], history=[]):
61
  # 4. POST it to OPENAI API
62
  history.append(inputs)
63
  print(f"payload is - {payload}")
64
- # make a POST request to the API endpoint using the requests.post method, passing in stream=True
65
  response = requests.post(API_URL, headers=headers, json=payload, stream=True)
66
- #response = requests.post(API_URL, headers=headers, json=payload, stream=True)
67
  token_counter = 0
68
  partial_words = ""
69
 
70
  # 5. Iterate through response lines and structure readable response
71
- # TODO - make this parse out markdown so we can have similar interface
72
  counter=0
73
  for chunk in response.iter_lines():
74
- #Skipping first chunk
75
  if counter == 0:
76
  counter+=1
77
  continue
78
- #counter+=1
79
- # check whether each line is non-empty
80
  if chunk.decode() :
81
  chunk = chunk.decode()
82
- # decode each line as response data is in bytes
83
  if len(chunk) > 12 and "content" in json.loads(chunk[6:])['choices'][0]['delta']:
84
- #if len(json.loads(chunk.decode()[6:])['choices'][0]["delta"]) == 0:
85
- # break
86
  partial_words = partial_words + json.loads(chunk[6:])['choices'][0]["delta"]["content"]
87
  if token_counter == 0:
88
  history.append(" " + partial_words)
@@ -90,15 +80,72 @@ def predict(inputs, top_p, temperature, chat_counter, chatbot=[], history=[]):
90
  history[-1] = partial_words
91
  chat = [(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2) ] # convert to tuples of list
92
  token_counter+=1
93
- yield chat, history, chat_counter # resembles {chatbot: chat, state: history}
94
 
95
 
96
  def reset_textbox():
97
  return gr.update(value='')
98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
  title = """<h1 align="center">Memory Chat Story Generator ChatGPT</h1>"""
100
  description = """
101
-
102
  ## ChatGPT Datasets πŸ“š
103
  - WebText
104
  - Common Crawl
@@ -106,7 +153,6 @@ description = """
106
  - English Wikipedia
107
  - Toronto Books Corpus
108
  - OpenWebText
109
-
110
  ## ChatGPT Datasets - Details πŸ“š
111
  - **WebText:** A dataset of web pages crawled from domains on the Alexa top 5,000 list. This dataset was used to pretrain GPT-2.
112
  - [WebText: A Large-Scale Unsupervised Text Corpus by Radford et al.](https://paperswithcode.com/dataset/webtext)
@@ -119,33 +165,45 @@ description = """
119
  - **Toronto Books Corpus:** A dataset of over 7,000 books from a variety of genres, collected by the University of Toronto.
120
  - [Massively Multilingual Sentence Embeddings for Zero-Shot Cross-Lingual Transfer and Beyond](https://paperswithcode.com/dataset/bookcorpus) by Schwenk and Douze.
121
  - **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.
122
- - [Language Models are Few-Shot Learners](https://paperswithcode.com/dataset/openwebtext) by Brown et al.
123
-
124
  """
125
 
126
  # 6. Use Gradio to pull it all together
127
- with gr.Blocks(css = """#col_container {width: 1000px; margin-left: auto; margin-right: auto;}
128
- #chatbot {height: 520px; overflow: auto;}""") as demo:
129
-
130
-
131
  gr.HTML(title)
132
-
133
-
134
  with gr.Column(elem_id = "col_container"):
135
- chatbot = gr.Chatbot(elem_id='chatbot') #c
136
- inputs = gr.Textbox(placeholder= "Hi there!", label= "Type an input and press Enter") #t
137
- state = gr.State([]) #s
138
  b1 = gr.Button()
139
-
140
  with gr.Accordion("Parameters", open=False):
141
  top_p = gr.Slider( minimum=-0, maximum=1.0, value=1.0, step=0.05, interactive=True, label="Top-p (nucleus sampling)",)
142
  temperature = gr.Slider( minimum=-0, maximum=5.0, value=1.0, step=0.1, interactive=True, label="Temperature",)
143
- chat_counter = gr.Number(value=0, visible=False, precision=0)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
144
 
145
- inputs.submit( predict, [inputs, top_p, temperature,chat_counter, chatbot, state], [chatbot, state, chat_counter],)
146
- b1.click( predict, [inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter],)
 
147
  b1.click(reset_textbox, [], [inputs])
148
  inputs.submit(reset_textbox, [], [inputs])
149
-
150
  gr.Markdown(description)
151
- demo.queue().launch(debug=True)
 
 
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}"}],
 
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)
 
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
 
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)
 
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)