Create multi_inference.py
Browse files- multi_inference.py +81 -0
multi_inference.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# multi_inference.py
|
2 |
+
|
3 |
+
import requests
|
4 |
+
import os
|
5 |
+
|
6 |
+
# API keys (ideally you should load these from Hugging Face Secrets)
|
7 |
+
DEEPSEEK_KEYS = [
|
8 |
+
"sk-5b4ecbbd66864a88925525fab14430d1",
|
9 |
+
"sk-9c216dd03c9945218f12475a0fa6c8c4",
|
10 |
+
"sk-79f0d8a98b88447880c01d012c572831",
|
11 |
+
"sk-2be33a5e561b4788b9446e4e4e99bc2e"
|
12 |
+
]
|
13 |
+
|
14 |
+
OPENAI_KEYS = [
|
15 |
+
"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",
|
16 |
+
"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",
|
17 |
+
"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",
|
18 |
+
"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",
|
19 |
+
"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"
|
20 |
+
]
|
21 |
+
|
22 |
+
|
23 |
+
def try_deepseek(prompt, key):
|
24 |
+
try:
|
25 |
+
url = "https://api.deepseek.com/v1/chat/completions"
|
26 |
+
headers = {
|
27 |
+
"Authorization": f"Bearer {key}",
|
28 |
+
"Content-Type": "application/json"
|
29 |
+
}
|
30 |
+
data = {
|
31 |
+
"model": "deepseek-chat",
|
32 |
+
"messages": [
|
33 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
34 |
+
{"role": "user", "content": prompt}
|
35 |
+
]
|
36 |
+
}
|
37 |
+
res = requests.post(url, headers=headers, json=data)
|
38 |
+
result = res.json()
|
39 |
+
if "choices" in result:
|
40 |
+
return result["choices"][0]["message"]["content"]
|
41 |
+
return f"[ERROR] DeepSeek: {result.get('error', {}).get('message', 'Unknown Error')}"
|
42 |
+
except Exception as e:
|
43 |
+
return f"[ERROR] DeepSeek Exception: {str(e)}"
|
44 |
+
|
45 |
+
|
46 |
+
def try_openai(prompt, key):
|
47 |
+
try:
|
48 |
+
url = "https://api.openai.com/v1/chat/completions"
|
49 |
+
headers = {
|
50 |
+
"Authorization": f"Bearer {key}",
|
51 |
+
"Content-Type": "application/json"
|
52 |
+
}
|
53 |
+
data = {
|
54 |
+
"model": "gpt-3.5-turbo",
|
55 |
+
"messages": [
|
56 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
57 |
+
{"role": "user", "content": prompt}
|
58 |
+
]
|
59 |
+
}
|
60 |
+
res = requests.post(url, headers=headers, json=data)
|
61 |
+
result = res.json()
|
62 |
+
if "choices" in result:
|
63 |
+
return result["choices"][0]["message"]["content"]
|
64 |
+
return f"[ERROR] OpenAI: {result.get('error', {}).get('message', 'Unknown Error')}"
|
65 |
+
except Exception as e:
|
66 |
+
return f"[ERROR] OpenAI Exception: {str(e)}"
|
67 |
+
|
68 |
+
|
69 |
+
# Main smart fallback wrapper
|
70 |
+
def multi_query(prompt):
|
71 |
+
for key in DEEPSEEK_KEYS:
|
72 |
+
output = try_deepseek(prompt, key)
|
73 |
+
if not output.startswith("[ERROR]"):
|
74 |
+
return output
|
75 |
+
|
76 |
+
for key in OPENAI_KEYS:
|
77 |
+
output = try_openai(prompt, key)
|
78 |
+
if not output.startswith("[ERROR]"):
|
79 |
+
return output
|
80 |
+
|
81 |
+
return "[ALL APIs FAILED: Out of quota or network error]"
|