SamiKoen commited on
Commit
0b48047
·
1 Parent(s): 36c2db4

Fix: Module-level API route setup for HF Spaces compatibility

Browse files
Files changed (1) hide show
  1. app.py +12 -24
app.py CHANGED
@@ -1150,15 +1150,16 @@ with gr.Blocks(css=custom_css, theme="soft", title="Trek Asistanı", head=storag
1150
  msg.submit(respond, [msg, chatbot], [msg, chatbot], show_progress=True)
1151
  msg.submit(respond, [msg, chatbot], [msg, chatbot], show_progress=True)
1152
 
1153
- # API endpoints using Gradio's native mechanism
1154
- # Gradio 4+ allows adding custom routes via blocks.load event
1155
-
1156
  from fastapi import Request
1157
  from fastapi.responses import JSONResponse
1158
 
1159
- def setup_api_routes(app):
1160
- """Setup API routes on Gradio's FastAPI app"""
1161
- @app.get("/api/conversations")
 
 
 
1162
  async def get_conversations():
1163
  try:
1164
  from conversation_tracker import load_conversations
@@ -1170,7 +1171,7 @@ def setup_api_routes(app):
1170
  except Exception as e:
1171
  return JSONResponse(content={"error": str(e)}, status_code=500)
1172
 
1173
- @app.get("/api/conversations/latest")
1174
  async def get_latest_conversations():
1175
  try:
1176
  from conversation_tracker import load_conversations
@@ -1182,27 +1183,14 @@ def setup_api_routes(app):
1182
  except Exception as e:
1183
  return JSONResponse(content={"error": str(e)}, status_code=500)
1184
 
1185
- print("✅ API routes added successfully")
 
 
1186
 
1187
- # Launch with route setup
1188
  if __name__ == "__main__":
1189
- # Launch returns (local_url, share_url) but also sets up demo.app
1190
  demo.launch(
1191
  server_name="0.0.0.0",
1192
  server_port=7860,
1193
  share=False,
1194
- show_api=False,
1195
- prevent_thread_lock=True # Don't block, we'll add routes then block
1196
  )
1197
-
1198
- # Now add routes to the running app
1199
- if hasattr(demo, 'app') and demo.app:
1200
- setup_api_routes(demo.app)
1201
-
1202
- # Now block the main thread
1203
- import time
1204
- try:
1205
- while True:
1206
- time.sleep(3600)
1207
- except KeyboardInterrupt:
1208
- pass
 
1150
  msg.submit(respond, [msg, chatbot], [msg, chatbot], show_progress=True)
1151
  msg.submit(respond, [msg, chatbot], [msg, chatbot], show_progress=True)
1152
 
1153
+ # API endpoints - Module level setup
 
 
1154
  from fastapi import Request
1155
  from fastapi.responses import JSONResponse
1156
 
1157
+ # Get Gradio's underlying FastAPI app (created when Blocks is defined)
1158
+ # In Gradio 5+, demo has .app attribute after the "with" block
1159
+ try:
1160
+ gradio_app = demo.app
1161
+
1162
+ @gradio_app.get("/api/conversations")
1163
  async def get_conversations():
1164
  try:
1165
  from conversation_tracker import load_conversations
 
1171
  except Exception as e:
1172
  return JSONResponse(content={"error": str(e)}, status_code=500)
1173
 
1174
+ @gradio_app.get("/api/conversations/latest")
1175
  async def get_latest_conversations():
1176
  try:
1177
  from conversation_tracker import load_conversations
 
1183
  except Exception as e:
1184
  return JSONResponse(content={"error": str(e)}, status_code=500)
1185
 
1186
+ print("✅ API routes added to Gradio app")
1187
+ except Exception as e:
1188
+ print(f"⚠️ Could not add API routes: {e}")
1189
 
 
1190
  if __name__ == "__main__":
 
1191
  demo.launch(
1192
  server_name="0.0.0.0",
1193
  server_port=7860,
1194
  share=False,
1195
+ show_api=False
 
1196
  )