Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -46,7 +46,52 @@ def chat_with_model(prompt, document_section):
|
|
46 |
response = openai.ChatCompletion.create(model=model, messages=conversation)
|
47 |
return response['choices'][0]['message']['content']
|
48 |
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
def main():
|
52 |
user_prompt = st.text_area("Your question:", '', height=120)
|
|
|
46 |
response = openai.ChatCompletion.create(model=model, messages=conversation)
|
47 |
return response['choices'][0]['message']['content']
|
48 |
|
49 |
+
|
50 |
+
def get_table_download_link(file_path):
|
51 |
+
with open(file_path, 'r') as file:
|
52 |
+
data = file.read()
|
53 |
+
b64 = base64.b64encode(data.encode()).decode()
|
54 |
+
file_name = os.path.basename(file_path)
|
55 |
+
ext = os.path.splitext(file_name)[1] # get the file extension
|
56 |
+
if ext == '.txt':
|
57 |
+
mime_type = 'text/plain'
|
58 |
+
elif ext == '.htm':
|
59 |
+
mime_type = 'text/html'
|
60 |
+
elif ext == '.md':
|
61 |
+
mime_type = 'text/markdown'
|
62 |
+
else:
|
63 |
+
mime_type = 'application/octet-stream' # general binary data type
|
64 |
+
href = f'<a href="data:{mime_type};base64,{b64}" target="_blank" download="{file_name}">{file_name}</a>'
|
65 |
+
return href
|
66 |
+
|
67 |
+
|
68 |
+
def CompressXML(xml_text):
|
69 |
+
root = ET.fromstring(xml_text)
|
70 |
+
for elem in list(root.iter()):
|
71 |
+
if isinstance(elem.tag, str) and 'Comment' in elem.tag:
|
72 |
+
elem.parent.remove(elem)
|
73 |
+
return ET.tostring(root, encoding='unicode', method="xml")
|
74 |
+
|
75 |
+
def read_file_content(file,max_length):
|
76 |
+
if file.type == "application/json":
|
77 |
+
content = json.load(file)
|
78 |
+
return str(content)
|
79 |
+
elif file.type == "text/html" or file.type == "text/htm":
|
80 |
+
content = BeautifulSoup(file, "html.parser")
|
81 |
+
return content.text
|
82 |
+
elif file.type == "application/xml" or file.type == "text/xml":
|
83 |
+
tree = ET.parse(file)
|
84 |
+
root = tree.getroot()
|
85 |
+
xml = CompressXML(ET.tostring(root, encoding='unicode'))
|
86 |
+
return xml
|
87 |
+
elif file.type == "text/markdown" or file.type == "text/md":
|
88 |
+
md = mistune.create_markdown()
|
89 |
+
content = md(file.read().decode())
|
90 |
+
return content
|
91 |
+
elif file.type == "text/plain":
|
92 |
+
return file.getvalue().decode()
|
93 |
+
else:
|
94 |
+
return ""
|
95 |
|
96 |
def main():
|
97 |
user_prompt = st.text_area("Your question:", '', height=120)
|