awacke1 commited on
Commit
9d8bc5f
Β·
1 Parent(s): 45dfb45

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -38
app.py CHANGED
@@ -7,35 +7,68 @@ import json
7
  import mistune
8
  import pytz
9
  import math
 
 
10
  from datetime import datetime
11
  from openai import ChatCompletion
12
  from xml.etree import ElementTree as ET
13
  from bs4 import BeautifulSoup
14
  from collections import deque
 
15
 
16
  openai.api_key = os.getenv('OPENAI_KEY')
17
- st.set_page_config(
18
- page_title="GPT Streamlit Document Reasoner",
19
- layout="wide")
20
-
21
- menu = ["txt", "htm", "md", "py"]
22
- choice = st.sidebar.selectbox("Output file type:", menu)
23
- choicePrefix = "Output file type is "
24
- if choice == "txt":
25
- st.sidebar.write(choicePrefix + "Text File.")
26
- elif choice == "htm":
27
- st.sidebar.write(choicePrefix + "HTML5.")
28
- elif choice == "md":
29
- st.sidebar.write(choicePrefix + "Markdown.")
30
- elif choice == "py":
31
- st.sidebar.write(choicePrefix + "Python Code.")
32
 
33
  def generate_filename(prompt, file_type):
34
  central = pytz.timezone('US/Central')
35
  safe_date_time = datetime.now(central).strftime("%m%d_%I%M")
36
- safe_prompt = "".join(x for x in prompt if x.isalnum())[:28]
37
  return f"{safe_date_time}_{safe_prompt}.{file_type}"
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  def create_file(filename, prompt, response):
40
  if filename.endswith(".txt"):
41
  with open(filename, 'w') as file:
@@ -46,21 +79,10 @@ def create_file(filename, prompt, response):
46
  elif filename.endswith(".md"):
47
  with open(filename, 'w') as file:
48
  file.write(f"# Prompt:\n{prompt}\n# Response:\n{response}")
49
-
50
  def truncate_document(document, length):
51
  return document[:length]
52
-
53
  def divide_document(document, max_length):
54
  return [document[i:i+max_length] for i in range(0, len(document), max_length)]
55
-
56
- def chat_with_model(prompt, document_section):
57
- model = "gpt-3.5-turbo"
58
- conversation = [{'role': 'system', 'content': 'You are a helpful assistant.'}]
59
- conversation.append({'role': 'user', 'content': prompt})
60
- conversation.append({'role': 'assistant', 'content': document_section})
61
- response = openai.ChatCompletion.create(model=model, messages=conversation)
62
- return response['choices'][0]['message']['content']
63
-
64
  def get_table_download_link(file_path):
65
  with open(file_path, 'r') as file:
66
  data = file.read()
@@ -69,6 +91,12 @@ def get_table_download_link(file_path):
69
  ext = os.path.splitext(file_name)[1] # get the file extension
70
  if ext == '.txt':
71
  mime_type = 'text/plain'
 
 
 
 
 
 
72
  elif ext == '.htm':
73
  mime_type = 'text/html'
74
  elif ext == '.md':
@@ -78,6 +106,22 @@ def get_table_download_link(file_path):
78
  href = f'<a href="data:{mime_type};base64,{b64}" target="_blank" download="{file_name}">{file_name}</a>'
79
  return href
80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  def CompressXML(xml_text):
82
  root = ET.fromstring(xml_text)
83
  for elem in list(root.iter()):
@@ -107,14 +151,14 @@ def read_file_content(file,max_length):
107
  return ""
108
 
109
  def main():
110
- user_prompt = st.text_area("Your question:", '', height=120)
111
 
112
  collength, colupload = st.columns([2,3]) # adjust the ratio as needed
113
  with collength:
114
  #max_length = 12000 - optimal for gpt35 turbo. 2x=24000 for gpt4. 8x=96000 for gpt4-32k.
115
- max_length = st.slider("Context Section Length", min_value=1000, max_value=128000, value=12000, step=1000)
116
  with colupload:
117
- uploaded_file = st.file_uploader("Choose a file", type=["xml", "json", "html", "htm", "md", "txt"])
118
 
119
  document_sections = deque()
120
  document_responses = {}
@@ -136,7 +180,7 @@ def main():
136
  st.markdown(f"**Section {i+1}**\n{document_responses[i]}")
137
  else:
138
  if st.button(f"Chat about Section {i+1}"):
139
- st.write('Thinking and Reasoning with your inputs...')
140
  response = chat_with_model(user_prompt, section)
141
  st.write('Response:')
142
  st.write(response)
@@ -146,7 +190,7 @@ def main():
146
  st.sidebar.markdown(get_table_download_link(filename), unsafe_allow_html=True)
147
 
148
  if st.button('πŸ’¬ Chat'):
149
- st.write('Thinking and Reasoning with your inputs...')
150
  response = chat_with_model(user_prompt, ''.join(list(document_sections)))
151
  st.write('Response:')
152
  st.write(response)
@@ -155,15 +199,18 @@ def main():
155
  create_file(filename, user_prompt, response)
156
  st.sidebar.markdown(get_table_download_link(filename), unsafe_allow_html=True)
157
 
158
- all_files = glob.glob("*.txt") + glob.glob("*.htm") + glob.glob("*.md")
 
 
 
159
  for file in all_files:
160
- col1, col2 = st.sidebar.columns([4,1]) # adjust the ratio as needed
161
  with col1:
162
  st.markdown(get_table_download_link(file), unsafe_allow_html=True)
163
- with col2:
164
- if st.button("πŸ—‘", key=file):
165
  os.remove(file)
166
  st.experimental_rerun()
167
-
168
  if __name__ == "__main__":
169
- main()
 
7
  import mistune
8
  import pytz
9
  import math
10
+ import requests
11
+
12
  from datetime import datetime
13
  from openai import ChatCompletion
14
  from xml.etree import ElementTree as ET
15
  from bs4 import BeautifulSoup
16
  from collections import deque
17
+ from audio_recorder_streamlit import audio_recorder
18
 
19
  openai.api_key = os.getenv('OPENAI_KEY')
20
+ st.set_page_config(page_title="GPT Streamlit Document Reasoner",layout="wide")
21
+
22
+ menu = ["htm", "txt", "xlsx", "csv", "md", "py"] #619
23
+ choice = st.sidebar.selectbox("Output File Type:", menu)
24
+ model_choice = st.sidebar.radio("Select Model:", ('gpt-3.5-turbo', 'gpt-3.5-turbo-0301'))
 
 
 
 
 
 
 
 
 
 
25
 
26
  def generate_filename(prompt, file_type):
27
  central = pytz.timezone('US/Central')
28
  safe_date_time = datetime.now(central).strftime("%m%d_%I%M")
29
+ safe_prompt = "".join(x for x in prompt if x.isalnum())[:45]
30
  return f"{safe_date_time}_{safe_prompt}.{file_type}"
31
 
32
+ def chat_with_model(prompt, document_section):
33
+ model = model_choice
34
+ conversation = [{'role': 'system', 'content': 'You are a helpful assistant.'}]
35
+ conversation.append({'role': 'user', 'content': prompt})
36
+ if len(document_section)>0:
37
+ conversation.append({'role': 'assistant', 'content': document_section})
38
+ response = openai.ChatCompletion.create(model=model, messages=conversation)
39
+ return response
40
+ #return response['choices'][0]['message']['content']
41
+
42
+ def transcribe_audio(openai_key, file_path, model):
43
+ OPENAI_API_URL = "https://api.openai.com/v1/audio/transcriptions"
44
+ headers = {
45
+ "Authorization": f"Bearer {openai_key}",
46
+ }
47
+ with open(file_path, 'rb') as f:
48
+ data = {'file': f}
49
+ response = requests.post(OPENAI_API_URL, headers=headers, files=data, data={'model': model})
50
+ if response.status_code == 200:
51
+ st.write(response.json())
52
+ response2 = chat_with_model(response.json().get('text'), '')
53
+ st.write('Responses:')
54
+ #st.write(response)
55
+ st.write(response2)
56
+ return response.json().get('text')
57
+ else:
58
+ st.write(response.json())
59
+ st.error("Error in API call.")
60
+ return None
61
+
62
+ def save_and_play_audio(audio_recorder):
63
+ audio_bytes = audio_recorder()
64
+ if audio_bytes:
65
+ filename = generate_filename("Recording", "wav")
66
+ with open(filename, 'wb') as f:
67
+ f.write(audio_bytes)
68
+ st.audio(audio_bytes, format="audio/wav")
69
+ return filename
70
+ return None
71
+
72
  def create_file(filename, prompt, response):
73
  if filename.endswith(".txt"):
74
  with open(filename, 'w') as file:
 
79
  elif filename.endswith(".md"):
80
  with open(filename, 'w') as file:
81
  file.write(f"# Prompt:\n{prompt}\n# Response:\n{response}")
 
82
  def truncate_document(document, length):
83
  return document[:length]
 
84
  def divide_document(document, max_length):
85
  return [document[i:i+max_length] for i in range(0, len(document), max_length)]
 
 
 
 
 
 
 
 
 
86
  def get_table_download_link(file_path):
87
  with open(file_path, 'r') as file:
88
  data = file.read()
 
91
  ext = os.path.splitext(file_name)[1] # get the file extension
92
  if ext == '.txt':
93
  mime_type = 'text/plain'
94
+ elif ext == '.py':
95
+ mime_type = 'text/plain'
96
+ elif ext == '.xlsx':
97
+ mime_type = 'text/plain'
98
+ elif ext == '.csv':
99
+ mime_type = 'text/plain'
100
  elif ext == '.htm':
101
  mime_type = 'text/html'
102
  elif ext == '.md':
 
106
  href = f'<a href="data:{mime_type};base64,{b64}" target="_blank" download="{file_name}">{file_name}</a>'
107
  return href
108
 
109
+
110
+
111
+ # Audio, transcribe, GPT:
112
+ filename = save_and_play_audio(audio_recorder)
113
+ if filename is not None:
114
+ transcription = transcribe_audio(openai.api_key, filename, "whisper-1")
115
+ st.write(transcription)
116
+ gptOutput = chat_with_model(transcription, '') # push transcript through as prompt
117
+ filename = generate_filename(transcription, choice)
118
+ create_file(filename, transcription, gptOutput)
119
+ st.sidebar.markdown(get_table_download_link(filename), unsafe_allow_html=True)
120
+
121
+
122
+
123
+
124
+
125
  def CompressXML(xml_text):
126
  root = ET.fromstring(xml_text)
127
  for elem in list(root.iter()):
 
151
  return ""
152
 
153
  def main():
154
+ user_prompt = st.text_area("Enter prompts, instructions & questions:", '', height=100)
155
 
156
  collength, colupload = st.columns([2,3]) # adjust the ratio as needed
157
  with collength:
158
  #max_length = 12000 - optimal for gpt35 turbo. 2x=24000 for gpt4. 8x=96000 for gpt4-32k.
159
+ max_length = st.slider("File section length for large files", min_value=1000, max_value=128000, value=12000, step=1000)
160
  with colupload:
161
+ uploaded_file = st.file_uploader("Add a file for context:", type=["xml", "json", "xlsx","csv","html", "htm", "md", "txt"])
162
 
163
  document_sections = deque()
164
  document_responses = {}
 
180
  st.markdown(f"**Section {i+1}**\n{document_responses[i]}")
181
  else:
182
  if st.button(f"Chat about Section {i+1}"):
183
+ st.write('Reasoning with your inputs...')
184
  response = chat_with_model(user_prompt, section)
185
  st.write('Response:')
186
  st.write(response)
 
190
  st.sidebar.markdown(get_table_download_link(filename), unsafe_allow_html=True)
191
 
192
  if st.button('πŸ’¬ Chat'):
193
+ st.write('Reasoning with your inputs...')
194
  response = chat_with_model(user_prompt, ''.join(list(document_sections)))
195
  st.write('Response:')
196
  st.write(response)
 
199
  create_file(filename, user_prompt, response)
200
  st.sidebar.markdown(get_table_download_link(filename), unsafe_allow_html=True)
201
 
202
+ all_files = glob.glob("*.*")
203
+ all_files = [file for file in all_files if len(os.path.splitext(file)[0]) >= 20] # exclude files with short names
204
+ all_files.sort(key=lambda x: (os.path.splitext(x)[1], x), reverse=True) # sort by file type and file name in descending order
205
+
206
  for file in all_files:
207
+ col1, col3 = st.sidebar.columns([5,1]) # adjust the ratio as needed
208
  with col1:
209
  st.markdown(get_table_download_link(file), unsafe_allow_html=True)
210
+ with col3:
211
+ if st.button("πŸ—‘", key="delete_"+file):
212
  os.remove(file)
213
  st.experimental_rerun()
214
+
215
  if __name__ == "__main__":
216
+ main()