awacke1 commited on
Commit
82aec87
·
1 Parent(s): 7ca5d2e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +154 -0
app.py ADDED
@@ -0,0 +1,154 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import openai
3
+ import os
4
+ import base64
5
+ import glob
6
+ import json
7
+ import mistune
8
+ import pytz
9
+
10
+ from datetime import datetime
11
+ from openai import ChatCompletion
12
+ from xml.etree import ElementTree as ET
13
+ from bs4 import BeautifulSoup
14
+
15
+ openai.api_key = os.getenv('OPENAI_KEY')
16
+ st.set_page_config(
17
+ page_title="GPT Streamlit Document Reasoner",
18
+ layout="wide")
19
+
20
+ menu = ["txt", "htm", "md", "py"]
21
+ choice = st.sidebar.selectbox("Choose output file type to save results", menu)
22
+ choicePrefix = "Output and download file set to "
23
+ if choice == "txt":
24
+ st.sidebar.write(choicePrefix + "Text file.")
25
+ elif choice == "htm":
26
+ st.sidebar.write(choicePrefix + "HTML5.")
27
+ elif choice == "md":
28
+ st.sidebar.write(choicePrefix + "Markdown.")
29
+ elif choice == "py":
30
+ st.sidebar.write(choicePrefix + "Python Code.")
31
+
32
+ max_length = st.sidebar.slider("Max document length", min_value=1000, max_value=32000, value=2000, step=1000)
33
+
34
+ def truncate_document(document, length):
35
+ return document[:length]
36
+
37
+ def chat_with_model(prompts):
38
+ model = "gpt-3.5-turbo"
39
+ conversation = [{'role': 'system', 'content': 'You are a helpful assistant.'}]
40
+ conversation.extend([{'role': 'user', 'content': prompt} for prompt in prompts])
41
+ response = openai.ChatCompletion.create(model=model, messages=conversation)
42
+ return response['choices'][0]['message']['content']
43
+
44
+ def generate_filename(prompt, file_type):
45
+ central = pytz.timezone('US/Central')
46
+ safe_date_time = datetime.now(central).strftime("%m%d_%I%M")
47
+ safe_prompt = "".join(x for x in prompt if x.isalnum())[:28]
48
+ return f"{safe_date_time}_{safe_prompt}.{file_type}"
49
+
50
+ def create_file(filename, prompt, response):
51
+ if filename.endswith(".txt"):
52
+ with open(filename, 'w') as file:
53
+ file.write(f"Prompt:\n{prompt}\nResponse:\n{response}")
54
+ elif filename.endswith(".htm"):
55
+ with open(filename, 'w') as file:
56
+ file.write(f"<h1>Prompt:</h1> <p>{prompt}</p> <h1>Response:</h1> <p>{response}</p>")
57
+ elif filename.endswith(".md"):
58
+ with open(filename, 'w') as file:
59
+ file.write(f"# Prompt:\n{prompt}\n# Response:\n{response}")
60
+
61
+ def get_table_download_link_old(file_path):
62
+ with open(file_path, 'r') as file:
63
+ data = file.read()
64
+ b64 = base64.b64encode(data.encode()).decode()
65
+ href = f'<a href="data:file/htm;base64,{b64}" target="_blank" download="{os.path.basename(file_path)}">{os.path.basename(file_path)}</a>'
66
+ return href
67
+
68
+ def get_table_download_link(file_path):
69
+ import os
70
+ import base64
71
+ with open(file_path, 'r') as file:
72
+ data = file.read()
73
+ b64 = base64.b64encode(data.encode()).decode()
74
+ file_name = os.path.basename(file_path)
75
+ ext = os.path.splitext(file_name)[1] # get the file extension
76
+
77
+ if ext == '.txt':
78
+ mime_type = 'text/plain'
79
+ elif ext == '.htm':
80
+ mime_type = 'text/html'
81
+ elif ext == '.md':
82
+ mime_type = 'text/markdown'
83
+ else:
84
+ mime_type = 'application/octet-stream' # general binary data type
85
+
86
+ href = f'<a href="data:{mime_type};base64,{b64}" target="_blank" download="{file_name}">{file_name}</a>'
87
+ return href
88
+
89
+ def CompressXML(xml_text):
90
+ root = ET.fromstring(xml_text)
91
+ for elem in list(root.iter()):
92
+ if isinstance(elem.tag, str) and 'Comment' in elem.tag:
93
+ elem.parent.remove(elem)
94
+ return ET.tostring(root, encoding='unicode', method="xml")
95
+
96
+ def read_file_content(file,max_length):
97
+ if file.type == "application/json":
98
+ content = json.load(file)
99
+ return str(content)
100
+ elif file.type == "text/html" or file.type == "text/htm":
101
+ content = BeautifulSoup(file, "html.parser")
102
+ return content.text
103
+ elif file.type == "application/xml" or file.type == "text/xml":
104
+ tree = ET.parse(file)
105
+ root = tree.getroot()
106
+ xml = CompressXML(ET.tostring(root, encoding='unicode'))
107
+ return xml
108
+ elif file.type == "text/markdown" or file.type == "text/md":
109
+ md = mistune.create_markdown()
110
+ content = md(file.read().decode())
111
+ return content
112
+ elif file.type == "text/plain":
113
+ return file.getvalue().decode()
114
+ else:
115
+ return ""
116
+
117
+ def main():
118
+ prompts = ['']
119
+ file_content = ""
120
+ user_prompt = st.text_area("Your question:", '', height=120)
121
+ uploaded_file = st.file_uploader("Choose a file", type=["xml", "json", "html", "htm", "md", "txt"])
122
+
123
+ if user_prompt:
124
+ prompts.append(user_prompt)
125
+
126
+ if uploaded_file is not None:
127
+ file_content = read_file_content(uploaded_file, max_length)
128
+ prompts.append(file_content)
129
+
130
+ if st.button('💬 Chat'):
131
+ st.write('Thinking and Reasoning with your inputs...')
132
+ response = chat_with_model(prompts)
133
+ st.write('Response:')
134
+ st.write(response)
135
+
136
+ filename = generate_filename(user_prompt, choice)
137
+ create_file(filename, user_prompt, response)
138
+ st.sidebar.markdown(get_table_download_link(filename), unsafe_allow_html=True)
139
+
140
+ if len(file_content) > 0:
141
+ st.markdown(f"**File Content Added:**\n{file_content}")
142
+
143
+ all_files = glob.glob("*.txt") + glob.glob("*.htm") + glob.glob("*.md")
144
+ for file in all_files:
145
+ col1, col2 = st.sidebar.columns([4,1]) # adjust the ratio as needed
146
+ with col1:
147
+ st.markdown(get_table_download_link(file), unsafe_allow_html=True)
148
+ with col2:
149
+ if st.button("🗑", key=file):
150
+ os.remove(file)
151
+ st.experimental_rerun()
152
+
153
+ if __name__ == "__main__":
154
+ main()