Spaces:
Build error
Build error
| import pandas as pd | |
| import openai, json | |
| from collections import Counter | |
| from random import choice | |
| from utilities import constants | |
| INPUT_DATAPATH = "slack_processing/data/themes.json" | |
| OUTPUT_GAME_IDEAS_DATAPATH="create_games/data/game_ideas.json" | |
| themes = [] | |
| themes_by_person={} | |
| people_by_theme = {} | |
| players=[] | |
| def ReadThemesFromFile(): | |
| global themes | |
| with open(INPUT_DATAPATH, "r") as json_file: | |
| themes = json.load(json_file) | |
| def PopulateThemesByPerson(): | |
| global themes, themes_by_person | |
| themes_by_person = {} | |
| for theme in themes: | |
| person = theme['person'] | |
| postId = theme['postId'] | |
| theme_info = (theme['theme'], postId) | |
| themes_by_person.setdefault(person, set()).add(theme_info) | |
| def PopulatePeopleByThemes(): | |
| global themes, people_by_theme | |
| people_by_theme = {} | |
| for theme in themes: | |
| person = theme['person'] | |
| theme_name = theme['theme'] | |
| people_by_theme.setdefault(theme_name, set()).add(person) | |
| def FindCommonTheme(people_set): | |
| global themes_by_person, people_by_theme | |
| theme_counter = Counter() | |
| for person in people_set: | |
| if person in themes_by_person: | |
| unique_themes = set(theme for theme, _ in themes_by_person[person]) | |
| for theme in unique_themes: | |
| theme_counter[theme] += 1 | |
| # Case 1: Theme shared by all people | |
| for theme, count in theme_counter.items(): | |
| if count == len(people_set): | |
| print("case 1: shared by all", theme) | |
| return theme | |
| # Case 2: Theme shared by most people | |
| print('theme_counter', theme_counter) | |
| if theme_counter: | |
| # Find the highest frequency among themes | |
| max_count = theme_counter.most_common(1)[0][1] | |
| # Gather all themes with that frequency | |
| most_common_themes = [theme for theme, count in theme_counter.items() if count == max_count] | |
| # Randomly choose among them | |
| most_common_theme = choice(most_common_themes) | |
| print("case 2: shared by most", most_common_theme) | |
| return most_common_theme | |
| # Case 3: Most popular theme | |
| if people_by_theme: | |
| most_popular_theme, _ = Counter({theme: len(people) for theme, people in people_by_theme.items()}).most_common(1)[0] | |
| print("case 3: most_popular_theme", most_popular_theme) | |
| return most_popular_theme | |
| else: | |
| return None | |
| def Completion(messages): | |
| response = openai.ChatCompletion.create( | |
| model="gpt-3.5-turbo", | |
| messages=messages | |
| ) | |
| return response["choices"][0]["message"]["content"] | |
| def GenerateGameIdeas(common_topic, group, gameType): | |
| prompt=None | |
| if gameType==constants.CHAT: | |
| prompt = f"""Given this game topic '{common_topic}', create a chat-based game that a player can play against YOU via chat interface. Refer to yourself as BuildBot. Provide 1-2 Tips, example questions to get players started (Tip=Ask about color). Include 1-3 Rules that explain how to play. Keep answers short. Answer must be in JSON, in this format: {{"Name":"Name of game", "Description":"Game rules", "Type":"Draw or Chat", "Rules":"Some rules for the game.", "Tips":"String of 1-2 questions to get players started", "InitialQuestion":"Pose the first question that starts the game."}}""" | |
| elif gameType==constants.DRAW: | |
| prompt = f"""Given this game topic '{common_topic}', create a drawing-based game where a player draws a single thing. Include 1-3 simple rules for how to play and the first thing for them to draw (InitialQuestion). Keep answers short. Answer must be in JSON, in this format: {{"Name": "Name of game", "Description": "Game rules", "Rules": "Some rules for the game.", "Tips": "String of 1-2 questions to get players started","InitialQuestion": "Tell the player what to draw first."}}""" | |
| messages=[] | |
| messages.append({"role": "system", "content": prompt}) | |
| game_data = Completion(messages) | |
| # Process the response and extract the game ideas | |
| game_ideas = [] | |
| errorMsg=None | |
| try: | |
| game_data_dict = json.loads(game_data) | |
| game_ideas.append(game_data_dict) | |
| except json.JSONDecodeError: | |
| errorMsg="Error decoding JSON." | |
| if game_ideas: | |
| with open(OUTPUT_GAME_IDEAS_DATAPATH, 'w') as file: | |
| json.dump(game_ideas, file, indent=4) | |
| return game_ideas, None | |
| else: | |
| if not errorMsg == None: | |
| return None, errorMsg | |
| print("No game idea found.") | |
| return None, None | |
| def GetPlayersAndIndex(): | |
| players = [] | |
| index = 0 | |
| for theme in themes: | |
| person = theme['person'] | |
| if person not in [t[0] for t in players]: | |
| players.append((person, index)) | |
| index += 1 | |
| return players | |
| def GetPlayers(): | |
| players = [] | |
| for theme in themes: | |
| person = theme['person'] | |
| if person not in players: | |
| players.append((person)) | |
| return players | |
| def GetPeopleByThemesDF(): | |
| global people_by_theme | |
| flattened_data = [(theme, person) for theme, persons in people_by_theme.items() for person in persons] | |
| # Create a DataFrame | |
| df = pd.DataFrame(flattened_data, columns=['Theme', 'Person']) | |
| return df | |
| def MostCommonTheme(): | |
| global people_by_theme | |
| return max(people_by_theme, key=lambda k: len(people_by_theme[k])) | |
| def LoadThemes(): | |
| global themes,people_by_theme, themes_by_person, players | |
| ReadThemesFromFile() | |
| PopulatePeopleByThemes() | |
| PopulateThemesByPerson() | |
| def CreateGameForGroup(group, gameType, retry=False): | |
| global themes,themes_by_person | |
| if not retry: | |
| common_topic = FindCommonTheme(group) | |
| else: | |
| common_topic = MostCommonTheme() | |
| ideas,error = GenerateGameIdeas(common_topic, group, gameType) | |
| if ideas is None or not ideas: | |
| if not retry: | |
| retry=True | |
| players=GetPlayers() | |
| return CreateGameForGroup(players, gameType, retry) | |
| else: | |
| return None, None | |
| else: | |
| if error is not None: | |
| print("Error: ", error) | |
| return None, error | |
| print('above df, ideas: ', ideas) | |
| gDF=pd.DataFrame(ideas) | |
| return gDF,common_topic |