-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.py
236 lines (201 loc) · 8 KB
/
app.py
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
226
227
228
229
230
231
232
233
234
235
236
import os
from openai import OpenAI
from flask import Flask, render_template, request, jsonify, send_file
from flask_login import LoginManager, current_user, login_user, login_required, logout_user
from flask_bcrypt import Bcrypt
from models import User
from firestore_database import FirestoreDB
from sciflow_agent import ScienceFlowAgent
from verifier import Verifier
from reviewer_agents import ReviewerAgents
from weasyprint import HTML, CSS
from jinja2 import Template
import tempfile
from datetime import datetime
app = Flask(__name__)
app.config['SECRET_KEY'] = 'a-very-secret-key'
bcrypt = Bcrypt(app)
login_manager = LoginManager(app)
login_manager.login_view = "login"
db = FirestoreDB()
agent = ScienceFlowAgent()
verifier = Verifier()
reviewer = ReviewerAgents()
@login_manager.user_loader
def load_user(user_id):
user_data = db.get_user_by_email(user_id)
if user_data:
return User(
email=user_data['email'],
password=user_data['password'],
subscription_tier=user_data.get('subscription_tier', 'free')
)
return None
@app.route("/register", methods=["GET", "POST"])
def register():
if request.method == "POST":
email = request.form.get("email")
password = request.form.get("password")
subscription_tier = request.form.get("subscription_tier", "free")
existing_user = db.get_user_by_email(email)
if existing_user:
return "Email already registered.", 400
hashed_pw = bcrypt.generate_password_hash(password).decode('utf-8')
db.create_user(email, hashed_pw, subscription_tier)
return "Registration successful.", 200
return "Register endpoint. Send a POST request to register."
@app.route("/login", methods=["POST"])
def login():
email = request.form.get("email")
password = request.form.get("password")
user_data = db.get_user_by_email(email)
if user_data and bcrypt.check_password_hash(user_data['password'], password):
user_obj = User(
email=user_data['email'],
password=user_data['password'],
subscription_tier=user_data.get('subscription_tier', 'free')
)
login_user(user_obj)
return "Login successful.", 200
else:
return "Invalid credentials.", 401
@app.route("/logout")
@login_required
def logout():
logout_user()
return "Logged out."
@app.route("/discovery")
def discovery_page():
return render_template("discovery_pipeline.html", is_authenticated=current_user.is_authenticated)
def generate_paper_title(pipeline_result: str, client: OpenAI) -> str:
try:
completion = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "You are an expert mathematics paper title generator. Generate a formal, academic title that accurately reflects the mathematical content and is styled like a research paper title."},
{"role": "user", "content": f"""Based on this mathematical research:
{pipeline_result}
Generate a formal mathematics paper title that:
1. Accurately reflects the specific conjecture or finding
2. Uses proper mathematical terminology
3. Is styled like a research paper title
4. Is concise but descriptive
5. Captures the novelty of the work
Return only the title, nothing else."""}
],
max_tokens=100,
temperature=0.7
)
return completion.choices[0].message.content.strip()
except Exception as e:
print(f"Error generating title: {str(e)}")
return "A Novel Conjecture in Number Theory"
@app.route("/run_pipeline", methods=["POST"])
def run_pipeline():
prompt = request.json.get("prompt")
if not prompt:
return jsonify({"error": "No prompt provided"}), 400
client = OpenAI(api_key="ENTER KEY")
pipeline_result = agent.generate_pipeline(prompt)
verification_result = verifier.verify(pipeline_result)
peer_review = reviewer.get_peer_review(pipeline_result, verification_result)
# Generate title using GPT
paper_title = generate_paper_title(pipeline_result, client)
return jsonify({
"pipeline_result": pipeline_result,
"verification_report": verification_result,
"peer_review_report": peer_review,
"paper_title": paper_title
})
def format_paper_html(title, content, verification, review):
# LaTeX-style template for the paper
template = Template("""
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: "Times New Roman", Times, serif; line-height: 1.5; margin: 60px; }
.title { font-size: 24px; font-weight: bold; text-align: center; margin-bottom: 20px; }
.date { text-align: center; margin-bottom: 40px; }
.abstract { margin: 20px 40px; }
.section { margin-top: 30px; }
.section-title { font-size: 18px; font-weight: bold; margin-bottom: 15px; }
.math { font-style: italic; }
@page { margin: 60px; size: letter; }
.verification { background-color: #f8f9fa; padding: 20px; margin: 20px 0; }
.peer-review { background-color: #f8f9fa; padding: 20px; margin: 20px 0; }
</style>
</head>
<body>
<div class="title">{{ title }}</div>
<div class="date">{{ date }}</div>
<div class="section">
<div class="section-title">Abstract</div>
<div class="abstract">
This paper presents a novel mathematical conjecture generated through an AI-assisted
mathematical discovery pipeline. The conjecture and its implications are formally stated,
verified, and peer-reviewed through a systematic process.
</div>
</div>
<div class="section">
<div class="section-title">Main Content</div>
{{ content | replace('\n', '<br>') | safe }}
</div>
<div class="section">
<div class="section-title">Verification Results</div>
<div class="verification">
{{ verification | replace('\n', '<br>') | safe }}
</div>
</div>
<div class="section">
<div class="section-title">Peer Review</div>
<div class="peer-review">
{{ review | replace('\n', '<br>') | safe }}
</div>
</div>
</body>
</html>
""")
return template.render(
title=title,
content=content,
verification=verification,
review=review,
date=datetime.now().strftime("%B %d, %Y")
)
@app.route('/generate_pdf', methods=['POST'])
def generate_pdf():
try:
data = request.json
html_content = format_paper_html(
data['title'],
data['content'],
data['verification'],
data['review']
)
# Create temporary files for the PDF generation
with tempfile.NamedTemporaryFile(suffix='.html') as html_file, \
tempfile.NamedTemporaryFile(suffix='.pdf') as pdf_file:
# Write HTML content to temporary file
html_file.write(html_content.encode('utf-8'))
html_file.flush()
# Generate PDF
HTML(filename=html_file.name).write_pdf(
pdf_file.name,
stylesheets=[CSS(string='@page { size: letter; margin: 1in; }')])
# Return the PDF file
return send_file(
pdf_file.name,
mimetype='application/pdf',
as_attachment=True,
download_name=f"paper_{datetime.now().strftime('%Y%m%d')}.pdf"
)
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == "__main__":
# Before running:
# export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account.json
# export OPENAI_API_KEY=your_openai_key
# pip install -r requirements.txt
# flask run --host=0.0.0.0 --port=8080
app.run(host="0.0.0.0", port=8080, debug=True)