AI-RPG / data /save_system.py
necdet.sayiner
Refine debug logs
5fe82f2
Raw
History Blame Contribute Delete
4.1 kB
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)