SamiKoen commited on
Commit
26b34bc
·
1 Parent(s): 0b48047

Add allowed_paths for file serving

Browse files
Files changed (1) hide show
  1. app.py +46 -39
app.py CHANGED
@@ -1148,47 +1148,54 @@ with gr.Blocks(css=custom_css, theme="soft", title="Trek Asistanı", head=storag
1148
  yield "", chat_history
1149
 
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
1166
- convs = load_conversations()
1167
- return JSONResponse(
1168
- content=convs,
1169
- headers={"Access-Control-Allow-Origin": "*"}
1170
- )
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
1178
- convs = load_conversations()
1179
- return JSONResponse(
1180
- content=convs[-50:] if len(convs) > 50 else convs,
1181
- headers={"Access-Control-Allow-Origin": "*"}
1182
- )
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,
 
1148
  yield "", chat_history
1149
 
1150
  msg.submit(respond, [msg, chatbot], [msg, chatbot], show_progress=True)
 
 
 
 
 
 
 
 
 
 
1151
 
1152
+ # API will be handled separately in production
1153
+
1154
+ # FastAPI endpoints disabled for better HF Spaces compatibility
1155
+ # Use Gradio's built-in API instead
1156
+
1157
+ # from fastapi import FastAPI
1158
+ # from fastapi.middleware.cors import CORSMiddleware
1159
+ # from fastapi.responses import JSONResponse
1160
+ # import json
1161
+
1162
+ # # Create FastAPI app
1163
+ # api = FastAPI()
1164
+
1165
+ # # Add CORS middleware
1166
+ # api.add_middleware(
1167
+ # CORSMiddleware,
1168
+ # allow_origins=["*"], # Allow all origins
1169
+ # allow_credentials=True,
1170
+ # allow_methods=["*"],
1171
+ # allow_headers=["*"],
1172
+ # )
1173
+
1174
+ # @api.get("/api/conversations")
1175
+ # async def get_conversations():
1176
+ # """API endpoint to get conversations with CORS"""
1177
+ # try:
1178
+ # from conversation_tracker import load_conversations
1179
+ # conversations = load_conversations()
1180
+ # return JSONResponse(content=conversations)
1181
+ # except Exception as e:
1182
+ # return JSONResponse(content={"error": str(e)}, status_code=500)
1183
+
1184
+ # @api.get("/api/conversations/latest")
1185
+ # async def get_latest_conversations():
1186
+ # """Get last 50 conversations"""
1187
+ # try:
1188
+ # from conversation_tracker import load_conversations
1189
+ # conversations = load_conversations()
1190
+ # return JSONResponse(content=conversations[-50:])
1191
+ # except Exception as e:
1192
+ # return JSONResponse(content={"error": str(e)}, status_code=500)
1193
+
1194
+
1195
+
1196
+ # Use Gradio directly instead of mounting on FastAPI - Better for HF Spaces
1197
  if __name__ == "__main__":
1198
+ demo.launch(allowed_paths=["/app/conversations.json", "/app/public"],
1199
  server_name="0.0.0.0",
1200
  server_port=7860,
1201
  share=False,