Create inference.py
Browse files- inference.py +28 -0
inference.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# inference.py
|
| 2 |
+
|
| 3 |
+
import requests
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Get the API key from Hugging Face Secrets
|
| 7 |
+
API_KEY = os.getenv("DEEPSEEK_KEY")
|
| 8 |
+
|
| 9 |
+
def deepseek_query(prompt):
|
| 10 |
+
url = "https://api.deepseek.com/v1/chat/completions"
|
| 11 |
+
headers = {
|
| 12 |
+
"Authorization": f"Bearer {API_KEY}",
|
| 13 |
+
"Content-Type": "application/json"
|
| 14 |
+
}
|
| 15 |
+
data = {
|
| 16 |
+
"model": "deepseek-chat",
|
| 17 |
+
"messages": [
|
| 18 |
+
{"role": "system", "content": "You are a helpful, creative AI agent."},
|
| 19 |
+
{"role": "user", "content": prompt}
|
| 20 |
+
]
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
try:
|
| 24 |
+
response = requests.post(url, json=data, headers=headers)
|
| 25 |
+
result = response.json()
|
| 26 |
+
return result["choices"][0]["message"]["content"]
|
| 27 |
+
except Exception as e:
|
| 28 |
+
return f"[ERROR] {str(e)}"
|