Black Goku commited on
Update app.py
Browse files8 unmodified lines91011121314152 unmodified lines1819202122232425262728293031323319 unmodified lines535455565758597 unmodified lines67686970717273747576777879808182838485868710 unmodified lines98991001011021031041051061071088 unmodified linesapp = FastAPI()# Security Passapp.add_middleware(CORSMiddleware,allow_origins=["*"],2 unmodified linesallow_headers=["*"],)# Fetch tokenHF_TOKEN = os.getenv("HF_TOKEN")if not HF_TOKEN:print("WARNING: HF_TOKEN environment variable is missing!")# Model to use (If Qwen fails, try "meta-llama/Llama-3.1-8B-Instruct")MODEL_NAME = "Qwen/Qwen2.5-7B-Instruct"# Connect to Hugging Faceclient = InferenceClient(api_key=HF_TOKEN)SYSTEM_PROMPT = ("You are Vex, the AI assistant for the Vex SMP Minecraft server, created by Xirrik. "19 unmodified lines@app.post("/chat")async def chat(request: ChatRequest):try:response = client.chat_completion(model=MODEL_NAME,messages=[{"role": "system", "content": SYSTEM_PROMPT},7 unmodified linesreturn {"reply": reply}except Exception as e:# Print full error details to server logs for debuggingprint(f"[Error in /chat]: {e}")traceback.print_exc()error_msg = str(e)if "503" in error_msg or "loading" in error_msg.lower():return {"reply": "My Cloud Brain is currently waking up. Please send this message again in 10 seconds!"}return {"reply": "I had a tiny hiccup in my thoughts. Could you repeat that for me?"}@app.post("/chat/stream")async def chat_stream(request: ChatRequest):def token_generator():try:stream = client.chat_completion(model=MODEL_NAME,messages=[{"role": "system", "content": SYSTEM_PROMPT},10 unmodified linesyield json.dumps({"done": True}) + "\n"except Exception as e:print(f"[Error in /chat/stream]: {e}")error_msg = str(e)if "503" in error_msg or "loading" in error_msg.lower():yield json.dumps({"error": "My Cloud Brain is currently waking up. Please send this message again in 10 seconds!"}) + "\n"else:yield json.dumps({"error": "I had a tiny hiccup in my thoughts. Could you repeat that for me?"}) + "\n"return StreamingResponse(token_generator(), media_type="application/x-ndjson")8 unmodified lines910111213142 unmodified lines1718192021222324252627282930313219 unmodified lines525354555657587 unmodified lines666768697071727374757677787980818283848510 unmodified lines969798991001011028 unmodified linesapp = FastAPI()app.add_middleware(CORSMiddleware,allow_origins=["*"],2 unmodified linesallow_headers=["*"],)HF_TOKEN = os.getenv("HF_TOKEN")# Initialize client using the new router providerclient = InferenceClient(provider="hf-inference",api_key=HF_TOKEN)# Supported router modelMODEL_NAME = "Qwen/Qwen2.5-Coder-32B-Instruct"SYSTEM_PROMPT = ("You are Vex, the AI assistant for the Vex SMP Minecraft server, created by Xirrik. "19 unmodified lines@app.post("/chat")async def chat(request: ChatRequest):try:response = client.chat.completions.create(model=MODEL_NAME,messages=[{"role": "system", "content": SYSTEM_PROMPT},7 unmodified linesreturn {"reply": reply}except Exception as e:print("[Error in /chat]:", e)traceback.print_exc()error_msg = str(e)if "503" in error_msg or "loading" in error_msg.lower():return {"reply": "My Cloud Brain is currently waking up. Please send this message again in 10 seconds!"}return {"reply": f"Backend Error: {error_msg}"}@app.post("/chat/stream")async def chat_stream(request: ChatRequest):def token_generator():try:stream = client.chat.completions.create(model=MODEL_NAME,messages=[{"role": "system", "content": SYSTEM_PROMPT},10 unmodified linesyield json.dumps({"done": True}) + "\n"except Exception as e:print("[Error in /chat/stream]:", e)yield json.dumps({"error": str(e)}) + "\n"return StreamingResponse(token_generator(), media_type="application/x-ndjson")
app.py+14-20