Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,14 @@
|
|
1 |
-
|
2 |
from flask_sqlalchemy import SQLAlchemy
|
3 |
from flask_bcrypt import Bcrypt
|
4 |
from flask_mail import Mail, Message
|
5 |
import openai
|
6 |
-
import
|
7 |
|
|
|
|
|
|
|
|
|
8 |
app = Flask(__name__)
|
9 |
app.secret_key = 's3cr3t'
|
10 |
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///sociocracy.db'
|
@@ -14,7 +18,7 @@ bcrypt = Bcrypt(app)
|
|
14 |
# Configuring Flask-Mail
|
15 |
app.config['MAIL_SERVER'] = 'smtp.freesmtpservers.com'
|
16 |
app.config['MAIL_PORT'] = 25
|
17 |
-
app.config['MAIL_USERNAME'] = '
|
18 |
app.config['MAIL_PASSWORD'] = ''
|
19 |
app.config['MAIL_USE_TLS'] = False
|
20 |
app.config['MAIL_USE_SSL'] = False
|
@@ -40,100 +44,177 @@ class Solution(db.Model):
|
|
40 |
content = db.Column(db.Text, nullable=False)
|
41 |
issue_id = db.Column(db.Integer, db.ForeignKey('issue.id'), nullable=False)
|
42 |
created_by = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
|
43 |
-
|
44 |
# Collaborator model
|
45 |
class Collaborator(db.Model):
|
46 |
id = db.Column(db.Integer, primary_key=True)
|
47 |
issue_id = db.Column(db.Integer, db.ForeignKey('issue.id'), nullable=False)
|
48 |
-
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
|
|
|
|
|
|
|
|
|
49 |
|
50 |
-
|
51 |
def register():
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
-
|
61 |
def login():
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
|
|
|
|
|
|
|
|
70 |
def dashboard():
|
71 |
-
|
72 |
-
|
|
|
|
|
|
|
|
|
|
|
73 |
collaboration_issues = Issue.query.join(Collaborator).filter(Collaborator.user_id == user_id).all()
|
74 |
-
return render_template('dashboard.html', issues=issues, collaboration_issues=collaboration_issues)
|
75 |
|
76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
def create_issue():
|
78 |
-
if
|
79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
db.session.add(new_issue)
|
81 |
db.session.commit()
|
82 |
-
|
83 |
-
|
|
|
|
|
|
|
|
|
|
|
84 |
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
issue = Issue.query.get(issue_id)
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
|
|
|
|
|
|
101 |
|
102 |
-
|
103 |
def submit_solution(issue_id):
|
104 |
-
if
|
105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
db.session.add(new_solution)
|
107 |
db.session.commit()
|
108 |
-
|
109 |
-
return render_template('submit_solution.html', issue_id=issue_id)
|
110 |
|
111 |
-
|
112 |
def decide(issue_id):
|
113 |
issue = Issue.query.get(issue_id)
|
114 |
solutions = Solution.query.filter_by(issue_id=issue_id).all()
|
115 |
-
if
|
116 |
-
|
117 |
-
openai.api_key = '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'
|
118 |
-
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"
|
119 |
for solution in solutions:
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
from flask_sqlalchemy import SQLAlchemy
|
3 |
from flask_bcrypt import Bcrypt
|
4 |
from flask_mail import Mail, Message
|
5 |
import openai
|
6 |
+
from flask import Flask
|
7 |
|
8 |
+
# Streamlit Configuration
|
9 |
+
st.set_page_config(page_title="Sociocracy App")
|
10 |
+
|
11 |
+
# Flask Configuration for Database and Mail
|
12 |
app = Flask(__name__)
|
13 |
app.secret_key = 's3cr3t'
|
14 |
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///sociocracy.db'
|
|
|
18 |
# Configuring Flask-Mail
|
19 |
app.config['MAIL_SERVER'] = 'smtp.freesmtpservers.com'
|
20 |
app.config['MAIL_PORT'] = 25
|
21 |
+
app.config['MAIL_USERNAME'] = 'your_email@example.com'
|
22 |
app.config['MAIL_PASSWORD'] = ''
|
23 |
app.config['MAIL_USE_TLS'] = False
|
24 |
app.config['MAIL_USE_SSL'] = False
|
|
|
44 |
content = db.Column(db.Text, nullable=False)
|
45 |
issue_id = db.Column(db.Integer, db.ForeignKey('issue.id'), nullable=False)
|
46 |
created_by = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
|
47 |
+
|
48 |
# Collaborator model
|
49 |
class Collaborator(db.Model):
|
50 |
id = db.Column(db.Integer, primary_key=True)
|
51 |
issue_id = db.Column(db.Integer, db.ForeignKey('issue.id'), nullable=False)
|
52 |
+
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
|
53 |
+
|
54 |
+
# Streamlit Pages
|
55 |
+
st.sidebar.title("Navigation")
|
56 |
+
page = st.sidebar.radio("Go to", ["Register", "Login", "Dashboard", "Create Issue", "Invite Collaborators", "Submit Solution", "Decide"])
|
57 |
|
58 |
+
# Register Page
|
59 |
def register():
|
60 |
+
st.title("Register")
|
61 |
+
username = st.text_input("Username")
|
62 |
+
email = st.text_input("Email")
|
63 |
+
password = st.text_input("Password", type="password")
|
64 |
+
if st.button("Register"):
|
65 |
+
if username and email and password:
|
66 |
+
hashed_pw = bcrypt.generate_password_hash(password).decode('utf-8')
|
67 |
+
new_user = User(username=username, email=email, password=hashed_pw)
|
68 |
+
db.session.add(new_user)
|
69 |
+
db.session.commit()
|
70 |
+
st.success("Registration successful! Please check your email for further instructions.")
|
71 |
+
|
72 |
+
# Send registration email
|
73 |
+
msg = Message('Welcome to Sociocracy App', sender='[email protected]', recipients=[email])
|
74 |
+
msg.body = "Thank you for registering. You can now log in and start collaborating."
|
75 |
+
mail.send(msg)
|
76 |
+
else:
|
77 |
+
st.error("Please fill all fields.")
|
78 |
|
79 |
+
# Login Page
|
80 |
def login():
|
81 |
+
st.title("Login")
|
82 |
+
username = st.text_input("Username")
|
83 |
+
password = st.text_input("Password", type="password")
|
84 |
+
if st.button("Login"):
|
85 |
+
user = User.query.filter_by(username=username).first()
|
86 |
+
if user and bcrypt.check_password_hash(user.password, password):
|
87 |
+
st.session_state['user_id'] = user.id
|
88 |
+
st.success("Login successful!")
|
89 |
+
else:
|
90 |
+
st.error("Invalid username or password.")
|
91 |
+
|
92 |
+
# Dashboard Page
|
93 |
def dashboard():
|
94 |
+
if 'user_id' not in st.session_state:
|
95 |
+
st.warning("You need to log in first.")
|
96 |
+
return
|
97 |
+
|
98 |
+
st.title("Dashboard")
|
99 |
+
user_id = st.session_state['user_id']
|
100 |
+
user_issues = Issue.query.filter_by(created_by=user_id).all()
|
101 |
collaboration_issues = Issue.query.join(Collaborator).filter(Collaborator.user_id == user_id).all()
|
|
|
102 |
|
103 |
+
st.header("Your Issues")
|
104 |
+
for issue in user_issues:
|
105 |
+
st.write(f"Title: {issue.title}")
|
106 |
+
if st.button(f"Submit Solution for {issue.title}"):
|
107 |
+
submit_solution(issue.id)
|
108 |
+
if st.button(f"Decide on {issue.title}"):
|
109 |
+
decide(issue.id)
|
110 |
+
|
111 |
+
st.header("Issues You Are Collaborating On")
|
112 |
+
for issue in collaboration_issues:
|
113 |
+
st.write(f"Title: {issue.title}")
|
114 |
+
if st.button(f"Submit Solution for {issue.title}"):
|
115 |
+
submit_solution(issue.id)
|
116 |
+
if st.button(f"Decide on {issue.title}"):
|
117 |
+
decide(issue.id)
|
118 |
+
|
119 |
+
# Create Issue Page
|
120 |
def create_issue():
|
121 |
+
if 'user_id' not in st.session_state:
|
122 |
+
st.warning("You need to log in first.")
|
123 |
+
return
|
124 |
+
|
125 |
+
st.title("Create Issue")
|
126 |
+
title = st.text_input("Issue Title")
|
127 |
+
description = st.text_area("Issue Description")
|
128 |
+
if st.button("Create Issue"):
|
129 |
+
new_issue = Issue(title=title, description=description, created_by=st.session_state['user_id'])
|
130 |
db.session.add(new_issue)
|
131 |
db.session.commit()
|
132 |
+
st.success("Issue created successfully!")
|
133 |
+
|
134 |
+
# Invite Collaborators Page
|
135 |
+
def invite_collaborators():
|
136 |
+
if 'user_id' not in st.session_state:
|
137 |
+
st.warning("You need to log in first.")
|
138 |
+
return
|
139 |
|
140 |
+
st.title("Invite Collaborators")
|
141 |
+
issue_id = st.number_input("Issue ID", min_value=1, step=1)
|
142 |
+
emails = st.text_input("Collaborator Emails (comma-separated)")
|
143 |
+
if st.button("Send Invites"):
|
144 |
issue = Issue.query.get(issue_id)
|
145 |
+
if issue:
|
146 |
+
for email in emails.split(','):
|
147 |
+
email = email.strip()
|
148 |
+
user = User.query.filter_by(email=email).first()
|
149 |
+
if user:
|
150 |
+
new_collaborator = Collaborator(issue_id=issue_id, user_id=user.id)
|
151 |
+
db.session.add(new_collaborator)
|
152 |
+
msg = Message('Collaboration Invite', sender='your_email@example.com', recipients=[email])
|
153 |
+
msg.body = f"You have been invited to collaborate on the issue: {issue.title}."
|
154 |
+
mail.send(msg)
|
155 |
+
db.session.commit()
|
156 |
+
st.success("Invitations sent successfully!")
|
157 |
+
else:
|
158 |
+
st.error("Issue not found.")
|
159 |
|
160 |
+
# Submit Solution Page
|
161 |
def submit_solution(issue_id):
|
162 |
+
if 'user_id' not in st.session_state:
|
163 |
+
st.warning("You need to log in first.")
|
164 |
+
return
|
165 |
+
|
166 |
+
st.title(f"Submit Solution for Issue {issue_id}")
|
167 |
+
solution_content = st.text_area("Your Solution")
|
168 |
+
if st.button("Submit Solution"):
|
169 |
+
new_solution = Solution(content=solution_content, issue_id=issue_id, created_by=st.session_state['user_id'])
|
170 |
db.session.add(new_solution)
|
171 |
db.session.commit()
|
172 |
+
st.success("Solution submitted successfully!")
|
|
|
173 |
|
174 |
+
# Decide Page
|
175 |
def decide(issue_id):
|
176 |
issue = Issue.query.get(issue_id)
|
177 |
solutions = Solution.query.filter_by(issue_id=issue_id).all()
|
178 |
+
if issue:
|
179 |
+
st.title(f"Decide on Issue: {issue.title}")
|
|
|
|
|
180 |
for solution in solutions:
|
181 |
+
st.write(f"- {solution.content}")
|
182 |
+
|
183 |
+
if st.button("Get AI Decision"):
|
184 |
+
openai.api_key = 'st.secrets["OpenAI"]'
|
185 |
+
prompt = f"Given the following solutions for the issue '{issue.title}', which one is the most socio-democratic decision?\n"
|
186 |
+
for solution in solutions:
|
187 |
+
prompt += f"- {solution.content}\n"
|
188 |
+
prompt += "Please consider socio-democratic values and provide a summary on the best decision."
|
189 |
+
|
190 |
+
response = openai.ChatCompletion.create(
|
191 |
+
model="gpt-3.5-turbo",
|
192 |
+
messages=[
|
193 |
+
{"role": "system", "content": "You are an assistant helping to make socio-democratic decisions."},
|
194 |
+
{"role": "user", "content": prompt}
|
195 |
+
],
|
196 |
+
max_tokens=150
|
197 |
+
)
|
198 |
+
decision = response.choices[0].message['content'].strip()
|
199 |
+
st.write(f"AI Decision: {decision}")
|
200 |
+
|
201 |
+
# Main Flow
|
202 |
+
if 'user_id' not in st.session_state:
|
203 |
+
st.session_state['user_id'] = None
|
204 |
+
|
205 |
+
if page == "Register":
|
206 |
+
register()
|
207 |
+
elif page == "Login":
|
208 |
+
login()
|
209 |
+
elif page == "Dashboard":
|
210 |
+
dashboard()
|
211 |
+
elif page == "Create Issue":
|
212 |
+
create_issue()
|
213 |
+
elif page == "Invite Collaborators":
|
214 |
+
invite_collaborators()
|
215 |
+
elif page == "Submit Solution":
|
216 |
+
issue_id = st.number_input("Issue ID", min_value=1, step=1)
|
217 |
+
submit_solution(issue_id)
|
218 |
+
elif page == "Decide":
|
219 |
+
issue_id = st.number_input("Issue ID", min_value=1, step=1)
|
220 |
+
decide(issue_id)
|