import os
import json
import uvicorn
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse, StreamingResponse
from huggingface_hub import hf_hub_download
from llama_cpp import Llama
# --- ENGINE SETUP ---
# Using the 1B model for maximum speed on Free Tier
repo_id = "unsloth/Llama-3.2-1B-Instruct-GGUF"
filename = "Llama-3.2-1B-Instruct-Q4_K_M.gguf"
print("🚀 Mahoba Logic: Downloading/Loading Model...")
model_path = hf_hub_download(repo_id=repo_id, filename=filename)
llm = Llama(model_path=model_path, n_ctx=2048, n_threads=4, verbose=False)
print("✅ Engine Ready!")
# --- THE FRONTEND ---
HTML_CONTENT = """
Mayank's AI Career Guider | The 1% Filter
🎯 The 1% Filter
Stop following the crowd. Build your roadmap for career domination.
"""
# --- BACKEND LOGIC ---
app = FastAPI()
@app.get("/")
async def get_ui():
return HTMLResponse(content=HTML_CONTENT)
@app.post("/guide")
async def guide(request: Request):
data = await request.json()
user_name = data.get("name", "Student")
interest = data.get("interest", "Career")
query = data.get("followup", "")
# Optimized prompt for Llama 3.2 1B
prompt = f"<|begin_of_text|><|start_header_id|>system<|end_header_id|>\n\nYou are the 1% Filter AI by Mayank. Helping {user_name} with {interest}. Be short, brutal, and strategic. Focus on real skills, not degrees.<|eot_id|><|start_header_id|>user<|end_header_id|>\n\n{query}<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n"
def stream_generator():
stream = llm(
prompt,
max_tokens=150,
stream=True,
stop=["<|eot_id|>", "User:"]
)
for output in stream:
if "choices" in output:
token = output["choices"][0].get("text", "")
yield json.dumps({"token": token}) + "\n"
return StreamingResponse(
stream_generator(),
media_type="application/x-ndjson",
headers={"Cache-Control": "no-cache", "X-Content-Type-Options": "nosniff"}
)
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=7860)