awacke1 commited on
Commit
5d39a3e
β€’
1 Parent(s): 5f27703

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -7
app.py CHANGED
@@ -3,14 +3,16 @@ import openai
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
 
15
  openai.api_key = os.getenv('OPENAI_KEY')
16
 
@@ -19,7 +21,6 @@ def chat_with_model(prompts):
19
  #model = "gpt-4-32k"
20
  conversation = [{'role': 'system', 'content': 'You are a helpful assistant.'}]
21
  conversation.extend([{'role': 'user', 'content': prompt} for prompt in prompts])
22
-
23
  response = openai.ChatCompletion.create(model=model, messages=conversation)
24
  return response['choices'][0]['message']['content']
25
 
@@ -32,13 +33,34 @@ def create_file(filename, prompt, response):
32
  with open(filename, 'w') as file:
33
  file.write(f"<h1>Prompt:</h1> <p>{prompt}</p> <h1>Response:</h1> <p>{response}</p>")
34
 
35
- def get_table_download_link(file_path):
36
  with open(file_path, 'r') as file:
37
  data = file.read()
38
  b64 = base64.b64encode(data.encode()).decode()
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(xml_text):
43
  root = ET.fromstring(xml_text)
44
  for elem in list(root.iter()):
@@ -96,10 +118,9 @@ def main():
96
  htm_files = glob.glob("*.txt")
97
  for file in htm_files:
98
  st.sidebar.markdown(get_table_download_link(file), unsafe_allow_html=True)
99
-
100
- if st.sidebar.button(f"Delete {file}"):
101
  os.remove(file)
102
- # Reload page after deletion to refresh the file list
103
  st.experimental_rerun()
104
 
105
  if __name__ == "__main__":
 
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
 
 
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
 
 
33
  with open(filename, 'w') as file:
34
  file.write(f"<h1>Prompt:</h1> <p>{prompt}</p> <h1>Response:</h1> <p>{response}</p>")
35
 
36
+ def get_table_download_link_old(file_path):
37
  with open(file_path, 'r') as file:
38
  data = file.read()
39
  b64 = base64.b64encode(data.encode()).decode()
40
  href = f'<a href="data:file/htm;base64,{b64}" target="_blank" download="{os.path.basename(file_path)}">{os.path.basename(file_path)}</a>'
41
  return href
42
 
43
+ def get_table_download_link(file_path):
44
+ import os
45
+ import base64
46
+ with open(file_path, 'r') as file:
47
+ data = file.read()
48
+ b64 = base64.b64encode(data.encode()).decode()
49
+ file_name = os.path.basename(file_path)
50
+ ext = os.path.splitext(file_name)[1] # get the file extension
51
+
52
+ if ext == '.txt':
53
+ mime_type = 'text/plain'
54
+ elif ext == '.htm':
55
+ mime_type = 'text/html'
56
+ elif ext == '.md':
57
+ mime_type = 'text/markdown'
58
+ else:
59
+ mime_type = 'application/octet-stream' # general binary data type
60
+
61
+ href = f'<a href="data:{mime_type};base64,{b64}" target="_blank" download="{file_name}">{file_name}</a>'
62
+ return href
63
+
64
  def CompressXML(xml_text):
65
  root = ET.fromstring(xml_text)
66
  for elem in list(root.iter()):
 
118
  htm_files = glob.glob("*.txt")
119
  for file in htm_files:
120
  st.sidebar.markdown(get_table_download_link(file), unsafe_allow_html=True)
121
+ # if st.sidebar.button(f"Delete {file}"):
122
+ if st.sidebar.button("πŸ—‘ Delete"):
123
  os.remove(file)
 
124
  st.experimental_rerun()
125
 
126
  if __name__ == "__main__":