Spaces:
Sleeping
Sleeping
File size: 9,576 Bytes
66f321d 87e0a70 66f321d 87e0a70 66f321d 87e0a70 66f321d 87e0a70 66f321d 87e0a70 66f321d 87e0a70 66f321d 87e0a70 66f321d 87e0a70 66f321d 87e0a70 66f321d 87e0a70 66f321d 87e0a70 66f321d 87e0a70 66f321d 87e0a70 66f321d 87e0a70 66f321d 87e0a70 66f321d 87e0a70 66f321d 87e0a70 66f321d |
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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# app.py
import streamlit as st
import sqlite3
from passlib.hash import pbkdf2_sha256
from flask import Flask, jsonify, request
import uuid
import threading
# Initialize Flask app
flask_app = Flask(__name__)
flask_app.secret_key = str(uuid.uuid4())
# Database setup
def init_db():
conn = sqlite3.connect('conntribe.db')
c = conn.cursor()
# Create users table
c.execute('''CREATE TABLE IF NOT EXISTS users
(id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
email TEXT UNIQUE NOT NULL,
whatsapp TEXT UNIQUE NOT NULL,
sem INTEGER NOT NULL,
password TEXT NOT NULL)''')
# Create projects table
c.execute('''CREATE TABLE IF NOT EXISTS projects
(id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
description TEXT,
members_needed INTEGER NOT NULL,
whatsapp TEXT NOT NULL,
user_id INTEGER NOT NULL,
FOREIGN KEY(user_id) REFERENCES users(id))''')
conn.commit()
conn.close()
init_db()
# Flask API Endpoints
@flask_app.route('/api/signup', methods=['POST'])
def signup():
data = request.json
try:
conn = sqlite3.connect('conntribe.db')
c = conn.cursor()
hashed_password = pbkdf2_sha256.hash(data['password'])
c.execute('INSERT INTO users (name, email, whatsapp, sem, password) VALUES (?, ?, ?, ?, ?)',
(data['name'], data['email'], data['whatsapp'], data['sem'], hashed_password))
conn.commit()
return jsonify({'message': 'User created successfully'}), 201
except sqlite3.IntegrityError:
return jsonify({'error': 'Email or WhatsApp number already exists'}), 400
finally:
conn.close()
@flask_app.route('/api/login', methods=['POST'])
def login():
data = request.json
conn = sqlite3.connect('conntribe.db')
c = conn.cursor()
c.execute('SELECT * FROM users WHERE email = ? OR whatsapp = ?', (data['username'], data['username']))
user = c.fetchone()
conn.close()
if user and pbkdf2_sha256.verify(data['password'], user[5]):
return jsonify({
'id': user[0],
'name': user[1],
'email': user[2],
'whatsapp': user[3]
}), 200
return jsonify({'error': 'Invalid credentials'}), 401
@flask_app.route('/api/projects', methods=['GET', 'POST'])
def projects():
if request.method == 'GET':
conn = sqlite3.connect('conntribe.db')
c = conn.cursor()
c.execute('SELECT projects.*, users.name FROM projects JOIN users ON projects.user_id = users.id')
projects = c.fetchall()
conn.close()
return jsonify([{
'id': p[0],
'title': p[1],
'description': p[2],
'members_needed': p[3],
'whatsapp': p[4],
'author': p[6]
} for p in projects]), 200
elif request.method == 'POST':
data = request.json
try:
conn = sqlite3.connect('conntribe.db')
c = conn.cursor()
c.execute('INSERT INTO projects (title, description, members_needed, whatsapp, user_id) VALUES (?, ?, ?, ?, ?)',
(data['title'], data.get('description'), data['members_needed'], data['whatsapp'], data['user_id']))
conn.commit()
return jsonify({'message': 'Project created successfully'}), 201
finally:
conn.close()
# Streamlit UI
def main():
st.set_page_config(page_title="ConnTribe", page_icon="π€", layout="wide")
if 'user' not in st.session_state:
st.session_state.user = None
# Navigation
if st.session_state.user:
tabs = st.sidebar.radio("Navigation", ["Home", "Projects"])
else:
tabs = st.sidebar.radio("Navigation", ["Login", "Sign Up"])
# Authentication Pages
if not st.session_state.user:
if tabs == "Login":
with st.form("Login"):
st.header("Login to ConnTribe")
username = st.text_input("Email/WhatsApp Number")
password = st.text_input("Password", type="password")
if st.form_submit_button("Login"):
response = requests.post('http://localhost:5000/api/login',
json={'username': username, 'password': password})
if response.status_code == 200:
st.session_state.user = response.json()
st.experimental_rerun()
else:
st.error("Invalid credentials")
elif tabs == "Sign Up":
with st.form("Sign Up"):
st.header("Create New Account")
name = st.text_input("Full Name")
email = st.text_input("Email")
whatsapp = st.text_input("WhatsApp Number")
sem = st.number_input("Current Semester", min_value=1, max_value=8)
password = st.text_input("Password", type="password")
confirm_password = st.text_input("Confirm Password", type="password")
if st.form_submit_button("Sign Up"):
if password != confirm_password:
st.error("Passwords do not match")
else:
response = requests.post('http://localhost:5000/api/signup',
json={'name': name, 'email': email,
'whatsapp': whatsapp, 'sem': sem,
'password': password})
if response.status_code == 201:
st.success("Account created successfully! Please login.")
else:
st.error(response.json().get('error', 'Registration failed'))
# Authenticated Pages
else:
st.sidebar.header(f"Welcome, {st.session_state.user['name']}")
if st.sidebar.button("Logout"):
st.session_state.user = None
st.experimental_rerun()
if tabs == "Home":
st.header("Welcome to ConnTribe π")
st.markdown("""
### Your Project Collaboration Hub
**ConnTribe** helps you connect with fellow students to collaborate on projects. Here's how to use it:
1. **Create a Project**: Share your project details and required team members
2. **Browse Projects**: Explore existing projects looking for collaborators
3. **Join Projects**: Connect with project owners directly via WhatsApp
Start collaborating today and build amazing things together! π
""")
elif tabs == "Projects":
st.header("Project Marketplace")
# Create Project
with st.expander("Create New Project"):
with st.form("New Project"):
title = st.text_input("Project Title*")
description = st.text_area("Description")
members_needed = st.number_input("Members Needed*", min_value=1)
whatsapp = st.text_input("Contact WhatsApp*", value=st.session_state.user['whatsapp'])
if st.form_submit_button("Post Project"):
if title and members_needed and whatsapp:
response = requests.post('http://localhost:5000/api/projects',
json={'title': title, 'description': description,
'members_needed': members_needed,
'whatsapp': whatsapp,
'user_id': st.session_state.user['id']})
if response.status_code == 201:
st.success("Project posted successfully!")
else:
st.error("Failed to post project")
# Display Projects
st.subheader("Available Projects")
response = requests.get('http://localhost:5000/api/projects')
if response.status_code == 200:
projects = response.json()
for project in projects:
with st.container():
st.markdown(f"""
### {project['title']}
**By**: {project['author']}
**Members Needed**: {project['members_needed']}
**Description**: {project['description'] or 'No description provided'}
""")
if st.button(f"Join {project['title']}", key=project['id']):
st.markdown(f"[Contact on WhatsApp](https://wa.me/{project['whatsapp']})")
st.markdown("---")
else:
st.warning("No projects available yet. Be the first to post one!")
# Run Flask in background
def run_flask():
flask_app.run(host='0.0.0.0', port=5000, threaded=True)
if __name__ == "__main__":
threading.Thread(target=run_flask, daemon=True).start()
main() |