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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# import streamlit as st
|
| 2 |
+
# import os
|
| 3 |
+
# from PIL import Image
|
| 4 |
+
# import cv2
|
| 5 |
+
# import pytesseract
|
| 6 |
+
# from docx import Document
|
| 7 |
+
# from pptx import Presentation
|
| 8 |
+
# from io import BytesIO
|
| 9 |
+
# import base64
|
| 10 |
+
# from zipfile import ZipFile
|
| 11 |
+
# import pandas as pd
|
| 12 |
+
|
| 13 |
+
# # Set the path where uploaded files will be stored
|
| 14 |
+
# UPLOAD_FOLDER = 'data/'
|
| 15 |
+
# os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
| 16 |
+
|
| 17 |
+
# # Function to handle login
|
| 18 |
+
# def login():
|
| 19 |
+
# st.subheader("Login")
|
| 20 |
+
# username = st.text_input("Username")
|
| 21 |
+
# password = st.text_input("Password", type="password")
|
| 22 |
+
# if username == "admin" and password == "secret":
|
| 23 |
+
# st.success("You are logged in!")
|
| 24 |
+
# return True
|
| 25 |
+
# else:
|
| 26 |
+
# st.error("Invalid credentials.")
|
| 27 |
+
# return False
|
| 28 |
+
|
| 29 |
+
# # Function to handle file uploads
|
| 30 |
+
# def upload_files():
|
| 31 |
+
# st.subheader("Upload Files")
|
| 32 |
+
# uploaded_files = st.file_uploader("Choose files", type=["pdf", "docx", "pptx", "jpg", "png", "txt"], accept_multiple_files=True)
|
| 33 |
+
# for uploaded_file in uploaded_files:
|
| 34 |
+
# file_path = os.path.join(UPLOAD_FOLDER, uploaded_file.name)
|
| 35 |
+
# with open(file_path, "wb") as f:
|
| 36 |
+
# f.write(uploaded_file.getbuffer())
|
| 37 |
+
# st.success(f"{uploaded_file.name} uploaded successfully!")
|
| 38 |
+
|
| 39 |
+
# # Main function to run the app
|
| 40 |
+
# def main():
|
| 41 |
+
# st.title("Admin Panel")
|
| 42 |
+
|
| 43 |
+
# # Check if user is logged in
|
| 44 |
+
# if not login():
|
| 45 |
+
# st.stop()
|
| 46 |
+
|
| 47 |
+
# # Display uploaded files (optional)
|
| 48 |
+
# files = os.listdir(UPLOAD_FOLDER)
|
| 49 |
+
# st.write("Uploaded Files:")
|
| 50 |
+
# for file in files:
|
| 51 |
+
# st.write(file)
|
| 52 |
+
|
| 53 |
+
# # Upload files
|
| 54 |
+
# upload_files()
|
| 55 |
+
|
| 56 |
+
# if __name__ == "__main__":
|
| 57 |
+
# main()
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
import streamlit as st
|
| 62 |
+
import os
|
| 63 |
+
from PIL import Image
|
| 64 |
+
import cv2
|
| 65 |
+
import pytesseract
|
| 66 |
+
from docx import Document
|
| 67 |
+
from pptx import Presentation
|
| 68 |
+
from io import BytesIO
|
| 69 |
+
import base64
|
| 70 |
+
from zipfile import ZipFile
|
| 71 |
+
import pandas as pd
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
# Set the path where uploaded files will be stored
|
| 75 |
+
UPLOAD_FOLDER = 'data/'
|
| 76 |
+
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
| 77 |
+
|
| 78 |
+
# Function to handle login
|
| 79 |
+
def login():
|
| 80 |
+
st.sidebar.subheader("Login")
|
| 81 |
+
username = st.sidebar.text_input("Username")
|
| 82 |
+
password = st.sidebar.text_input("Password", type="password")
|
| 83 |
+
if st.sidebar.button("Login"):
|
| 84 |
+
if username == "admin" and password == "secret":
|
| 85 |
+
st.session_state.logged_in = True # Set login state to True
|
| 86 |
+
st.sidebar.success("You are logged in!")
|
| 87 |
+
st.rerun() # Rerun the app to refresh the page
|
| 88 |
+
else:
|
| 89 |
+
st.sidebar.error("Invalid credentials.")
|
| 90 |
+
|
| 91 |
+
# Function to handle logout
|
| 92 |
+
def logout():
|
| 93 |
+
if st.sidebar.button("Logout"):
|
| 94 |
+
st.session_state.logged_in = False # Reset login state
|
| 95 |
+
st.sidebar.success("You have been logged out!")
|
| 96 |
+
st.rerun() # Rerun the app to refresh the page
|
| 97 |
+
|
| 98 |
+
# Function to handle file uploads
|
| 99 |
+
def upload_files():
|
| 100 |
+
st.subheader("Upload Files")
|
| 101 |
+
uploaded_files = st.file_uploader("Choose files", type=["pdf", "docx", "pptx", "jpg", "png", "txt"], accept_multiple_files=True)
|
| 102 |
+
if uploaded_files:
|
| 103 |
+
for uploaded_file in uploaded_files:
|
| 104 |
+
file_path = os.path.join(UPLOAD_FOLDER, uploaded_file.name)
|
| 105 |
+
with open(file_path, "wb") as f:
|
| 106 |
+
f.write(uploaded_file.getbuffer())
|
| 107 |
+
st.success(f"✅ {uploaded_file.name} uploaded successfully!") # Green success alert
|
| 108 |
+
st.rerun() # Rerun the app to refresh the file list
|
| 109 |
+
|
| 110 |
+
# Function to display and manage uploaded files
|
| 111 |
+
def manage_files():
|
| 112 |
+
st.subheader("Manage Files")
|
| 113 |
+
files = os.listdir(UPLOAD_FOLDER)
|
| 114 |
+
|
| 115 |
+
if files:
|
| 116 |
+
# Create a DataFrame for all files
|
| 117 |
+
file_data = []
|
| 118 |
+
for file in files:
|
| 119 |
+
file_path = os.path.join(UPLOAD_FOLDER, file)
|
| 120 |
+
file_size = os.path.getsize(file_path) / 1024 # Size in KB
|
| 121 |
+
file_type = file.split(".")[-1].upper() # Extract file extension
|
| 122 |
+
file_data.append({"File Name": file, "Type": file_type, "Size (KB)": round(file_size, 2)})
|
| 123 |
+
|
| 124 |
+
df = pd.DataFrame(file_data)
|
| 125 |
+
|
| 126 |
+
# Display the table
|
| 127 |
+
st.dataframe(df, use_container_width=True)
|
| 128 |
+
|
| 129 |
+
# Add options to delete or download files
|
| 130 |
+
selected_file = st.selectbox("Select a file to manage", files)
|
| 131 |
+
col1, col2 = st.columns(2)
|
| 132 |
+
with col1:
|
| 133 |
+
if st.button("Delete Selected File"):
|
| 134 |
+
os.remove(os.path.join(UPLOAD_FOLDER, selected_file))
|
| 135 |
+
st.error(f"❌ {selected_file} deleted successfully!") # Red error alert
|
| 136 |
+
st.rerun() # Rerun the app to refresh the file list
|
| 137 |
+
with col2:
|
| 138 |
+
with open(os.path.join(UPLOAD_FOLDER, selected_file), "rb") as f:
|
| 139 |
+
st.download_button(
|
| 140 |
+
label="Download Selected File",
|
| 141 |
+
data=f,
|
| 142 |
+
file_name=selected_file,
|
| 143 |
+
mime="application/octet-stream"
|
| 144 |
+
)
|
| 145 |
+
else:
|
| 146 |
+
st.info("No files uploaded yet.")
|
| 147 |
+
|
| 148 |
+
# Main function to run the app
|
| 149 |
+
def main():
|
| 150 |
+
st.title("Admin Panel")
|
| 151 |
+
|
| 152 |
+
# Initialize session state for login
|
| 153 |
+
if "logged_in" not in st.session_state:
|
| 154 |
+
st.session_state.logged_in = False
|
| 155 |
+
|
| 156 |
+
# Check if user is logged in
|
| 157 |
+
if not st.session_state.logged_in:
|
| 158 |
+
login()
|
| 159 |
+
st.stop() # Stop the app if not logged in
|
| 160 |
+
|
| 161 |
+
# Logout button
|
| 162 |
+
logout()
|
| 163 |
+
|
| 164 |
+
# Upload files
|
| 165 |
+
upload_files()
|
| 166 |
+
|
| 167 |
+
# Manage files (table for all files)
|
| 168 |
+
manage_files()
|
| 169 |
+
|
| 170 |
+
if __name__ == "__main__":
|
| 171 |
+
main()
|