awacke1 commited on
Commit
f6ff404
β€’
1 Parent(s): 9a60d11

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -31
app.py CHANGED
@@ -6,6 +6,7 @@ import glob
6
  import json
7
  import mistune
8
  import pytz
 
9
 
10
  from datetime import datetime
11
  from openai import ChatCompletion
@@ -58,21 +59,12 @@ def create_file(filename, prompt, response):
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'
@@ -81,7 +73,7 @@ def get_table_download_link(file_path):
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
@@ -115,34 +107,40 @@ def read_file_content(file,max_length):
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:
@@ -151,4 +149,4 @@ def main():
151
  st.experimental_rerun()
152
 
153
  if __name__ == "__main__":
154
- main()
 
6
  import json
7
  import mistune
8
  import pytz
9
+ import textwrap
10
 
11
  from datetime import datetime
12
  from openai import ChatCompletion
 
59
  with open(filename, 'w') as file:
60
  file.write(f"# Prompt:\n{prompt}\n# Response:\n{response}")
61
 
 
 
 
 
 
 
 
62
  def get_table_download_link(file_path):
 
 
63
  with open(file_path, 'r') as file:
64
  data = file.read()
65
  b64 = base64.b64encode(data.encode()).decode()
66
  file_name = os.path.basename(file_path)
67
+ ext = os.path.splitext(file_name)[1]
68
 
69
  if ext == '.txt':
70
  mime_type = 'text/plain'
 
73
  elif ext == '.md':
74
  mime_type = 'text/markdown'
75
  else:
76
+ mime_type = 'application/octet-stream'
77
 
78
  href = f'<a href="data:{mime_type};base64,{b64}" target="_blank" download="{file_name}">{file_name}</a>'
79
  return href
 
107
  return ""
108
 
109
  def main():
 
 
110
  user_prompt = st.text_area("Your question:", '', height=120)
111
  uploaded_file = st.file_uploader("Choose a file", type=["xml", "json", "html", "htm", "md", "txt"])
112
 
 
 
 
 
 
 
 
113
  if st.button('πŸ’¬ Chat'):
114
  st.write('Thinking and Reasoning with your inputs...')
115
+ file_content = ""
 
 
116
 
117
+ if user_prompt:
118
+ prompts = textwrap.wrap(user_prompt, max_length)
119
+ for prompt in prompts:
120
+ response = chat_with_model([prompt])
121
+ st.write('Response:')
122
+ st.write(response)
123
+ filename = generate_filename(prompt, choice)
124
+ create_file(filename, prompt, response)
125
+ st.sidebar.markdown(get_table_download_link(filename), unsafe_allow_html=True)
126
+
127
+ if uploaded_file is not None:
128
+ file_content = read_file_content(uploaded_file, max_length)
129
+ document_parts = textwrap.wrap(file_content, max_length)
130
+ for part in document_parts:
131
+ response = chat_with_model([part])
132
+ st.write('Response:')
133
+ st.write(response)
134
+ filename = generate_filename(part, choice)
135
+ create_file(filename, part, response)
136
+ st.sidebar.markdown(get_table_download_link(filename), unsafe_allow_html=True)
137
+
138
+ if len(file_content) > 0:
139
+ st.text_area("File Content:", file_content, height=400) # Display file content in a scrollable text box
140
 
141
  all_files = glob.glob("*.txt") + glob.glob("*.htm") + glob.glob("*.md")
142
  for file in all_files:
143
+ col1, col2 = st.sidebar.columns([4,1])
144
  with col1:
145
  st.markdown(get_table_download_link(file), unsafe_allow_html=True)
146
  with col2:
 
149
  st.experimental_rerun()
150
 
151
  if __name__ == "__main__":
152
+ main()