Spaces:
Sleeping
Sleeping
| import requests | |
| import os | |
| import time | |
| API_URL = "https://router.huggingface.co/hf-inference/models/mistralai/Mistral-7B-Instruct-v0.2" | |
| HF_TOKEN = os.getenv("HF_TOKEN") | |
| headers = { | |
| "Authorization": f"Bearer {HF_TOKEN}" | |
| } | |
| def query_model(prompt): | |
| payload = { | |
| "inputs": prompt, | |
| "parameters": { | |
| "max_new_tokens": 500, | |
| "temperature": 0.7 | |
| } | |
| } | |
| response = requests.post(API_URL, headers=headers, json=payload) | |
| # ✅ Check status code first | |
| if response.status_code != 200: | |
| return f"HTTP Error {response.status_code}: {response.text}" | |
| # ✅ Safe JSON parsing | |
| try: | |
| result = response.json() | |
| except Exception: | |
| return f"Invalid response received: {response.text}" | |
| if isinstance(result, list): | |
| return result[0]["generated_text"] | |
| return f"Unexpected response: {result}" |