awacke1 commited on
Commit
51bd956
·
1 Parent(s): 6231b89

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -97
app.py CHANGED
@@ -6,136 +6,73 @@ 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("Output file type:", menu)
22
- choicePrefix = "Output file type is "
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
- with open(file_path, 'r') as file:
70
- data = file.read()
71
- b64 = base64.b64encode(data.encode()).decode()
72
- file_name = os.path.basename(file_path)
73
- ext = os.path.splitext(file_name)[1] # get the file extension
74
- if ext == '.txt':
75
- mime_type = 'text/plain'
76
- elif ext == '.htm':
77
- mime_type = 'text/html'
78
- elif ext == '.md':
79
- mime_type = 'text/markdown'
80
- else:
81
- mime_type = 'application/octet-stream' # general binary data type
82
- href = f'<a href="data:{mime_type};base64,{b64}" target="_blank" download="{file_name}">{file_name}</a>'
83
- return href
84
-
85
- def CompressXML(xml_text):
86
- root = ET.fromstring(xml_text)
87
- for elem in list(root.iter()):
88
- if isinstance(elem.tag, str) and 'Comment' in elem.tag:
89
- elem.parent.remove(elem)
90
- return ET.tostring(root, encoding='unicode', method="xml")
91
-
92
- def read_file_content(file,max_length):
93
- if file.type == "application/json":
94
- content = json.load(file)
95
- return str(content)
96
- elif file.type == "text/html" or file.type == "text/htm":
97
- content = BeautifulSoup(file, "html.parser")
98
- return content.text
99
- elif file.type == "application/xml" or file.type == "text/xml":
100
- tree = ET.parse(file)
101
- root = tree.getroot()
102
- xml = CompressXML(ET.tostring(root, encoding='unicode'))
103
- return xml
104
- elif file.type == "text/markdown" or file.type == "text/md":
105
- md = mistune.create_markdown()
106
- content = md(file.read().decode())
107
- return content
108
- elif file.type == "text/plain":
109
- return file.getvalue().decode()
110
- else:
111
- return ""
112
-
113
  def main():
114
- prompts = ['']
115
- file_content = ""
116
  user_prompt = st.text_area("Your question:", '', height=120)
117
  uploaded_file = st.file_uploader("Choose a file", type=["xml", "json", "html", "htm", "md", "txt"])
 
118
 
119
- if user_prompt:
120
- prompts.append(user_prompt)
121
 
122
  if uploaded_file is not None:
123
  file_content = read_file_content(uploaded_file, max_length)
124
- prompts.append(file_content)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
 
126
  if st.button('💬 Chat'):
127
  st.write('Thinking and Reasoning with your inputs...')
128
- response = chat_with_model(prompts)
129
  st.write('Response:')
130
  st.write(response)
131
 
132
  filename = generate_filename(user_prompt, choice)
133
  create_file(filename, user_prompt, response)
134
  st.sidebar.markdown(get_table_download_link(filename), unsafe_allow_html=True)
135
-
136
- if len(file_content) > 0:
137
- st.markdown(f"**File Content Added:**\n{file_content}")
138
-
139
  all_files = glob.glob("*.txt") + glob.glob("*.htm") + glob.glob("*.md")
140
  for file in all_files:
141
  col1, col2 = st.sidebar.columns([4,1]) # adjust the ratio as needed
@@ -147,4 +84,4 @@ def main():
147
  st.experimental_rerun()
148
 
149
  if __name__ == "__main__":
150
- main()
 
6
  import json
7
  import mistune
8
  import pytz
9
+ import math
10
 
11
  from datetime import datetime
12
  from openai import ChatCompletion
13
  from xml.etree import ElementTree as ET
14
  from bs4 import BeautifulSoup
15
+ from collections import deque
16
 
17
+ # Rest of your code goes here...
18
  openai.api_key = os.getenv('OPENAI_KEY')
19
  st.set_page_config(
20
  page_title="GPT Streamlit Document Reasoner",
21
  layout="wide")
22
 
23
+ # Rest of your code goes here...
 
 
 
 
 
 
 
 
 
 
24
 
25
+ def divide_document(document, max_length):
26
+ # Split document into sections, each of about 2000 words or 4000 characters
27
+ return [document[i:i+max_length] for i in range(0, len(document), max_length)]
28
 
29
+ def chat_with_model(prompt, document_section):
 
 
 
30
  model = "gpt-3.5-turbo"
31
  conversation = [{'role': 'system', 'content': 'You are a helpful assistant.'}]
32
+ conversation.append({'role': 'user', 'content': prompt})
33
+ conversation.append({'role': 'assistant', 'content': document_section})
34
  response = openai.ChatCompletion.create(model=model, messages=conversation)
35
  return response['choices'][0]['message']['content']
36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  def main():
 
 
38
  user_prompt = st.text_area("Your question:", '', height=120)
39
  uploaded_file = st.file_uploader("Choose a file", type=["xml", "json", "html", "htm", "md", "txt"])
40
+ max_length = 4000
41
 
42
+ document_sections = deque()
43
+ document_responses = {}
44
 
45
  if uploaded_file is not None:
46
  file_content = read_file_content(uploaded_file, max_length)
47
+ document_sections.extend(divide_document(file_content, max_length))
48
+
49
+ if len(document_sections) > 0:
50
+ st.markdown("**Sections of the uploaded file:**")
51
+ for i, section in enumerate(list(document_sections)):
52
+ st.markdown(f"**Section {i+1}**\n{section}")
53
+
54
+ st.markdown("**Chat with the model:**")
55
+ for i, section in enumerate(list(document_sections)):
56
+ if i in document_responses:
57
+ st.markdown(f"**Section {i+1}**\n{document_responses[i]}")
58
+ else:
59
+ if st.button(f"Chat about Section {i+1}"):
60
+ st.write('Thinking and Reasoning with your inputs...')
61
+ response = chat_with_model(user_prompt, section)
62
+ st.write('Response:')
63
+ st.write(response)
64
+ document_responses[i] = response
65
 
66
  if st.button('💬 Chat'):
67
  st.write('Thinking and Reasoning with your inputs...')
68
+ response = chat_with_model(user_prompt, ''.join(list(document_sections)))
69
  st.write('Response:')
70
  st.write(response)
71
 
72
  filename = generate_filename(user_prompt, choice)
73
  create_file(filename, user_prompt, response)
74
  st.sidebar.markdown(get_table_download_link(filename), unsafe_allow_html=True)
75
+
 
 
 
76
  all_files = glob.glob("*.txt") + glob.glob("*.htm") + glob.glob("*.md")
77
  for file in all_files:
78
  col1, col2 = st.sidebar.columns([4,1]) # adjust the ratio as needed
 
84
  st.experimental_rerun()
85
 
86
  if __name__ == "__main__":
87
+ main()