luminoussg commited on
Commit
6617dfe
·
verified ·
1 Parent(s): f38ba07

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -7
app.py CHANGED
@@ -14,30 +14,36 @@ MODEL_ENDPOINTS = {
14
 
15
  def query_model(prompt, model_endpoint):
16
  """
17
- Query a model via Hugging Face Inference API using a requests.post call.
18
- This assumes an OpenAI-compatible endpoint structure.
19
  """
20
  headers = {
21
  "Authorization": f"Bearer {HF_API_KEY}",
22
  "Content-Type": "application/json"
23
  }
 
24
  data = {
25
- "messages": [{"role": "user", "content": prompt}],
26
- "max_tokens": 512,
27
- "temperature": 0.7
 
 
28
  }
29
 
30
  response = requests.post(model_endpoint, headers=headers, json=data)
 
31
  try:
32
  result = response.json()
33
  except Exception:
34
  return f"Error: Unable to parse JSON. Response: {response.text}"
35
 
36
- if "error" in result:
 
37
  return f"Error: {result['error']}"
38
 
39
  try:
40
- return result["choices"][0]["message"]["content"]
 
41
  except Exception:
42
  return f"Error: Unexpected response format: {result}"
43
 
 
14
 
15
  def query_model(prompt, model_endpoint):
16
  """
17
+ Query a model via the Hugging Face Inference API using a requests.post call.
18
+ This payload now uses the 'inputs' field as required.
19
  """
20
  headers = {
21
  "Authorization": f"Bearer {HF_API_KEY}",
22
  "Content-Type": "application/json"
23
  }
24
+ # Use 'inputs' instead of 'messages' and pass parameters inside the 'parameters' field.
25
  data = {
26
+ "inputs": prompt,
27
+ "parameters": {
28
+ "max_new_tokens": 512,
29
+ "temperature": 0.7,
30
+ }
31
  }
32
 
33
  response = requests.post(model_endpoint, headers=headers, json=data)
34
+
35
  try:
36
  result = response.json()
37
  except Exception:
38
  return f"Error: Unable to parse JSON. Response: {response.text}"
39
 
40
+ # If there's an error message in the result, return it.
41
+ if isinstance(result, dict) and "error" in result:
42
  return f"Error: {result['error']}"
43
 
44
  try:
45
+ # Expected result format is a list of outputs with a "generated_text" field.
46
+ return result[0]["generated_text"]
47
  except Exception:
48
  return f"Error: Unexpected response format: {result}"
49