yup
This commit is contained in:
parent
7eb29cce83
commit
020813fc30
3 changed files with 59 additions and 34 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -2,3 +2,4 @@
|
|||
env
|
||||
instance/
|
||||
__pycache__/
|
||||
.flaskenv
|
||||
|
|
|
|||
15
Dockerfile
Normal file
15
Dockerfile
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
FROM python:latest
|
||||
|
||||
COPY requirements.txt /opt/requirements.txt
|
||||
|
||||
RUN pip3 install -r /opt/requirements.txt
|
||||
|
||||
COPY . /opt/server
|
||||
|
||||
WORKDIR /opt/server
|
||||
|
||||
ENV PYTHONPATH=/opt
|
||||
|
||||
CMD ["python", "app.py"]
|
||||
|
||||
EXPOSE 5001
|
||||
77
app.py
77
app.py
|
|
@ -3,20 +3,22 @@ from flask_socketio import SocketIO, emit, join_room
|
|||
|
||||
import eventlet
|
||||
|
||||
import openai
|
||||
|
||||
openai.api_key = "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"
|
||||
|
||||
import markdown
|
||||
|
||||
import openai
|
||||
|
||||
import os
|
||||
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config['SECRET_KEY'] = 'your_secret_key'
|
||||
socketio = SocketIO(app, async_mode='eventlet')
|
||||
app.config["SECRET_KEY"] = "your_secret_key"
|
||||
socketio = SocketIO(app, async_mode="eventlet")
|
||||
|
||||
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///chat.db'
|
||||
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
||||
|
||||
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///chat.db"
|
||||
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
|
||||
db = SQLAlchemy(app)
|
||||
|
||||
|
||||
|
|
@ -31,21 +33,25 @@ class Message(db.Model):
|
|||
self.content = content
|
||||
self.room = room
|
||||
|
||||
|
||||
# Create the database and tables
|
||||
#with app.app_context():
|
||||
# with app.app_context():
|
||||
# db.create_all()
|
||||
|
||||
@app.route('/')
|
||||
|
||||
@app.route("/")
|
||||
def index():
|
||||
return render_template('index.html')
|
||||
return render_template("index.html")
|
||||
|
||||
@app.route('/chat/<room>')
|
||||
|
||||
@app.route("/chat/<room>")
|
||||
def chat(room):
|
||||
return render_template('chat.html', room=room)
|
||||
return render_template("chat.html", room=room)
|
||||
|
||||
@socketio.on('join')
|
||||
|
||||
@socketio.on("join")
|
||||
def on_join(data):
|
||||
room = data['room']
|
||||
room = data["room"]
|
||||
join_room(room)
|
||||
|
||||
# Fetch previous messages from the database
|
||||
|
|
@ -53,54 +59,57 @@ def on_join(data):
|
|||
|
||||
for message in previous_messages:
|
||||
emit(
|
||||
'previous_messages',
|
||||
{'username': message.username, 'message': message.content},
|
||||
room=request.sid
|
||||
"previous_messages",
|
||||
{"username": message.username, "message": message.content},
|
||||
room=request.sid,
|
||||
)
|
||||
|
||||
emit('message', f"{data['username']} has joined the room.", room=room)
|
||||
emit("message", f"{data['username']} has joined the room.", room=room)
|
||||
|
||||
@socketio.on('message')
|
||||
|
||||
@socketio.on("message")
|
||||
def handle_message(data):
|
||||
# Save the message to the database
|
||||
new_message = Message(username=data['username'], content=data['message'], room=data['room'])
|
||||
new_message = Message(
|
||||
username=data["username"], content=data["message"], room=data["room"]
|
||||
)
|
||||
db.session.add(new_message)
|
||||
db.session.commit()
|
||||
|
||||
emit('message', f"{data['username']}: {data['message']}", room=data['room'])
|
||||
emit("message", f"{data['username']}: {data['message']}", room=data["room"])
|
||||
|
||||
# Emit a temporary message indicating that GPT is processing
|
||||
emit('message', f"<span id='processing'>Processing...</span>", room=data['room'])
|
||||
|
||||
emit("message", f"<span id='processing'>Processing...</span>", room=data["room"])
|
||||
|
||||
# Call the chat_gpt function without blocking using eventlet.spawn
|
||||
eventlet.spawn(chat_gpt, data['username'], data['room'], data['message'])
|
||||
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}]
|
||||
model="gpt-3.5-turbo", messages=[{"role": "user", "content": message}]
|
||||
)
|
||||
|
||||
# Extract response from ChatGPT API
|
||||
response_text = response['choices'][0]['message']['content']
|
||||
response_text = response["choices"][0]["message"]["content"]
|
||||
|
||||
# Convert response_text to Markdown
|
||||
response_md = markdown.markdown(response_text, extensions=['fenced_code'])
|
||||
response_md = markdown.markdown(response_text, extensions=["fenced_code"])
|
||||
|
||||
# Save ChatGPT's response in the database
|
||||
with app.app_context():
|
||||
chatgpt_response_message = Message(username="GPT-3.5", content=response_md, room=room)
|
||||
chatgpt_response_message = Message(
|
||||
username="GPT-3.5", content=response_md, room=room
|
||||
)
|
||||
db.session.add(chatgpt_response_message)
|
||||
db.session.commit()
|
||||
|
||||
socketio.emit('delete_processing_message', "", room=room)
|
||||
socketio.emit("delete_processing_message", "", room=room)
|
||||
|
||||
# Emit the response to the room
|
||||
socketio.emit('message', f"{username} (GPT-3.5): {response_md}", room=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=5001)
|
||||
|
||||
if __name__ == "__main__":
|
||||
socketio.run(app, host="0.0.0.0", port=5001)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue