Create model_api.py
Browse files- model_api.py +25 -0
model_api.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from huggingface_hub import InferenceClient
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
def query_model(prompt):
|
| 5 |
+
try:
|
| 6 |
+
HF_TOKEN = os.getenv("HF_TOKENN")
|
| 7 |
+
|
| 8 |
+
client = InferenceClient(
|
| 9 |
+
model="mistralai/Mistral-7B-Instruct-v0.2",
|
| 10 |
+
token=HF_TOKEN
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
response = client.chat_completion(
|
| 14 |
+
messages=[
|
| 15 |
+
{"role": "system", "content": "You are a certified professional fitness trainer."},
|
| 16 |
+
{"role": "user", "content": prompt}
|
| 17 |
+
],
|
| 18 |
+
max_tokens=600,
|
| 19 |
+
temperature=0.7
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
return response.choices[0].message.content
|
| 23 |
+
|
| 24 |
+
except Exception as e:
|
| 25 |
+
return f"Error: {str(e)}"
|