commit 20053db1d9d061d319640f681dc376c417b17575 Author: Russell Ballestrini Date: Mon Apr 3 15:19:27 2023 -0400 sorta works. new file: .gitignore new file: app.py new file: requirements.txt new file: templates/chat.html new file: templates/index.html diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3bf2621 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.swp +env diff --git a/app.py b/app.py new file mode 100644 index 0000000..a6845fc --- /dev/null +++ b/app.py @@ -0,0 +1,56 @@ +from flask import Flask, render_template, request +from flask_socketio import SocketIO, emit, join_room + +import eventlet + +import openai + +import markdown + +app = Flask(__name__) +app.config['SECRET_KEY'] = 'your_secret_key' +socketio = SocketIO(app, async_mode='eventlet') + +@app.route('/') +def index(): + return render_template('index.html') + +@app.route('/chat/') +def chat(room): + return render_template('chat.html', room=room) + +@socketio.on('join') +def on_join(data): + room = data['room'] + join_room(room) + emit('message', f"{data['username']} has joined the room.", room=room) + +@socketio.on('message') +def handle_message(data): + emit('message', f"{data['username']}: {data['message']}", room=data['room']) + + openai.api_key = "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" + + # Call the chat_gpt function without blocking using eventlet.spawn + eventlet.spawn(chat_gpt, data['username'], data['room'], data['message']) + + +def chat_gpt(username, room, message): + # Send user's message to ChatGPT API + response = openai.ChatCompletion.create( + model="gpt-3.5-turbo", + messages=[{"role": "user", "content": message}] + ) + + # Extract response from ChatGPT API + response_text = response['choices'][0]['message']['content'] + + # Convert response_text to Markdown + response_md = markdown.markdown(response_text, extensions=['fenced_code']) + + # Emit the response to the room + socketio.emit('message', f"{username} (GPT-3.5): {response_md}", room=room) + +if __name__ == '__main__': + socketio.run(app, host='0.0.0.0', port=5000) + diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..859657a --- /dev/null +++ b/requirements.txt @@ -0,0 +1,9 @@ +flask +flask-socketio +eventlet +openai +markdown + +# code highlights +Pygments +mdx_codehilite diff --git a/templates/chat.html b/templates/chat.html new file mode 100644 index 0000000..282e76a --- /dev/null +++ b/templates/chat.html @@ -0,0 +1,84 @@ + + + + + + Chatroom + + + + + + + + + + + +
+

{{ room }}

+
+
+ +
+
+ + + + + + diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..71d85ef --- /dev/null +++ b/templates/index.html @@ -0,0 +1,64 @@ + + + + + + Chatroom + + + + + +
+

Join a Chat Room

+
+ + + +
+
+ + + + +