Spaces:
Sleeping
Sleeping
File size: 6,270 Bytes
82ac72c |
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 |
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)
@app.route('/register', methods=['GET', 'POST'])
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')
@app.route('/login', methods=['GET', 'POST'])
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')
@app.route('/dashboard')
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)
@app.route('/create_issue', methods=['GET', 'POST'])
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')
@app.route('/invite_collaborators/<int:issue_id>', methods=['GET', 'POST'])
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)
@app.route('/submit_solution/<int:issue_id>', methods=['GET', 'POST'])
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)
@app.route('/decide/<int:issue_id>', methods=['GET', 'POST'])
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) |