Update ai_api.py
Browse files
ai_api.py
CHANGED
|
@@ -1,45 +1,45 @@
|
|
| 1 |
-
import requests
|
| 2 |
-
import json
|
| 3 |
-
import time
|
| 4 |
-
|
| 5 |
-
def thinking_animation():
|
| 6 |
-
"""
|
| 7 |
-
دالة بسيطة للاستخدام أثناء الانتظار (ليست ضرورية).
|
| 8 |
-
"""
|
| 9 |
-
for _ in range(3):
|
| 10 |
-
print(".", end="", flush=True)
|
| 11 |
-
time.sleep(0.5)
|
| 12 |
-
|
| 13 |
-
def call_o1_ai_api(formatted_chat_history):
|
| 14 |
-
"""
|
| 15 |
-
يُرسِل المحادثة الحالية إلى واجهة الذكاء الاصطناعي o1
|
| 16 |
-
ويعيد الرد مع تحديث المحادثة.
|
| 17 |
-
"""
|
| 18 |
-
url = "https://corvo-ai-xx-
|
| 19 |
-
headers = {"Content-Type": "application/json"}
|
| 20 |
-
payload = {
|
| 21 |
-
"chat_history": formatted_chat_history
|
| 22 |
-
}
|
| 23 |
-
max_retries = 5
|
| 24 |
-
retry_delay = 10
|
| 25 |
-
timeout = 600
|
| 26 |
-
|
| 27 |
-
for attempt in range(max_retries):
|
| 28 |
-
try:
|
| 29 |
-
print("AI THINKING", end="", flush=True)
|
| 30 |
-
thinking_animation()
|
| 31 |
-
response = requests.post(url, headers=headers, data=json.dumps(payload), timeout=timeout)
|
| 32 |
-
response.raise_for_status()
|
| 33 |
-
assistant_response = response.json().get("assistant_response", "No response received.")
|
| 34 |
-
# Append the assistant response to chat history
|
| 35 |
-
formatted_chat_history.append({"role": "assistant", "content": assistant_response})
|
| 36 |
-
return assistant_response, formatted_chat_history
|
| 37 |
-
except requests.exceptions.Timeout:
|
| 38 |
-
print(f"Timeout on attempt {attempt + 1}, retrying...")
|
| 39 |
-
time.sleep(retry_delay)
|
| 40 |
-
except Exception as e:
|
| 41 |
-
print(f"Error on attempt {attempt + 1}: {e}, retrying...")
|
| 42 |
-
time.sleep(retry_delay)
|
| 43 |
-
|
| 44 |
-
return "Error processing request. Please try again.", formatted_chat_history
|
| 45 |
-
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import json
|
| 3 |
+
import time
|
| 4 |
+
|
| 5 |
+
def thinking_animation():
|
| 6 |
+
"""
|
| 7 |
+
دالة بسيطة للاستخدام أثناء الانتظار (ليست ضرورية).
|
| 8 |
+
"""
|
| 9 |
+
for _ in range(3):
|
| 10 |
+
print(".", end="", flush=True)
|
| 11 |
+
time.sleep(0.5)
|
| 12 |
+
|
| 13 |
+
def call_o1_ai_api(formatted_chat_history):
|
| 14 |
+
"""
|
| 15 |
+
يُرسِل المحادثة الحالية إلى واجهة الذكاء الاصطناعي o1
|
| 16 |
+
ويعيد الرد مع تحديث المحادثة.
|
| 17 |
+
"""
|
| 18 |
+
url = "https://corvo-ai-xx-o1.hf.space/chat"
|
| 19 |
+
headers = {"Content-Type": "application/json"}
|
| 20 |
+
payload = {
|
| 21 |
+
"chat_history": formatted_chat_history
|
| 22 |
+
}
|
| 23 |
+
max_retries = 5
|
| 24 |
+
retry_delay = 10
|
| 25 |
+
timeout = 600
|
| 26 |
+
|
| 27 |
+
for attempt in range(max_retries):
|
| 28 |
+
try:
|
| 29 |
+
print("AI THINKING", end="", flush=True)
|
| 30 |
+
thinking_animation()
|
| 31 |
+
response = requests.post(url, headers=headers, data=json.dumps(payload), timeout=timeout)
|
| 32 |
+
response.raise_for_status()
|
| 33 |
+
assistant_response = response.json().get("assistant_response", "No response received.")
|
| 34 |
+
# Append the assistant response to chat history
|
| 35 |
+
formatted_chat_history.append({"role": "assistant", "content": assistant_response})
|
| 36 |
+
return assistant_response, formatted_chat_history
|
| 37 |
+
except requests.exceptions.Timeout:
|
| 38 |
+
print(f"Timeout on attempt {attempt + 1}, retrying...")
|
| 39 |
+
time.sleep(retry_delay)
|
| 40 |
+
except Exception as e:
|
| 41 |
+
print(f"Error on attempt {attempt + 1}: {e}, retrying...")
|
| 42 |
+
time.sleep(retry_delay)
|
| 43 |
+
|
| 44 |
+
return "Error processing request. Please try again.", formatted_chat_history
|
| 45 |
+
|