Spaces:
Paused
Paused
Commit
·
f54c072
1
Parent(s):
7da7107
save monster and geenrate monster fixed
Browse files- app.py +17 -3
- core/game_mechanics.py +39 -3
app.py
CHANGED
|
@@ -71,31 +71,45 @@ def generate_monster(oauth_profile: gr.OAuthProfile | None, audio_input=None, te
|
|
| 71 |
monster = game_mechanics.create_monster(result, {
|
| 72 |
"training_focus": training_focus,
|
| 73 |
"care_level": care_level
|
| 74 |
-
})
|
| 75 |
|
| 76 |
# Save to persistent storage
|
| 77 |
state_manager.save_monster(user_id, monster)
|
| 78 |
|
| 79 |
# Prepare response
|
| 80 |
-
|
| 81 |
"message": f"✨ {monster.name} has been created!",
|
| 82 |
"image": result.get('image'),
|
| 83 |
"model_3d": result.get('model_3d'),
|
| 84 |
"stats": monster.get_stats_display(),
|
| 85 |
"dialogue": result.get('dialogue', "🤖💚1️⃣0️⃣0️⃣")
|
| 86 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
except Exception as e:
|
| 89 |
print(f"Error generating monster: {str(e)}")
|
| 90 |
# Use fallback generation
|
| 91 |
fallback_result = pipeline.fallback_generation(text_input or "friendly digital creature")
|
| 92 |
-
|
| 93 |
"message": "⚡ Created using quick generation mode",
|
| 94 |
"image": fallback_result.get('image'),
|
| 95 |
"model_3d": None,
|
| 96 |
"stats": fallback_result.get('stats'),
|
| 97 |
"dialogue": "🤖❓9️⃣9️⃣"
|
| 98 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
# Training function
|
| 101 |
def train_monster(oauth_profile: gr.OAuthProfile | None, training_type, intensity):
|
|
|
|
| 71 |
monster = game_mechanics.create_monster(result, {
|
| 72 |
"training_focus": training_focus,
|
| 73 |
"care_level": care_level
|
| 74 |
+
}, user_id)
|
| 75 |
|
| 76 |
# Save to persistent storage
|
| 77 |
state_manager.save_monster(user_id, monster)
|
| 78 |
|
| 79 |
# Prepare response
|
| 80 |
+
response_dict = {
|
| 81 |
"message": f"✨ {monster.name} has been created!",
|
| 82 |
"image": result.get('image'),
|
| 83 |
"model_3d": result.get('model_3d'),
|
| 84 |
"stats": monster.get_stats_display(),
|
| 85 |
"dialogue": result.get('dialogue', "🤖💚1️⃣0️⃣0️⃣")
|
| 86 |
}
|
| 87 |
+
return (
|
| 88 |
+
response_dict["message"],
|
| 89 |
+
response_dict["image"],
|
| 90 |
+
response_dict["model_3d"],
|
| 91 |
+
response_dict["stats"],
|
| 92 |
+
response_dict["dialogue"]
|
| 93 |
+
)
|
| 94 |
|
| 95 |
except Exception as e:
|
| 96 |
print(f"Error generating monster: {str(e)}")
|
| 97 |
# Use fallback generation
|
| 98 |
fallback_result = pipeline.fallback_generation(text_input or "friendly digital creature")
|
| 99 |
+
fallback_dict = {
|
| 100 |
"message": "⚡ Created using quick generation mode",
|
| 101 |
"image": fallback_result.get('image'),
|
| 102 |
"model_3d": None,
|
| 103 |
"stats": fallback_result.get('stats'),
|
| 104 |
"dialogue": "🤖❓9️⃣9️⃣"
|
| 105 |
}
|
| 106 |
+
return (
|
| 107 |
+
fallback_dict["message"],
|
| 108 |
+
fallback_dict["image"],
|
| 109 |
+
fallback_dict["model_3d"],
|
| 110 |
+
fallback_dict["stats"],
|
| 111 |
+
fallback_dict["dialogue"]
|
| 112 |
+
)
|
| 113 |
|
| 114 |
# Training function
|
| 115 |
def train_monster(oauth_profile: gr.OAuthProfile | None, training_type, intensity):
|
core/game_mechanics.py
CHANGED
|
@@ -4,6 +4,8 @@ from datetime import datetime, timedelta
|
|
| 4 |
from typing import Dict, List, Any, Optional
|
| 5 |
from dataclasses import dataclass, asdict
|
| 6 |
import numpy as np
|
|
|
|
|
|
|
| 7 |
|
| 8 |
@dataclass
|
| 9 |
class Monster:
|
|
@@ -135,7 +137,7 @@ class GameMechanics:
|
|
| 135 |
}
|
| 136 |
}
|
| 137 |
|
| 138 |
-
def create_monster(self, generation_result: Dict[str, Any], user_preferences: Dict = None) -> Monster:
|
| 139 |
"""Create a new monster from AI generation results"""
|
| 140 |
|
| 141 |
traits = generation_result.get('traits', {})
|
|
@@ -158,6 +160,10 @@ class GameMechanics:
|
|
| 158 |
# Create monster name
|
| 159 |
name = traits.get('name', self._generate_name(traits))
|
| 160 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
# Create monster instance
|
| 162 |
monster = Monster(
|
| 163 |
name=name,
|
|
@@ -167,12 +173,42 @@ class GameMechanics:
|
|
| 167 |
care_state=care_state,
|
| 168 |
personality=personality,
|
| 169 |
birth_time=datetime.now(),
|
| 170 |
-
image_path=
|
| 171 |
-
model_3d_path=
|
| 172 |
)
|
| 173 |
|
| 174 |
return monster
|
| 175 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
def _generate_base_stats(self, traits: Dict, focus: str) -> Dict[str, int]:
|
| 177 |
"""Generate base stats based on traits and focus"""
|
| 178 |
# Base values
|
|
|
|
| 4 |
from typing import Dict, List, Any, Optional
|
| 5 |
from dataclasses import dataclass, asdict
|
| 6 |
import numpy as np
|
| 7 |
+
from PIL import Image
|
| 8 |
+
import os
|
| 9 |
|
| 10 |
@dataclass
|
| 11 |
class Monster:
|
|
|
|
| 137 |
}
|
| 138 |
}
|
| 139 |
|
| 140 |
+
def create_monster(self, generation_result: Dict[str, Any], user_preferences: Dict = None, user_id: str = "unknown") -> Monster:
|
| 141 |
"""Create a new monster from AI generation results"""
|
| 142 |
|
| 143 |
traits = generation_result.get('traits', {})
|
|
|
|
| 160 |
# Create monster name
|
| 161 |
name = traits.get('name', self._generate_name(traits))
|
| 162 |
|
| 163 |
+
# Convert PIL Images to file paths
|
| 164 |
+
image_path = self._convert_image_to_path(generation_result.get('image'), user_id, name)
|
| 165 |
+
model_3d_path = self._convert_image_to_path(generation_result.get('model_3d'), user_id, name)
|
| 166 |
+
|
| 167 |
# Create monster instance
|
| 168 |
monster = Monster(
|
| 169 |
name=name,
|
|
|
|
| 173 |
care_state=care_state,
|
| 174 |
personality=personality,
|
| 175 |
birth_time=datetime.now(),
|
| 176 |
+
image_path=image_path,
|
| 177 |
+
model_3d_path=model_3d_path
|
| 178 |
)
|
| 179 |
|
| 180 |
return monster
|
| 181 |
|
| 182 |
+
def _convert_image_to_path(self, image_data, user_id: str, monster_name: str) -> Optional[str]:
|
| 183 |
+
"""Convert PIL Image to file path for storage"""
|
| 184 |
+
if image_data is None:
|
| 185 |
+
return None
|
| 186 |
+
|
| 187 |
+
# If it's already a string path, return it
|
| 188 |
+
if isinstance(image_data, str):
|
| 189 |
+
return image_data
|
| 190 |
+
|
| 191 |
+
# If it's a PIL Image, save it to a file
|
| 192 |
+
if hasattr(image_data, 'save'): # PIL Image check
|
| 193 |
+
try:
|
| 194 |
+
# Create user-specific monsters directory
|
| 195 |
+
monsters_dir = Path("./data/monsters") / user_id
|
| 196 |
+
monsters_dir.mkdir(parents=True, exist_ok=True)
|
| 197 |
+
|
| 198 |
+
# Generate unique filename
|
| 199 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 200 |
+
image_path = monsters_dir / f"{monster_name}_{timestamp}.png"
|
| 201 |
+
|
| 202 |
+
# Save the image
|
| 203 |
+
image_data.save(image_path)
|
| 204 |
+
return str(image_path)
|
| 205 |
+
|
| 206 |
+
except Exception as e:
|
| 207 |
+
print(f"Error saving image: {e}")
|
| 208 |
+
return None
|
| 209 |
+
|
| 210 |
+
return None
|
| 211 |
+
|
| 212 |
def _generate_base_stats(self, traits: Dict, focus: str) -> Dict[str, int]:
|
| 213 |
"""Generate base stats based on traits and focus"""
|
| 214 |
# Base values
|