-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrontend.py
115 lines (83 loc) · 3.19 KB
/
frontend.py
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
from flask import Flask, jsonify,render_template,request
from flask_cors import CORS
from g4f.client import Client
import json
import random
app = Flask(__name__)
# Enable CORS for all routes
CORS(app)
memory = {}
user_data = {}
@app.route('/')
def home():
return render_template('main.html')
@app.route('/question', methods=['POST'])
def question():
data = request.get_json()
# Process the data (this is just an example)
if not data:
return jsonify({"error": "No data provided"}), 400
if data['user_id'] not in memory:
memory[data['user_id']] = []
memory[data['user_id']].append({"role": "user_id", "content": data['message']})
if len(memory[data['user_id']]) >= 10:
memory[data['user_id']].pop(0)
client = Client()
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=memory[data['user_id']]
)
reply = response.choices[0].message.content
memory[data['user_id']].append({"role": "assistant", "content": reply})
return jsonify({'response':reply})
# Load riddles from the JSON file
def load_riddles():
with open('riddles.json', 'r') as file:
return json.load(file)
@app.route('/get_riddle', methods=['POST'])
def get_riddle():
user_id = request.json.get('user_id')
if not user_id:
return jsonify({"error": "User ID is required"}), 400
riddles = load_riddles()
asked_riddle = random.choice(riddles)
# Store the user ID and riddle in the dictionary
user_data[user_id] = {
"riddle_asked": asked_riddle['question'],
"answer": asked_riddle['answer']
}
return jsonify({"response": asked_riddle['question']}), 200
@app.route('/submit_answer', methods=['POST'])
def submit_answer():
user_id = request.json.get('user_id')
answer = request.json.get('message')
if not user_id or not answer:
return jsonify({"error": "user_id ID and answer are required"}), 400
if user_id not in user_data:
data = request.get_json()
# Process the data (this is just an example)
if not data:
return jsonify({"error": "No data provided"}), 400
if data['user_id'] not in memory:
memory[data['user_id']] = []
memory[data['user_id']].append({"role": "user_id", "content": data['message']})
if len(memory[data['user_id']]) >= 10:
memory[data['user_id']].pop(0)
client = Client()
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=memory[data['user_id']]
)
reply = response.choices[0].message.content
memory[data['user_id']].append({"role": "assistant", "content": reply})
return jsonify({'response':reply})
correct_answer = user_data[user_id]["answer"]
if answer.strip().lower() == correct_answer.lower():
result = "Correct!"
else:
result = f"Wrong! The correct answer was: {correct_answer}"
# Clean up user_id data after answer is submitted
del user_data[user_id]
return jsonify({"response": result}), 200
if __name__ == '__main__':
app.run(debug=True)