Spaces:
Sleeping
Sleeping
Update model_api.py
Browse files- model_api.py +13 -12
model_api.py
CHANGED
|
@@ -1,11 +1,13 @@
|
|
| 1 |
import requests
|
| 2 |
-
import
|
| 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 {
|
| 9 |
}
|
| 10 |
|
| 11 |
def query_model(prompt):
|
|
@@ -17,15 +19,14 @@ def query_model(prompt):
|
|
| 17 |
}
|
| 18 |
}
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
# If model is loading
|
| 24 |
-
if isinstance(result, dict) and "error" in result:
|
| 25 |
-
return f"⚠ Model Error: {result['error']}"
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
return result[0]["generated_text"]
|
| 30 |
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 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}"
|