Spaces:
Runtime error
Runtime error
ai: Back to 5 minute of cool down.
Browse files
jarvis.py
CHANGED
|
@@ -34,6 +34,8 @@ LINUX_SERVER_PROVIDER_KEYS = [key for key in json.loads(os.getenv("LINUX_SERVER_
|
|
| 34 |
LINUX_SERVER_PROVIDER_KEYS_MARKED = set()
|
| 35 |
LINUX_SERVER_PROVIDER_KEYS_ATTEMPTS = {}
|
| 36 |
|
|
|
|
|
|
|
| 37 |
AI_TYPES = {f"AI_TYPE_{i}": os.getenv(f"AI_TYPE_{i}") for i in range(1, 7)}
|
| 38 |
RESPONSES = {f"RESPONSE_{i}": os.getenv(f"RESPONSE_{i}") for i in range(1, 10)}
|
| 39 |
|
|
@@ -60,7 +62,7 @@ def marked_item(item, marked, attempts):
|
|
| 60 |
def remove_fail():
|
| 61 |
marked.discard(item)
|
| 62 |
attempts.pop(item, None)
|
| 63 |
-
threading.Timer(
|
| 64 |
|
| 65 |
class SessionWithID(requests.Session):
|
| 66 |
def __init__(self):
|
|
@@ -114,19 +116,21 @@ async def fetch_response_async(host, provider_key, selected_model, messages, mod
|
|
| 114 |
for timeout in timeouts:
|
| 115 |
try:
|
| 116 |
async with httpx.AsyncClient(timeout=timeout) as client:
|
| 117 |
-
|
| 118 |
-
resp
|
|
|
|
|
|
|
| 119 |
resp.raise_for_status()
|
| 120 |
resp_json = resp.json()
|
| 121 |
if isinstance(resp_json, dict) and resp_json.get("choices"):
|
| 122 |
choice = resp_json["choices"][0]
|
| 123 |
if choice.get("message") and isinstance(choice["message"].get("content"), str):
|
| 124 |
return choice["message"]["content"]
|
| 125 |
-
return
|
| 126 |
except Exception:
|
| 127 |
continue
|
| 128 |
marked_item(provider_key, LINUX_SERVER_PROVIDER_KEYS_MARKED, LINUX_SERVER_PROVIDER_KEYS_ATTEMPTS)
|
| 129 |
-
return
|
| 130 |
|
| 131 |
async def chat_with_model_async(history, user_input, selected_model_display, sess):
|
| 132 |
if not get_available_items(LINUX_SERVER_PROVIDER_KEYS, LINUX_SERVER_PROVIDER_KEYS_MARKED) or not get_available_items(LINUX_SERVER_HOSTS, LINUX_SERVER_HOSTS_MARKED):
|
|
@@ -142,7 +146,7 @@ async def chat_with_model_async(history, user_input, selected_model_display, ses
|
|
| 142 |
global ACTIVE_CANDIDATE
|
| 143 |
if ACTIVE_CANDIDATE:
|
| 144 |
result = await fetch_response_async(ACTIVE_CANDIDATE[0], ACTIVE_CANDIDATE[1], selected_model, messages, model_config, sess.session_id)
|
| 145 |
-
if result
|
| 146 |
return result
|
| 147 |
ACTIVE_CANDIDATE = None
|
| 148 |
keys = get_available_items(LINUX_SERVER_PROVIDER_KEYS, LINUX_SERVER_PROVIDER_KEYS_MARKED)
|
|
@@ -151,7 +155,7 @@ async def chat_with_model_async(history, user_input, selected_model_display, ses
|
|
| 151 |
random.shuffle(candidates)
|
| 152 |
for host, key in candidates:
|
| 153 |
result = await fetch_response_async(host, key, selected_model, messages, model_config, sess.session_id)
|
| 154 |
-
if result
|
| 155 |
ACTIVE_CANDIDATE = (host, key)
|
| 156 |
return result
|
| 157 |
return RESPONSES["RESPONSE_2"]
|
|
@@ -197,6 +201,6 @@ with gr.Blocks(fill_height=True, fill_width=True, title=AI_TYPES["AI_TYPE_4"], h
|
|
| 197 |
msg = gr.MultimodalTextbox(show_label=False, placeholder=RESPONSES["RESPONSE_5"], interactive=True, file_count="single", file_types=ALLOWED_EXTENSIONS)
|
| 198 |
with gr.Accordion(AI_TYPES["AI_TYPE_6"], open=False):
|
| 199 |
model_dropdown = gr.Dropdown(show_label=False, choices=MODEL_CHOICES, value=MODEL_CHOICES[0])
|
| 200 |
-
model_dropdown.change(fn=change_model, inputs=[model_dropdown], outputs=[user_history, user_session, selected_model]
|
| 201 |
msg.submit(fn=respond_async, inputs=[msg, user_history, selected_model, user_session], outputs=[chatbot, msg, user_session], api_name=INTERNAL_AI_GET_SERVER)
|
| 202 |
jarvis.launch(max_file_size="1mb")
|
|
|
|
| 34 |
LINUX_SERVER_PROVIDER_KEYS_MARKED = set()
|
| 35 |
LINUX_SERVER_PROVIDER_KEYS_ATTEMPTS = {}
|
| 36 |
|
| 37 |
+
LINUX_SERVER_ERRORS = set(map(int, os.getenv("LINUX_SERVER_ERROR").split(",")))
|
| 38 |
+
|
| 39 |
AI_TYPES = {f"AI_TYPE_{i}": os.getenv(f"AI_TYPE_{i}") for i in range(1, 7)}
|
| 40 |
RESPONSES = {f"RESPONSE_{i}": os.getenv(f"RESPONSE_{i}") for i in range(1, 10)}
|
| 41 |
|
|
|
|
| 62 |
def remove_fail():
|
| 63 |
marked.discard(item)
|
| 64 |
attempts.pop(item, None)
|
| 65 |
+
threading.Timer(300, remove_fail).start()
|
| 66 |
|
| 67 |
class SessionWithID(requests.Session):
|
| 68 |
def __init__(self):
|
|
|
|
| 116 |
for timeout in timeouts:
|
| 117 |
try:
|
| 118 |
async with httpx.AsyncClient(timeout=timeout) as client:
|
| 119 |
+
resp = await client.post(host, json={"model": selected_model, "messages": messages, **model_config, "session_id": session_id}, headers={"Authorization": f"Bearer {provider_key}"})
|
| 120 |
+
if resp.status_code in LINUX_SERVER_ERRORS:
|
| 121 |
+
marked_item(provider_key, LINUX_SERVER_PROVIDER_KEYS_MARKED, LINUX_SERVER_PROVIDER_KEYS_ATTEMPTS)
|
| 122 |
+
return None
|
| 123 |
resp.raise_for_status()
|
| 124 |
resp_json = resp.json()
|
| 125 |
if isinstance(resp_json, dict) and resp_json.get("choices"):
|
| 126 |
choice = resp_json["choices"][0]
|
| 127 |
if choice.get("message") and isinstance(choice["message"].get("content"), str):
|
| 128 |
return choice["message"]["content"]
|
| 129 |
+
return None
|
| 130 |
except Exception:
|
| 131 |
continue
|
| 132 |
marked_item(provider_key, LINUX_SERVER_PROVIDER_KEYS_MARKED, LINUX_SERVER_PROVIDER_KEYS_ATTEMPTS)
|
| 133 |
+
return None
|
| 134 |
|
| 135 |
async def chat_with_model_async(history, user_input, selected_model_display, sess):
|
| 136 |
if not get_available_items(LINUX_SERVER_PROVIDER_KEYS, LINUX_SERVER_PROVIDER_KEYS_MARKED) or not get_available_items(LINUX_SERVER_HOSTS, LINUX_SERVER_HOSTS_MARKED):
|
|
|
|
| 146 |
global ACTIVE_CANDIDATE
|
| 147 |
if ACTIVE_CANDIDATE:
|
| 148 |
result = await fetch_response_async(ACTIVE_CANDIDATE[0], ACTIVE_CANDIDATE[1], selected_model, messages, model_config, sess.session_id)
|
| 149 |
+
if result:
|
| 150 |
return result
|
| 151 |
ACTIVE_CANDIDATE = None
|
| 152 |
keys = get_available_items(LINUX_SERVER_PROVIDER_KEYS, LINUX_SERVER_PROVIDER_KEYS_MARKED)
|
|
|
|
| 155 |
random.shuffle(candidates)
|
| 156 |
for host, key in candidates:
|
| 157 |
result = await fetch_response_async(host, key, selected_model, messages, model_config, sess.session_id)
|
| 158 |
+
if result:
|
| 159 |
ACTIVE_CANDIDATE = (host, key)
|
| 160 |
return result
|
| 161 |
return RESPONSES["RESPONSE_2"]
|
|
|
|
| 201 |
msg = gr.MultimodalTextbox(show_label=False, placeholder=RESPONSES["RESPONSE_5"], interactive=True, file_count="single", file_types=ALLOWED_EXTENSIONS)
|
| 202 |
with gr.Accordion(AI_TYPES["AI_TYPE_6"], open=False):
|
| 203 |
model_dropdown = gr.Dropdown(show_label=False, choices=MODEL_CHOICES, value=MODEL_CHOICES[0])
|
| 204 |
+
model_dropdown.change(fn=change_model, inputs=[model_dropdown], outputs=[user_history, user_session, selected_model])
|
| 205 |
msg.submit(fn=respond_async, inputs=[msg, user_history, selected_model, user_session], outputs=[chatbot, msg, user_session], api_name=INTERNAL_AI_GET_SERVER)
|
| 206 |
jarvis.launch(max_file_size="1mb")
|