Black Goku commited on
Commit
b4ddf84
·
verified ·
1 Parent(s): bf84799

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -20
app.py CHANGED
@@ -9,7 +9,6 @@ from fastapi.middleware.cors import CORSMiddleware
9
 
10
  app = FastAPI()
11
 
12
- # Security Pass
13
  app.add_middleware(
14
  CORSMiddleware,
15
  allow_origins=["*"],
@@ -18,16 +17,16 @@ app.add_middleware(
18
  allow_headers=["*"],
19
  )
20
 
21
- # Fetch token
22
  HF_TOKEN = os.getenv("HF_TOKEN")
23
- if not HF_TOKEN:
24
- print("WARNING: HF_TOKEN environment variable is missing!")
25
 
26
- # Model to use (If Qwen fails, try "meta-llama/Llama-3.1-8B-Instruct")
27
- MODEL_NAME = "Qwen/Qwen2.5-7B-Instruct"
 
 
 
28
 
29
- # Connect to Hugging Face
30
- client = InferenceClient(api_key=HF_TOKEN)
31
 
32
  SYSTEM_PROMPT = (
33
  "You are Vex, the AI assistant for the Vex SMP Minecraft server, created by Xirrik. "
@@ -53,7 +52,7 @@ def home():
53
  @app.post("/chat")
54
  async def chat(request: ChatRequest):
55
  try:
56
- response = client.chat_completion(
57
  model=MODEL_NAME,
58
  messages=[
59
  {"role": "system", "content": SYSTEM_PROMPT},
@@ -67,21 +66,20 @@ async def chat(request: ChatRequest):
67
  return {"reply": reply}
68
 
69
  except Exception as e:
70
- # Print full error details to server logs for debugging
71
- print(f"[Error in /chat]: {e}")
72
  traceback.print_exc()
73
-
74
  error_msg = str(e)
75
  if "503" in error_msg or "loading" in error_msg.lower():
76
  return {"reply": "My Cloud Brain is currently waking up. Please send this message again in 10 seconds!"}
77
- return {"reply": "I had a tiny hiccup in my thoughts. Could you repeat that for me?"}
78
 
79
 
80
  @app.post("/chat/stream")
81
  async def chat_stream(request: ChatRequest):
82
  def token_generator():
83
  try:
84
- stream = client.chat_completion(
85
  model=MODEL_NAME,
86
  messages=[
87
  {"role": "system", "content": SYSTEM_PROMPT},
@@ -98,11 +96,7 @@ async def chat_stream(request: ChatRequest):
98
  yield json.dumps({"done": True}) + "\n"
99
 
100
  except Exception as e:
101
- print(f"[Error in /chat/stream]: {e}")
102
- error_msg = str(e)
103
- if "503" in error_msg or "loading" in error_msg.lower():
104
- yield json.dumps({"error": "My Cloud Brain is currently waking up. Please send this message again in 10 seconds!"}) + "\n"
105
- else:
106
- yield json.dumps({"error": "I had a tiny hiccup in my thoughts. Could you repeat that for me?"}) + "\n"
107
 
108
  return StreamingResponse(token_generator(), media_type="application/x-ndjson")
 
9
 
10
  app = FastAPI()
11
 
 
12
  app.add_middleware(
13
  CORSMiddleware,
14
  allow_origins=["*"],
 
17
  allow_headers=["*"],
18
  )
19
 
 
20
  HF_TOKEN = os.getenv("HF_TOKEN")
 
 
21
 
22
+ # Initialize client using the new router provider
23
+ client = InferenceClient(
24
+ provider="hf-inference",
25
+ api_key=HF_TOKEN
26
+ )
27
 
28
+ # Supported router model
29
+ MODEL_NAME = "Qwen/Qwen2.5-Coder-32B-Instruct"
30
 
31
  SYSTEM_PROMPT = (
32
  "You are Vex, the AI assistant for the Vex SMP Minecraft server, created by Xirrik. "
 
52
  @app.post("/chat")
53
  async def chat(request: ChatRequest):
54
  try:
55
+ response = client.chat.completions.create(
56
  model=MODEL_NAME,
57
  messages=[
58
  {"role": "system", "content": SYSTEM_PROMPT},
 
66
  return {"reply": reply}
67
 
68
  except Exception as e:
69
+ print("[Error in /chat]:", e)
 
70
  traceback.print_exc()
71
+
72
  error_msg = str(e)
73
  if "503" in error_msg or "loading" in error_msg.lower():
74
  return {"reply": "My Cloud Brain is currently waking up. Please send this message again in 10 seconds!"}
75
+ return {"reply": f"Backend Error: {error_msg}"}
76
 
77
 
78
  @app.post("/chat/stream")
79
  async def chat_stream(request: ChatRequest):
80
  def token_generator():
81
  try:
82
+ stream = client.chat.completions.create(
83
  model=MODEL_NAME,
84
  messages=[
85
  {"role": "system", "content": SYSTEM_PROMPT},
 
96
  yield json.dumps({"done": True}) + "\n"
97
 
98
  except Exception as e:
99
+ print("[Error in /chat/stream]:", e)
100
+ yield json.dumps({"error": str(e)}) + "\n"
 
 
 
 
101
 
102
  return StreamingResponse(token_generator(), media_type="application/x-ndjson")