Spaces:
Sleeping
Sleeping
| from flask import Flask, render_template, request, redirect, url_for, session | |
| from flask_sqlalchemy import SQLAlchemy | |
| from flask_bcrypt import Bcrypt | |
| from flask_mail import Mail, Message | |
| import openai | |
| app = Flask(__name__) | |
| app.secret_key = 's3cr3t' | |
| app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///sociocracy.db' | |
| db = SQLAlchemy(app) | |
| bcrypt = Bcrypt(app) | |
| # Configuring Flask-Mail | |
| app.config['MAIL_SERVER'] = 'smtp.freesmtpservers.com' | |
| app.config['MAIL_PORT'] = 25 | |
| app.config['MAIL_USERNAME'] = 'testmail@wdwtcom' | |
| app.config['MAIL_PASSWORD'] = '' | |
| app.config['MAIL_USE_TLS'] = False | |
| app.config['MAIL_USE_SSL'] = False | |
| mail = Mail(app) | |
| # User model | |
| class User(db.Model): | |
| id = db.Column(db.Integer, primary_key=True) | |
| username = db.Column(db.String(50), unique=True, nullable=False) | |
| password = db.Column(db.String(100), nullable=False) | |
| email = db.Column(db.String(100), unique=True, nullable=False) | |
| # Issue model | |
| class Issue(db.Model): | |
| id = db.Column(db.Integer, primary_key=True) | |
| title = db.Column(db.String(100), nullable=False) | |
| description = db.Column(db.Text, nullable=False) | |
| created_by = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) | |
| # Solution model | |
| class Solution(db.Model): | |
| id = db.Column(db.Integer, primary_key=True) | |
| content = db.Column(db.Text, nullable=False) | |
| issue_id = db.Column(db.Integer, db.ForeignKey('issue.id'), nullable=False) | |
| created_by = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) | |
| # Collaborator model | |
| class Collaborator(db.Model): | |
| id = db.Column(db.Integer, primary_key=True) | |
| issue_id = db.Column(db.Integer, db.ForeignKey('issue.id'), nullable=False) | |
| user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) | |
| def register(): | |
| if request.method == 'POST': | |
| hashed_pw = bcrypt.generate_password_hash(request.form['password']).decode('utf-8') | |
| new_user = User(username=request.form['username'], email=request.form['email'], password=hashed_pw) | |
| db.session.add(new_user) | |
| db.session.commit() | |
| return redirect(url_for('login')) | |
| return render_template('register.html') | |
| def login(): | |
| if request.method == 'POST': | |
| user = User.query.filter_by(username=request.form['username']).first() | |
| if user and bcrypt.check_password_hash(user.password, request.form['password']): | |
| session['user_id'] = user.id | |
| return redirect(url_for('dashboard')) | |
| return render_template('login.html') | |
| def dashboard(): | |
| user_id = session['user_id'] | |
| issues = Issue.query.filter_by(created_by=user_id).all() | |
| collaboration_issues = Issue.query.join(Collaborator).filter(Collaborator.user_id == user_id).all() | |
| return render_template('dashboard.html', issues=issues, collaboration_issues=collaboration_issues) | |
| def create_issue(): | |
| if request.method == 'POST': | |
| new_issue = Issue(title=request.form['title'], description=request.form['description'], created_by=session['user_id']) | |
| db.session.add(new_issue) | |
| db.session.commit() | |
| return redirect(url_for('invite_collaborators', issue_id=new_issue.id)) | |
| return render_template('create_issue.html') | |
| def invite_collaborators(issue_id): | |
| if request.method == 'POST': | |
| emails = request.form['emails'].split(',') | |
| issue = Issue.query.get(issue_id) | |
| for email in emails: | |
| user = User.query.filter_by(email=email.strip()).first() | |
| if user: | |
| new_collaborator = Collaborator(issue_id=issue_id, user_id=user.id) | |
| db.session.add(new_collaborator) | |
| msg = Message('Collaboration Invite', sender='[email protected]', recipients=[email]) | |
| msg.body = f"You have been invited to collaborate on the issue: {issue.title}." | |
| mail.send(msg) | |
| db.session.commit() | |
| return redirect(url_for('dashboard')) | |
| return render_template('invite_collaborators.html', issue_id=issue_id) | |
| def submit_solution(issue_id): | |
| if request.method == 'POST': | |
| new_solution = Solution(content=request.form['solution'], issue_id=issue_id, created_by=session['user_id']) | |
| db.session.add(new_solution) | |
| db.session.commit() | |
| return redirect(url_for('dashboard')) | |
| return render_template('submit_solution.html', issue_id=issue_id) | |
| def decide(issue_id): | |
| issue = Issue.query.get(issue_id) | |
| solutions = Solution.query.filter_by(issue_id=issue_id).all() | |
| if request.method == 'POST': | |
| # AI Prompt to get the optimal decision suggestion | |
| openai.api_key = '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' | |
| prompt = f"Given the following solutions for the issue '{issue.title}', which one is the most socio-democratic decision? Do not mention socio-democratic in your response.\n" | |
| for solution in solutions: | |
| prompt += f"- {solution.content}\n" | |
| prompt += "Please consider socio-democratic values and provide a summary on the best decision. Do not mention socio-democratic in your response." | |
| # Use gpt-3.5-turbo | |
| response = openai.ChatCompletion.create( | |
| model="gpt-3.5-turbo", | |
| messages=[ | |
| {"role": "system", "content": "You are an assistant helping to make socio-democratic decisions. Do not mention socio-democratic in your response"}, | |
| {"role": "user", "content": prompt} | |
| ], | |
| max_tokens=150 | |
| ) | |
| decision = response.choices[0].message['content'].strip() | |
| return render_template('decision.html', decision=decision) | |
| return render_template('decide.html', issue=issue, solutions=solutions) | |
| if __name__ == '__main__': | |
| with app.app_context(): | |
| db.create_all() | |
| app.run(debug=True) |