Spaces:
Running
Running
File size: 4,099 Bytes
90d81ff f834d3b 90d81ff 5fe82f2 90d81ff | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | import gradio as gr
import json
import os
import uuid
import time
import re
from datasets import Dataset, load_dataset
# ----------------------------
# SAVE / LOAD (HF DATASET)
# ----------------------------
def save_to_hf(dataset_repo, state_json, history_json):
data = {
"state": [state_json],
"history": [history_json]
}
ds = Dataset.from_dict(data)
ds.push_to_hub(
dataset_repo,
private=True
)
return "Game Saved ☁️"
def load_from_hf(dataset_repo):
ds = load_dataset(
dataset_repo,
split="train"
)
state_json = ds[0]["state"]
history_json = ds[0]["history"]
return state_json, history_json
def load_and_enter_game(dataset_repo, from_char_screen=True):
"""
Load saved game and switch to game screen.
If from_char_screen=True, also hide stat_screen and show game_screen.
If False, assume we are already in game screen (just refresh data).
"""
try:
state_json, history_json = load_from_hf(dataset_repo)
state = json.loads(state_json)
history = json.loads(history_json) if history_json else []
# EKSİK STATLARI DOLDUR (eski kayıtlar için)
default_stats = {
"strength": 5, "agility": 5, "intelligence": 5,
"charisma": 5, "willpower": 5,
"light_dark": 0, "order_chaos": 0, "notoriety": 0
}
for key, default in default_stats.items():
if key not in state:
state[key] = default
# Statları formatla
stats_md = f"""
**Strength:** {state['strength']}
**Agility:** {state['agility']}
**Intelligence:** {state['intelligence']}
**Charisma:** {state['charisma']}
**Willpower:** {state['willpower']}
"""
# Kaydedilmiş model seçimini al (yoksa "Auto")
saved_model = state.get("saved_model", "Auto (Language Routing)")
# Eğer karakter ekranından geliyorsak, stat_screen'i gizleyip game_screen'i göster
if from_char_screen:
char_screen_update = gr.update(visible=False)
stat_screen_update = gr.update(visible=False)
game_screen_update = gr.update(visible=True)
else:
char_screen_update = gr.update()
stat_screen_update = gr.update()
game_screen_update = gr.update()
return (
json.dumps(state), # player_state
history, # chatbot
char_screen_update, # char_screen visibility
stat_screen_update, # stat_screen visibility
game_screen_update, # game_screen visibility
state.get("avatar"), # profile_pic
f"### {state.get('name', 'Unknown')}", # profile_info
state["light_dark"], # light_dark_slider
state["order_chaos"], # order_chaos_slider
stats_md, # stat_display
gr.update(), # scene_image (temizle)
saved_model # model_selector value
)
except Exception as e:
print(f"--- DEBUG INFO [SAVE SYSTEM]: Load error: {e}")
# Hata durumunda mevcut ekranda kal, uyarı göster (isteğe bağlı)
if from_char_screen:
return (
"{}", [],
gr.update(visible=True), gr.update(visible=False),
None, "### Error loading game", 0, 0,
"Load failed. No saved game found.", gr.update(), "Auto (Language Routing)"
)
else:
# Oyun içinde hata olursa sadece state'i değiştirme
return (
"{}", [], gr.update(), gr.update(), None, "### Error", 0, 0,
"Load failed.", gr.update(), "Auto (Language Routing)"
)
def load_in_game(dataset_repo):
"""Oyun içindeki load butonu için - from_char_screen=False ile çağır"""
return load_and_enter_game(dataset_repo, from_char_screen=False) |