Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -23,6 +23,8 @@ menu = ["txt", "htm", "md", "py"]
|
|
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")
|
@@ -49,7 +51,6 @@ def transcribe_audio(openai_key, file_path, model):
|
|
49 |
st.write(response.json())
|
50 |
response2 = chat_with_model(response.json().get('text'), '')
|
51 |
st.write('Responses:')
|
52 |
-
#st.write(response)
|
53 |
st.write(response2)
|
54 |
return response.json().get('text')
|
55 |
else:
|
@@ -64,95 +65,17 @@ def save_and_play_audio(audio_recorder):
|
|
64 |
with open(filename, 'wb') as f:
|
65 |
f.write(audio_bytes)
|
66 |
st.audio(audio_bytes, format="audio/wav")
|
|
|
67 |
return filename
|
68 |
return None
|
69 |
|
70 |
-
|
71 |
-
if filename is not None:
|
72 |
-
if st.button("Transcribe"):
|
73 |
-
transcription = transcribe_audio(openai.api_key, filename, "whisper-1")
|
74 |
-
st.write(transcription)
|
75 |
-
chat_with_model(transcription, '') # push transcript through as prompt
|
76 |
-
|
77 |
-
def create_file(filename, prompt, response):
|
78 |
-
if filename.endswith(".txt"):
|
79 |
-
with open(filename, 'w') as file:
|
80 |
-
file.write(f"Prompt:\n{prompt}\nResponse:\n{response}")
|
81 |
-
elif filename.endswith(".htm"):
|
82 |
-
with open(filename, 'w') as file:
|
83 |
-
file.write(f"<h1>Prompt:</h1> <p>{prompt}</p> <h1>Response:</h1> <p>{response}</p>")
|
84 |
-
elif filename.endswith(".md"):
|
85 |
-
with open(filename, 'w') as file:
|
86 |
-
file.write(f"# Prompt:\n{prompt}\n# Response:\n{response}")
|
87 |
-
|
88 |
-
def truncate_document(document, length):
|
89 |
-
return document[:length]
|
90 |
-
|
91 |
-
def divide_document(document, max_length):
|
92 |
-
return [document[i:i+max_length] for i in range(0, len(document), max_length)]
|
93 |
-
|
94 |
-
def get_table_download_link(file_path):
|
95 |
-
with open(file_path, 'r') as file:
|
96 |
-
data = file.read()
|
97 |
-
b64 = base64.b64encode(data.encode()).decode()
|
98 |
-
file_name = os.path.basename(file_path)
|
99 |
-
ext = os.path.splitext(file_name)[1] # get the file extension
|
100 |
-
if ext == '.txt':
|
101 |
-
mime_type = 'text/plain'
|
102 |
-
elif ext == '.htm':
|
103 |
-
mime_type = 'text/html'
|
104 |
-
elif ext == '.md':
|
105 |
-
mime_type = 'text/markdown'
|
106 |
-
else:
|
107 |
-
mime_type = 'application/octet-stream' # general binary data type
|
108 |
-
href = f'<a href="data:{mime_type};base64,{b64}" target="_blank" download="{file_name}">{file_name}</a>'
|
109 |
-
return href
|
110 |
-
|
111 |
-
def CompressXML(xml_text):
|
112 |
-
root = ET.fromstring(xml_text)
|
113 |
-
for elem in list(root.iter()):
|
114 |
-
if isinstance(elem.tag, str) and 'Comment' in elem.tag:
|
115 |
-
elem.parent.remove(elem)
|
116 |
-
return ET.tostring(root, encoding='unicode', method="xml")
|
117 |
-
|
118 |
-
def read_file_content(file,max_length):
|
119 |
-
if file.type == "application/json":
|
120 |
-
content = json.load(file)
|
121 |
-
return str(content)
|
122 |
-
elif file.type == "text/html" or file.type == "text/htm":
|
123 |
-
content = BeautifulSoup(file, "html.parser")
|
124 |
-
return content.text
|
125 |
-
elif file.type == "application/xml" or file.type == "text/xml":
|
126 |
-
tree = ET.parse(file)
|
127 |
-
root = tree.getroot()
|
128 |
-
xml = CompressXML(ET.tostring(root, encoding='unicode'))
|
129 |
-
return xml
|
130 |
-
elif file.type == "text/markdown" or file.type == "text/md":
|
131 |
-
md = mistune.create_markdown()
|
132 |
-
content = md(file.read().decode())
|
133 |
-
return content
|
134 |
-
elif file.type == "text/plain":
|
135 |
-
return file.getvalue().decode()
|
136 |
-
else:
|
137 |
-
return ""
|
138 |
-
|
139 |
-
def transcribe_and_chat(openai_key, file_path, model):
|
140 |
-
transcription = transcribe_audio(openai_key, file_path, model)
|
141 |
-
if transcription is not None:
|
142 |
-
response = chat_with_model(transcription, '')
|
143 |
-
st.write('Chat Response:')
|
144 |
-
st.write(response)
|
145 |
-
return transcription, response
|
146 |
-
else:
|
147 |
-
return None, None
|
148 |
|
149 |
-
|
150 |
def main():
|
151 |
user_prompt = st.text_area("Enter prompts, instructions & questions:", '', height=100)
|
152 |
|
153 |
collength, colupload = st.columns([2,3]) # adjust the ratio as needed
|
154 |
with collength:
|
155 |
-
#max_length = 12000 - optimal for gpt35 turbo. 2x=24000 for gpt4. 8x=96000 for gpt4-32k.
|
156 |
max_length = st.slider("File section length for large files", min_value=1000, max_value=128000, value=12000, step=1000)
|
157 |
with colupload:
|
158 |
uploaded_file = st.file_uploader("Add a file for context:", type=["xml", "json", "html", "htm", "md", "txt"])
|
@@ -161,64 +84,43 @@ def main():
|
|
161 |
document_responses = {}
|
162 |
|
163 |
if uploaded_file is not None:
|
164 |
-
file_content =
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
if len(document_sections) > 0:
|
169 |
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
if i in document_responses:
|
178 |
-
st.markdown(f"**Section {i+1}**\n{document_responses[i]}")
|
179 |
-
else:
|
180 |
-
if st.button(f"Chat about Section {i+1}"):
|
181 |
-
st.write('Reasoning with your inputs...')
|
182 |
-
response = chat_with_model(user_prompt, section)
|
183 |
-
st.write('Response:')
|
184 |
-
st.write(response)
|
185 |
-
document_responses[i] = response
|
186 |
-
filename = generate_filename(f"{user_prompt}_section_{i+1}", choice)
|
187 |
-
create_file(filename, user_prompt, response)
|
188 |
-
st.sidebar.markdown(get_table_download_link(filename), unsafe_allow_html=True)
|
189 |
|
190 |
if st.button('💬 Chat'):
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
203 |
transcription, response = transcribe_and_chat(openai.api_key, filename, "whisper-1")
|
204 |
if transcription is not None and response is not None:
|
205 |
filename = generate_filename(transcription, choice)
|
206 |
create_file(filename, transcription, response)
|
207 |
st.sidebar.markdown(get_table_download_link(filename), unsafe_allow_html=True)
|
208 |
|
209 |
-
|
210 |
-
all_files = glob.glob("*.*")
|
211 |
-
all_files = [file for file in all_files if len(os.path.splitext(file)[0]) >= 20] # exclude files with short names
|
212 |
-
all_files.sort(key=lambda x: (os.path.splitext(x)[1], x), reverse=True) # sort by file type and file name in descending order
|
213 |
-
|
214 |
-
for file in all_files:
|
215 |
-
col1, col3 = st.sidebar.columns([5,1]) # adjust the ratio as needed
|
216 |
-
with col1:
|
217 |
-
st.markdown(get_table_download_link(file), unsafe_allow_html=True)
|
218 |
-
with col3:
|
219 |
-
if st.button("🗑", key="delete_"+file):
|
220 |
-
os.remove(file)
|
221 |
-
st.experimental_rerun()
|
222 |
-
|
223 |
if __name__ == "__main__":
|
224 |
-
main()
|
|
|
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 |
+
uploaded_files = []
|
27 |
+
|
28 |
def generate_filename(prompt, file_type):
|
29 |
central = pytz.timezone('US/Central')
|
30 |
safe_date_time = datetime.now(central).strftime("%m%d_%I%M")
|
|
|
51 |
st.write(response.json())
|
52 |
response2 = chat_with_model(response.json().get('text'), '')
|
53 |
st.write('Responses:')
|
|
|
54 |
st.write(response2)
|
55 |
return response.json().get('text')
|
56 |
else:
|
|
|
65 |
with open(filename, 'wb') as f:
|
66 |
f.write(audio_bytes)
|
67 |
st.audio(audio_bytes, format="audio/wav")
|
68 |
+
uploaded_files.append(filename) # Add the new file name to the list
|
69 |
return filename
|
70 |
return None
|
71 |
|
72 |
+
# ... (All your other function definitions)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
|
|
|
74 |
def main():
|
75 |
user_prompt = st.text_area("Enter prompts, instructions & questions:", '', height=100)
|
76 |
|
77 |
collength, colupload = st.columns([2,3]) # adjust the ratio as needed
|
78 |
with collength:
|
|
|
79 |
max_length = st.slider("File section length for large files", min_value=1000, max_value=128000, value=12000, step=1000)
|
80 |
with colupload:
|
81 |
uploaded_file = st.file_uploader("Add a file for context:", type=["xml", "json", "html", "htm", "md", "txt"])
|
|
|
84 |
document_responses = {}
|
85 |
|
86 |
if uploaded_file is not None:
|
87 |
+
file_content = uploaded_file.getvalue().decode('utf-8')
|
88 |
+
# Handle different file types here
|
89 |
+
# ...
|
90 |
+
document_sections.append(file_content)
|
|
|
91 |
|
92 |
+
new_filename = save_and_play_audio(audio_recorder)
|
93 |
+
if new_filename is not None:
|
94 |
+
st.write(f'File {new_filename} uploaded.')
|
95 |
+
if st.button("Transcribe"):
|
96 |
+
transcription = transcribe_audio(openai.api_key, new_filename, "whisper-1")
|
97 |
+
st.write(transcription)
|
98 |
+
chat_with_model(transcription, '') # push transcript through as prompt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
|
100 |
if st.button('💬 Chat'):
|
101 |
+
user_responses = document_responses.get(user_prompt, [])
|
102 |
+
if len(user_responses) > 0:
|
103 |
+
st.write(user_responses[-1])
|
104 |
+
else:
|
105 |
+
if document_sections:
|
106 |
+
st.write('First document section:')
|
107 |
+
st.write(document_sections[0])
|
108 |
+
document_responses[user_prompt] = [chat_with_model(user_prompt, document_sections[0])]
|
109 |
+
st.write(document_responses[user_prompt][-1])
|
110 |
+
else:
|
111 |
+
document_responses[user_prompt] = [chat_with_model(user_prompt, '')]
|
112 |
+
st.write(document_responses[user_prompt][-1])
|
113 |
+
|
114 |
+
if uploaded_files:
|
115 |
+
st.write(f'Last uploaded file: {uploaded_files[-1]}')
|
116 |
+
|
117 |
+
for filename in uploaded_files:
|
118 |
+
if st.button(f"Transcribe and Chat for {filename}"):
|
119 |
transcription, response = transcribe_and_chat(openai.api_key, filename, "whisper-1")
|
120 |
if transcription is not None and response is not None:
|
121 |
filename = generate_filename(transcription, choice)
|
122 |
create_file(filename, transcription, response)
|
123 |
st.sidebar.markdown(get_table_download_link(filename), unsafe_allow_html=True)
|
124 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
if __name__ == "__main__":
|
126 |
+
main()
|