minzo456 commited on
Commit
02eb11e
·
verified ·
1 Parent(s): 48ceb9f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -11
app.py CHANGED
@@ -14,7 +14,8 @@ app.add_middleware(
14
  )
15
 
16
  HF_TOKEN = os.getenv("HF_TOKEN")
17
- MODEL_ID = "meta-llama/Llama-3.1-8B-Instruct"
 
18
 
19
  @app.post("/chat")
20
  async def chat(request: Request):
@@ -27,22 +28,30 @@ async def chat(request: Request):
27
  "Content-Type": "application/json"
28
  }
29
 
30
- # 🛡️ මෙහ ඇතURL එනව 'router' endpoint එකට මාරු කරන ලදී
 
 
 
 
 
 
 
 
31
  response = requests.post(
32
- f"https://router.huggingface.co/openai/v1/chat/completions",
33
  headers=headers,
34
- json={
35
- "model": MODEL_ID,
36
- "messages": messages,
37
- "max_tokens": 1024,
38
- "temperature": 0.7,
39
- "stream": False
40
- }
41
  )
42
 
 
 
 
 
43
  return response.json()
 
44
  except Exception as e:
45
- return {"error": str(e)}
46
 
47
  @app.get("/")
48
  def home():
 
14
  )
15
 
16
  HF_TOKEN = os.getenv("HF_TOKEN")
17
+ # පාවිච්චි කරන Model එක
18
+ MODEL_ID = "Qwen/Qwen2.5-72B-Instruct"
19
 
20
  @app.post("/chat")
21
  async def chat(request: Request):
 
28
  "Content-Type": "application/json"
29
  }
30
 
31
+ # 🛡️ පහදු Payload එක
32
+ payload = {
33
+ "model": MODEL_ID,
34
+ "messages": messages,
35
+ "max_tokens": 1024,
36
+ "temperature": 0.7
37
+ }
38
+
39
+ # Hugging Face Router වෙත දත්ත යැවීම
40
  response = requests.post(
41
+ "https://router.huggingface.co/openai/v1/chat/completions",
42
  headers=headers,
43
+ json=payload,
44
+ timeout=30 # කාලය ඉක්මවා යාම වැළැක්වීමට
 
 
 
 
 
45
  )
46
 
47
+ # ⚠️ වැදගත්: ප්‍රතිචාරය JSON ද කියා පරීක්ෂා කිරීම
48
+ if response.status_code != 200:
49
+ return {"error": f"HF API Error {response.status_code}", "details": response.text}
50
+
51
  return response.json()
52
+
53
  except Exception as e:
54
+ return {"error": "Internal System Failure", "details": str(e)}
55
 
56
  @app.get("/")
57
  def home():