Spaces:
Sleeping
Sleeping
Kim Adams commited on
Commit ·
03fcc07
1
Parent(s): b74a846
game gen
Browse files
app.py
CHANGED
|
@@ -5,13 +5,13 @@ from io import BytesIO
|
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
from openai.embeddings_utils import get_embedding, cosine_similarity
|
| 7 |
from slack_processing.slack_data_prep import FetchSlack, ProcessSlack, CreateEmbeddings
|
|
|
|
| 8 |
##from ml_to_nl_translation.translation import getTranslations, getJSONDF
|
| 9 |
#from lookups.translate_pdf_to_text import PreparePDF
|
| 10 |
#from lookups.create_searchable_content import CreateSearchableContent
|
| 11 |
from utilities import api_keys
|
| 12 |
import PyPDF2
|
| 13 |
|
| 14 |
-
|
| 15 |
import pkg_resources
|
| 16 |
dotenv_version = pkg_resources.get_distribution("tiktoken").version
|
| 17 |
print(f"tiktoken version: {dotenv_version}")
|
|
@@ -38,13 +38,10 @@ def FetchEmbeddings():
|
|
| 38 |
print(result)
|
| 39 |
return result
|
| 40 |
|
| 41 |
-
def
|
| 42 |
-
|
| 43 |
-
print("
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
def fetch_content():
|
| 47 |
-
# result = CreateSearchableContent()
|
| 48 |
return result
|
| 49 |
|
| 50 |
with gr.Blocks() as ui1:
|
|
@@ -77,7 +74,7 @@ with gr.Blocks() as ui4:
|
|
| 77 |
with gr.Row():
|
| 78 |
with gr.Column(scale=1, min_width=600):
|
| 79 |
df4 =gr.Dataframe(type="pandas")
|
| 80 |
-
b4.click(
|
| 81 |
|
| 82 |
demo = gr.TabbedInterface([ui1,ui2,ui3,ui4], ("Fetch Slack", "Process & Tag", "Create Embeddings", "Find Games"))
|
| 83 |
demo.launch()
|
|
|
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
from openai.embeddings_utils import get_embedding, cosine_similarity
|
| 7 |
from slack_processing.slack_data_prep import FetchSlack, ProcessSlack, CreateEmbeddings
|
| 8 |
+
from create_games.create_games_slack import CreateGames
|
| 9 |
##from ml_to_nl_translation.translation import getTranslations, getJSONDF
|
| 10 |
#from lookups.translate_pdf_to_text import PreparePDF
|
| 11 |
#from lookups.create_searchable_content import CreateSearchableContent
|
| 12 |
from utilities import api_keys
|
| 13 |
import PyPDF2
|
| 14 |
|
|
|
|
| 15 |
import pkg_resources
|
| 16 |
dotenv_version = pkg_resources.get_distribution("tiktoken").version
|
| 17 |
print(f"tiktoken version: {dotenv_version}")
|
|
|
|
| 38 |
print(result)
|
| 39 |
return result
|
| 40 |
|
| 41 |
+
def FetchGames():
|
| 42 |
+
result = CreateGames()
|
| 43 |
+
print("result")
|
| 44 |
+
print(result)
|
|
|
|
|
|
|
|
|
|
| 45 |
return result
|
| 46 |
|
| 47 |
with gr.Blocks() as ui1:
|
|
|
|
| 74 |
with gr.Row():
|
| 75 |
with gr.Column(scale=1, min_width=600):
|
| 76 |
df4 =gr.Dataframe(type="pandas")
|
| 77 |
+
b4.click(FetchGames,outputs=df4)
|
| 78 |
|
| 79 |
demo = gr.TabbedInterface([ui1,ui2,ui3,ui4], ("Fetch Slack", "Process & Tag", "Create Embeddings", "Find Games"))
|
| 80 |
demo.launch()
|
create_games/__pycache__/create_games_slack.cpython-311.pyc
ADDED
|
Binary file (11.5 kB). View file
|
|
|
create_games/create_games_slack.py
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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6 |
+
from sklearn.metrics import classification_report, PrecisionRecallDisplay
|
| 7 |
+
from collections import Counter
|
| 8 |
+
from nltk.corpus import wordnet
|
| 9 |
+
from nltk.tokenize import word_tokenize
|
| 10 |
+
import random
|
| 11 |
+
|
| 12 |
+
INPUT_DATAPATH = "slack_processing/data/themes.json"
|
| 13 |
+
OUTPUT_GAME_IDEAS_DATAPATH="create_games/data/game_ideas.json"
|
| 14 |
+
themes = []
|
| 15 |
+
themes_by_person={}
|
| 16 |
+
people_by_theme = {}
|
| 17 |
+
|
| 18 |
+
def ReadThemesFromFile():
|
| 19 |
+
global themes
|
| 20 |
+
with open(INPUT_DATAPATH, "r") as json_file:
|
| 21 |
+
themes = json.load(json_file)
|
| 22 |
+
print("done reading")
|
| 23 |
+
|
| 24 |
+
def PopulateThemesByPerson():
|
| 25 |
+
global themes, themes_by_person
|
| 26 |
+
print("*** themes:")
|
| 27 |
+
print (themes)
|
| 28 |
+
themes_by_person = {}
|
| 29 |
+
for theme in themes:
|
| 30 |
+
person = theme['person']
|
| 31 |
+
postId = theme['postId']
|
| 32 |
+
theme_info = (theme['theme'], postId)
|
| 33 |
+
themes_by_person.setdefault(person, set()).add(theme_info)
|
| 34 |
+
|
| 35 |
+
def PrintThemesByPerson():
|
| 36 |
+
print("PrintThemesByPerson")
|
| 37 |
+
global themes_by_person
|
| 38 |
+
print("Themes by person: ")
|
| 39 |
+
print (themes_by_person)
|
| 40 |
+
for person, person_themes in themes_by_person.items():
|
| 41 |
+
print(f"Themes associated with {person}:")
|
| 42 |
+
print("Person themes:", person_themes) # Add this line to debug
|
| 43 |
+
for theme in person_themes:
|
| 44 |
+
print(f"Theme: {theme}")
|
| 45 |
+
|
| 46 |
+
def PopulatePeopleByThemes():
|
| 47 |
+
global themes, people_by_theme
|
| 48 |
+
people_by_theme = {}
|
| 49 |
+
for theme in themes:
|
| 50 |
+
person = theme['person']
|
| 51 |
+
theme_name = theme['theme']
|
| 52 |
+
people_by_theme.setdefault(theme_name, set()).add(person)
|
| 53 |
+
|
| 54 |
+
def PrintPeopleByThemes():
|
| 55 |
+
print("PrintPeopleByThemes")
|
| 56 |
+
global people_by_theme
|
| 57 |
+
print ("-- people by theme --")
|
| 58 |
+
print (people_by_theme)
|
| 59 |
+
for theme, people in people_by_theme.items():
|
| 60 |
+
print(f"Theme: {theme}")
|
| 61 |
+
print("People:", people)
|
| 62 |
+
|
| 63 |
+
def CreateRandomGroup(group_size):
|
| 64 |
+
global themes
|
| 65 |
+
# Extract all unique people from the theme list
|
| 66 |
+
people = list(set(theme['person'] for theme in themes))
|
| 67 |
+
# Check if the group size is larger than the available people
|
| 68 |
+
if group_size > len(people):
|
| 69 |
+
print("Group size is larger than the available number of people.")
|
| 70 |
+
return None
|
| 71 |
+
# Randomly select a group of people
|
| 72 |
+
random_group = random.sample(people, group_size)
|
| 73 |
+
return random_group
|
| 74 |
+
|
| 75 |
+
def FindOverlappingTopics(people):
|
| 76 |
+
global themes_by_person
|
| 77 |
+
if not people:
|
| 78 |
+
print("No people provided.")
|
| 79 |
+
return None
|
| 80 |
+
|
| 81 |
+
# Get the themes for the first person in the people set
|
| 82 |
+
first_person_themes = themes_by_person.get(next(iter(people)), set())
|
| 83 |
+
# Initialize common_topics with the themes of the first person
|
| 84 |
+
common_topics = set(first_person_themes)
|
| 85 |
+
|
| 86 |
+
# Iterate through the remaining people, updating common_topics with the intersection of their themes
|
| 87 |
+
for person in people:
|
| 88 |
+
person_themes = themes_by_person.get(person, set())
|
| 89 |
+
common_topics.intersection_update(person_themes)
|
| 90 |
+
|
| 91 |
+
if not common_topics:
|
| 92 |
+
print("There are no overlapping themes among the given group.")
|
| 93 |
+
return None
|
| 94 |
+
|
| 95 |
+
return common_topics
|
| 96 |
+
|
| 97 |
+
def CreatePersonToThemeMapping():
|
| 98 |
+
global themes, people_by_theme
|
| 99 |
+
people_by_theme = {}
|
| 100 |
+
|
| 101 |
+
# Iterate through the list of themes
|
| 102 |
+
for theme in themes:
|
| 103 |
+
person = theme['person']
|
| 104 |
+
postId = theme['postId']
|
| 105 |
+
theme_info = (theme['theme'], postId)
|
| 106 |
+
|
| 107 |
+
# Check if the person already exists in the dictionary
|
| 108 |
+
if person not in people_by_theme:
|
| 109 |
+
people_by_theme[person] = set()
|
| 110 |
+
people_by_theme[person].add(theme_info)
|
| 111 |
+
|
| 112 |
+
def CreateThemeToPersonMapping():
|
| 113 |
+
global people_by_theme, themes_by_person
|
| 114 |
+
themes_by_person = {}
|
| 115 |
+
print("Createthemetopersonmapping")
|
| 116 |
+
# Iterate through the dictionary to create themes with unique people
|
| 117 |
+
for person, person_themes in people_by_theme.items():
|
| 118 |
+
for theme_info in person_themes:
|
| 119 |
+
theme, _ = theme_info
|
| 120 |
+
if theme not in themes_by_person:
|
| 121 |
+
themes_by_person[theme] = set()
|
| 122 |
+
themes_by_person[theme].add(person)
|
| 123 |
+
|
| 124 |
+
def GetEnjoymentIndex(choice):
|
| 125 |
+
# Extract the enjoyment index from the choice
|
| 126 |
+
print("choice", choice)
|
| 127 |
+
try:
|
| 128 |
+
choice_data = choice.get("metadata", {}).get("completion", {})
|
| 129 |
+
enjoyment_index = choice_data.get("Enjoyment index")
|
| 130 |
+
return float(enjoyment_index) if enjoyment_index else 0.0
|
| 131 |
+
except (ValueError, TypeError):
|
| 132 |
+
print("Failed to extract enjoyment index.")
|
| 133 |
+
return 0.0
|
| 134 |
+
|
| 135 |
+
def GenerateGameIdeas(themes_by_theme):
|
| 136 |
+
print("GenerateGameIdeas, themes_by_theme:", themes_by_theme)
|
| 137 |
+
# Filter themes with more than two people sharing it
|
| 138 |
+
eligible_themes = {theme: people for theme, people in themes_by_theme.items() if len(people) > 2}
|
| 139 |
+
print('\n****eligible themes')
|
| 140 |
+
for theme,people in eligible_themes.items():
|
| 141 |
+
print(f"Theme: {theme}")
|
| 142 |
+
print("People:", people)
|
| 143 |
+
|
| 144 |
+
# Check if there are eligible themes
|
| 145 |
+
if not eligible_themes:
|
| 146 |
+
print("No theme found with more than two people sharing it.")
|
| 147 |
+
return None
|
| 148 |
+
|
| 149 |
+
# Select a random eligible theme
|
| 150 |
+
chosen_theme = random.choice(list(eligible_themes.keys()))
|
| 151 |
+
print ("chosen_theme",chosen_theme)
|
| 152 |
+
# Prepare the prompt for GenAI
|
| 153 |
+
prompt = f"Provide your top 1 ideas for a text-based game this group should play based on their theme '{chosen_theme}'. For each idea, games should be fully playable via text using chat interface. Provide a rationale of why you chose that game, an enjoyment index (probability guess that the entire group will enjoy playing). Keep your answers fairly short. Answer in JSON, like this: {{\"Game name\": \"Description of game\", \"Enjoyment index\": Your guess as a probability from 0.0 to 1.0, \"Rationale\": \"Reasoning for game selection\"}}"
|
| 154 |
+
# Make the GenAI request
|
| 155 |
+
response = openai.Completion.create(
|
| 156 |
+
engine="text-davinci-003",
|
| 157 |
+
prompt=prompt,
|
| 158 |
+
max_tokens=300,
|
| 159 |
+
n=1,
|
| 160 |
+
stop=None,
|
| 161 |
+
temperature=0.3,
|
| 162 |
+
top_p=1.0,
|
| 163 |
+
frequency_penalty=0.0,
|
| 164 |
+
presence_penalty=0.0
|
| 165 |
+
)
|
| 166 |
+
# Process the response and extract the game ideas
|
| 167 |
+
game_ideas = []
|
| 168 |
+
for choice in response.choices:
|
| 169 |
+
try:
|
| 170 |
+
game_data = json.loads(choice.text)
|
| 171 |
+
game_ideas.append(game_data)
|
| 172 |
+
except (json.JSONDecodeError, AttributeError):
|
| 173 |
+
print(f"Failed to decode game data: {choice.text}")
|
| 174 |
+
|
| 175 |
+
# Sort the game ideas by enjoyment index in descending order
|
| 176 |
+
sorted_game_ideas = sorted(game_ideas, key=lambda x: x.get("Enjoyment index", 0.0), reverse=True)
|
| 177 |
+
|
| 178 |
+
if sorted_game_ideas:
|
| 179 |
+
with open(OUTPUT_GAME_IDEAS_DATAPATH, 'w') as file:
|
| 180 |
+
json.dump(sorted_game_ideas, file, indent=4)
|
| 181 |
+
return sorted_game_ideas
|
| 182 |
+
else:
|
| 183 |
+
print("No game idea found.")
|
| 184 |
+
return None
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
def CreateGames():
|
| 188 |
+
global themes
|
| 189 |
+
ReadThemesFromFile()
|
| 190 |
+
group=CreateRandomGroup(3)
|
| 191 |
+
print("group", group)
|
| 192 |
+
common_topics = FindOverlappingTopics(group)
|
| 193 |
+
print("common topics: ", common_topics)
|
| 194 |
+
|
| 195 |
+
PopulatePeopleByThemes()
|
| 196 |
+
CreatePersonToThemeMapping()
|
| 197 |
+
PrintPeopleByThemes()
|
| 198 |
+
|
| 199 |
+
PopulateThemesByPerson()
|
| 200 |
+
CreateThemeToPersonMapping()
|
| 201 |
+
PrintThemesByPerson()
|
| 202 |
+
|
| 203 |
+
game_ideas = GenerateGameIdeas(people_by_theme)
|
| 204 |
+
# Print the generated game ideas
|
| 205 |
+
if game_ideas:
|
| 206 |
+
print("Generated Game Idea:")
|
| 207 |
+
print(game_ideas)
|
| 208 |
+
return pd.DataFrame(game_ideas)
|
| 209 |
+
return None
|
create_games/data/game_ideas.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[
|
| 2 |
+
{
|
| 3 |
+
"Game name": "Ahmad Shareef's Adventure",
|
| 4 |
+
"Enjoyment index": 0.8,
|
| 5 |
+
"Rationale": "This game allows the group to explore a world based on Ahmad Shareef's life and adventures. Players can explore the world, solve puzzles, and battle enemies as they progress through the game. The game provides an engaging and interactive experience for the group, and allows them to explore a world based on Ahmad Shareef's life and adventures."
|
| 6 |
+
}
|
| 7 |
+
]
|