-
Notifications
You must be signed in to change notification settings - Fork 0
/
flask_app.py
81 lines (64 loc) · 2.27 KB
/
flask_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
from flask import Flask, request, render_template
import openai
import os
from dotenv import load_dotenv
import logging
<<<<<<< HEAD
=======
# Set up your app environment
>>>>>>> stephen-dev-branch-02
app = Flask(__name__, template_folder='templates')
load_dotenv()
openai.api_key = os.getenv('OPENAI_API_KEY')
<<<<<<< HEAD
=======
>>>>>>> stephen-dev-branch-02
# Set up root root_logger
root_logger = logging.getLogger(__name__)
root_logger.setLevel(logging.DEBUG)
# Set up formatter for logs
file_handler_log_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s ')
console_handler_log_formatter = logging.Formatter('%(message)s ')
# Set up file handler object for logging events to file
file_handler = logging.FileHandler('chatgpt_conversation_history.log', mode='w')
file_handler.setFormatter(file_handler_log_formatter)
# Set up console handler object for writing event logs to console in real time (i.e. streams events to stderr)
console_handler = logging.StreamHandler()
console_handler.setFormatter(console_handler_log_formatter)
# Add the file and console handlers
root_logger.addHandler(file_handler)
root_logger.addHandler(console_handler)
<<<<<<< HEAD
@app.route('/')
def index():
return render_template('index.html')
@app.route('/chat', methods=['POST'])
def chat():
user_input = request.form['text']
root_logger.removeHandler(console_handler)
root_logger.info(f':: Me: {user_input}' )
=======
@app.route('/')
def render_index_html():
return render_template('index.html')
@app.route('/chat', methods=['POST'])
def render_chat_with_chatgpt():
user_input = request.form['text']
root_logger.removeHandler(console_handler)
root_logger.info(f':: Me (SDW): {user_input}' )
>>>>>>> stephen-dev-branch-02
root_logger.addHandler(console_handler)
chatgpt_response = openai.Completion.create(
model="text-davinci-003",
prompt=user_input,
temperature=0.3,
max_tokens=1000,
top_p=1.0,
frequency_penalty=0,
presence_penalty=0
)
root_logger.info(f':: ChatGPT: {chatgpt_response["choices"][0]["text"]} ')
root_logger.debug('--------------------------------------------------')
return chatgpt_response["choices"][0]["text"]
if __name__ == '__main__':
app.run(debug=True)