File size: 1,693 Bytes
9dfc96d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
@app.get("/")
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
@app.post("/explain")
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)