Spaces:
Sleeping
Sleeping
Download app.py from codeninja3d/askBot: direct link, hf CLI and curl.
- Browser
- Download file 1.69 kB
-
https://huggingface.co/spaces/codeninja3d/askBot/resolve/main/app.py
- Command line
-
hf download hf://spaces/codeninja3d/askBot/app.py
-
curl -L -o app.py https://huggingface.co/spaces/codeninja3d/askBot/resolve/main/app.py
1.69 kB
| from fastapi import FastAPI | |
| from fastapi.responses import FileResponse | |
| from fastapi.middleware.cors import CORSMiddleware | |
| import uvicorn | |
| import os | |
| # Import our custom modules | |
| from schemas import BotRequest | |
| from llm_cpu import load_llm, generate_answer | |
| from rag import initialize_vector_store, retrieve_context | |
| app = FastAPI(title="AskJulia-Modular-v1") | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| # 1. Initialize Systems on Startup | |
| llm = load_llm() | |
| faiss_index = initialize_vector_store() | |
| print("--- Server Ready ---") | |
| # 2. Serve the HTML Interface | |
| async def get_ui(): | |
| # Instead of a massive string in Python, we just serve the raw HTML file | |
| return FileResponse("index.html") | |
| # 3. Handle the Ask Julia API | |
| async def explain(request: BotRequest): | |
| clean_query = request.user_query.lower().replace("@bot", "").strip() | |
| # Step A: Find relevant notes from the database | |
| retrieved_context = retrieve_context(faiss_index, clean_query) | |
| # Step B: Ask the LLM to generate the final answer | |
| reply = generate_answer(llm, retrieved_context, clean_query) | |
| return {"reply": reply} | |
| if __name__ == "__main__": | |
| # uvicorn.run(app, host="0.0.0.0", port=8000) | |
| # Hugging Face provides the PORT environment variable. | |
| # If it's not found (like when you run it locally), it defaults to 7860. | |
| port = int(os.environ.get("PORT", 7860)) | |
| # "0.0.0.0" is critical. It tells the server to accept connections | |
| # from outside the Docker container (which is how Hugging Face routes traffic). | |
| uvicorn.run(app, host="0.0.0.0", port=port) |