Springboardmen commited on
Commit
7e10d04
·
verified ·
1 Parent(s): 5f1d48b

Update model_api.py

Browse files
Files changed (1) hide show
  1. model_api.py +13 -12
model_api.py CHANGED
@@ -1,11 +1,13 @@
1
  import requests
2
- import streamlit as st
3
  import time
4
 
5
  API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2"
6
 
 
 
7
  headers = {
8
- "Authorization": f"Bearer {st.secrets['HF_TOKEN']}"
9
  }
10
 
11
  def query_model(prompt):
@@ -17,15 +19,14 @@ def query_model(prompt):
17
  }
18
  }
19
 
20
- response = requests.post(API_URL, headers=headers, json=payload)
21
- result = response.json()
22
-
23
- # If model is loading
24
- if isinstance(result, dict) and "error" in result:
25
- return f"⚠ Model Error: {result['error']}"
26
 
27
- # If normal success response
28
- if isinstance(result, list) and len(result) > 0:
29
- return result[0]["generated_text"]
30
 
31
- return "Unexpected response from model."
 
 
 
 
1
  import requests
2
+ import os
3
  import time
4
 
5
  API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2"
6
 
7
+ HF_TOKEN = os.getenv("HF_TOKEN")
8
+
9
  headers = {
10
+ "Authorization": f"Bearer {HF_TOKEN}"
11
  }
12
 
13
  def query_model(prompt):
 
19
  }
20
  }
21
 
22
+ while True:
23
+ response = requests.post(API_URL, headers=headers, json=payload)
24
+ result = response.json()
 
 
 
25
 
26
+ if isinstance(result, list):
27
+ return result[0]["generated_text"]
 
28
 
29
+ if "estimated_time" in result:
30
+ time.sleep(result["estimated_time"])
31
+ else:
32
+ return f"Error: {result}"