-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathapp.py
More file actions
421 lines (354 loc) · 15.3 KB
/
Copy pathapp.py
File metadata and controls
421 lines (354 loc) · 15.3 KB
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
import os
from dotenv import load_dotenv
import time
import traceback
from flask import Flask, render_template, request, jsonify, send_file, Response, abort
import openai
from together import Together
from reportlab.lib.pagesizes import letter
from reportlab.lib import colors
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.enums import TA_CENTER, TA_JUSTIFY
import io
import asyncio
import aiohttp
from queue import Queue
import sqlite3
from datetime import datetime
import secrets
load_dotenv() # Load environment variables from .env file
app = Flask(__name__)
progress_queue = Queue()
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
TOGETHER_API_KEY = os.getenv('TOGETHER_API_KEY')
if not OPENAI_API_KEY:
raise ValueError("No OpenAI API key found. Please set the OPENAI_API_KEY environment variable.")
if not TOGETHER_API_KEY:
raise ValueError("No Together API key found. Please set the TOGETHER_API_KEY environment variable.")
# Initialize Together client
together_client = Together(api_key=TOGETHER_API_KEY)
# Initialize SQLite database
def init_db():
conn = sqlite3.connect('pdfs.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS pdfs
(id INTEGER PRIMARY KEY AUTOINCREMENT,
ip TEXT,
title TEXT,
filepath TEXT,
timestamp TEXT)''')
c.execute('''CREATE TABLE IF NOT EXISTS api_keys
(id INTEGER PRIMARY KEY AUTOINCREMENT,
ip TEXT,
api_key TEXT UNIQUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP)''')
conn.commit()
conn.close()
init_db()
async def generate_chunk(api, model, topic, current_word_count, language, is_new_chapter=False):
if is_new_chapter:
prompt = f"Write a detailed chapter for a book about {topic} in {language}. This is around word {current_word_count} of the book. Start with a chapter title, then write at least {current_word_count} words of content."
else:
prompt = f"Continue writing a detailed book about {topic} in {language}. This is around word {current_word_count} of the book. Write at least {current_word_count} words, ensuring the narrative flows smoothly from the previous section."
try:
if api == 'openai':
async with aiohttp.ClientSession() as session:
async with session.post(
"https://api.openai.com/v1/chat/completions",
headers={"Authorization": f"Bearer {OPENAI_API_KEY}"},
json={
"model": model,
"messages": [
{"role": "system", "content": f"You are an author writing a book in {language}. Format your response as a part of a book chapter."},
{"role": "user", "content": prompt}
],
"max_tokens": 3000
}
) as response:
result = await response.json()
if 'choices' not in result or len(result['choices']) == 0:
raise ValueError(f"Unexpected API response: {result}")
return result['choices'][0]['message']['content'].strip()
elif api == 'together':
response = together_client.chat.completions.create(
model=model,
messages=[
{"role": "system", "content": f"You are an author writing a detailed book in {language}. Provide long, comprehensive responses with at least {current_word_count} words per chunk."},
{"role": "user", "content": prompt}
],
max_tokens=2000,
temperature=0.7,
top_p=0.9,
top_k=50,
repetition_penalty=1.03,
stop=None
)
generated_text = response.choices[0].message.content.strip()
# Ensure minimum word count
while len(generated_text.split()) < 500:
additional_prompt = f"Continue the previous text, adding more details and expanding the narrative. Write at least {current_word_count} more words."
additional_response = together_client.chat.completions.create(
model=model,
messages=[
{"role": "system", "content": f"You are an author writing a detailed book in {language}. Provide long, comprehensive responses."},
{"role": "user", "content": generated_text},
{"role": "user", "content": additional_prompt}
],
max_tokens=1000,
temperature=0.7,
top_p=0.9,
top_k=50,
repetition_penalty=1.03,
stop=None
)
generated_text += "\n" + additional_response.choices[0].message.content.strip()
return generated_text
except Exception as e:
print(f"An error occurred: {e}")
await asyncio.sleep(60)
return await generate_chunk(api, model, topic, current_word_count, language, is_new_chapter)
def create_pdf(content, title, language):
buffer = io.BytesIO()
doc = SimpleDocTemplate(buffer, pagesize=letter,
rightMargin=72, leftMargin=72,
topMargin=72, bottomMargin=18)
styles = getSampleStyleSheet()
font = 'Helvetica'
styles.add(ParagraphStyle(name='Chapter',
fontName=font,
fontSize=18,
spaceAfter=12,
alignment=TA_CENTER))
styles.add(ParagraphStyle(name='Content',
fontName=font,
fontSize=12,
spaceAfter=12,
alignment=TA_JUSTIFY))
story = []
story.append(Paragraph(title, styles['Title']))
story.append(Spacer(1, 24))
lines = content.split('\n')
for line in lines:
if line.strip().startswith("Chapter"):
story.append(Spacer(1, 24))
story.append(Paragraph(line.strip(), styles['Chapter']))
else:
story.append(Paragraph(line, styles['Content']))
doc.build(story)
buffer.seek(0)
return buffer
@app.route('/')
def hello():
return render_template('hello.html')
@app.route('/about')
def about():
return render_template('about.html')
@app.route('/fr')
def french():
return render_template('home(fr).html')
@app.route('/jobs')
def jobs():
return render_template('jobs.html')
@app.route('/playground')
def playground():
return render_template('index.html')
@app.route('/generate', methods=['POST'])
async def generate_book():
api = request.form['api'] # 'openai' or 'together'
model = request.form['model']
topic = request.form['topic']
language = request.form['language']
target_word_count = int(request.form['word_count'])
current_word_count = 0
book_content = []
chapter_count = 0
tasks = []
while current_word_count < target_word_count:
is_new_chapter = (chapter_count == 0) or (current_word_count > 0 and current_word_count % 3000 < 500)
if is_new_chapter:
chapter_count += 1
task = asyncio.create_task(generate_chunk(api, model, topic, current_word_count, language, is_new_chapter=True))
else:
task = asyncio.create_task(generate_chunk(api, model, topic, current_word_count, language))
tasks.append(task)
current_word_count += 500 # Approximate word count per chunk
if len(tasks) >= 5 or current_word_count >= target_word_count:
chunks = await asyncio.gather(*tasks)
for chunk in chunks:
book_content.append(chunk)
actual_word_count = len(" ".join(book_content).split())
progress_queue.put(actual_word_count)
tasks = []
await asyncio.sleep(1) # Small delay to avoid rate limits
formatted_book = "\n\n".join(book_content)
actual_word_count = len(formatted_book.split())
return jsonify({
'content': formatted_book,
'word_count': actual_word_count
})
@app.route('/progress')
def progress():
def generate():
while True:
if not progress_queue.empty():
yield f"data: {progress_queue.get()}\n\n"
else:
yield "data: keep-alive\n\n"
time.sleep(1)
return Response(generate(), mimetype='text/event-stream')
@app.route('/download-pdf', methods=['POST'])
def download_pdf():
try:
content = request.json['content']
title = request.json['title']
language = request.json['language']
pdf_buffer = create_pdf(content, title, language)
# Save the PDF
ip = request.remote_addr
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"{ip}_{timestamp}.pdf"
filepath = os.path.join('saved_pdfs', filename)
os.makedirs('saved_pdfs', exist_ok=True)
with open(filepath, 'wb') as f:
f.write(pdf_buffer.getvalue())
# Save metadata to database
conn = sqlite3.connect('pdfs.db')
c = conn.cursor()
c.execute("INSERT INTO pdfs (ip, title, filepath, timestamp) VALUES (?, ?, ?, ?)",
(ip, title, filepath, timestamp))
conn.commit()
conn.close()
return send_file(io.BytesIO(pdf_buffer.getvalue()), download_name=f"{title}.pdf", as_attachment=True, mimetype='application/pdf')
except Exception as e:
app.logger.error(f"PDF download error: {str(e)}")
app.logger.error(traceback.format_exc())
return jsonify({'error': str(e)}), 500
@app.route('/save-pdf', methods=['POST'])
def save_pdf():
try:
content = request.json['content']
title = request.json['title']
language = request.json['language']
ip = request.remote_addr
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
pdf_buffer = create_pdf(content, title, language)
# Save PDF to file
filename = f"{ip}_{timestamp}.pdf"
filepath = os.path.join('saved_pdfs', filename)
os.makedirs('saved_pdfs', exist_ok=True)
with open(filepath, 'wb') as f:
f.write(pdf_buffer.getvalue())
# Save metadata to database
conn = sqlite3.connect('pdfs.db')
c = conn.cursor()
c.execute("INSERT INTO pdfs (ip, title, filepath, timestamp) VALUES (?, ?, ?, ?)",
(ip, title, filepath, timestamp))
conn.commit()
conn.close()
return jsonify({'success': True})
except Exception as e:
app.logger.error(f"PDF save error: {str(e)}")
app.logger.error(traceback.format_exc())
return jsonify({'error': str(e)}), 500
@app.route('/get-saved-pdfs', methods=['GET'])
def get_saved_pdfs():
try:
ip = request.remote_addr
conn = sqlite3.connect('pdfs.db')
c = conn.cursor()
c.execute("SELECT id, title, timestamp FROM pdfs WHERE ip = ? ORDER BY timestamp DESC", (ip,))
pdfs = [{'id': row[0], 'title': row[1], 'timestamp': row[2]} for row in c.fetchall()]
conn.close()
return jsonify({'pdfs': pdfs})
except Exception as e:
app.logger.error(f"Get saved PDFs error: {str(e)}")
app.logger.error(traceback.format_exc())
return jsonify({'error': str(e)}), 500
@app.route('/download-saved-pdf/<int:pdf_id>', methods=['GET'])
def download_saved_pdf(pdf_id):
try:
ip = request.remote_addr
conn = sqlite3.connect('pdfs.db')
c = conn.cursor()
c.execute("SELECT filepath, title FROM pdfs WHERE id = ? AND ip = ?", (pdf_id, ip))
result = c.fetchone()
conn.close()
if result is None:
return jsonify({'error': 'PDF not found or unauthorized'}), 404
filepath, title = result
return send_file(filepath, download_name=f"{title}.pdf", as_attachment=True, mimetype='application/pdf')
except Exception as e:
app.logger.error(f"Download saved PDF error: {str(e)}")
app.logger.error(traceback.format_exc())
return jsonify({'error': str(e)}), 500
def generate_api_key():
return secrets.token_urlsafe(32)
@app.route('/generate-api-key', methods=['POST'])
def create_api_key():
ip = request.remote_addr
api_key = generate_api_key()
conn = sqlite3.connect('pdfs.db')
c = conn.cursor()
c.execute("INSERT INTO api_keys (ip, api_key) VALUES (?, ?)", (ip, api_key))
conn.commit()
conn.close()
return jsonify({'api_key': api_key})
@app.route('/api/generate-book', methods=['POST'])
async def api_generate_book():
api_key = request.headers.get('X-API-Key')
if not api_key:
abort(401, description="API key is missing")
conn = sqlite3.connect('pdfs.db')
c = conn.cursor()
c.execute("SELECT ip FROM api_keys WHERE api_key = ?", (api_key,))
result = c.fetchone()
conn.close()
if not result:
abort(401, description="Invalid API key")
api = request.json.get('api')
model = request.json.get('model')
topic = request.json.get('topic')
language = request.json.get('language')
target_word_count = request.json.get('word_count')
if not all([api, model, topic, language, target_word_count]):
abort(400, description="Missing required parameters")
try:
target_word_count = int(target_word_count)
except ValueError:
abort(400, description="Invalid word count")
current_word_count = 0
book_content = []
chapter_count = 0
tasks = []
while current_word_count < target_word_count:
is_new_chapter = (chapter_count == 0) or (current_word_count > 0 and current_word_count % 3000 < 500)
if is_new_chapter:
chapter_count += 1
tasks = []
while current_word_count < target_word_count:
is_new_chapter = (chapter_count == 0) or (current_word_count > 0 and current_word_count % 3000 < 500)
if is_new_chapter:
chapter_count += 1
task = asyncio.create_task(generate_chunk(api, model, topic, current_word_count, language, is_new_chapter=True))
else:
task = asyncio.create_task(generate_chunk(api, model, topic, current_word_count, language))
tasks.append(task)
current_word_count += 500 # Approximate word count per chunk
if len(tasks) >= 5 or current_word_count >= target_word_count:
chunks = await asyncio.gather(*tasks)
for chunk in chunks:
book_content.append(chunk)
tasks = []
await asyncio.sleep(1) # Small delay to avoid rate limits
formatted_book = "\n\n".join(book_content)
actual_word_count = len(formatted_book.split())
return jsonify({
'content': formatted_book,
'word_count': actual_word_count
})
@app.route('/api')
def api_page():
return render_template('api.html')
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0", port=5151)