Spaces:
Sleeping
Sleeping
File size: 9,752 Bytes
10a9483 1e9b315 10a9483 1e9b315 10a9483 1e9b315 10a9483 f7b09fd 10a9483 2f38067 10a9483 2f38067 10a9483 2f38067 10a9483 2f38067 10a9483 2f38067 10a9483 2f38067 10a9483 2f38067 10a9483 2f38067 10a9483 2f38067 10a9483 1e9b315 10a9483 1e9b315 10a9483 1e9b315 10a9483 1e9b315 10a9483 2f38067 10a9483 2f38067 10a9483 2f38067 10a9483 2f38067 10a9483 2f38067 10a9483 2f38067 10a9483 2f38067 10a9483 2f38067 10a9483 1e9b315 10a9483 1e9b315 |
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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
from email.message import EmailMessage
from flask import Flask, request, jsonify, render_template
import random
import string
from flask_cors import CORS
import smtplib
import bcrypt
import openai
app = Flask(__name__)
CORS(app)
# Buffered Caches
rooms = {}
room_data = {}
started_rooms = {}
complete = {}
openai.api_key='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'
def generate_room_code():
return ''.join(random.choices(string.ascii_uppercase, k=6))
@app.route('/create-room', methods=['POST'])
def create_room():
room_code = generate_room_code()
rooms[room_code] = {'players': [], 'started': False, 'process': 0, 'generated': False, 'story_content': '',
'reactions': 0, 'chats': [], 'pollCount': 0}
player_name = request.json.get('name')
if not player_name:
return jsonify({'error': 'Player name is required'})
# Add the player to the room.
player_id = len(rooms[room_code]['players']) + 1
rooms[room_code]['players'].append({'id': player_id, 'name': player_name})
return jsonify({'room_code': room_code})
@app.route('/join-room/<room_code>', methods=['POST', 'GET'])
def join_room(room_code):
if request.method == 'POST':
if room_code not in rooms:
return jsonify({'noRoom': True})
if len(rooms[room_code]['players']) >= 5:
return jsonify({'error': 'Room is full'})
player_name = request.json.get('name')
if not player_name:
return jsonify({'error': 'Player name is required'})
# Add the player to the room.
player_id = len(rooms[room_code]['players']) + 1
rooms[room_code]['players'].append({'id': player_id, 'name': player_name})
return jsonify({'player_id': player_id, 'players': rooms[room_code]['players']})
if request.method == 'GET':
if room_code not in rooms:
return jsonify({'noRoom': True})
ready = False
if len(rooms[room_code]['players']) == 5: # do it <= 5
ready = True
return jsonify({'players': rooms[room_code]['players'], 'ready': ready, 'started': rooms[room_code]['started'],
'meta_data': rooms[room_code]})
@app.route('/start-room/<room_code>', methods=['POST'])
def start_room(room_code):
if room_code not in rooms:
return jsonify({'error': 'Room not found'}), 404
rooms[room_code]['started'] = True
rooms[room_code]['process'] = 0
return jsonify({'success': True})
@app.route('/handle-tokens/<room_code>/<int:player_id>', methods=['POST', 'GET'])
def handle_tokens(room_code, player_id):
if request.method == 'POST':
if room_code not in rooms:
return jsonify({'error': 'Room not found'})
data = request.get_json()
word = data['word']
genres = data['genres']
additional_changes = data['additionalChanges']
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']]
rooms[room_code]['process'] += 1
if rooms[room_code]['process'] == 5:
process(room_code)
return jsonify({'success': True, 'process': True})
print(rooms[room_code]['players'])
print(rooms[room_code]['process'])
return jsonify({'success': True, 'process': False})
if request.method == 'GET':
if room_code not in rooms:
return jsonify({'error': 'Room not found'})
processed = False
generated = False
if rooms[room_code]['process'] == 5:
processed = True
if rooms[room_code]['generated']:
generated = True
return jsonify({'process': processed, 'generated': generated})
@app.route('/leave-room/<room_code>/<int:player_id>', methods=['POST'])
def leave_room(room_code, player_id):
if room_code not in rooms:
return jsonify({'error': 'Room not found'})
rooms[room_code]['players'] = [player for player in rooms[room_code]['players'] if player['id'] != player_id]
if player_id == 1 or not rooms[room_code]['players']:
del rooms[room_code]
return jsonify({'message': 'Player has left the room'})
def process(room_code):
if room_code not in rooms:
return jsonify({'error': 'Room not found'})
players = rooms[room_code]['players']
# Initialize variables to store aggregated data
room_data[room_code] = {
'word_data': {},
'additional_changes': ''
}
for player in players:
player_id = player['id']
# Store word data for each player
room_data[room_code]['word_data'][f'word {player_id}'] = player['word']
# Aggregate additional_changes
if room_data[room_code]['additional_changes']:
room_data[room_code]['additional_changes'] += ' ' + player['additional_changes']
else:
room_data[room_code]['additional_changes'] = player['additional_changes']
# Find the most repeated genre in the room
# genre_counts = {}
# for player in players:
# for genre in player['genres']:
# genre_counts[genre] = genre_counts.get(genre, 0) + 1
# most_repeated_genre = max(genre_counts, key=genre_counts.get)
# room_data[room_code]['most_repeated_genre'] = most_repeated_genre
genre_counts = {}
for player in players:
for genre in player['genres']:
if genre in genre_counts:
genre_counts[genre] += 1
else:
genre_counts[genre] = 1
most_repeated_genre = ''
maxct = 0
for key, value in genre_counts.items():
if maxct < value:
most_repeated_genre = key
maxct = value
room_data[room_code]['most_repeated_genre'] = most_repeated_genre
print(room_data)
@app.route('/generate-story/<room_code>', methods=['POST'])
def generate_story(room_code):
if room_code not in rooms:
return jsonify({'error': 'Room not found'})
data = request.get_json()
word_limit = data.get('wordLimit')
word_data = room_data[room_code]['word_data']
additional_context = room_data[room_code]['additional_changes']
genre = room_data[room_code]['most_repeated_genre']
words = ''
ct = 0
for key, value in word_data.items():
ct += 1
if ct < len(word_data):
words += (value + ', ')
else:
words += value
if len(additional_context.split()) >= 250:
additional_context = ''
prompt = 'Keyword: ' + str(words) + '.' + 'Genres: ' + str(genre) + '.' + 'Additional Context: ' + str(
additional_context) + '.' + 'Word Limit: ' + str(word_limit) + '.'
print(prompt)
profanity.load_censor_words()
if profanity.contains_profanity(prompt):
return jsonify({'success': False, 'message': 'Content is improper.'})
conversation = [
{"role": "system",
"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."},
{"role": "user", "content": prompt}
]
response = openai.ChatCompletion.create(
model='gpt-3.5-turbo',
messages=conversation,
temperature=0.7,
n=1
)
story = response['choices'][0]['message']['content']
rooms[room_code]['generated'] = True
rooms[room_code]['story_content'] = story
print(rooms[room_code])
complete[room_code] = rooms[room_code]
return jsonify({'success': True, 'storyContent': story})
@app.route('/reaction-true/<room_code>', methods=['GET'])
def reaction_true(room_code):
rooms[room_code]['reactions'] += 1
return jsonify({'success': True, 'likeCount': rooms[room_code]['reactions']})
@app.route('/reaction-false/<room_code>', methods=['GET'])
def reaction_false(room_code):
rooms[room_code]['reactions'] -= 1
return jsonify({'success': True, 'likeCount': rooms[room_code]['reactions']})
@app.route('/room-chat/<room_code>', methods=['GET'])
def room_chat(room_code):
if room_code not in rooms:
return jsonify({'success': False, 'error': 'Room not found'})
print(rooms[room_code]['chats'])
return jsonify({'success': True, 'chats': rooms[room_code]['chats'], 'pollCount': rooms[room_code]['pollCount']})
@app.route('/chat-input/<room_code>/<int:player_id>', methods=['POST'])
def chat_input(room_code, player_id):
data = request.get_json()
if room_code not in rooms:
return jsonify({'success': False, 'error': 'Room not found'})
rooms[room_code]['chats'].append({'id': player_id, 'text': data.get('text')})
return jsonify({'success': True})
@app.route('/poll-data/<room_code>', methods=['POST'])
def poll_count(room_code):
if room_code not in rooms:
return jsonify({'success': False, 'error': 'Room not found'})
rooms[room_code]['pollCount'] += 1
return jsonify({'success': True, 'pollCount': rooms[room_code]['pollCount']})
@app.route('/complete/<room_code>', methods=['GET'])
def completed(room_code):
if room_code not in rooms:
return jsonify({'success': False, 'error': 'Room not found'})
return jsonify(complete[room_code])
@app.route('/list-rooms', methods=['GET'])
def list_rooms():
room_info = {}
for room_code, player in rooms.items():
room_info[room_code] = len(player['players'])
return jsonify({'roomInfo': room_info})
if __name__ == '__main__':
app.run() |