awacke1 commited on
Commit
a02f054
β€’
1 Parent(s): afd67ae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -2
app.py CHANGED
@@ -3,6 +3,8 @@ 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
@@ -22,7 +24,6 @@ def chat_with_model(prompts):
22
 
23
  def generate_filename(prompt):
24
  safe_date_time = datetime.now().strftime("%m_%d_%H_%M")
25
-
26
  safe_prompt = "".join(x for x in prompt if x.isalnum())[:50]
27
  return f"{safe_date_time}_{safe_prompt}.htm"
28
 
@@ -33,20 +34,40 @@ def create_file(filename, prompt, response):
33
  def get_table_download_link(file_path):
34
  with open(file_path, 'r') as file:
35
  data = file.read()
36
- b64 = base64.b64encode(data.encode()).decode()
37
  href = f'<a href="data:file/htm;base64,{b64}" target="_blank" download="{os.path.basename(file_path)}">{os.path.basename(file_path)}</a>'
38
  return href
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  def main():
41
  st.title("Chat with AI")
42
 
43
  prompts = ['']
44
 
45
  user_prompt = st.text_area("Your question:", '', height=120)
 
46
 
47
  if user_prompt:
48
  prompts.append(user_prompt)
49
 
 
 
 
 
50
  if st.button('Chat'):
51
  st.write('Chatting with GPT-3...')
52
  response = chat_with_model(prompts)
 
3
  import os
4
  import base64
5
  import glob
6
+ import json
7
+ from bs4 import BeautifulSoup
8
  from datetime import datetime
9
  from dotenv import load_dotenv
10
  from openai import ChatCompletion
 
24
 
25
  def generate_filename(prompt):
26
  safe_date_time = datetime.now().strftime("%m_%d_%H_%M")
 
27
  safe_prompt = "".join(x for x in prompt if x.isalnum())[:50]
28
  return f"{safe_date_time}_{safe_prompt}.htm"
29
 
 
34
  def get_table_download_link(file_path):
35
  with open(file_path, 'r') as file:
36
  data = file.read()
37
+ b64 = base64.b64encode(data.encode()).decode()
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 read_file_content(file):
42
+ if file.type == "application/json":
43
+ content = json.load(file)
44
+ return str(content)
45
+ elif file.type == "text/html":
46
+ content = BeautifulSoup(file, "html.parser")
47
+ return content.text
48
+ elif file.type == "application/xml" or file.type == "text/xml":
49
+ content = BeautifulSoup(file, "lxml")
50
+ return content.text
51
+ elif file.type == "text/plain":
52
+ return file.getvalue().decode()
53
+ else:
54
+ return ""
55
+
56
  def main():
57
  st.title("Chat with AI")
58
 
59
  prompts = ['']
60
 
61
  user_prompt = st.text_area("Your question:", '', height=120)
62
+ uploaded_file = st.file_uploader("Choose a file", type=["xml", "json", "htm", "txt"])
63
 
64
  if user_prompt:
65
  prompts.append(user_prompt)
66
 
67
+ if uploaded_file is not None:
68
+ file_content = read_file_content(uploaded_file)
69
+ prompts.append(file_content)
70
+
71
  if st.button('Chat'):
72
  st.write('Chatting with GPT-3...')
73
  response = chat_with_model(prompts)