awacke1 commited on
Commit
f8850ff
β€’
1 Parent(s): 8e4ee34

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +107 -14
app.py CHANGED
@@ -4,7 +4,6 @@ import os
4
  import base64
5
  import glob
6
  import json
7
- import re
8
  from xml.etree import ElementTree as ET
9
  from datetime import datetime
10
  from dotenv import load_dotenv
@@ -39,11 +38,109 @@ def get_table_download_link(file_path):
39
  href = f'<a href="data:file/htm;base64,{b64}" target="_blank" download="{os.path.basename(file_path)}">{os.path.basename(file_path)}</a>'
40
  return href
41
 
42
- def CompressXML_Old(xml_text):
43
- words = xml_text.split()
44
- english_words = [word for word in words if re.fullmatch(r'[A-Za-z ]*', word)]
45
- compressed_text = ' '.join(english_words)
46
- return compressed_text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
  def CompressXML(xml_text):
49
  tree = ET.ElementTree(ET.fromstring(xml_text))
@@ -59,20 +156,16 @@ def read_file_content(file):
59
  elif file.type == "text/html":
60
  content = BeautifulSoup(file, "html.parser")
61
  return content.text
62
- elif file.type == "application/xmlold" or file.type == "text/xmlold":
63
- tree = ElementTree.parse(file)
64
- root = tree.getroot()
65
- return ElementTree.tostring(root, encoding='unicode')
66
  elif file.type == "application/xml" or file.type == "text/xml":
67
- tree = ElementTree.parse(file)
68
- root = tree.getroot()
69
- xml_text = ElementTree.tostring(root, encoding='unicode')
70
- return CompressXML(xml_text)
71
  elif file.type == "text/plain":
72
  return file.getvalue().decode()
73
  else:
74
  return ""
75
 
 
76
  def main():
77
  st.title("Chat with AI")
78
 
 
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
 
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
+ tree = ET.ElementTree(ET.fromstring(xml_text))
43
+ for elem in tree.iter():
44
+ if isinstance(elem.tag, ET.Comment):
45
+ elem.getparent().remove(elem)
46
+ return ET.tostring(tree.getroot(), encoding='unicode')
47
+
48
+ def read_file_content(file):
49
+ if file.type == "application/json":
50
+ content = json.load(file)
51
+ return str(content)
52
+ elif file.type == "text/html":
53
+ content = BeautifulSoup(file, "html.parser")
54
+ return content.text
55
+ elif file.type == "application/xml" or file.type == "text/xml":
56
+ xml_text = file.getvalue().decode()
57
+ compressed_text = CompressXML(xml_text)
58
+ return compressed_text
59
+ elif file.type == "text/plain":
60
+ return file.getvalue().decode()
61
+ else:
62
+ return ""
63
+
64
+ def main():
65
+ st.title("Chat with AI")
66
+
67
+ prompts = ['']
68
+
69
+ user_prompt = st.text_area("Your question:", '', height=120)
70
+ uploaded_file = st.file_uploader("Choose a file", type=["xml", "json", "htm", "txt"])
71
+
72
+ if user_prompt:
73
+ prompts.append(user_prompt)
74
+
75
+ if uploaded_file is not None:
76
+ file_content = read_file_content(uploaded_file)
77
+ st.markdown(f"**Content Added to Prompt:**\n{file_content}")
78
+ prompts.append(file_content)
79
+
80
+ if st.button('Chat'):
81
+ st.write('Chatting with GPT-3...')
82
+ response = chat_with_model(prompts)
83
+ st.write('Response:')
84
+ st.write(response)
85
+
86
+ filename = generate_filename(user_prompt)
87
+ create_file(filename, user_prompt, response)
88
+
89
+ st.sidebar.markdown(get_table_download_link(filename), unsafe_allow_html=True)
90
+
91
+ htm_files = glob.glob("*.htm")
92
+ for file in htm_files:
93
+ st.sidebar.mark
94
+
95
+
96
+
97
+
98
+ User
99
+ show full code listing
100
+ ChatGPT
101
+ Here is the full Python script:
102
+
103
+ python
104
+ Copy code
105
+ import streamlit as st
106
+ import openai
107
+ import os
108
+ import base64
109
+ import glob
110
+ import json
111
+ from xml.etree import ElementTree as ET
112
+ from datetime import datetime
113
+ from dotenv import load_dotenv
114
+ from openai import ChatCompletion
115
+
116
+ load_dotenv()
117
+
118
+ openai.api_key = os.getenv('OPENAI_KEY')
119
+
120
+ def chat_with_model(prompts):
121
+ model = "gpt-3.5-turbo"
122
+
123
+ conversation = [{'role': 'system', 'content': 'You are a helpful assistant.'}]
124
+ conversation.extend([{'role': 'user', 'content': prompt} for prompt in prompts])
125
+
126
+ response = openai.ChatCompletion.create(model=model, messages=conversation)
127
+ return response['choices'][0]['message']['content']
128
+
129
+ def generate_filename(prompt):
130
+ safe_date_time = datetime.now().strftime("%m_%d_%H_%M")
131
+ safe_prompt = "".join(x for x in prompt if x.isalnum())[:50]
132
+ return f"{safe_date_time}_{safe_prompt}.htm"
133
+
134
+ def create_file(filename, prompt, response):
135
+ with open(filename, 'w') as file:
136
+ file.write(f"<h1>Prompt:</h1> <p>{prompt}</p> <h1>Response:</h1> <p>{response}</p>")
137
+
138
+ def get_table_download_link(file_path):
139
+ with open(file_path, 'r') as file:
140
+ data = file.read()
141
+ b64 = base64.b64encode(data.encode()).decode()
142
+ href = f'<a href="data:file/htm;base64,{b64}" target="_blank" download="{os.path.basename(file_path)}">{os.path.basename(file_path)}</a>'
143
+ return href
144
 
145
  def CompressXML(xml_text):
146
  tree = ET.ElementTree(ET.fromstring(xml_text))
 
156
  elif file.type == "text/html":
157
  content = BeautifulSoup(file, "html.parser")
158
  return content.text
 
 
 
 
159
  elif file.type == "application/xml" or file.type == "text/xml":
160
+ xml_text = file.getvalue().decode()
161
+ compressed_text = CompressXML(xml_text)
162
+ return compressed_text
 
163
  elif file.type == "text/plain":
164
  return file.getvalue().decode()
165
  else:
166
  return ""
167
 
168
+
169
  def main():
170
  st.title("Chat with AI")
171