SamiKoen commited on
Commit
af24cf7
·
1 Parent(s): 3159dce

Enable API endpoints for dashboard integration

Browse files
Files changed (1) hide show
  1. app.py +42 -51
app.py CHANGED
@@ -1149,55 +1149,46 @@ with gr.Blocks(css=custom_css, theme="soft", title="Trek Asistanı", head=storag
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(
1199
- server_name="0.0.0.0",
1200
- server_port=7860,
1201
- share=False,
1202
- show_api=False
1203
- )
 
1149
 
1150
  msg.submit(respond, [msg, chatbot], [msg, chatbot], show_progress=True)
1151
 
1152
+ # FastAPI endpoints for dashboard integration
1153
+ from fastapi import FastAPI
1154
+ from fastapi.middleware.cors import CORSMiddleware
1155
+ from fastapi.responses import JSONResponse
1156
+
1157
+ # Create FastAPI app
1158
+ api = FastAPI()
1159
+
1160
+ # Add CORS middleware
1161
+ api.add_middleware(
1162
+ CORSMiddleware,
1163
+ allow_origins=["*"], # Allow all origins
1164
+ allow_credentials=True,
1165
+ allow_methods=["*"],
1166
+ allow_headers=["*"],
1167
+ )
1168
+
1169
+ @api.get("/api/conversations")
1170
+ async def get_conversations():
1171
+ """API endpoint to get conversations with CORS"""
1172
+ try:
1173
+ from conversation_tracker import load_conversations
1174
+ conversations = load_conversations()
1175
+ return JSONResponse(content=conversations)
1176
+ except Exception as e:
1177
+ return JSONResponse(content={"error": str(e)}, status_code=500)
1178
+
1179
+ @api.get("/api/conversations/latest")
1180
+ async def get_latest_conversations():
1181
+ """Get last 50 conversations"""
1182
+ try:
1183
+ from conversation_tracker import load_conversations
1184
+ conversations = load_conversations()
1185
+ return JSONResponse(content=conversations[-50:])
1186
+ except Exception as e:
1187
+ return JSONResponse(content={"error": str(e)}, status_code=500)
1188
+
1189
+ # Mount Gradio on FastAPI for HF Spaces
1190
+ app = gr.mount_gradio_app(api, demo, path="/")
1191
+
 
 
 
 
 
1192
  if __name__ == "__main__":
1193
+ import uvicorn
1194
+ uvicorn.run(app, host="0.0.0.0", port=7860)