-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
101 lines (73 loc) · 2.99 KB
/
server.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
import logging
import sys
import uuid
from flask import Flask, request, redirect, render_template
import openai
import os
openai.api_key = os.getenv('OPENAI_API_KEY')
app = Flask(__name__)
log = logging.getLogger(__file__)
log.setLevel(logging.INFO)
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.INFO)
log.addHandler(handler)
def get_or_create_session_id() -> str:
if get_session_id := request.args.get('session_id'):
return get_session_id
else:
return str(uuid.uuid4())
def session_template_filename(s: str) -> str:
return f'index-{s}.html'
def template_filename_for_reading(session_id: str) -> str:
return session_template_filename(session_id) if session_template_exists(session_id) else 'index.html'
def session_template_exists(session_id: str) -> bool:
return os.path.exists(f'templates/{session_template_filename(session_id)}')
@app.route('/', methods=['GET'])
def index():
session_id = get_or_create_session_id()
log.info("index %s", {session_id: session_id})
return render_template(template_filename_for_reading(session_id), session_id=session_id)
@app.route('/change', methods=['POST'])
def handle_change():
text = request.form.get('change-input')
session_id = get_or_create_session_id()
log.info("handle_change %s", {"session_id": session_id, "text": text})
with open(f'templates/{template_filename_for_reading(session_id)}', 'r') as f:
file_content = f.read()
# TODO(derwiki) construct in haml to save tokens
# Construct OpenAI request with the input text as prompt
prompt = f"""Change the code in index.html according to the prompt.
Your response should only include html and nothing to escape the code.
Make sure the new index.html is less than the token limit.
The new code must include the POST form with the change-input text field.
The generated html should be optimized for smallest possible size while fulfilling the prompt.
Prompt:
{text}
index.html:
{file_content}
"""
response = openai.ChatCompletion.create(
model='gpt-3.5-turbo', # so slow
messages=[{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt}],
max_tokens=1999,
temperature=0.0
)
'''
response = openai.Completion.create(
enginer='davinci-codex',
temperature=0.0,
max_tokens=8001,
prompt=prompt,
)
'''
# response_content = response.choices[0].text.strip()
response_content = response.choices[0].message['content'].strip()
log.info("handle_change response_content:\n%s", response_content)
# Get the generated code and replace it in index-{session_id}.html
log.info(f'writing to templates/{session_template_filename(session_id)}')
with open(f'templates/{session_template_filename(session_id)}', 'w') as f:
f.write(response_content)
return redirect(f'/?session_id={session_id}')
if __name__ == '__main__':
app.run(debug=os.getenv('ENV') == 'production')