Jofthomas HF staff commited on
Commit
ebd0b97
β€’
1 Parent(s): 6e87d13

Create gemini.py

Browse files
Files changed (1) hide show
  1. TextGen/gemini.py +134 -0
TextGen/gemini.py ADDED
@@ -0,0 +1,134 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import google.generativeai as genai
2
+ from google.api_core import retry
3
+
4
+ import pathlib
5
+ import textwrap
6
+ import json
7
+
8
+ GOOGLE_API_KEY = os.environ['GOOGLE_API_KEY']
9
+
10
+ genai.configure(api_key=GOOGLE_API_KEY)
11
+
12
+ model = genai.GenerativeModel(model_name='models/gemini-1.5-pro-latest')
13
+
14
+ def generate_story(available_items):
15
+ response = model.generate_content(f"""
16
+ You are game master in a roguelike game. You should help me create an consise objective for the player to fulfill in the current level.
17
+ Our protagonist, A girl with a red hood is wandering inside a dungeon.
18
+ You are ONLY allowed to only use the following elements and not modify either the map or the enemies:
19
+ {available_items}
20
+ Example of possible Objectives:
21
+ Example 1 : Defeat N monster $monster_name and then report it to npc :$npc_name
22
+ Example 2 : Loot two treasure chest then defeat the boss $boss_name to exit.
23
+ A object of type NPC can be talked to but can't move and can't be fought, an ennemy type object can be fought but not talked to. A boss can be fought but not talked to
24
+ To escape, the player will have to pass by the portal.
25
+
26
+ """, request_options={'retry': retry.Retry()})
27
+ story = response.text
28
+ return story
29
+
30
+ def place_objects(available_items,story,myMap):
31
+ prompt = f"""
32
+ Please return JSON describing the objective of this level, the objects that should be placed as well as all enemies/npcs that shall be placed:
33
+
34
+ {{"objective": str, "objects":list[OBJECT], "enemies":list[ENEMIES], "boss":list[BOSS],"npcs": list[NPC]}}
35
+ In all of the following example, placement is the id of the room to place the object in.
36
+ # The Objects that should be placed in the treasure chests
37
+ OBJECT = {{"name": str, "description": str, "stength": str, "speed": str}}
38
+ # The infos necessary about the enemies
39
+ ENEMIES = {{"name": str, "placement": int}}
40
+ # The NPC that should be added to the map
41
+ NPC = {{"name": str, "persona": str, "placement": int}}
42
+ # The Boss monster of this level.
43
+ BOSS = {{"name": str, "placement": int}}
44
+
45
+ All fields are not necessarily required; if null, don't add it. it should make sense with the crafted story.
46
+ Items can only be weapons or objects that can be useful for a special quest. "stength" and "speed" should only be declared for weapons and can only range between 0 and 1. 0 being a rusty sword and 1 being the best sword in the game.
47
+
48
+ Important: Only return a single piece of valid JSON text.
49
+
50
+ Here is the map:
51
+
52
+ {myMap}
53
+
54
+ X: is the starting position of the player
55
+ ?: represent and interesting room where it could be interesting to place an object
56
+ numbers represent the index of the room
57
+
58
+ and the objective of this level: {story}
59
+ The objective MUST be feasible, otherwise the player will be stuck.
60
+
61
+ REMEMBER, You are ONLY allowed to only use the following elements and not modify either the map or the enemies:
62
+ {available_items}
63
+
64
+ """
65
+
66
+ print(prompt)
67
+ response = model.generate_content(
68
+ prompt,
69
+ generation_config={'response_mime_type':'application/json'}
70
+ )
71
+ try:
72
+ map_placements=json.dumps(json.loads(response.text), indent=4)
73
+ except:
74
+ map_placements="error"
75
+ return map_placements
76
+
77
+ def get_items_string_representation(items_list):
78
+ result = ""
79
+ for item in items_list:
80
+ item_str = "Name: " + item["name"] + ", Type: " + item["type"] + ", Description: " + item["description"] + "\n"
81
+ result += item_str
82
+ return result
83
+
84
+ def generate_map_markdown(data):
85
+ import numpy as np
86
+
87
+ # Define the room structure with walls and markers
88
+ def create_room(room_char, index):
89
+ index_str = f"{index}".zfill(2) # Zero pad index to 2 digits if needed
90
+ if index_str[0] !="0":
91
+ first_number=index_str[0]
92
+ else:
93
+ first_number=" "
94
+ return [
95
+ f"╔═{room_char}═╗",
96
+ f"β•‘ {first_number}{index_str[1]}β•‘",
97
+ f"β•šβ•β•β•β•"
98
+ ]
99
+
100
+ # Extract rooms and rooms of interest
101
+ rooms = [eval(room) for room in data["rooms"]]
102
+ rooms_of_interest = [eval(room) for room in data["room_of_interest"]]
103
+
104
+ # Determine grid size
105
+ min_x = min(room[0] for room in rooms)
106
+ max_x = max(room[0] for room in rooms)
107
+ min_y = min(room[1] for room in rooms)
108
+ max_y = max(room[1] for room in rooms)
109
+
110
+ # Create grid with empty spaces represented by a room-like structure
111
+ map_height = (max_y - min_y + 1) * 3
112
+ map_width = (max_x - min_x + 1) * 5
113
+ grid = np.full((map_height, map_width), " ")
114
+
115
+ # Populate grid with rooms and their characteristics
116
+ for i, room in enumerate(rooms):
117
+ x, y = room
118
+ x_offset = (x - min_x) * 5
119
+ y_offset = (max_y - y) * 3
120
+ if room == (0, 0):
121
+ room_char = "X"
122
+ elif room in rooms_of_interest:
123
+ room_char = "P" if i == data["index_exit"] else "?"
124
+ else:
125
+ room_char = " "
126
+ room_structure = create_room(room_char, i)
127
+ for j, row in enumerate(room_structure):
128
+ grid[y_offset + j, x_offset:x_offset + 5] = list(row)
129
+
130
+ # Convert grid to a string format suitable for display
131
+ markdown_map = "\n".join("".join(row) for row in grid)
132
+
133
+ # Return the map wrapped in triple backticks for proper display in markdown
134
+ return f"```\n{markdown_map}\n```"