Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, render_template, request, redirect, url_for, session
|
2 |
+
from flask_sqlalchemy import SQLAlchemy
|
3 |
+
from flask_bcrypt import Bcrypt
|
4 |
+
from flask_mail import Mail, Message
|
5 |
+
import openai
|
6 |
+
|
7 |
+
app = Flask(__name__)
|
8 |
+
app.secret_key = 's3cr3t'
|
9 |
+
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///sociocracy.db'
|
10 |
+
db = SQLAlchemy(app)
|
11 |
+
bcrypt = Bcrypt(app)
|
12 |
+
|
13 |
+
# Configuring Flask-Mail
|
14 |
+
app.config['MAIL_SERVER'] = 'smtp.freesmtpservers.com'
|
15 |
+
app.config['MAIL_PORT'] = 25
|
16 |
+
app.config['MAIL_USERNAME'] = 'testmail@wdwtcom'
|
17 |
+
app.config['MAIL_PASSWORD'] = ''
|
18 |
+
app.config['MAIL_USE_TLS'] = False
|
19 |
+
app.config['MAIL_USE_SSL'] = False
|
20 |
+
mail = Mail(app)
|
21 |
+
|
22 |
+
# User model
|
23 |
+
class User(db.Model):
|
24 |
+
id = db.Column(db.Integer, primary_key=True)
|
25 |
+
username = db.Column(db.String(50), unique=True, nullable=False)
|
26 |
+
password = db.Column(db.String(100), nullable=False)
|
27 |
+
email = db.Column(db.String(100), unique=True, nullable=False)
|
28 |
+
|
29 |
+
# Issue model
|
30 |
+
class Issue(db.Model):
|
31 |
+
id = db.Column(db.Integer, primary_key=True)
|
32 |
+
title = db.Column(db.String(100), nullable=False)
|
33 |
+
description = db.Column(db.Text, nullable=False)
|
34 |
+
created_by = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
|
35 |
+
|
36 |
+
# Solution model
|
37 |
+
class Solution(db.Model):
|
38 |
+
id = db.Column(db.Integer, primary_key=True)
|
39 |
+
content = db.Column(db.Text, nullable=False)
|
40 |
+
issue_id = db.Column(db.Integer, db.ForeignKey('issue.id'), nullable=False)
|
41 |
+
created_by = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
|
42 |
+
|
43 |
+
# Collaborator model
|
44 |
+
class Collaborator(db.Model):
|
45 |
+
id = db.Column(db.Integer, primary_key=True)
|
46 |
+
issue_id = db.Column(db.Integer, db.ForeignKey('issue.id'), nullable=False)
|
47 |
+
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
|
48 |
+
|
49 |
+
@app.route('/register', methods=['GET', 'POST'])
|
50 |
+
def register():
|
51 |
+
if request.method == 'POST':
|
52 |
+
hashed_pw = bcrypt.generate_password_hash(request.form['password']).decode('utf-8')
|
53 |
+
new_user = User(username=request.form['username'], email=request.form['email'], password=hashed_pw)
|
54 |
+
db.session.add(new_user)
|
55 |
+
db.session.commit()
|
56 |
+
return redirect(url_for('login'))
|
57 |
+
return render_template('register.html')
|
58 |
+
|
59 |
+
@app.route('/login', methods=['GET', 'POST'])
|
60 |
+
def login():
|
61 |
+
if request.method == 'POST':
|
62 |
+
user = User.query.filter_by(username=request.form['username']).first()
|
63 |
+
if user and bcrypt.check_password_hash(user.password, request.form['password']):
|
64 |
+
session['user_id'] = user.id
|
65 |
+
return redirect(url_for('dashboard'))
|
66 |
+
return render_template('login.html')
|
67 |
+
|
68 |
+
@app.route('/dashboard')
|
69 |
+
def dashboard():
|
70 |
+
user_id = session['user_id']
|
71 |
+
issues = Issue.query.filter_by(created_by=user_id).all()
|
72 |
+
collaboration_issues = Issue.query.join(Collaborator).filter(Collaborator.user_id == user_id).all()
|
73 |
+
return render_template('dashboard.html', issues=issues, collaboration_issues=collaboration_issues)
|
74 |
+
|
75 |
+
@app.route('/create_issue', methods=['GET', 'POST'])
|
76 |
+
def create_issue():
|
77 |
+
if request.method == 'POST':
|
78 |
+
new_issue = Issue(title=request.form['title'], description=request.form['description'], created_by=session['user_id'])
|
79 |
+
db.session.add(new_issue)
|
80 |
+
db.session.commit()
|
81 |
+
return redirect(url_for('invite_collaborators', issue_id=new_issue.id))
|
82 |
+
return render_template('create_issue.html')
|
83 |
+
|
84 |
+
@app.route('/invite_collaborators/<int:issue_id>', methods=['GET', 'POST'])
|
85 |
+
def invite_collaborators(issue_id):
|
86 |
+
if request.method == 'POST':
|
87 |
+
emails = request.form['emails'].split(',')
|
88 |
+
issue = Issue.query.get(issue_id)
|
89 |
+
for email in emails:
|
90 |
+
user = User.query.filter_by(email=email.strip()).first()
|
91 |
+
if user:
|
92 |
+
new_collaborator = Collaborator(issue_id=issue_id, user_id=user.id)
|
93 |
+
db.session.add(new_collaborator)
|
94 |
+
msg = Message('Collaboration Invite', sender='[email protected]', recipients=[email])
|
95 |
+
msg.body = f"You have been invited to collaborate on the issue: {issue.title}."
|
96 |
+
mail.send(msg)
|
97 |
+
db.session.commit()
|
98 |
+
return redirect(url_for('dashboard'))
|
99 |
+
return render_template('invite_collaborators.html', issue_id=issue_id)
|
100 |
+
|
101 |
+
@app.route('/submit_solution/<int:issue_id>', methods=['GET', 'POST'])
|
102 |
+
def submit_solution(issue_id):
|
103 |
+
if request.method == 'POST':
|
104 |
+
new_solution = Solution(content=request.form['solution'], issue_id=issue_id, created_by=session['user_id'])
|
105 |
+
db.session.add(new_solution)
|
106 |
+
db.session.commit()
|
107 |
+
return redirect(url_for('dashboard'))
|
108 |
+
return render_template('submit_solution.html', issue_id=issue_id)
|
109 |
+
|
110 |
+
@app.route('/decide/<int:issue_id>', methods=['GET', 'POST'])
|
111 |
+
def decide(issue_id):
|
112 |
+
issue = Issue.query.get(issue_id)
|
113 |
+
solutions = Solution.query.filter_by(issue_id=issue_id).all()
|
114 |
+
if request.method == 'POST':
|
115 |
+
# AI Prompt to get the optimal decision suggestion
|
116 |
+
openai.api_key = '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'
|
117 |
+
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"
|
118 |
+
for solution in solutions:
|
119 |
+
prompt += f"- {solution.content}\n"
|
120 |
+
prompt += "Please consider socio-democratic values and provide a summary on the best decision. Do not mention socio-democratic in your response."
|
121 |
+
|
122 |
+
# Use gpt-3.5-turbo
|
123 |
+
response = openai.ChatCompletion.create(
|
124 |
+
model="gpt-3.5-turbo",
|
125 |
+
messages=[
|
126 |
+
{"role": "system", "content": "You are an assistant helping to make socio-democratic decisions. Do not mention socio-democratic in your response"},
|
127 |
+
{"role": "user", "content": prompt}
|
128 |
+
],
|
129 |
+
max_tokens=150
|
130 |
+
)
|
131 |
+
decision = response.choices[0].message['content'].strip()
|
132 |
+
return render_template('decision.html', decision=decision)
|
133 |
+
return render_template('decide.html', issue=issue, solutions=solutions)
|
134 |
+
|
135 |
+
if __name__ == '__main__':
|
136 |
+
with app.app_context():
|
137 |
+
db.create_all()
|
138 |
+
app.run(debug=True)
|