File size: 2,509 Bytes
36561a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c9bdc5d
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
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']

    # Initialize message_history in session if it doesn't exist
    if 'message_history' not in session:
        session['message_history'] = []

    # Append the user input to message_history
    session['message_history'].append("User: " + prompt)

    def get_interpreter_response(user_input, message_history):
        # Convert message history to the format required by Open Interpreter
        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'])
        # Append the assistant's response to message_history
        session['message_history'].append("Assistant: " + response_text.strip())
        session.modified = True  # Ensure the session is saved after modification
        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']
        # Convert the message history array to a JSON string
        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)