Spaces:
Sleeping
Sleeping
Download main.py from Shakeel401/AskRoss-Chatbot: direct link, hf CLI and curl.
- Browser
- Download file 2.24 kB
-
https://huggingface.co/spaces/Shakeel401/AskRoss-Chatbot/resolve/1794deff2b2befe5c98af749b8a1d074f4e5bb45/main.py
- Command line
-
hf download hf://spaces/Shakeel401/AskRoss-Chatbot@1794deff2b2befe5c98af749b8a1d074f4e5bb45/main.py
-
curl -L -o main.py https://huggingface.co/spaces/Shakeel401/AskRoss-Chatbot/resolve/1794deff2b2befe5c98af749b8a1d074f4e5bb45/main.py
2.24 kB
| # FastAPI entrypoint for AskRoss chatbot backend | |
| import os | |
| from fastapi import FastAPI, Request | |
| from fastapi.responses import HTMLResponse, FileResponse, JSONResponse | |
| from fastapi.staticfiles import StaticFiles | |
| from dotenv import load_dotenv | |
| import asyncio | |
| # Load environment variables | |
| load_dotenv() | |
| from askross_agent import askross_agent | |
| from session import get_session | |
| app = FastAPI() | |
| # FIX: Define the absolute path to the 'static' folder in your root directory | |
| base_dir = os.path.dirname(os.path.abspath(__file__)) | |
| static_dir = os.path.join(base_dir, "static") | |
| # Mount the static directory | |
| # This allows access via /static/filename.js | |
| app.mount("/static", StaticFiles(directory=static_dir, html=True), name="static") | |
| def root(): | |
| # Serve index.html from the static folder | |
| index_path = os.path.join(static_dir, "index.html") | |
| with open(index_path, "r", encoding="utf-8") as f: | |
| return f.read() | |
| def widget_js(): | |
| # Serve chatbot.js from the static folder | |
| js_path = os.path.join(static_dir, "chatbot.js") | |
| return FileResponse(js_path, media_type="application/javascript") | |
| def widget_css(): | |
| # Serve style.css from the static folder | |
| css_path = os.path.join(static_dir, "style.css") | |
| return FileResponse(css_path, media_type="text/css") | |
| async def chat_endpoint(request: Request): | |
| data = await request.json() | |
| user_message = data.get("message") | |
| thread_id = data.get("thread_id") | |
| page_id = data.get("page_id", "home") | |
| page_title = data.get("page_title", "AskRoss") | |
| if not user_message or not thread_id: | |
| return JSONResponse({"error": "Missing message or thread_id"}, status_code=400) | |
| session = get_session(thread_id) | |
| # Run agent asynchronously | |
| try: | |
| from agents import Runner | |
| prompt = f"[page_id={page_id}] [page_title={page_title}] {user_message}" | |
| result = await Runner.run( | |
| askross_agent, | |
| prompt, | |
| session=session | |
| ) | |
| return {"message": result.final_output, "thread_id": thread_id} | |
| except Exception as e: | |
| return JSONResponse({"error": str(e)}, status_code=500) |