ruv commited on
Commit
36561a8
·
verified ·
1 Parent(s): 444d3af

Create app2.py

Browse files
Files changed (1) hide show
  1. app2.py +75 -0
app2.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from interpreter import interpreter
2
+ import os
3
+ from flask import Flask, render_template, request, jsonify, session
4
+ from flask_session import Session
5
+ from datetime import datetime, timedelta
6
+
7
+ app = Flask(__name__)
8
+ app.secret_key = 'your_secret_key'
9
+
10
+ app.config['SESSION_TYPE'] = 'filesystem'
11
+ Session(app)
12
+
13
+ prompt_dict = {}
14
+
15
+ @app.route('/')
16
+ def index():
17
+ return render_template('index.html', prompts=prompt_dict)
18
+
19
+ @app.route('/add', methods=['POST'])
20
+ def add_prompt():
21
+ prompt = request.form['prompt'].strip()
22
+ response = request.form['response'].strip()
23
+ if prompt and response:
24
+ prompt_dict[prompt] = response
25
+ flash('Prompt added successfully.')
26
+ else:
27
+ flash('Prompt or response cannot be empty.')
28
+ return redirect(url_for('index'))
29
+
30
+ @app.route('/gpt3', methods=['POST'])
31
+ def gpt3():
32
+ prompt = request.form['prompt']
33
+
34
+ # Initialize message_history in session if it doesn't exist
35
+ if 'message_history' not in session:
36
+ session['message_history'] = []
37
+
38
+ # Append the user input to message_history
39
+ session['message_history'].append("User: " + prompt)
40
+
41
+ def get_interpreter_response(user_input, message_history):
42
+ # Convert message history to the format required by Open Interpreter
43
+ messages = [{"role": msg.split(': ')[0].lower(), "content": msg.split(': ')[1]} for msg in message_history]
44
+
45
+ response = interpreter.chat(user_input, messages=messages)
46
+ return response
47
+
48
+ try:
49
+ response_text = get_interpreter_response(prompt, session['message_history'])
50
+ # Append the assistant's response to message_history
51
+ session['message_history'].append("Assistant: " + response_text.strip())
52
+ session.modified = True # Ensure the session is saved after modification
53
+ return jsonify({"text": response_text.strip()})
54
+ except Exception as e:
55
+ return jsonify({"error": str(e)})
56
+
57
+ import json
58
+
59
+ @app.route('/clear_session', methods=['GET'])
60
+ def clear_session():
61
+ session.clear()
62
+ return jsonify({"result": "Session cleared"})
63
+
64
+ @app.route('/history')
65
+ def history():
66
+ if 'message_history' in session:
67
+ message_history = session['message_history']
68
+ # Convert the message history array to a JSON string
69
+ history_str = json.dumps(message_history)
70
+ return jsonify({"history": history_str})
71
+ else:
72
+ return jsonify({"history": "No message history found in the current session."})
73
+
74
+ if __name__ == '__main__':
75
+ app.run(host='0.0.0.0', port=8080)