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ativeai as genai
|
| 5 |
+
|
| 6 |
+
testgeminiapi = os.getenv("testgemini")
|
| 7 |
+
genai.configure(api_key=testgeminiapi)
|
| 8 |
+
|
| 9 |
+
configcomptestexample = {
|
| 10 |
+
"village": {
|
| 11 |
+
"start": {
|
| 12 |
+
"description": "You wake up in a small village. You hear a rumor about a lost treasure.",
|
| 13 |
+
"choices": [
|
| 14 |
+
"explore village",
|
| 15 |
+
"gather supplies",
|
| 16 |
+
"rest"
|
| 17 |
+
],
|
| 18 |
+
"transitions": {
|
| 19 |
+
"explore village": "village_rumor",
|
| 20 |
+
"gather supplies": "village_supplies",
|
| 21 |
+
"rest": "village_start"
|
| 22 |
+
},
|
| 23 |
+
"consequences": {
|
| 24 |
+
"gather supplies": {}
|
| 25 |
+
},
|
| 26 |
+
"media": []
|
| 27 |
+
},
|
| 28 |
+
"rumor": {
|
| 29 |
+
"description": "You hear more details about the treasure hidden in the ancient ruins nearby.",
|
| 30 |
+
"choices": [
|
| 31 |
+
"decide to go",
|
| 32 |
+
"ignore"
|
| 33 |
+
],
|
| 34 |
+
"transitions": {
|
| 35 |
+
"decide to go": "village_supplies",
|
| 36 |
+
"ignore": "village_start"
|
| 37 |
+
},
|
| 38 |
+
"consequences": {
|
| 39 |
+
"decide to go": {}
|
| 40 |
+
},
|
| 41 |
+
"media": []
|
| 42 |
+
},
|
| 43 |
+
"supplies": {
|
| 44 |
+
"description": "You gather supplies for your journey.",
|
| 45 |
+
"choices": [
|
| 46 |
+
"head to forest",
|
| 47 |
+
"stay in village"
|
| 48 |
+
],
|
| 49 |
+
"transitions": {
|
| 50 |
+
"head to forest": "forest_forest",
|
| 51 |
+
"stay in village": "village_start"
|
| 52 |
+
},
|
| 53 |
+
"media": []
|
| 54 |
+
}
|
| 55 |
+
},
|
| 56 |
+
"forest": {
|
| 57 |
+
"forest": {
|
| 58 |
+
"description": "You enter the dense forest, heading towards the ruins.",
|
| 59 |
+
"choices": [
|
| 60 |
+
"travel further",
|
| 61 |
+
"return to village"
|
| 62 |
+
],
|
| 63 |
+
"transitions": {
|
| 64 |
+
"travel further": "ruins_ruins",
|
| 65 |
+
"return to village": "village_start"
|
| 66 |
+
},
|
| 67 |
+
"media": []
|
| 68 |
+
}
|
| 69 |
+
},
|
| 70 |
+
"ruins": {
|
| 71 |
+
"ruins": {
|
| 72 |
+
"description": "You reach the ancient ruins. The entrance is dark and eerie.",
|
| 73 |
+
"choices": [
|
| 74 |
+
"enter ruins",
|
| 75 |
+
"return to forest"
|
| 76 |
+
],
|
| 77 |
+
"transitions": {
|
| 78 |
+
"enter ruins": "ruins_explore",
|
| 79 |
+
"return to forest": "forest_forest"
|
| 80 |
+
},
|
| 81 |
+
"media": []
|
| 82 |
+
},
|
| 83 |
+
"explore": {
|
| 84 |
+
"description": "You explore the ruins, encountering traps and puzzles.",
|
| 85 |
+
"choices": [
|
| 86 |
+
"solve puzzle",
|
| 87 |
+
"avoid traps"
|
| 88 |
+
],
|
| 89 |
+
"transitions": {
|
| 90 |
+
"solve puzzle": "ruins_hiddenPassage",
|
| 91 |
+
"avoid traps": "ruins_ruins"
|
| 92 |
+
},
|
| 93 |
+
"media": []
|
| 94 |
+
},
|
| 95 |
+
"hiddenPassage": {
|
| 96 |
+
"description": "You solve a challenging puzzle and unlock a hidden passage.",
|
| 97 |
+
"choices": [
|
| 98 |
+
"enter passage",
|
| 99 |
+
"go back"
|
| 100 |
+
],
|
| 101 |
+
"transitions": {
|
| 102 |
+
"enter passage": "ruins_treasureRoom",
|
| 103 |
+
"go back": "ruins_explore"
|
| 104 |
+
},
|
| 105 |
+
"media": []
|
| 106 |
+
},
|
| 107 |
+
"treasureRoom": {
|
| 108 |
+
"description": "You enter the treasure room and find the treasure chest.",
|
| 109 |
+
"choices": [
|
| 110 |
+
"take treasure",
|
| 111 |
+
"leave"
|
| 112 |
+
],
|
| 113 |
+
"transitions": {
|
| 114 |
+
"take treasure": "ruins_celebrate",
|
| 115 |
+
"leave": "ruins_ruins"
|
| 116 |
+
},
|
| 117 |
+
"consequences": {
|
| 118 |
+
"take treasure": {}
|
| 119 |
+
},
|
| 120 |
+
"media": []
|
| 121 |
+
},
|
| 122 |
+
"celebrate": {
|
| 123 |
+
"description": "You celebrate your discovery and decide to bring the treasure back to the village.",
|
| 124 |
+
"choices": [
|
| 125 |
+
"return to village"
|
| 126 |
+
],
|
| 127 |
+
"transitions": {
|
| 128 |
+
"return to village": "village_return"
|
| 129 |
+
},
|
| 130 |
+
"media": []
|
| 131 |
+
}
|
| 132 |
+
},
|
| 133 |
+
"village_return": {
|
| 134 |
+
"village_return": {
|
| 135 |
+
"description": "You return to the village with the treasure and share it with the villagers.",
|
| 136 |
+
"choices": [
|
| 137 |
+
"end adventure"
|
| 138 |
+
],
|
| 139 |
+
"transitions": {
|
| 140 |
+
"end adventure": "end_end"
|
| 141 |
+
},
|
| 142 |
+
"media": []
|
| 143 |
+
}
|
| 144 |
+
},
|
| 145 |
+
"end": {
|
| 146 |
+
"end": {
|
| 147 |
+
"description": "Your adventure ends here. The villagers are grateful and everyone's lives improve.",
|
| 148 |
+
"choices": [],
|
| 149 |
+
"transitions": {},
|
| 150 |
+
"media": []
|
| 151 |
+
}
|
| 152 |
+
}
|
| 153 |
+
}
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
#------
|
| 157 |
+
|
| 158 |
+
# Create the model
|
| 159 |
+
generation_config = {
|
| 160 |
+
"temperature": 1,
|
| 161 |
+
"top_p": 0.95,
|
| 162 |
+
"top_k": 64,
|
| 163 |
+
"max_output_tokens": 8192,
|
| 164 |
+
"response_mime_type": "text/plain",
|
| 165 |
+
}
|
| 166 |
+
|
| 167 |
+
model = genai.GenerativeModel(
|
| 168 |
+
model_name="gemini-1.5-flash",
|
| 169 |
+
generation_config=generation_config,
|
| 170 |
+
# safety_settings = Adjust safety settings
|
| 171 |
+
# See https://ai.google.dev/gemini-api/docs/safety-settings
|
| 172 |
+
)
|
| 173 |
+
|
| 174 |
+
def geminiinferenceinstance(text):
|
| 175 |
+
global model
|
| 176 |
+
chat_session = model.start_chat(
|
| 177 |
+
history=[
|
| 178 |
+
]
|
| 179 |
+
)
|
| 180 |
+
|
| 181 |
+
response = chat_session.send_message(f"Use this as inspiration to make a pirate story aka keep same format to work with an existing app: /n/n{configcomptestexample} ")
|
| 182 |
+
|
| 183 |
+
return response.text #print(response.text)
|
| 184 |
+
|
| 185 |
+
#-----
|
| 186 |
+
|
| 187 |
+
# Define the states
|
| 188 |
+
all_states = {
|
| 189 |
+
'village': {
|
| 190 |
+
'start': {
|
| 191 |
+
"description": "You wake up in a small village. You hear a rumor about a lost treasure.",
|
| 192 |
+
"choices": ['explore village', 'gather supplies', 'rest'],
|
| 193 |
+
"transitions": {'explore village': 'village_rumor', 'gather supplies': 'village_supplies', 'rest': 'village_start'},
|
| 194 |
+
"consequences": {
|
| 195 |
+
'gather supplies': lambda player: player.add_item('basic supplies')
|
| 196 |
+
},
|
| 197 |
+
"media": []
|
| 198 |
+
},
|
| 199 |
+
'rumor': {
|
| 200 |
+
"description": "You hear more details about the treasure hidden in the ancient ruins nearby.",
|
| 201 |
+
"choices": ['decide to go', 'ignore'],
|
| 202 |
+
"transitions": {'decide to go': 'village_supplies', 'ignore': 'village_start'},
|
| 203 |
+
"consequences": {
|
| 204 |
+
'decide to go': lambda player: player.update_knowledge('treasure location')
|
| 205 |
+
},
|
| 206 |
+
"media": []
|
| 207 |
+
},
|
| 208 |
+
'supplies': {
|
| 209 |
+
"description": "You gather supplies for your journey.",
|
| 210 |
+
"choices": ['head to forest', 'stay in village'],
|
| 211 |
+
"transitions": {'head to forest': 'forest_forest', 'stay in village': 'village_start'},
|
| 212 |
+
"media": []
|
| 213 |
+
},
|
| 214 |
+
},
|
| 215 |
+
'forest': {
|
| 216 |
+
'forest': {
|
| 217 |
+
"description": "You enter the dense forest, heading towards the ruins.",
|
| 218 |
+
"choices": ['travel further', 'return to village'],
|
| 219 |
+
"transitions": {'travel further': 'ruins_ruins', 'return to village': 'village_start'},
|
| 220 |
+
"media": []
|
| 221 |
+
},
|
| 222 |
+
},
|
| 223 |
+
'ruins': {
|
| 224 |
+
'ruins': {
|
| 225 |
+
"description": "You reach the ancient ruins. The entrance is dark and eerie.",
|
| 226 |
+
"choices": ['enter ruins', 'return to forest'],
|
| 227 |
+
"transitions": {'enter ruins': 'ruins_explore', 'return to forest': 'forest_forest'},
|
| 228 |
+
"media": []
|
| 229 |
+
},
|
| 230 |
+
'explore': {
|
| 231 |
+
"description": "You explore the ruins, encountering traps and puzzles.",
|
| 232 |
+
"choices": ['solve puzzle', 'avoid traps'],
|
| 233 |
+
"transitions": {'solve puzzle': 'ruins_hiddenPassage', 'avoid traps': 'ruins_ruins'},
|
| 234 |
+
"media": []
|
| 235 |
+
},
|
| 236 |
+
'hiddenPassage': {
|
| 237 |
+
"description": "You solve a challenging puzzle and unlock a hidden passage.",
|
| 238 |
+
"choices": ['enter passage', 'go back'],
|
| 239 |
+
"transitions": {'enter passage': 'ruins_treasureRoom', 'go back': 'ruins_explore'},
|
| 240 |
+
"media": []
|
| 241 |
+
},
|
| 242 |
+
'treasureRoom': {
|
| 243 |
+
"description": "You enter the treasure room and find the treasure chest.",
|
| 244 |
+
"choices": ['take treasure', 'leave'],
|
| 245 |
+
"transitions": {'take treasure': 'ruins_celebrate', 'leave': 'ruins_ruins'},
|
| 246 |
+
"consequences": {
|
| 247 |
+
'take treasure': lambda player: player.add_item('treasure')
|
| 248 |
+
},
|
| 249 |
+
"media": []
|
| 250 |
+
},
|
| 251 |
+
'celebrate': {
|
| 252 |
+
"description": "You celebrate your discovery and decide to bring the treasure back to the village.",
|
| 253 |
+
"choices": ['return to village'],
|
| 254 |
+
"transitions": {'return to village': 'village_return'},
|
| 255 |
+
"media": []
|
| 256 |
+
},
|
| 257 |
+
},
|
| 258 |
+
'village_return': {
|
| 259 |
+
'village_return': {
|
| 260 |
+
"description": "You return to the village with the treasure and share it with the villagers.",
|
| 261 |
+
"choices": ['end adventure'],
|
| 262 |
+
"transitions": {'end adventure': 'end_end'},
|
| 263 |
+
"media": []
|
| 264 |
+
},
|
| 265 |
+
},
|
| 266 |
+
'end': {
|
| 267 |
+
'end': {
|
| 268 |
+
"description": "Your adventure ends here. The villagers are grateful and everyone's lives improve.",
|
| 269 |
+
"choices": [],
|
| 270 |
+
"transitions": {},
|
| 271 |
+
"media": []
|
| 272 |
+
},
|
| 273 |
+
}
|
| 274 |
+
}
|
| 275 |
+
|
| 276 |
+
class Player:
|
| 277 |
+
def __init__(self):
|
| 278 |
+
self.inventory = []
|
| 279 |
+
self.money = 20
|
| 280 |
+
self.knowledge = {}
|
| 281 |
+
|
| 282 |
+
def add_item(self, item):
|
| 283 |
+
self.inventory.append(item)
|
| 284 |
+
|
| 285 |
+
def has_item(self, item):
|
| 286 |
+
return item in self.inventory
|
| 287 |
+
|
| 288 |
+
def update_knowledge(self, topic):
|
| 289 |
+
self.knowledge[topic] = True
|
| 290 |
+
|
| 291 |
+
#importing all_states from relatively_constant_variables
|
| 292 |
+
|
| 293 |
+
def validate_transitions(all_states):
|
| 294 |
+
errors = []
|
| 295 |
+
for location, states in all_states.items():
|
| 296 |
+
for state_key, state in states.items():
|
| 297 |
+
for transition_key, transition_state in state['transitions'].items():
|
| 298 |
+
# Check if the transition is to another location
|
| 299 |
+
if transition_state in all_states:
|
| 300 |
+
trans_location, trans_state = transition_state, 'start' # Assuming 'start' state for new locations
|
| 301 |
+
elif '_' in transition_state:
|
| 302 |
+
trans_location, trans_state = transition_state.split('_')
|
| 303 |
+
else:
|
| 304 |
+
trans_location, trans_state = location, transition_state
|
| 305 |
+
|
| 306 |
+
# Validate the transition state
|
| 307 |
+
if trans_location not in all_states or trans_state not in all_states[trans_location]:
|
| 308 |
+
errors.append(f"Invalid transition from {location}.{state_key} to {trans_location}.{trans_state}")
|
| 309 |
+
|
| 310 |
+
return errors
|
| 311 |
+
|
| 312 |
+
path_errors = validate_transitions(all_states)
|
| 313 |
+
if path_errors:
|
| 314 |
+
for error in path_errors:
|
| 315 |
+
print(error)
|
| 316 |
+
else:
|
| 317 |
+
print("All transitions are valid.")
|
| 318 |
+
|
| 319 |
+
class GameSession:
|
| 320 |
+
def __init__(self, starting_location='village', starting_state='start'):
|
| 321 |
+
self.player = Player()
|
| 322 |
+
self.current_location = starting_location
|
| 323 |
+
self.current_state = starting_state
|
| 324 |
+
self.game_log = []
|
| 325 |
+
|
| 326 |
+
def make_choice(self, choice_index):
|
| 327 |
+
state = all_states[self.current_location][self.current_state]
|
| 328 |
+
if 0 <= choice_index < len(state['choices']):
|
| 329 |
+
choice = state['choices'][choice_index]
|
| 330 |
+
next_state = state['transitions'][choice]
|
| 331 |
+
|
| 332 |
+
self.game_log.append(f"You chose: {choice}")
|
| 333 |
+
self.game_log.append(state['description'])
|
| 334 |
+
|
| 335 |
+
if 'consequences' in state and choice in state['consequences']:
|
| 336 |
+
if state['consequences'][choice]:
|
| 337 |
+
state['consequences'][choice](self.player)
|
| 338 |
+
else:
|
| 339 |
+
# Handle empty consequence, e.g., log a message or provide a default action
|
| 340 |
+
print(f"No consequence for choice: {choice}")
|
| 341 |
+
# You can add any default action here if needed
|
| 342 |
+
|
| 343 |
+
if '_' in next_state:
|
| 344 |
+
self.current_location, self.current_state = next_state.split('_')
|
| 345 |
+
else:
|
| 346 |
+
self.current_state = next_state
|
| 347 |
+
|
| 348 |
+
return self.get_current_state_info()
|
| 349 |
+
else:
|
| 350 |
+
return "Invalid choice. Please try again."
|
| 351 |
+
|
| 352 |
+
def get_current_state_info(self):
|
| 353 |
+
state = all_states[self.current_location][self.current_state]
|
| 354 |
+
choices = [f"{idx + 1}. {choice}" for idx, choice in enumerate(state['choices'])]
|
| 355 |
+
return state['description'], choices, "\n".join(self.game_log)
|
| 356 |
+
|
| 357 |
+
def get_current_state_media(self):
|
| 358 |
+
media = all_states[self.current_location][self.current_state]['media']
|
| 359 |
+
return media
|
| 360 |
+
|
| 361 |
+
def start_game(starting_location='village', starting_state='start'):
|
| 362 |
+
game_session = GameSession(starting_location, starting_state)
|
| 363 |
+
description, choices, game_log = game_session.get_current_state_info()
|
| 364 |
+
return description, choices, game_log, game_session
|
| 365 |
+
|
| 366 |
+
def make_choice(choice, game_session, with_media=False): #Calls the nested make choice function in the game session class
|
| 367 |
+
if not choice:
|
| 368 |
+
description, choices, game_log = game_session.get_current_state_info()
|
| 369 |
+
return description, choices, "Please select a choice before proceeding.", game_session
|
| 370 |
+
|
| 371 |
+
choice_index = int(choice.split('.')[0]) - 1
|
| 372 |
+
result = game_session.make_choice(choice_index)
|
| 373 |
+
|
| 374 |
+
if with_media:
|
| 375 |
+
media = game_session.get_current_state_media()
|
| 376 |
+
return result[0], gr.update(choices=result[1]), result[2], game_session, media
|
| 377 |
+
else:
|
| 378 |
+
return result[0], gr.update(choices=result[1]), result[2], game_session
|
| 379 |
+
|
| 380 |
+
def load_game(custom_config=None, with_media=False):
|
| 381 |
+
global all_states
|
| 382 |
+
if not custom_config:
|
| 383 |
+
return gr.update(value="No custom configuration provided."), None, None, None, None, None, None
|
| 384 |
+
|
| 385 |
+
try:
|
| 386 |
+
new_config = json.loads(custom_config)
|
| 387 |
+
all_states = new_config
|
| 388 |
+
|
| 389 |
+
# Determine the starting location and state
|
| 390 |
+
starting_location = next(iter(all_states.keys()))
|
| 391 |
+
starting_state = next(iter(all_states[starting_location].keys()))
|
| 392 |
+
print(f"Starting location: {starting_location}, Starting state: {starting_state}")
|
| 393 |
+
|
| 394 |
+
game_session = GameSession(starting_location, starting_state)
|
| 395 |
+
description, choices, game_log = game_session.get_current_state_info()
|
| 396 |
+
new_path_errors = validate_transitions(all_states)
|
| 397 |
+
|
| 398 |
+
output_media = []
|
| 399 |
+
|
| 400 |
+
if with_media:
|
| 401 |
+
media_list = all_states[starting_location][starting_state].get('media', [])
|
| 402 |
+
print(f"Media list: {media_list}")
|
| 403 |
+
|
| 404 |
+
if media_list:
|
| 405 |
+
for media_path in media_list:
|
| 406 |
+
#media_component = create_media_component(media_path)
|
| 407 |
+
output_media.append(media_path)
|
| 408 |
+
print(f"Created {len(output_media)} media components")
|
| 409 |
+
|
| 410 |
+
success_message = f"Custom configuration loaded successfully!\n{new_path_errors}"
|
| 411 |
+
return (
|
| 412 |
+
gr.update(value=success_message),
|
| 413 |
+
game_log,
|
| 414 |
+
description,
|
| 415 |
+
gr.update(choices=choices),
|
| 416 |
+
gr.update(value=custom_config),
|
| 417 |
+
game_session,
|
| 418 |
+
output_media if with_media else None
|
| 419 |
+
)
|
| 420 |
+
|
| 421 |
+
except json.JSONDecodeError as e:
|
| 422 |
+
error_message = format_json_error(custom_config, e)
|
| 423 |
+
return gr.update(value=error_message), None, None, None, None, gr.update(value=custom_config), None
|
| 424 |
+
|
| 425 |
+
except Exception as e:
|
| 426 |
+
error_message = f"Error loading custom configuration: {str(e)}"
|
| 427 |
+
return gr.update(value=error_message), None, None, None, None, gr.update(value=custom_config), None
|
| 428 |
+
|
| 429 |
+
def format_json_error(config, error):
|
| 430 |
+
lineno, colno = error.lineno, error.colno
|
| 431 |
+
lines = config.split('\n')
|
| 432 |
+
error_line = lines[lineno - 1] if lineno <= len(lines) else ""
|
| 433 |
+
pointer = ' ' * (colno - 1) + '^'
|
| 434 |
+
|
| 435 |
+
return f"""Invalid JSON format in custom configuration:
|
| 436 |
+
Error at line {lineno}, column {colno}:
|
| 437 |
+
{error_line}
|
| 438 |
+
{pointer}
|
| 439 |
+
Error details: {str(error)}"""
|
| 440 |
+
|
| 441 |
+
initgameinfo = start_game()
|
| 442 |
+
|
| 443 |
+
with gr.Blocks() as geminiapidemo:
|
| 444 |
+
with gr.Tab("Manual - Config With Assets"):
|
| 445 |
+
gr.HTML("Placeholder as not complete yet (3D not supported, and time (esp need for audio)")
|
| 446 |
+
with gr.Row():
|
| 447 |
+
with gr.Column(scale=2):
|
| 448 |
+
gr.Markdown("# Text-based Adventure Game")
|
| 449 |
+
|
| 450 |
+
wadescription = gr.Textbox(label="Current Situation", lines=4, value=initgameinfo[0])
|
| 451 |
+
wamediabool = gr.State(value=True)
|
| 452 |
+
wamedia = gr.State(["testmedia/Stable Audio - Raindrops, output.wav"])
|
| 453 |
+
|
| 454 |
+
@gr.render(inputs=wamedia)
|
| 455 |
+
def dynamic_with_media(media_items):
|
| 456 |
+
print(media_items)
|
| 457 |
+
with gr.Group() as wamediagrouping:
|
| 458 |
+
gr.HTML("Placeholder to load all media tests - still need to test clearing media on ")
|
| 459 |
+
if media_items == []:
|
| 460 |
+
gr.Markdown("No media items to display.")
|
| 461 |
+
else:
|
| 462 |
+
for item in media_items:
|
| 463 |
+
render = create_media_component(item)
|
| 464 |
+
|
| 465 |
+
return wamediagrouping
|
| 466 |
+
|
| 467 |
+
wachoices = gr.Radio(label="Your Choices", choices=initgameinfo[1])
|
| 468 |
+
wasubmit_btn = gr.Button("Make Choice")
|
| 469 |
+
wagame_log = gr.Textbox(label="Game Log", lines=20, value=initgameinfo[2])
|
| 470 |
+
wagame_session = gr.State(value=initgameinfo[3])
|
| 471 |
+
wasubmit_btn.click(
|
| 472 |
+
make_choice,
|
| 473 |
+
inputs=[wachoices, wagame_session, wamediabool],
|
| 474 |
+
outputs=[wadescription, wachoices, wagame_log, wagame_session, wamedia]
|
| 475 |
+
)
|
| 476 |
+
with gr.Column(scale=1):
|
| 477 |
+
gr.Markdown("# Debugging")
|
| 478 |
+
waerror_box = gr.Textbox(label="Path Errors", lines=4, value=path_errors)
|
| 479 |
+
with gr.Accordion("Config (Game Spoiler and Example for llm to remix)", open=False):
|
| 480 |
+
wacustom_config = gr.Textbox(label="Custom Configuration (JSON)", value=json.dumps(all_states, default=lambda o: o.__dict__, indent=2), lines=8)
|
| 481 |
+
wacustom_configbtn = gr.Button("Load Custom Config")
|
| 482 |
+
|
| 483 |
+
wacustom_configbtn.click(
|
| 484 |
+
load_game,
|
| 485 |
+
inputs=[wacustom_config, wamediabool],
|
| 486 |
+
outputs=[waerror_box, wagame_log, wadescription, wachoices, wacustom_config, wagame_session, wamedia]
|
| 487 |
+
)
|
| 488 |
+
with gr.Accordion("Gemini Config", open=False):
|
| 489 |
+
gr.Interface(geminiinferenceinstance, inputs=["text"], outputs=["text"], description="test generated config")
|
| 490 |
+
|
| 491 |
+
|
| 492 |
+
geminiapidemo.queue().launch()
|