Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
# FastAPI entrypoint for AskRoss chatbot backend
|
| 2 |
-
|
| 3 |
import os
|
| 4 |
from fastapi import FastAPI, Request
|
| 5 |
from fastapi.responses import HTMLResponse, FileResponse, JSONResponse
|
|
@@ -15,26 +14,31 @@ from session import get_session
|
|
| 15 |
|
| 16 |
app = FastAPI()
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
@app.get("/", response_class=HTMLResponse)
|
| 23 |
def root():
|
| 24 |
-
# Serve
|
| 25 |
-
index_path = os.path.join(
|
| 26 |
with open(index_path, "r", encoding="utf-8") as f:
|
| 27 |
return f.read()
|
| 28 |
|
| 29 |
@app.get("/widget.js")
|
| 30 |
def widget_js():
|
| 31 |
-
# Serve chatbot.js
|
| 32 |
-
js_path = os.path.join(
|
| 33 |
return FileResponse(js_path, media_type="application/javascript")
|
| 34 |
|
| 35 |
@app.get("/widget.css")
|
| 36 |
def widget_css():
|
| 37 |
-
|
|
|
|
| 38 |
return FileResponse(css_path, media_type="text/css")
|
| 39 |
|
| 40 |
@app.post("/chat")
|
|
@@ -44,13 +48,15 @@ async def chat_endpoint(request: Request):
|
|
| 44 |
thread_id = data.get("thread_id")
|
| 45 |
page_id = data.get("page_id", "home")
|
| 46 |
page_title = data.get("page_title", "AskRoss")
|
|
|
|
| 47 |
if not user_message or not thread_id:
|
| 48 |
return JSONResponse({"error": "Missing message or thread_id"}, status_code=400)
|
| 49 |
|
| 50 |
session = get_session(thread_id)
|
|
|
|
| 51 |
# Run agent asynchronously
|
| 52 |
try:
|
| 53 |
-
from agents import Runner
|
| 54 |
prompt = f"[page_id={page_id}] [page_title={page_title}] {user_message}"
|
| 55 |
result = await Runner.run(
|
| 56 |
askross_agent,
|
|
@@ -59,4 +65,4 @@ async def chat_endpoint(request: Request):
|
|
| 59 |
)
|
| 60 |
return {"message": result.final_output, "thread_id": thread_id}
|
| 61 |
except Exception as e:
|
| 62 |
-
return JSONResponse({"error": str(e)}, status_code=500)
|
|
|
|
| 1 |
# FastAPI entrypoint for AskRoss chatbot backend
|
|
|
|
| 2 |
import os
|
| 3 |
from fastapi import FastAPI, Request
|
| 4 |
from fastapi.responses import HTMLResponse, FileResponse, JSONResponse
|
|
|
|
| 14 |
|
| 15 |
app = FastAPI()
|
| 16 |
|
| 17 |
+
# FIX: Define the absolute path to the 'static' folder in your root directory
|
| 18 |
+
base_dir = os.path.dirname(os.path.abspath(__file__))
|
| 19 |
+
static_dir = os.path.join(base_dir, "static")
|
| 20 |
+
|
| 21 |
+
# Mount the static directory
|
| 22 |
+
# This allows access via /static/filename.js
|
| 23 |
+
app.mount("/static", StaticFiles(directory=static_dir, html=True), name="static")
|
| 24 |
|
| 25 |
@app.get("/", response_class=HTMLResponse)
|
| 26 |
def root():
|
| 27 |
+
# Serve index.html from the static folder
|
| 28 |
+
index_path = os.path.join(static_dir, "index.html")
|
| 29 |
with open(index_path, "r", encoding="utf-8") as f:
|
| 30 |
return f.read()
|
| 31 |
|
| 32 |
@app.get("/widget.js")
|
| 33 |
def widget_js():
|
| 34 |
+
# Serve chatbot.js from the static folder
|
| 35 |
+
js_path = os.path.join(static_dir, "chatbot.js")
|
| 36 |
return FileResponse(js_path, media_type="application/javascript")
|
| 37 |
|
| 38 |
@app.get("/widget.css")
|
| 39 |
def widget_css():
|
| 40 |
+
# Serve style.css from the static folder
|
| 41 |
+
css_path = os.path.join(static_dir, "style.css")
|
| 42 |
return FileResponse(css_path, media_type="text/css")
|
| 43 |
|
| 44 |
@app.post("/chat")
|
|
|
|
| 48 |
thread_id = data.get("thread_id")
|
| 49 |
page_id = data.get("page_id", "home")
|
| 50 |
page_title = data.get("page_title", "AskRoss")
|
| 51 |
+
|
| 52 |
if not user_message or not thread_id:
|
| 53 |
return JSONResponse({"error": "Missing message or thread_id"}, status_code=400)
|
| 54 |
|
| 55 |
session = get_session(thread_id)
|
| 56 |
+
|
| 57 |
# Run agent asynchronously
|
| 58 |
try:
|
| 59 |
+
from agents import Runner
|
| 60 |
prompt = f"[page_id={page_id}] [page_title={page_title}] {user_message}"
|
| 61 |
result = await Runner.run(
|
| 62 |
askross_agent,
|
|
|
|
| 65 |
)
|
| 66 |
return {"message": result.final_output, "thread_id": thread_id}
|
| 67 |
except Exception as e:
|
| 68 |
+
return JSONResponse({"error": str(e)}, status_code=500)
|