sorta works.

new file:   .gitignore
	new file:   app.py
	new file:   requirements.txt
	new file:   templates/chat.html
	new file:   templates/index.html
This commit is contained in:
Russell Ballestrini 2023-04-03 15:19:27 -04:00
commit 20053db1d9
5 changed files with 215 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
*.swp
env

56
app.py Normal file
View file

@ -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/<room>')
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)

9
requirements.txt Normal file
View file

@ -0,0 +1,9 @@
flask
flask-socketio
eventlet
openai
markdown
# code highlights
Pygments
mdx_codehilite

84
templates/chat.html Normal file
View file

@ -0,0 +1,84 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chatroom</title>
<!-- Include highlight.js library -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.6.0/highlight.min.js"></script>
<!-- Include a highlight.js theme (replace 'default' with your preferred theme) -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.6.0/styles/default.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.0.1/socket.io.min.js"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f7f7f7;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
#chat-container {
background-color: #ffffff;
border-radius: 5px;
padding: 15px;
width: 100%;
max-width: 600px;
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
}
#chat {
height: 300px;
overflow-y: scroll;
border: 1px solid #e1e1e1;
border-radius: 5px;
padding: 10px;
margin-bottom: 10px;
}
#message {
width: 98%;
border: 1px solid #e1e1e1;
border-radius: 5px;
padding: 5px;
}
</style>
</head>
<body>
<div id="chat-container">
<h2>{{ room }}</h2>
<div id="chat"></div>
<form id="message-form">
<input id="message" type="text" placeholder="Type your message...">
</form>
</div>
<script>
const socket = io.connect('http://' + document.domain + ':' + location.port);
const urlParams = new URLSearchParams(window.location.search);
const username = urlParams.get('username');
const room = '{{ room }}';
socket.on('connect', () => {
socket.emit('join', {username: username, room: room});
});
$('#message-form').submit((e) => {
e.preventDefault();
const message = $('#message').val();
socket.emit('message', {'username': username, 'message': message, room: room});
$('#message').val('');
});
socket.on('message', (message) => {
$('#chat').append('<p>' + message + '</p>');
});
</script>
</body>
</html>

64
templates/index.html Normal file
View file

@ -0,0 +1,64 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chatroom</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.0.1/socket.io.min.js"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f7f7f7;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
#chat-container {
background-color: #ffffff;
border-radius: 5px;
padding: 15px;
width: 100%;
max-width: 600px;
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
}
#chat {
height: 300px;
overflow-y: scroll;
border: 1px solid #e1e1e1;
border-radius: 5px;
padding: 10px;
margin-bottom: 10px;
}
#message {
width: 98%;
border: 1px solid #e1e1e1;
border-radius: 5px;
padding: 5px;
}
</style>
</head>
<body>
<div id="chat-container">
<h2>Join a Chat Room</h2>
<form id="join-room-form">
<input id="username" type="text" placeholder="Enter your username">
<input id="room" type="text" placeholder="Enter room name">
<button type="submit">Join</button>
</form>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$('#join-room-form').submit((e) => {
e.preventDefault();
const username = $('#username').val();
const room = $('#room').val();
window.location.href = `/chat/${room}?username=${encodeURIComponent(username)}`;
});
</script>
</body>
</html>