Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -3,11 +3,12 @@ import openai
|
|
3 |
import os
|
4 |
import base64
|
5 |
import glob
|
6 |
-
import json
|
7 |
-
from xml.etree import ElementTree as ET
|
8 |
from datetime import datetime
|
9 |
from dotenv import load_dotenv
|
10 |
from openai import ChatCompletion
|
|
|
|
|
|
|
11 |
|
12 |
load_dotenv()
|
13 |
|
@@ -15,7 +16,6 @@ openai.api_key = os.getenv('OPENAI_KEY')
|
|
15 |
|
16 |
def chat_with_model(prompts):
|
17 |
model = "gpt-3.5-turbo"
|
18 |
-
|
19 |
conversation = [{'role': 'system', 'content': 'You are a helpful assistant.'}]
|
20 |
conversation.extend([{'role': 'user', 'content': prompt} for prompt in prompts])
|
21 |
|
@@ -23,7 +23,7 @@ def chat_with_model(prompts):
|
|
23 |
return response['choices'][0]['message']['content']
|
24 |
|
25 |
def generate_filename(prompt):
|
26 |
-
safe_date_time = datetime.now().strftime("%m_%d_%H_%M")
|
27 |
safe_prompt = "".join(x for x in prompt if x.isalnum())[:50]
|
28 |
return f"{safe_date_time}_{safe_prompt}.htm"
|
29 |
|
@@ -34,16 +34,16 @@ def create_file(filename, prompt, response):
|
|
34 |
def get_table_download_link(file_path):
|
35 |
with open(file_path, 'r') as file:
|
36 |
data = file.read()
|
37 |
-
b64 = base64.b64encode(data.encode()).decode()
|
38 |
href = f'<a href="data:file/htm;base64,{b64}" target="_blank" download="{os.path.basename(file_path)}">{os.path.basename(file_path)}</a>'
|
39 |
return href
|
40 |
|
41 |
def CompressXML(xml_text):
|
42 |
-
|
43 |
-
for elem in
|
44 |
-
if isinstance(elem.tag,
|
45 |
-
elem.
|
46 |
-
return ET.tostring(
|
47 |
|
48 |
def read_file_content(file):
|
49 |
if file.type == "application/json":
|
@@ -53,9 +53,9 @@ def read_file_content(file):
|
|
53 |
content = BeautifulSoup(file, "html.parser")
|
54 |
return content.text
|
55 |
elif file.type == "application/xml" or file.type == "text/xml":
|
56 |
-
|
57 |
-
|
58 |
-
return
|
59 |
elif file.type == "text/plain":
|
60 |
return file.getvalue().decode()
|
61 |
else:
|
@@ -91,8 +91,11 @@ def main():
|
|
91 |
htm_files = glob.glob("*.htm")
|
92 |
for file in htm_files:
|
93 |
st.sidebar.markdown(get_table_download_link(file), unsafe_allow_html=True)
|
|
|
94 |
if st.sidebar.button(f"Delete {file}"):
|
95 |
os.remove(file)
|
|
|
|
|
96 |
|
97 |
if __name__ == "__main__":
|
98 |
-
main()
|
|
|
3 |
import os
|
4 |
import base64
|
5 |
import glob
|
|
|
|
|
6 |
from datetime import datetime
|
7 |
from dotenv import load_dotenv
|
8 |
from openai import ChatCompletion
|
9 |
+
from xml.etree import ElementTree as ET
|
10 |
+
from bs4 import BeautifulSoup
|
11 |
+
import json
|
12 |
|
13 |
load_dotenv()
|
14 |
|
|
|
16 |
|
17 |
def chat_with_model(prompts):
|
18 |
model = "gpt-3.5-turbo"
|
|
|
19 |
conversation = [{'role': 'system', 'content': 'You are a helpful assistant.'}]
|
20 |
conversation.extend([{'role': 'user', 'content': prompt} for prompt in prompts])
|
21 |
|
|
|
23 |
return response['choices'][0]['message']['content']
|
24 |
|
25 |
def generate_filename(prompt):
|
26 |
+
safe_date_time = datetime.now().strftime("%m_%d_%H_%M")
|
27 |
safe_prompt = "".join(x for x in prompt if x.isalnum())[:50]
|
28 |
return f"{safe_date_time}_{safe_prompt}.htm"
|
29 |
|
|
|
34 |
def get_table_download_link(file_path):
|
35 |
with open(file_path, 'r') as file:
|
36 |
data = file.read()
|
37 |
+
b64 = base64.b64encode(data.encode()).decode()
|
38 |
href = f'<a href="data:file/htm;base64,{b64}" target="_blank" download="{os.path.basename(file_path)}">{os.path.basename(file_path)}</a>'
|
39 |
return href
|
40 |
|
41 |
def CompressXML(xml_text):
|
42 |
+
root = ET.fromstring(xml_text)
|
43 |
+
for elem in list(root.iter()):
|
44 |
+
if isinstance(elem.tag, str) and 'Comment' in elem.tag:
|
45 |
+
elem.parent.remove(elem)
|
46 |
+
return ET.tostring(root, encoding='unicode', method="xml")
|
47 |
|
48 |
def read_file_content(file):
|
49 |
if file.type == "application/json":
|
|
|
53 |
content = BeautifulSoup(file, "html.parser")
|
54 |
return content.text
|
55 |
elif file.type == "application/xml" or file.type == "text/xml":
|
56 |
+
tree = ET.parse(file)
|
57 |
+
root = tree.getroot()
|
58 |
+
return ElementTree.tostring(root, encoding='unicode')
|
59 |
elif file.type == "text/plain":
|
60 |
return file.getvalue().decode()
|
61 |
else:
|
|
|
91 |
htm_files = glob.glob("*.htm")
|
92 |
for file in htm_files:
|
93 |
st.sidebar.markdown(get_table_download_link(file), unsafe_allow_html=True)
|
94 |
+
|
95 |
if st.sidebar.button(f"Delete {file}"):
|
96 |
os.remove(file)
|
97 |
+
# Reload page after deletion to refresh the file list
|
98 |
+
st.experimental_rerun()
|
99 |
|
100 |
if __name__ == "__main__":
|
101 |
+
main()
|