Spaces:
Sleeping
Sleeping
| import pandas as pd | |
| import openai, json | |
| from collections import Counter | |
| from random import choice | |
| 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 | |
| print("PopulateThemesByPerson") | |
| 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) | |
| print("end, themes_by_person, ", themes_by_person) | |
| def PopulatePeopleByThemes(): | |
| global themes, people_by_theme | |
| print("PopulatePeopleByThemes") | |
| people_by_theme = {} | |
| for theme in themes: | |
| person = theme['person'] | |
| theme_name = theme['theme'] | |
| people_by_theme.setdefault(theme_name, set()).add(person) | |
| print("end, people_by_theme, ", people_by_theme) | |
| 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 GenerateGameIdeas(common_topic, group): | |
| prompt = f"""Provide rules for a drawing game that this group of players '{group}' may play centered on this theme '{common_topic}'. Keep your answers short. Answer in JSON, in this format: {{"Name":"Name of game","Description":"Game rules","Rationale":"Reasoning for game selection"}}""" | |
| response = openai.Completion.create( | |
| engine="text-davinci-003", | |
| prompt=prompt, | |
| max_tokens=800, | |
| n=1, | |
| temperature=0.1) | |
| # Process the response and extract the game ideas | |
| game_ideas = [] | |
| for choice in response["choices"]: | |
| choiceText = choice["text"] | |
| game_data = choiceText.strip('\n') | |
| print("--after strip game_data", game_data) | |
| try: | |
| game_data_dict = json.loads(game_data) | |
| game_ideas.append(game_data_dict) | |
| print("----game_data", game_data_dict) | |
| except json.JSONDecodeError: | |
| print("Error decoding JSON.") | |
| continue | |
| if game_ideas: | |
| with open(OUTPUT_GAME_IDEAS_DATAPATH, 'w') as file: | |
| json.dump(game_ideas, file, indent=4) | |
| return game_ideas | |
| else: | |
| print("No game idea found.") | |
| return 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 | |
| print("\n***players", players) | |
| return players | |
| def GetPlayers(): | |
| players = [] | |
| index = 0 | |
| for theme in themes: | |
| person = theme['person'] | |
| if person not in players: | |
| players.append((person)) | |
| print("\n***players", players) | |
| 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 LoadThemes(): | |
| global themes,people_by_theme, themes_by_person, players | |
| ReadThemesFromFile() | |
| PopulatePeopleByThemes() | |
| PopulateThemesByPerson() | |
| def CreateGameForGroup(group, retry=False): | |
| global themes | |
| if not retry: | |
| common_topic = FindCommonTheme(group) | |
| else: | |
| common_topic = themes | |
| game_ideas = GenerateGameIdeas(common_topic, group) | |
| if game_ideas is None: | |
| if not retry: | |
| retry=True | |
| return CreateGameForGroup(players, retry) | |
| else: | |
| gameDF=pd.DataFrame(game_ideas) | |
| return gameDF | |
| return None |