| | from interpreter import interpreter |
| | import os |
| | from flask import Flask, render_template, request, jsonify, session |
| | from flask_session import Session |
| | from datetime import datetime, timedelta |
| |
|
| | app = Flask(__name__) |
| | app.secret_key = 'your_secret_key' |
| |
|
| | app.config['SESSION_TYPE'] = 'filesystem' |
| | Session(app) |
| |
|
| | prompt_dict = {} |
| |
|
| | @app.route('/') |
| | def index(): |
| | return render_template('index.html', prompts=prompt_dict) |
| |
|
| | @app.route('/add', methods=['POST']) |
| | def add_prompt(): |
| | prompt = request.form['prompt'].strip() |
| | response = request.form['response'].strip() |
| | if prompt and response: |
| | prompt_dict[prompt] = response |
| | flash('Prompt added successfully.') |
| | else: |
| | flash('Prompt or response cannot be empty.') |
| | return redirect(url_for('index')) |
| |
|
| | @app.route('/gpt3', methods=['POST']) |
| | def gpt3(): |
| | prompt = request.form['prompt'] |
| |
|
| | |
| | if 'message_history' not in session: |
| | session['message_history'] = [] |
| |
|
| | |
| | session['message_history'].append("User: " + prompt) |
| |
|
| | def get_interpreter_response(user_input, message_history): |
| | |
| | messages = [{"role": msg.split(': ')[0].lower(), "content": msg.split(': ')[1]} for msg in message_history] |
| |
|
| | response = interpreter.chat(user_input, messages=messages) |
| | return response |
| |
|
| | try: |
| | response_text = get_interpreter_response(prompt, session['message_history']) |
| | |
| | session['message_history'].append("Assistant: " + response_text.strip()) |
| | session.modified = True |
| | return jsonify({"text": response_text.strip()}) |
| | except Exception as e: |
| | return jsonify({"error": str(e)}) |
| |
|
| | import json |
| |
|
| | @app.route('/clear_session', methods=['GET']) |
| | def clear_session(): |
| | session.clear() |
| | return jsonify({"result": "Session cleared"}) |
| |
|
| | @app.route('/history') |
| | def history(): |
| | if 'message_history' in session: |
| | message_history = session['message_history'] |
| | |
| | history_str = json.dumps(message_history) |
| | return jsonify({"history": history_str}) |
| | else: |
| | return jsonify({"history": "No message history found in the current session."}) |
| |
|
| | if __name__ == '__main__': |
| | app.run(host='0.0.0.0', port=7860) |
| |
|