File size: 5,974 Bytes
5e51056
4f98e67
 
8c27bb6
 
 
5e51056
4f98e67
 
5e51056
4f98e67
 
 
 
 
 
 
 
 
 
 
5e51056
4f98e67
 
 
 
 
 
 
 
 
 
5e51056
4f98e67
 
 
 
 
 
 
 
5e51056
4f98e67
5e51056
6e97b16
5e51056
4f98e67
 
5e51056
77cb200
4f98e67
 
8a5a9ad
 
02d5a36
77cb200
02d5a36
77cb200
02d5a36
 
 
 
 
 
 
 
 
 
 
 
 
 
77cb200
02d5a36
 
 
 
 
 
 
77cb200
02d5a36
 
 
 
 
77cb200
02d5a36
 
 
 
 
 
 
 
 
 
 
 
 
 
4f98e67
02d5a36
df99691
273c401
 
 
839c84a
273c401
839c84a
 
 
 
273c401
839c84a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
273c401
839c84a
 
 
 
 
273c401
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
839c84a
273c401
 
 
839c84a
5e51056
 
06e2745
77cb200
273c401
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
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()