SamiKoen
commited on
Commit
·
36c2db4
1
Parent(s):
f1d4bfa
Fix: Use post-launch route injection for API (preserves Gradio UI)
Browse files
app.py
CHANGED
|
@@ -1148,45 +1148,61 @@ 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 |
-
# API
|
| 1153 |
-
|
| 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 |
-
|
| 1179 |
-
|
| 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 |
-
|
| 1188 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1189 |
|
|
|
|
| 1190 |
if __name__ == "__main__":
|
| 1191 |
-
|
| 1192 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 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
|
| 1165 |
+
convs = load_conversations()
|
| 1166 |
+
return JSONResponse(
|
| 1167 |
+
content=convs,
|
| 1168 |
+
headers={"Access-Control-Allow-Origin": "*"}
|
| 1169 |
+
)
|
| 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
|
| 1177 |
+
convs = load_conversations()
|
| 1178 |
+
return JSONResponse(
|
| 1179 |
+
content=convs[-50:] if len(convs) > 50 else convs,
|
| 1180 |
+
headers={"Access-Control-Allow-Origin": "*"}
|
| 1181 |
+
)
|
| 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
|