Spaces:
Sleeping
Sleeping
Update multi_inference.py
Browse files- multi_inference.py +15 -17
multi_inference.py
CHANGED
|
@@ -3,16 +3,16 @@
|
|
| 3 |
import requests
|
| 4 |
import os
|
| 5 |
|
| 6 |
-
#
|
| 7 |
DEEPSEEK_KEYS = [
|
|
|
|
| 8 |
os.getenv("DEEPSEEK_KEY_1", "").strip(),
|
| 9 |
os.getenv("DEEPSEEK_KEY_2", "").strip(),
|
| 10 |
os.getenv("DEEPSEEK_KEY_3", "").strip(),
|
| 11 |
-
os.getenv("DEEPSEEK_KEY_4", "").strip()
|
| 12 |
-
os.getenv("DEEPSEEK_KEY_5", "").strip()
|
| 13 |
]
|
| 14 |
|
| 15 |
-
#
|
| 16 |
OPENAI_KEYS = [
|
| 17 |
os.getenv("open1", "").strip(),
|
| 18 |
os.getenv("open2", "").strip(),
|
|
@@ -21,7 +21,6 @@ OPENAI_KEYS = [
|
|
| 21 |
os.getenv("open5", "").strip()
|
| 22 |
]
|
| 23 |
|
| 24 |
-
|
| 25 |
def try_deepseek(prompt, key):
|
| 26 |
try:
|
| 27 |
url = "https://api.deepseek.com/v1/chat/completions"
|
|
@@ -40,11 +39,10 @@ def try_deepseek(prompt, key):
|
|
| 40 |
result = res.json()
|
| 41 |
if "choices" in result:
|
| 42 |
return result["choices"][0]["message"]["content"]
|
| 43 |
-
return f"[ERROR] DeepSeek: {result.get('error', {}).get('message', 'Unknown
|
| 44 |
except Exception as e:
|
| 45 |
return f"[ERROR] DeepSeek Exception: {str(e)}"
|
| 46 |
|
| 47 |
-
|
| 48 |
def try_openai(prompt, key):
|
| 49 |
try:
|
| 50 |
url = "https://api.openai.com/v1/chat/completions"
|
|
@@ -63,21 +61,21 @@ def try_openai(prompt, key):
|
|
| 63 |
result = res.json()
|
| 64 |
if "choices" in result:
|
| 65 |
return result["choices"][0]["message"]["content"]
|
| 66 |
-
return f"[ERROR] OpenAI: {result.get('error', {}).get('message', 'Unknown
|
| 67 |
except Exception as e:
|
| 68 |
return f"[ERROR] OpenAI Exception: {str(e)}"
|
| 69 |
|
| 70 |
-
|
| 71 |
-
# Main smart fallback wrapper
|
| 72 |
def multi_query(prompt):
|
| 73 |
for key in DEEPSEEK_KEYS:
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
| 77 |
|
| 78 |
for key in OPENAI_KEYS:
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
|
|
|
| 82 |
|
| 83 |
-
return "[ALL APIs FAILED: Out of quota or
|
|
|
|
| 3 |
import requests
|
| 4 |
import os
|
| 5 |
|
| 6 |
+
# DeepSeek API keys
|
| 7 |
DEEPSEEK_KEYS = [
|
| 8 |
+
os.getenv("DEEPSEEK_KEY", "").strip(),
|
| 9 |
os.getenv("DEEPSEEK_KEY_1", "").strip(),
|
| 10 |
os.getenv("DEEPSEEK_KEY_2", "").strip(),
|
| 11 |
os.getenv("DEEPSEEK_KEY_3", "").strip(),
|
| 12 |
+
os.getenv("DEEPSEEK_KEY_4", "").strip()
|
|
|
|
| 13 |
]
|
| 14 |
|
| 15 |
+
# OpenAI keys (named open1, open2, etc.)
|
| 16 |
OPENAI_KEYS = [
|
| 17 |
os.getenv("open1", "").strip(),
|
| 18 |
os.getenv("open2", "").strip(),
|
|
|
|
| 21 |
os.getenv("open5", "").strip()
|
| 22 |
]
|
| 23 |
|
|
|
|
| 24 |
def try_deepseek(prompt, key):
|
| 25 |
try:
|
| 26 |
url = "https://api.deepseek.com/v1/chat/completions"
|
|
|
|
| 39 |
result = res.json()
|
| 40 |
if "choices" in result:
|
| 41 |
return result["choices"][0]["message"]["content"]
|
| 42 |
+
return f"[ERROR] DeepSeek: {result.get('error', {}).get('message', 'Unknown error')}"
|
| 43 |
except Exception as e:
|
| 44 |
return f"[ERROR] DeepSeek Exception: {str(e)}"
|
| 45 |
|
|
|
|
| 46 |
def try_openai(prompt, key):
|
| 47 |
try:
|
| 48 |
url = "https://api.openai.com/v1/chat/completions"
|
|
|
|
| 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 |
def multi_query(prompt):
|
| 69 |
for key in DEEPSEEK_KEYS:
|
| 70 |
+
if key:
|
| 71 |
+
response = try_deepseek(prompt, key)
|
| 72 |
+
if not response.startswith("[ERROR]"):
|
| 73 |
+
return response
|
| 74 |
|
| 75 |
for key in OPENAI_KEYS:
|
| 76 |
+
if key:
|
| 77 |
+
response = try_openai(prompt, key)
|
| 78 |
+
if not response.startswith("[ERROR]"):
|
| 79 |
+
return response
|
| 80 |
|
| 81 |
+
return "[ALL APIs FAILED: Out of quota or invalid keys]"
|