awacke1's picture
Update app.py
d158e71
import os
import streamlit as st
def list_files():
icon_csv = "πŸ“„ "
icon_txt = "πŸ“‘ "
current_directory = os.getcwd()
file_list = []
for filename in os.listdir(current_directory):
if filename.endswith(".csv"):
file_list.append(icon_csv + filename)
elif filename.endswith(".txt"):
file_list.append(icon_txt + filename)
return file_list
def read_file(file_path):
try:
with open(file_path, "r") as file:
contents = file.read()
return f"{contents}"
except FileNotFoundError:
return "File not found."
def delete_file(file_path):
try:
os.remove(file_path)
return f"{file_path} has been deleted."
except FileNotFoundError:
return "File not found."
def write_file(file_path, content):
try:
with open(file_path, "w") as file:
file.write(content)
return f"Successfully written to {file_path}."
except:
return "Error occurred while writing to file."
def append_file(file_path, content):
try:
with open(file_path, "a") as file:
file.write(content)
return f"Successfully appended to {file_path}."
except:
return "Error occurred while appending to file."
st.set_page_config(layout='wide')
st.title("AI Feedback Memory System for Smart Communities")
# Sidebar
with st.sidebar:
st.subheader("Download Files")
file_list = list_files()
if file_list:
for file in file_list:
if st.button(file):
file_path = os.path.join(os.getcwd(), file[2:])
st.markdown(f"### {file}")
st.markdown(f"[Download]({file_path})")
else:
st.markdown("No .csv or .txt files found in the current directory.")
# Main content
fileName = st.text_input("Filename")
fileContent = st.text_area("File Content")
completedMessage_placeholder = st.empty()
col1, col2, col3, col4, col5 = st.columns(5)
listFiles = col1.button("πŸ“„ List File(s)")
readFile = col2.button("πŸ“– Read File")
saveFile = col3.button("πŸ’Ύ Save File")
deleteFile = col4.button("πŸ—‘οΈ Delete File")
appendFile = col5.button("βž• Append File")
if listFiles:
fileContent = "\n".join(list_files())
st.text_area("File Content", fileContent)
elif readFile:
fileContent = read_file(fileName)
st.text_area("File Content", fileContent)
elif saveFile:
completedMessage = write_file(fileName, fileContent)
completedMessage_placeholder.text(completedMessage)
elif deleteFile:
completedMessage = delete_file(fileName)
completedMessage_placeholder.text(completedMessage)
elif appendFile:
completedMessage = append_file(fileName, fileContent)
completedMessage_placeholder.text(completedMessage)
st.markdown("""
πŸ‘πŸ§ πŸš€
πŸ€–πŸ’­πŸ“ˆ
πŸ“πŸ€£πŸŒž
πŸ’―πŸ‘¨β€πŸ’ΌπŸ’¬
πŸ‘‹πŸ˜„πŸŒ‡
πŸ“±πŸ’»πŸ”œ
The new πŸ€– AI Feedback Memory System for Smart Communities πŸ‘πŸ§ πŸš€
""")