Spaces:
Runtime error
Runtime error
Create backupapp.py
Browse files- backupapp.py +128 -0
backupapp.py
ADDED
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import openai
|
3 |
+
import os
|
4 |
+
import base64
|
5 |
+
import glob
|
6 |
+
|
7 |
+
from datetime import datetime
|
8 |
+
from openai import ChatCompletion
|
9 |
+
from xml.etree import ElementTree as ET
|
10 |
+
from bs4 import BeautifulSoup
|
11 |
+
|
12 |
+
import json
|
13 |
+
|
14 |
+
# from dotenv import load_dotenv
|
15 |
+
# load_dotenv()
|
16 |
+
|
17 |
+
openai.api_key = os.getenv('OPENAI_KEY')
|
18 |
+
|
19 |
+
def chat_with_model(prompts):
|
20 |
+
model = "gpt-3.5-turbo"
|
21 |
+
#model = "gpt-4-32k"
|
22 |
+
conversation = [{'role': 'system', 'content': 'You are a helpful assistant.'}]
|
23 |
+
conversation.extend([{'role': 'user', 'content': prompt} for prompt in prompts])
|
24 |
+
response = openai.ChatCompletion.create(model=model, messages=conversation)
|
25 |
+
return response['choices'][0]['message']['content']
|
26 |
+
|
27 |
+
def generate_filename(prompt):
|
28 |
+
#safe_date_time = datetime.now().strftime("%m%d_%H%M")
|
29 |
+
safe_date_time = datetime.now().strftime("%m%d_%I_%M_%p")
|
30 |
+
safe_prompt = "".join(x for x in prompt if x.isalnum())[:30]
|
31 |
+
return f"{safe_date_time}_{safe_prompt}.txt"
|
32 |
+
|
33 |
+
def create_file(filename, prompt, response):
|
34 |
+
with open(filename, 'w') as file:
|
35 |
+
file.write(f"<h1>Prompt:</h1> <p>{prompt}</p> <h1>Response:</h1> <p>{response}</p>")
|
36 |
+
|
37 |
+
def get_table_download_link_old(file_path):
|
38 |
+
with open(file_path, 'r') as file:
|
39 |
+
data = file.read()
|
40 |
+
b64 = base64.b64encode(data.encode()).decode()
|
41 |
+
href = f'<a href="data:file/htm;base64,{b64}" target="_blank" download="{os.path.basename(file_path)}">{os.path.basename(file_path)}</a>'
|
42 |
+
return href
|
43 |
+
|
44 |
+
def get_table_download_link(file_path):
|
45 |
+
import os
|
46 |
+
import base64
|
47 |
+
with open(file_path, 'r') as file:
|
48 |
+
data = file.read()
|
49 |
+
b64 = base64.b64encode(data.encode()).decode()
|
50 |
+
file_name = os.path.basename(file_path)
|
51 |
+
ext = os.path.splitext(file_name)[1] # get the file extension
|
52 |
+
|
53 |
+
if ext == '.txt':
|
54 |
+
mime_type = 'text/plain'
|
55 |
+
elif ext == '.htm':
|
56 |
+
mime_type = 'text/html'
|
57 |
+
elif ext == '.md':
|
58 |
+
mime_type = 'text/markdown'
|
59 |
+
else:
|
60 |
+
mime_type = 'application/octet-stream' # general binary data type
|
61 |
+
|
62 |
+
href = f'<a href="data:{mime_type};base64,{b64}" target="_blank" download="{file_name}">{file_name}</a>'
|
63 |
+
return href
|
64 |
+
|
65 |
+
def CompressXML(xml_text):
|
66 |
+
root = ET.fromstring(xml_text)
|
67 |
+
for elem in list(root.iter()):
|
68 |
+
if isinstance(elem.tag, str) and 'Comment' in elem.tag:
|
69 |
+
elem.parent.remove(elem)
|
70 |
+
#return ET.tostring(root, encoding='unicode', method="xml")
|
71 |
+
return ET.tostring(root, encoding='unicode', method="xml")[:16000]
|
72 |
+
|
73 |
+
|
74 |
+
def read_file_content(file):
|
75 |
+
if file.type == "application/json":
|
76 |
+
content = json.load(file)
|
77 |
+
return str(content)
|
78 |
+
elif file.type == "text/html":
|
79 |
+
content = BeautifulSoup(file, "html.parser")
|
80 |
+
return content.text
|
81 |
+
elif file.type == "application/xml" or file.type == "text/xml":
|
82 |
+
tree = ET.parse(file)
|
83 |
+
root = tree.getroot()
|
84 |
+
#return ET.tostring(root, encoding='unicode')
|
85 |
+
return CompressXML(ET.tostring(root, encoding='unicode'))
|
86 |
+
|
87 |
+
elif file.type == "text/plain":
|
88 |
+
return file.getvalue().decode()
|
89 |
+
else:
|
90 |
+
return ""
|
91 |
+
|
92 |
+
def main():
|
93 |
+
st.title("Chat with AI")
|
94 |
+
|
95 |
+
prompts = ['']
|
96 |
+
|
97 |
+
user_prompt = st.text_area("Your question:", '', height=120)
|
98 |
+
uploaded_file = st.file_uploader("Choose a file", type=["xml", "json", "htm", "txt"])
|
99 |
+
|
100 |
+
if user_prompt:
|
101 |
+
prompts.append(user_prompt)
|
102 |
+
|
103 |
+
if uploaded_file is not None:
|
104 |
+
file_content = read_file_content(uploaded_file)
|
105 |
+
st.markdown(f"**Content Added to Prompt:**\n{file_content}")
|
106 |
+
prompts.append(file_content)
|
107 |
+
|
108 |
+
if st.button('Chat'):
|
109 |
+
st.write('Chatting with GPT-3...')
|
110 |
+
response = chat_with_model(prompts)
|
111 |
+
st.write('Response:')
|
112 |
+
st.write(response)
|
113 |
+
|
114 |
+
filename = generate_filename(user_prompt)
|
115 |
+
create_file(filename, user_prompt, response)
|
116 |
+
|
117 |
+
st.sidebar.markdown(get_table_download_link(filename), unsafe_allow_html=True)
|
118 |
+
|
119 |
+
htm_files = glob.glob("*.txt")
|
120 |
+
for file in htm_files:
|
121 |
+
st.sidebar.markdown(get_table_download_link(file), unsafe_allow_html=True)
|
122 |
+
if st.sidebar.button(f"🗑Delete {file}"):
|
123 |
+
#if st.sidebar.button("🗑 Delete"):
|
124 |
+
os.remove(file)
|
125 |
+
st.experimental_rerun()
|
126 |
+
|
127 |
+
if __name__ == "__main__":
|
128 |
+
main()
|