srbhavya01 commited on
Commit
aa75006
·
verified ·
1 Parent(s): ca8790e

Update model_api.py

Browse files
Files changed (1) hide show
  1. model_api.py +24 -8
model_api.py CHANGED
@@ -1,21 +1,37 @@
1
  from huggingface_hub import InferenceClient
2
  import os
 
 
3
 
4
  def query_model(prompt):
5
  try:
6
  HF_TOKEN = os.getenv("HF_TOKEN")
7
- # You can also set provider at the client level
8
- client = InferenceClient(api_key=HF_TOKEN, provider="auto")
9
-
 
 
 
10
  response = client.chat.completions.create(
11
  model="Qwen/Qwen2.5-7B-Instruct",
12
  messages=[
13
- {"role": "system", "content": "You are a professional fitness trainer."},
14
  {"role": "user", "content": prompt}
15
  ],
16
- max_tokens=1500,
17
- temperature=0.7
18
  )
19
- return response.choices[0].message.content
 
 
 
 
 
 
 
 
 
 
 
20
  except Exception as e:
21
- return f"Error: {str(e)}"
 
1
  from huggingface_hub import InferenceClient
2
  import os
3
+ import json
4
+ import re
5
 
6
  def query_model(prompt):
7
  try:
8
  HF_TOKEN = os.getenv("HF_TOKEN")
9
+
10
+ client = InferenceClient(
11
+ provider="auto",
12
+ api_key=HF_TOKEN
13
+ )
14
+
15
  response = client.chat.completions.create(
16
  model="Qwen/Qwen2.5-7B-Instruct",
17
  messages=[
18
+ {"role": "system", "content": "You are a professional fitness trainer that returns ONLY JSON workout plans."},
19
  {"role": "user", "content": prompt}
20
  ],
21
+ max_tokens=1200,
22
+ temperature=0.6
23
  )
24
+
25
+ result = response.choices[0].message.content
26
+
27
+ # Remove markdown JSON blocks if model returns ```json ```
28
+ result = re.sub(r"```json|```", "", result).strip()
29
+
30
+ try:
31
+ result_json = json.loads(result)
32
+ return result_json
33
+ except json.JSONDecodeError:
34
+ return {"error": "Model returned invalid JSON", "raw_output": result}
35
+
36
  except Exception as e:
37
+ return {"error": str(e)}