Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -18,6 +18,7 @@ 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 |
|
@@ -25,7 +26,8 @@ def generate_room_code():
|
|
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': '',
|
|
|
29 |
|
30 |
player_name = request.json.get('name')
|
31 |
if not player_name:
|
@@ -44,7 +46,7 @@ def join_room(room_code):
|
|
44 |
if room_code not in rooms:
|
45 |
return jsonify({'noRoom': True})
|
46 |
|
47 |
-
if len(rooms[room_code]['players']) >=
|
48 |
return jsonify({'error': 'Room is full'})
|
49 |
|
50 |
player_name = request.json.get('name')
|
@@ -63,10 +65,11 @@ def join_room(room_code):
|
|
63 |
|
64 |
ready = False
|
65 |
|
66 |
-
if len(rooms[room_code]['players']) ==
|
67 |
ready = True
|
68 |
|
69 |
-
return jsonify({'players': rooms[room_code]['players'], 'ready': ready, 'started': rooms[room_code]['started'],
|
|
|
70 |
|
71 |
|
72 |
@app.route('/start-room/<room_code>', methods=['POST'])
|
@@ -91,13 +94,19 @@ def handle_tokens(room_code, player_id):
|
|
91 |
genres = data['genres']
|
92 |
additional_changes = data['additionalChanges']
|
93 |
|
94 |
-
rooms[room_code]['players'] = [
|
|
|
|
|
|
|
95 |
rooms[room_code]['process'] += 1
|
96 |
|
97 |
-
if rooms[room_code]['process'] ==
|
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':
|
@@ -107,7 +116,7 @@ def handle_tokens(room_code, player_id):
|
|
107 |
processed = False
|
108 |
generated = False
|
109 |
|
110 |
-
if rooms[room_code]['process'] ==
|
111 |
processed = True
|
112 |
|
113 |
if rooms[room_code]['generated']:
|
@@ -154,11 +163,27 @@ def process(room_code):
|
|
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 |
-
|
161 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
162 |
room_data[room_code]['most_repeated_genre'] = most_repeated_genre
|
163 |
|
164 |
print(room_data)
|
@@ -169,15 +194,36 @@ def generate_story(room_code):
|
|
169 |
if room_code not in rooms:
|
170 |
return jsonify({'error': 'Room not found'})
|
171 |
|
172 |
-
|
173 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
174 |
|
175 |
-
|
176 |
|
177 |
-
|
|
|
178 |
|
179 |
conversation = [
|
180 |
-
{"role": "system",
|
|
|
181 |
{"role": "user", "content": prompt}
|
182 |
]
|
183 |
|
@@ -191,13 +237,13 @@ def generate_story(room_code):
|
|
191 |
story = response['choices'][0]['message']['content']
|
192 |
|
193 |
rooms[room_code]['generated'] = True
|
194 |
-
rooms[room_code]['story_content'] =
|
195 |
|
196 |
print(rooms[room_code])
|
197 |
|
198 |
complete[room_code] = rooms[room_code]
|
199 |
|
200 |
-
return jsonify({'success': True, 'storyContent':
|
201 |
|
202 |
|
203 |
@app.route('/reaction-true/<room_code>', methods=['GET'])
|
|
|
18 |
complete = {}
|
19 |
openai.api_key='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'
|
20 |
|
21 |
+
|
22 |
def generate_room_code():
|
23 |
return ''.join(random.choices(string.ascii_uppercase, k=6))
|
24 |
|
|
|
26 |
@app.route('/create-room', methods=['POST'])
|
27 |
def create_room():
|
28 |
room_code = generate_room_code()
|
29 |
+
rooms[room_code] = {'players': [], 'started': False, 'process': 0, 'generated': False, 'story_content': '',
|
30 |
+
'reactions': 0, 'chats': [], 'pollCount': 0}
|
31 |
|
32 |
player_name = request.json.get('name')
|
33 |
if not player_name:
|
|
|
46 |
if room_code not in rooms:
|
47 |
return jsonify({'noRoom': True})
|
48 |
|
49 |
+
if len(rooms[room_code]['players']) >= 5:
|
50 |
return jsonify({'error': 'Room is full'})
|
51 |
|
52 |
player_name = request.json.get('name')
|
|
|
65 |
|
66 |
ready = False
|
67 |
|
68 |
+
if len(rooms[room_code]['players']) == 5: # do it <= 5
|
69 |
ready = True
|
70 |
|
71 |
+
return jsonify({'players': rooms[room_code]['players'], 'ready': ready, 'started': rooms[room_code]['started'],
|
72 |
+
'meta_data': rooms[room_code]})
|
73 |
|
74 |
|
75 |
@app.route('/start-room/<room_code>', methods=['POST'])
|
|
|
94 |
genres = data['genres']
|
95 |
additional_changes = data['additionalChanges']
|
96 |
|
97 |
+
rooms[room_code]['players'] = [
|
98 |
+
{**player, 'word': word, 'genres': genres, 'additional_changes': additional_changes} if player[
|
99 |
+
'id'] == player_id else player
|
100 |
+
for player in rooms[room_code]['players']]
|
101 |
rooms[room_code]['process'] += 1
|
102 |
|
103 |
+
if rooms[room_code]['process'] == 5:
|
104 |
process(room_code)
|
105 |
return jsonify({'success': True, 'process': True})
|
106 |
|
107 |
+
print(rooms[room_code]['players'])
|
108 |
+
print(rooms[room_code]['process'])
|
109 |
+
|
110 |
return jsonify({'success': True, 'process': False})
|
111 |
|
112 |
if request.method == 'GET':
|
|
|
116 |
processed = False
|
117 |
generated = False
|
118 |
|
119 |
+
if rooms[room_code]['process'] == 5:
|
120 |
processed = True
|
121 |
|
122 |
if rooms[room_code]['generated']:
|
|
|
163 |
room_data[room_code]['additional_changes'] = player['additional_changes']
|
164 |
|
165 |
# Find the most repeated genre in the room
|
166 |
+
# genre_counts = {}
|
167 |
+
# for player in players:
|
168 |
+
# for genre in player['genres']:
|
169 |
+
# genre_counts[genre] = genre_counts.get(genre, 0) + 1
|
170 |
+
# most_repeated_genre = max(genre_counts, key=genre_counts.get)
|
171 |
+
# room_data[room_code]['most_repeated_genre'] = most_repeated_genre
|
172 |
+
|
173 |
genre_counts = {}
|
174 |
for player in players:
|
175 |
for genre in player['genres']:
|
176 |
+
if genre in genre_counts:
|
177 |
+
genre_counts[genre] += 1
|
178 |
+
else:
|
179 |
+
genre_counts[genre] = 1
|
180 |
+
most_repeated_genre = ''
|
181 |
+
maxct = 0
|
182 |
+
for key, value in genre_counts.items():
|
183 |
+
if maxct < value:
|
184 |
+
most_repeated_genre = key
|
185 |
+
maxct = value
|
186 |
+
|
187 |
room_data[room_code]['most_repeated_genre'] = most_repeated_genre
|
188 |
|
189 |
print(room_data)
|
|
|
194 |
if room_code not in rooms:
|
195 |
return jsonify({'error': 'Room not found'})
|
196 |
|
197 |
+
data = request.get_json()
|
198 |
+
word_limit = data.get('wordLimit')
|
199 |
+
word_data = room_data[room_code]['word_data']
|
200 |
+
additional_context = room_data[room_code]['additional_changes']
|
201 |
+
genre = room_data[room_code]['most_repeated_genre']
|
202 |
+
words = ''
|
203 |
+
ct = 0
|
204 |
+
for key, value in word_data.items():
|
205 |
+
ct += 1
|
206 |
+
if ct < len(word_data):
|
207 |
+
words += (value + ', ')
|
208 |
+
else:
|
209 |
+
words += value
|
210 |
+
|
211 |
+
if len(additional_context.split()) >= 250:
|
212 |
+
additional_context = ''
|
213 |
+
|
214 |
+
prompt = 'Keyword: ' + str(words) + '.' + 'Genres: ' + str(genre) + '.' + 'Additional Context: ' + str(
|
215 |
+
additional_context) + '.' + 'Word Limit: ' + str(word_limit) + '.'
|
216 |
+
|
217 |
+
print(prompt)
|
218 |
|
219 |
+
profanity.load_censor_words()
|
220 |
|
221 |
+
if profanity.contains_profanity(prompt):
|
222 |
+
return jsonify({'success': False, 'message': 'Content is improper.'})
|
223 |
|
224 |
conversation = [
|
225 |
+
{"role": "system",
|
226 |
+
"content": "You are a creative storyteller. User will give atmost 5 Keywords along with genres and additional context for the story. Also user will mention the word limit for the story."},
|
227 |
{"role": "user", "content": prompt}
|
228 |
]
|
229 |
|
|
|
237 |
story = response['choices'][0]['message']['content']
|
238 |
|
239 |
rooms[room_code]['generated'] = True
|
240 |
+
rooms[room_code]['story_content'] = story
|
241 |
|
242 |
print(rooms[room_code])
|
243 |
|
244 |
complete[room_code] = rooms[room_code]
|
245 |
|
246 |
+
return jsonify({'success': True, 'storyContent': story})
|
247 |
|
248 |
|
249 |
@app.route('/reaction-true/<room_code>', methods=['GET'])
|