awacke1 commited on
Commit
44237bb
·
verified ·
1 Parent(s): b9973f5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -30
app.py CHANGED
@@ -52,7 +52,7 @@ AUDIO_DIR = "audio_logs"
52
  STATE_FILE = "user_state.txt"
53
  CHAT_FILE = os.path.join(CHAT_DIR, "quest_log.md")
54
 
55
- # Helpers (Moved Above init_session_state to Define load_username First)
56
  def format_timestamp(username=""):
57
  now = datetime.now().strftime("%Y%m%d_%H%M%S")
58
  return f"{now}-by-{username}"
@@ -87,33 +87,7 @@ def load_username():
87
  return f.read().strip()
88
  return None
89
 
90
- # Session State Init (Now Can Safely Use load_username)
91
- def init_session_state():
92
- defaults = {
93
- 'server_running': False, 'server_task': None, 'active_connections': {},
94
- 'chat_history': [], 'audio_cache': {}, 'last_transcript': "",
95
- 'username': None, 'score': 0, 'treasures': 0, 'location': START_LOCATION,
96
- 'speech_processed': False, 'players': {}, 'last_update': time.time(),
97
- 'update_interval': 20, 'x_pos': 0, 'z_pos': 0, 'move_left': False,
98
- 'move_right': False, 'move_up': False, 'move_down': False
99
- }
100
- for k, v in defaults.items():
101
- if k not in st.session_state:
102
- st.session_state[k] = v
103
- # Ensure username is initialized immediately
104
- if st.session_state.username is None:
105
- saved_username = load_username()
106
- if saved_username and saved_username in CHARACTERS:
107
- st.session_state.username = saved_username
108
- else:
109
- st.session_state.username = random.choice(list(CHARACTERS.keys()))
110
- asyncio.run(save_chat_entry(st.session_state.username, "🗺️ Begins the Rocky Mountain Quest!", CHARACTERS[st.session_state.username]["voice"]))
111
- save_username(st.session_state.username)
112
-
113
- # Call init_session_state immediately to ensure username is set
114
- init_session_state()
115
-
116
- # Audio Processing
117
  async def async_edge_tts_generate(text, voice, username):
118
  cache_key = f"{text[:100]}_{voice}"
119
  if cache_key in st.session_state['audio_cache']:
@@ -132,7 +106,7 @@ def play_and_download_audio(file_path):
132
  st.audio(file_path)
133
  st.markdown(get_download_link(file_path), unsafe_allow_html=True)
134
 
135
- # Chat and Quest Log
136
  async def save_chat_entry(username, message, voice, is_markdown=False):
137
  if not message.strip() or message == st.session_state.last_transcript:
138
  return None, None
@@ -157,6 +131,32 @@ async def load_chat():
157
  content = f.read().strip()
158
  return content.split('\n')
159
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
160
  # ArXiv Integration
161
  async def perform_arxiv_search(query, username):
162
  gradio_client = Client("awacke1/Arxiv-Paper-Search-And-QA-RAG-Pattern")
@@ -175,7 +175,7 @@ async def websocket_handler(websocket, path):
175
  if room_id not in st.session_state.active_connections:
176
  st.session_state.active_connections[room_id] = {}
177
  st.session_state.active_connections[room_id][client_id] = websocket
178
- username = st.session_state.username # Now guaranteed to be set
179
  st.session_state.players[client_id] = {
180
  "username": username,
181
  "x": random.uniform(-20, 20),
 
52
  STATE_FILE = "user_state.txt"
53
  CHAT_FILE = os.path.join(CHAT_DIR, "quest_log.md")
54
 
55
+ # Helpers
56
  def format_timestamp(username=""):
57
  now = datetime.now().strftime("%Y%m%d_%H%M%S")
58
  return f"{now}-by-{username}"
 
87
  return f.read().strip()
88
  return None
89
 
90
+ # Audio Processing (Moved Up for save_chat_entry Dependency)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
  async def async_edge_tts_generate(text, voice, username):
92
  cache_key = f"{text[:100]}_{voice}"
93
  if cache_key in st.session_state['audio_cache']:
 
106
  st.audio(file_path)
107
  st.markdown(get_download_link(file_path), unsafe_allow_html=True)
108
 
109
+ # Chat and Quest Log (Moved Up for init_session_state Dependency)
110
  async def save_chat_entry(username, message, voice, is_markdown=False):
111
  if not message.strip() or message == st.session_state.last_transcript:
112
  return None, None
 
131
  content = f.read().strip()
132
  return content.split('\n')
133
 
134
+ # Session State Init (Now After All Dependencies)
135
+ def init_session_state():
136
+ defaults = {
137
+ 'server_running': False, 'server_task': None, 'active_connections': {},
138
+ 'chat_history': [], 'audio_cache': {}, 'last_transcript': "",
139
+ 'username': None, 'score': 0, 'treasures': 0, 'location': START_LOCATION,
140
+ 'speech_processed': False, 'players': {}, 'last_update': time.time(),
141
+ 'update_interval': 20, 'x_pos': 0, 'z_pos': 0, 'move_left': False,
142
+ 'move_right': False, 'move_up': False, 'move_down': False
143
+ }
144
+ for k, v in defaults.items():
145
+ if k not in st.session_state:
146
+ st.session_state[k] = v
147
+ # Ensure username is initialized immediately
148
+ if st.session_state.username is None:
149
+ saved_username = load_username()
150
+ if saved_username and saved_username in CHARACTERS:
151
+ st.session_state.username = saved_username
152
+ else:
153
+ st.session_state.username = random.choice(list(CHARACTERS.keys()))
154
+ asyncio.run(save_chat_entry(st.session_state.username, "🗺️ Begins the Rocky Mountain Quest!", CHARACTERS[st.session_state.username]["voice"]))
155
+ save_username(st.session_state.username)
156
+
157
+ # Call init_session_state immediately to ensure username is set
158
+ init_session_state()
159
+
160
  # ArXiv Integration
161
  async def perform_arxiv_search(query, username):
162
  gradio_client = Client("awacke1/Arxiv-Paper-Search-And-QA-RAG-Pattern")
 
175
  if room_id not in st.session_state.active_connections:
176
  st.session_state.active_connections[room_id] = {}
177
  st.session_state.active_connections[room_id][client_id] = websocket
178
+ username = st.session_state.username
179
  st.session_state.players[client_id] = {
180
  "username": username,
181
  "x": random.uniform(-20, 20),