import streamlit as st import sqlite3 import hashlib import os from git import Repo # Required for cloning GitHub repositories # Database setup DB_FILE = "users.db" def create_user_table(): conn = sqlite3.connect(DB_FILE) cursor = conn.cursor() cursor.execute(""" CREATE TABLE IF NOT EXISTS users ( username TEXT PRIMARY KEY, password TEXT ) """) conn.commit() conn.close() def add_user(username, password): conn = sqlite3.connect(DB_FILE) cursor = conn.cursor() hashed_password = hashlib.sha256(password.encode()).hexdigest() try: cursor.execute("INSERT INTO users (username, password) VALUES (?, ?)", (username, hashed_password)) conn.commit() except sqlite3.IntegrityError: st.error("Username already exists. Please choose a different username.") conn.close() def authenticate_user(username, password): conn = sqlite3.connect(DB_FILE) cursor = conn.cursor() hashed_password = hashlib.sha256(password.encode()).hexdigest() cursor.execute("SELECT * FROM users WHERE username = ? AND password = ?", (username, hashed_password)) user = cursor.fetchone() conn.close() return user # Main application def main(): st.title("SimplifAI") # Initialize database create_user_table() # Manage session state if "authenticated" not in st.session_state: st.session_state.authenticated = False if "username" not in st.session_state: st.session_state.username = None if "page" not in st.session_state: st.session_state.page = "login" # Page routing logic if st.session_state.page == "login": login_page() elif st.session_state.page == "workspace": workspace_page() def login_page(): st.subheader("Please Log In or Register to Continue") auth_mode = st.radio("Choose an Option", ["Log In", "Register"], horizontal=True) if auth_mode == "Log In": st.subheader("Log In") username = st.text_input("Username", key="login_username") password = st.text_input("Password", type="password", key="login_password") # Handle single-click login if st.button("Log In"): if authenticate_user(username, password): st.session_state.authenticated = True st.session_state.username = username st.session_state.page = "workspace" else: st.error("Invalid username or password. Please try again.") elif auth_mode == "Register": st.subheader("Register") username = st.text_input("Create Username", key="register_username") password = st.text_input("Create Password", type="password", key="register_password") # Handle single-click registration if st.button("Register"): if username and password: add_user(username, password) st.success("Account created successfully! You can now log in.") else: st.error("Please fill in all fields.") def workspace_page(): # Sidebar with logout button st.sidebar.title(f"Hello, {st.session_state.username}!") if st.sidebar.button("Log Out"): st.session_state.authenticated = False st.session_state.username = None st.session_state.page = "login" # Main content area st.subheader("Workspace") st.write("This is your personal workspace. You can upload files for a coding project or provide a GitHub repository link.") # User action selection action = st.radio("Choose an action", ["Upload Project Files", "Clone GitHub Repository"], horizontal=True) # Handle file uploads as a project if action == "Upload Project Files": st.subheader("Upload Project Files") project_name = st.text_input("Enter a name for your project") uploaded_files = st.file_uploader( "Upload one or more files for your project", accept_multiple_files=True ) if project_name and uploaded_files: if st.button("Upload Project"): user_folder = os.path.join("user_projects", st.session_state.username) project_folder = os.path.join(user_folder, project_name) if not os.path.exists(project_folder): os.makedirs(project_folder) for uploaded_file in uploaded_files: file_path = os.path.join(project_folder, uploaded_file.name) with open(file_path, "wb") as f: f.write(uploaded_file.getbuffer()) st.success(f"Project '{project_name}' uploaded successfully with {len(uploaded_files)} file(s)!") elif not project_name: st.warning("Please enter a project name.") elif not uploaded_files: st.warning("Please upload at least one file.") # Handle GitHub repository cloning elif action == "Clone GitHub Repository": st.subheader("Clone GitHub Repository") repo_url = st.text_input("Enter the GitHub repository URL") if st.button("Clone Repository"): if repo_url: user_folder = os.path.join("user_projects", st.session_state.username) os.makedirs(user_folder, exist_ok=True) repo_name = repo_url.split("/")[-1].replace(".git", "") repo_path = os.path.join(user_folder, repo_name) if not os.path.exists(repo_path): try: Repo.clone_from(repo_url, repo_path) st.success(f"Repository '{repo_name}' cloned successfully!") except Exception as e: st.error(f"Failed to clone repository: {e}") else: st.warning(f"Repository '{repo_name}' already exists in your workspace.") if __name__ == "__main__": main()