SamiKoen
commited on
Commit
·
af24cf7
1
Parent(s):
3159dce
Enable API endpoints for dashboard integration
Browse files
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 |
-
#
|
| 1153 |
-
|
| 1154 |
-
|
| 1155 |
-
|
| 1156 |
-
|
| 1157 |
-
#
|
| 1158 |
-
|
| 1159 |
-
|
| 1160 |
-
#
|
| 1161 |
-
|
| 1162 |
-
|
| 1163 |
-
#
|
| 1164 |
-
|
| 1165 |
-
|
| 1166 |
-
|
| 1167 |
-
|
| 1168 |
-
|
| 1169 |
-
|
| 1170 |
-
|
| 1171 |
-
|
| 1172 |
-
|
| 1173 |
-
|
| 1174 |
-
|
| 1175 |
-
|
| 1176 |
-
|
| 1177 |
-
|
| 1178 |
-
|
| 1179 |
-
|
| 1180 |
-
|
| 1181 |
-
|
| 1182 |
-
|
| 1183 |
-
|
| 1184 |
-
|
| 1185 |
-
|
| 1186 |
-
|
| 1187 |
-
|
| 1188 |
-
|
| 1189 |
-
#
|
| 1190 |
-
|
| 1191 |
-
|
| 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 |
-
|
| 1199 |
-
|
| 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)
|
|
|
|
|
|
|
|
|
|
|
|