Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -1,58 +1,263 @@
|
|
1 |
-
from
|
2 |
-
import
|
3 |
-
import
|
|
|
4 |
from flask_cors import CORS
|
5 |
-
|
|
|
|
|
6 |
|
7 |
app = Flask(__name__)
|
8 |
-
app
|
9 |
-
app.static_url_path = '/static'
|
10 |
|
11 |
-
app.secret_key = "roadsense-abhi-2023"
|
12 |
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
-
|
16 |
-
# Load the model
|
17 |
-
model = joblib.load('accident_prediction_model_Final.m5')
|
18 |
|
19 |
-
|
20 |
-
|
|
|
21 |
|
22 |
-
|
23 |
-
def main():
|
24 |
-
return {'message': 'Hello, World'}
|
25 |
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
data = request.get_json()
|
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 |
if __name__ == '__main__':
|
|
|
1 |
+
from email.message import EmailMessage
|
2 |
+
from flask import Flask, request, jsonify, render_template
|
3 |
+
import random
|
4 |
+
import string
|
5 |
from flask_cors import CORS
|
6 |
+
import smtplib
|
7 |
+
import bcrypt
|
8 |
+
import openai
|
9 |
|
10 |
app = Flask(__name__)
|
11 |
+
CORS(app)
|
|
|
12 |
|
|
|
13 |
|
14 |
+
# Buffered Caches
|
15 |
+
rooms = {}
|
16 |
+
room_data = {}
|
17 |
+
started_rooms = {}
|
18 |
+
complete = {}
|
19 |
+
openai.api_key='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'
|
20 |
+
|
21 |
+
def generate_room_code():
|
22 |
+
return ''.join(random.choices(string.ascii_uppercase, k=6))
|
23 |
+
|
24 |
+
|
25 |
+
@app.route('/create-room', methods=['POST'])
|
26 |
+
def create_room():
|
27 |
+
room_code = generate_room_code()
|
28 |
+
rooms[room_code] = {'players': [], 'started': False, 'process': 0, 'generated': False, 'story_content': '', 'reactions': 0, 'chats': [], 'pollCount': 0}
|
29 |
+
|
30 |
+
player_name = request.json.get('name')
|
31 |
+
if not player_name:
|
32 |
+
return jsonify({'error': 'Player name is required'})
|
33 |
+
|
34 |
+
# Add the player to the room.
|
35 |
+
player_id = len(rooms[room_code]['players']) + 1
|
36 |
+
rooms[room_code]['players'].append({'id': player_id, 'name': player_name})
|
37 |
+
|
38 |
+
return jsonify({'room_code': room_code})
|
39 |
+
|
40 |
+
|
41 |
+
@app.route('/join-room/<room_code>', methods=['POST', 'GET'])
|
42 |
+
def join_room(room_code):
|
43 |
+
if request.method == 'POST':
|
44 |
+
if room_code not in rooms:
|
45 |
+
return jsonify({'noRoom': True})
|
46 |
+
|
47 |
+
if len(rooms[room_code]['players']) >= 2:
|
48 |
+
return jsonify({'error': 'Room is full'})
|
49 |
+
|
50 |
+
player_name = request.json.get('name')
|
51 |
+
if not player_name:
|
52 |
+
return jsonify({'error': 'Player name is required'})
|
53 |
+
|
54 |
+
# Add the player to the room.
|
55 |
+
player_id = len(rooms[room_code]['players']) + 1
|
56 |
+
rooms[room_code]['players'].append({'id': player_id, 'name': player_name})
|
57 |
+
|
58 |
+
return jsonify({'player_id': player_id, 'players': rooms[room_code]['players']})
|
59 |
+
|
60 |
+
if request.method == 'GET':
|
61 |
+
if room_code not in rooms:
|
62 |
+
return jsonify({'noRoom': True})
|
63 |
+
|
64 |
+
ready = False
|
65 |
+
|
66 |
+
if len(rooms[room_code]['players']) == 2:
|
67 |
+
ready = True
|
68 |
+
|
69 |
+
return jsonify({'players': rooms[room_code]['players'], 'ready': ready, 'started': rooms[room_code]['started'], 'meta_data': rooms[room_code]})
|
70 |
+
|
71 |
+
|
72 |
+
@app.route('/start-room/<room_code>', methods=['POST'])
|
73 |
+
def start_room(room_code):
|
74 |
+
if room_code not in rooms:
|
75 |
+
return jsonify({'error': 'Room not found'}), 404
|
76 |
+
|
77 |
+
rooms[room_code]['started'] = True
|
78 |
+
rooms[room_code]['process'] = 0
|
79 |
+
|
80 |
+
return jsonify({'success': True})
|
81 |
+
|
82 |
+
|
83 |
+
@app.route('/handle-tokens/<room_code>/<int:player_id>', methods=['POST', 'GET'])
|
84 |
+
def handle_tokens(room_code, player_id):
|
85 |
+
if request.method == 'POST':
|
86 |
+
if room_code not in rooms:
|
87 |
+
return jsonify({'error': 'Room not found'})
|
88 |
+
|
89 |
+
data = request.get_json()
|
90 |
+
word = data['word']
|
91 |
+
genres = data['genres']
|
92 |
+
additional_changes = data['additionalChanges']
|
93 |
+
|
94 |
+
rooms[room_code]['players'] = [{**player, 'word': word, 'genres': genres, 'additional_changes': additional_changes} if player['id'] == player_id else player for player in rooms[room_code]['players']]
|
95 |
+
rooms[room_code]['process'] += 1
|
96 |
+
|
97 |
+
if rooms[room_code]['process'] == 2:
|
98 |
+
process(room_code)
|
99 |
+
return jsonify({'success': True, 'process': True})
|
100 |
+
|
101 |
+
return jsonify({'success': True, 'process': False})
|
102 |
+
|
103 |
+
if request.method == 'GET':
|
104 |
+
if room_code not in rooms:
|
105 |
+
return jsonify({'error': 'Room not found'})
|
106 |
+
|
107 |
+
processed = False
|
108 |
+
generated = False
|
109 |
+
|
110 |
+
if rooms[room_code]['process'] == 2:
|
111 |
+
processed = True
|
112 |
+
|
113 |
+
if rooms[room_code]['generated']:
|
114 |
+
generated = True
|
115 |
+
|
116 |
+
return jsonify({'process': processed, 'generated': generated})
|
117 |
+
|
118 |
+
|
119 |
+
@app.route('/leave-room/<room_code>/<int:player_id>', methods=['POST'])
|
120 |
+
def leave_room(room_code, player_id):
|
121 |
+
if room_code not in rooms:
|
122 |
+
return jsonify({'error': 'Room not found'})
|
123 |
+
|
124 |
+
rooms[room_code]['players'] = [player for player in rooms[room_code]['players'] if player['id'] != player_id]
|
125 |
+
|
126 |
+
if player_id == 1 or not rooms[room_code]['players']:
|
127 |
+
del rooms[room_code]
|
128 |
+
|
129 |
+
return jsonify({'message': 'Player has left the room'})
|
130 |
|
|
|
|
|
|
|
131 |
|
132 |
+
def process(room_code):
|
133 |
+
if room_code not in rooms:
|
134 |
+
return jsonify({'error': 'Room not found'})
|
135 |
|
136 |
+
players = rooms[room_code]['players']
|
|
|
|
|
137 |
|
138 |
+
# Initialize variables to store aggregated data
|
139 |
+
room_data[room_code] = {
|
140 |
+
'word_data': {},
|
141 |
+
'additional_changes': ''
|
142 |
+
}
|
143 |
|
144 |
+
for player in players:
|
145 |
+
player_id = player['id']
|
146 |
+
|
147 |
+
# Store word data for each player
|
148 |
+
room_data[room_code]['word_data'][f'word {player_id}'] = player['word']
|
149 |
+
|
150 |
+
# Aggregate additional_changes
|
151 |
+
if room_data[room_code]['additional_changes']:
|
152 |
+
room_data[room_code]['additional_changes'] += ' ' + player['additional_changes']
|
153 |
+
else:
|
154 |
+
room_data[room_code]['additional_changes'] = player['additional_changes']
|
155 |
+
|
156 |
+
# Find the most repeated genre in the room
|
157 |
+
genre_counts = {}
|
158 |
+
for player in players:
|
159 |
+
for genre in player['genres']:
|
160 |
+
genre_counts[genre] = genre_counts.get(genre, 0) + 1
|
161 |
+
most_repeated_genre = max(genre_counts, key=genre_counts.get)
|
162 |
+
room_data[room_code]['most_repeated_genre'] = most_repeated_genre
|
163 |
+
|
164 |
+
print(room_data)
|
165 |
+
|
166 |
+
|
167 |
+
@app.route('/generate-story/<room_code>', methods=['POST'])
|
168 |
+
def generate_story(room_code):
|
169 |
+
if room_code not in rooms:
|
170 |
+
return jsonify({'error': 'Room not found'})
|
171 |
+
|
172 |
+
if rooms[room_code]['process'] != 2:
|
173 |
+
return jsonify({'error': 'Room not ready'})
|
174 |
+
|
175 |
+
{'JFLMHJ': {'word_data': {'word 1': 'Word 1', 'word 2': 'word 2'},'additional_changes': 'Paragraphs should be 4. Paragraph 3', 'most_repeated_genre':'Fantasy'}}
|
176 |
+
|
177 |
+
prompt = 'Keyword: '+ 'word' + '.' + 'Genres: ' + 'genres' + '.' + 'Additional Context: ' + 'additional' + '.'
|
178 |
+
|
179 |
+
conversation = [
|
180 |
+
{"role": "system", "content": "You are a creative storyteller. User will give atmost 5 Keywords along with genres and additional context for the story."},
|
181 |
+
{"role": "user", "content": prompt}
|
182 |
+
]
|
183 |
+
|
184 |
+
response = openai.ChatCompletion.create(
|
185 |
+
model='gpt-3.5-turbo',
|
186 |
+
messages=conversation,
|
187 |
+
temperature=0.7,
|
188 |
+
n=1
|
189 |
+
)
|
190 |
+
|
191 |
+
story = response['choices'][0]['message']['content']
|
192 |
+
|
193 |
+
rooms[room_code]['generated'] = True
|
194 |
+
rooms[room_code]['story_content'] = 'Meow'
|
195 |
+
|
196 |
+
print(rooms[room_code])
|
197 |
+
|
198 |
+
complete[room_code] = rooms[room_code]
|
199 |
+
|
200 |
+
return jsonify({'success': True, 'storyContent': 'Meow Meow'})
|
201 |
+
|
202 |
+
|
203 |
+
@app.route('/reaction-true/<room_code>', methods=['GET'])
|
204 |
+
def reaction_true(room_code):
|
205 |
+
rooms[room_code]['reactions'] += 1
|
206 |
+
|
207 |
+
return jsonify({'success': True, 'likeCount': rooms[room_code]['reactions']})
|
208 |
+
|
209 |
+
|
210 |
+
@app.route('/reaction-false/<room_code>', methods=['GET'])
|
211 |
+
def reaction_false(room_code):
|
212 |
+
rooms[room_code]['reactions'] -= 1
|
213 |
+
|
214 |
+
return jsonify({'success': True, 'likeCount': rooms[room_code]['reactions']})
|
215 |
+
|
216 |
+
|
217 |
+
@app.route('/room-chat/<room_code>', methods=['GET'])
|
218 |
+
def room_chat(room_code):
|
219 |
+
if room_code not in rooms:
|
220 |
+
return jsonify({'success': False, 'error': 'Room not found'})
|
221 |
+
print(rooms[room_code]['chats'])
|
222 |
+
return jsonify({'success': True, 'chats': rooms[room_code]['chats'], 'pollCount': rooms[room_code]['pollCount']})
|
223 |
+
|
224 |
+
|
225 |
+
@app.route('/chat-input/<room_code>/<int:player_id>', methods=['POST'])
|
226 |
+
def chat_input(room_code, player_id):
|
227 |
data = request.get_json()
|
228 |
+
if room_code not in rooms:
|
229 |
+
return jsonify({'success': False, 'error': 'Room not found'})
|
230 |
+
|
231 |
+
rooms[room_code]['chats'].append({'id': player_id, 'text': data.get('text')})
|
232 |
+
|
233 |
+
return jsonify({'success': True})
|
234 |
+
|
235 |
+
|
236 |
+
@app.route('/poll-data/<room_code>', methods=['POST'])
|
237 |
+
def poll_count(room_code):
|
238 |
+
if room_code not in rooms:
|
239 |
+
return jsonify({'success': False, 'error': 'Room not found'})
|
240 |
+
|
241 |
+
rooms[room_code]['pollCount'] += 1
|
242 |
+
|
243 |
+
return jsonify({'success': True, 'pollCount': rooms[room_code]['pollCount']})
|
244 |
+
|
245 |
+
|
246 |
+
@app.route('/complete/<room_code>', methods=['GET'])
|
247 |
+
def completed(room_code):
|
248 |
+
if room_code not in rooms:
|
249 |
+
return jsonify({'success': False, 'error': 'Room not found'})
|
250 |
+
|
251 |
+
return jsonify(complete[room_code])
|
252 |
+
|
253 |
+
|
254 |
+
@app.route('/list-rooms', methods=['GET'])
|
255 |
+
def list_rooms():
|
256 |
+
room_info = {}
|
257 |
+
for room_code, player in rooms.items():
|
258 |
+
room_info[room_code] = len(player['players'])
|
259 |
+
|
260 |
+
return jsonify({'roomInfo': room_info})
|
261 |
|
262 |
|
263 |
if __name__ == '__main__':
|