SamiKoen
commited on
Commit
·
f1d4bfa
1
Parent(s):
af24cf7
Fix: Mount Gradio on FastAPI for API endpoints
Browse files
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 |
-
#
|
| 1153 |
-
from fastapi import FastAPI
|
| 1154 |
from fastapi.middleware.cors import CORSMiddleware
|
| 1155 |
from fastapi.responses import JSONResponse
|
|
|
|
| 1156 |
|
| 1157 |
-
# Create FastAPI
|
| 1158 |
-
|
| 1159 |
|
| 1160 |
-
#
|
| 1161 |
-
|
| 1162 |
CORSMiddleware,
|
| 1163 |
-
allow_origins=["*"],
|
| 1164 |
allow_credentials=True,
|
| 1165 |
allow_methods=["*"],
|
| 1166 |
allow_headers=["*"],
|
| 1167 |
)
|
| 1168 |
|
| 1169 |
-
@
|
| 1170 |
async def get_conversations():
|
| 1171 |
-
"""API endpoint to get conversations with CORS"""
|
| 1172 |
try:
|
| 1173 |
from conversation_tracker import load_conversations
|
| 1174 |
-
|
| 1175 |
-
return JSONResponse(content=conversations)
|
| 1176 |
except Exception as e:
|
| 1177 |
-
return
|
| 1178 |
|
| 1179 |
-
@
|
| 1180 |
async def get_latest_conversations():
|
| 1181 |
-
"""Get last 50 conversations"""
|
| 1182 |
try:
|
| 1183 |
from conversation_tracker import load_conversations
|
| 1184 |
-
|
| 1185 |
-
return
|
| 1186 |
except Exception as e:
|
| 1187 |
-
return
|
| 1188 |
|
| 1189 |
-
# Mount Gradio on FastAPI
|
| 1190 |
-
app = gr.mount_gradio_app(
|
| 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
|