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

Fix: Mount Gradio on FastAPI for API endpoints

Browse files
Files changed (1) hide show
  1. app.py +18 -20
app.py CHANGED
@@ -1148,46 +1148,44 @@ 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
-
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
 
1148
  yield "", chat_history
1149
 
1150
  msg.submit(respond, [msg, chatbot], [msg, chatbot], show_progress=True)
1151
+
1152
+ # API Setup using Gradio's blocks_with_server approach
1153
+ from fastapi import FastAPI, Response
1154
  from fastapi.middleware.cors import CORSMiddleware
1155
  from fastapi.responses import JSONResponse
1156
+ import json
1157
 
1158
+ # Create a separate FastAPI for API endpoints
1159
+ api_app = FastAPI()
1160
 
1161
+ # CORS
1162
+ api_app.add_middleware(
1163
  CORSMiddleware,
1164
+ allow_origins=["*"],
1165
  allow_credentials=True,
1166
  allow_methods=["*"],
1167
  allow_headers=["*"],
1168
  )
1169
 
1170
+ @api_app.get("/api/conversations")
1171
  async def get_conversations():
 
1172
  try:
1173
  from conversation_tracker import load_conversations
1174
+ return load_conversations()
 
1175
  except Exception as e:
1176
+ return {"error": str(e)}
1177
 
1178
+ @api_app.get("/api/conversations/latest")
1179
  async def get_latest_conversations():
 
1180
  try:
1181
  from conversation_tracker import load_conversations
1182
+ convs = load_conversations()
1183
+ return convs[-50:] if len(convs) > 50 else convs
1184
  except Exception as e:
1185
+ return {"error": str(e)}
1186
 
1187
+ # Mount Gradio app on the FastAPI app
1188
+ app = gr.mount_gradio_app(api_app, demo, path="/")
1189
 
1190
  if __name__ == "__main__":
1191
  import uvicorn