Spaces:
Sleeping
Sleeping
from flask import Flask, request, jsonify, render_template | |
import random | |
import string | |
from flask_cors import CORS | |
import smtplib | |
import bcrypt | |
import openai | |
from better_profanity import profanity | |
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)) | |
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 {'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 {'room_code': room_code} | |
def join_room(room_code): | |
if request.method == 'POST': | |
if room_code not in rooms: | |
return {'noRoom': True} | |
if len(rooms[room_code]['players']) >= 5: | |
return {'error': 'Room is full'} | |
player_name = request.json.get('name') | |
if not player_name: | |
return {'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 {'player_id': player_id, 'players': rooms[room_code]['players']} | |
if request.method == 'GET': | |
if room_code not in rooms: | |
return {'noRoom': True} | |
ready = False | |
if len(rooms[room_code]['players']) == 5: # do it <= 5 | |
ready = True | |
return {'players': rooms[room_code]['players'], 'ready': ready, 'started': rooms[room_code]['started'], | |
'meta_data': rooms[room_code]} | |
def start_room(room_code): | |
if room_code not in rooms: | |
return {'error': 'Room not found'} | |
rooms[room_code]['started'] = True | |
rooms[room_code]['process'] = 0 | |
return {'success': True} | |
def handle_tokens(room_code, player_id): | |
if request.method == 'POST': | |
if room_code not in rooms: | |
return {'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 {'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 {'process': processed, 'generated': generated} | |
def leave_room(room_code, player_id): | |
if room_code not in rooms: | |
return {'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 {'message': 'Player has left the room'} | |
def process(room_code): | |
if room_code not in rooms: | |
return {'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) | |
def generate_story(room_code): | |
if room_code not in rooms: | |
return {'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 {'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 {'success': True, 'storyContent': story} | |
def reaction_true(room_code): | |
rooms[room_code]['reactions'] += 1 | |
return {'success': True, 'likeCount': rooms[room_code]['reactions']} | |
def reaction_false(room_code): | |
rooms[room_code]['reactions'] -= 1 | |
return {'success': True, 'likeCount': rooms[room_code]['reactions']} | |
def room_chat(room_code): | |
if room_code not in rooms: | |
return {'success': False, 'error': 'Room not found'} | |
print(rooms[room_code]['chats']) | |
return {'success': True, 'chats': rooms[room_code]['chats'], 'pollCount': rooms[room_code]['pollCount']} | |
def chat_input(room_code, player_id): | |
data = request.get_json() | |
if room_code not in rooms: | |
return {'success': False, 'error': 'Room not found'} | |
rooms[room_code]['chats'].append({'id': player_id, 'text': data.get('text')}) | |
return {'success': True} | |
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 {'success': True, 'pollCount': rooms[room_code]['pollCount']} | |
def completed(room_code): | |
if room_code not in rooms: | |
return {'success': False, 'error': 'Room not found'} | |
return complete[room_code] | |
def list_rooms(): | |
room_info = {} | |
for room_code, player in rooms.items(): | |
room_info[room_code] = len(player['players']) | |
return {'roomInfo': room_info} | |
if __name__ == '__main__': | |
app.run() |