Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -49,6 +49,7 @@ 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(response2)
|
53 |
return response.json().get('text')
|
54 |
else:
|
@@ -66,13 +67,12 @@ def save_and_play_audio(audio_recorder):
|
|
66 |
return filename
|
67 |
return None
|
68 |
|
69 |
-
|
70 |
-
|
71 |
-
if
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
return None, None
|
76 |
|
77 |
def create_file(filename, prompt, response):
|
78 |
if filename.endswith(".txt"):
|
@@ -83,39 +83,120 @@ def create_file(filename, prompt, response):
|
|
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
|
87 |
-
|
88 |
-
|
89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
else:
|
91 |
-
|
|
|
92 |
def main():
|
93 |
-
st.
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
if
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
st.write('Response:')
|
118 |
st.write(response)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
if __name__ == "__main__":
|
121 |
-
main()
|
|
|
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:
|
|
|
67 |
return filename
|
68 |
return None
|
69 |
|
70 |
+
filename = save_and_play_audio(audio_recorder)
|
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"):
|
|
|
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 main():
|
140 |
+
user_prompt = st.text_area("Enter prompts, instructions & questions:", '', height=100)
|
141 |
+
|
142 |
+
collength, colupload = st.columns([2,3]) # adjust the ratio as needed
|
143 |
+
with collength:
|
144 |
+
#max_length = 12000 - optimal for gpt35 turbo. 2x=24000 for gpt4. 8x=96000 for gpt4-32k.
|
145 |
+
max_length = st.slider("File section length for large files", min_value=1000, max_value=128000, value=12000, step=1000)
|
146 |
+
with colupload:
|
147 |
+
uploaded_file = st.file_uploader("Add a file for context:", type=["xml", "json", "html", "htm", "md", "txt"])
|
148 |
+
|
149 |
+
document_sections = deque()
|
150 |
+
document_responses = {}
|
151 |
+
|
152 |
+
if uploaded_file is not None:
|
153 |
+
file_content = read_file_content(uploaded_file, max_length)
|
154 |
+
document_sections.extend(divide_document(file_content, max_length))
|
155 |
+
|
156 |
+
if len(document_sections) > 0:
|
157 |
+
|
158 |
+
if st.button("👁️ View Upload"):
|
159 |
+
st.markdown("**Sections of the uploaded file:**")
|
160 |
+
for i, section in enumerate(list(document_sections)):
|
161 |
+
st.markdown(f"**Section {i+1}**\n{section}")
|
162 |
+
|
163 |
+
st.markdown("**Chat with the model:**")
|
164 |
+
for i, section in enumerate(list(document_sections)):
|
165 |
+
if i in document_responses:
|
166 |
+
st.markdown(f"**Section {i+1}**\n{document_responses[i]}")
|
167 |
+
else:
|
168 |
+
if st.button(f"Chat about Section {i+1}"):
|
169 |
+
st.write('Reasoning with your inputs...')
|
170 |
+
response = chat_with_model(user_prompt, section)
|
171 |
st.write('Response:')
|
172 |
st.write(response)
|
173 |
+
document_responses[i] = response
|
174 |
+
filename = generate_filename(f"{user_prompt}_section_{i+1}", choice)
|
175 |
+
create_file(filename, user_prompt, response)
|
176 |
+
st.sidebar.markdown(get_table_download_link(filename), unsafe_allow_html=True)
|
177 |
+
|
178 |
+
if st.button('💬 Chat'):
|
179 |
+
st.write('Reasoning with your inputs...')
|
180 |
+
response = chat_with_model(user_prompt, ''.join(list(document_sections)))
|
181 |
+
st.write('Response:')
|
182 |
+
st.write(response)
|
183 |
+
|
184 |
+
filename = generate_filename(user_prompt, choice)
|
185 |
+
create_file(filename, user_prompt, response)
|
186 |
+
st.sidebar.markdown(get_table_download_link(filename), unsafe_allow_html=True)
|
187 |
|
188 |
+
all_files = glob.glob("*.*")
|
189 |
+
all_files = [file for file in all_files if len(os.path.splitext(file)[0]) >= 20] # exclude files with short names
|
190 |
+
all_files.sort(key=lambda x: (os.path.splitext(x)[1], x), reverse=True) # sort by file type and file name in descending order
|
191 |
+
|
192 |
+
for file in all_files:
|
193 |
+
col1, col3 = st.sidebar.columns([5,1]) # adjust the ratio as needed
|
194 |
+
with col1:
|
195 |
+
st.markdown(get_table_download_link(file), unsafe_allow_html=True)
|
196 |
+
with col3:
|
197 |
+
if st.button("🗑", key="delete_"+file):
|
198 |
+
os.remove(file)
|
199 |
+
st.experimental_rerun()
|
200 |
+
|
201 |
if __name__ == "__main__":
|
202 |
+
main()
|