Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,21 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import requests
|
| 3 |
from flask import Flask, request, jsonify
|
| 4 |
from flask_cors import CORS
|
| 5 |
|
| 6 |
app = Flask(__name__)
|
| 7 |
-
CORS(app)
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
# Note: HF secrets are loaded as environment variables
|
| 11 |
TOKENS_RAW = os.environ.get("AI_SERVICE_TOKEN", "")
|
| 12 |
TOKENS = [t.strip() for t in TOKENS_RAW.split(",") if t.strip()]
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
| 16 |
payload = {
|
| 17 |
"model": "gpt-4o-mini",
|
| 18 |
"messages": [
|
|
@@ -26,18 +29,39 @@ def call_azure_openai(filename, token):
|
|
| 26 |
headers = {
|
| 27 |
"Authorization": f"Bearer {token}",
|
| 28 |
"Content-Type": "application/json",
|
| 29 |
-
|
| 30 |
-
"User-Agent": "Mozilla/5.0"
|
| 31 |
}
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
@app.route('/')
|
| 37 |
def health_check():
|
| 38 |
return jsonify({
|
| 39 |
"status": "active",
|
| 40 |
-
"platform": "Hugging Face Space (AWS
|
| 41 |
"tokens_loaded": len(TOKENS)
|
| 42 |
})
|
| 43 |
|
|
@@ -51,51 +75,42 @@ def analyze():
|
|
| 51 |
|
| 52 |
last_error = ""
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
result = resp.json()
|
| 61 |
content = result['choices'][0]['message']['content']
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
# If it's valid stringified JSON, return it parsed
|
| 69 |
-
import json
|
| 70 |
-
try:
|
| 71 |
-
return jsonify(json.loads(clean_content))
|
| 72 |
-
except:
|
| 73 |
-
return jsonify({"raw_result": clean_content})
|
| 74 |
-
|
| 75 |
-
elif resp.status_code == 429:
|
| 76 |
-
last_error = "Rate Limited (429)"
|
| 77 |
-
continue # Try next token
|
| 78 |
-
else:
|
| 79 |
-
last_error = f"Error {resp.status_code}: {resp.text}"
|
| 80 |
|
| 81 |
-
|
| 82 |
-
last_error =
|
|
|
|
|
|
|
| 83 |
|
| 84 |
return jsonify({"error": "All tokens failed", "last_details": last_error}), 500
|
| 85 |
|
| 86 |
@app.route('/check-limit')
|
| 87 |
def check_limit():
|
| 88 |
results = []
|
| 89 |
-
url = "[https://models.inference.ai.azure.com/chat/completions](https://models.inference.ai.azure.com/chat/completions)"
|
| 90 |
|
|
|
|
| 91 |
for i, token in enumerate(TOKENS):
|
| 92 |
try:
|
| 93 |
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
|
| 94 |
-
resp = requests.post(
|
| 95 |
"model": "gpt-4o-mini",
|
| 96 |
"messages": [{"role":"user", "content":"ping"}],
|
| 97 |
"max_tokens": 1
|
| 98 |
-
})
|
|
|
|
| 99 |
results.append({
|
| 100 |
"token_index": i,
|
| 101 |
"status": resp.status_code,
|
|
|
|
| 1 |
import os
|
| 2 |
+
import time
|
| 3 |
import requests
|
| 4 |
from flask import Flask, request, jsonify
|
| 5 |
from flask_cors import CORS
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
+
CORS(app)
|
| 9 |
|
| 10 |
+
# Load Tokens
|
|
|
|
| 11 |
TOKENS_RAW = os.environ.get("AI_SERVICE_TOKEN", "")
|
| 12 |
TOKENS = [t.strip() for t in TOKENS_RAW.split(",") if t.strip()]
|
| 13 |
|
| 14 |
+
# Configuration
|
| 15 |
+
# NOTE: Ensure this is a clean string, NO brackets []
|
| 16 |
+
AZURE_URL = "https://models.inference.ai.azure.com/chat/completions"
|
| 17 |
+
|
| 18 |
+
def call_azure_openai_with_retry(filename, token, retries=3):
|
| 19 |
payload = {
|
| 20 |
"model": "gpt-4o-mini",
|
| 21 |
"messages": [
|
|
|
|
| 29 |
headers = {
|
| 30 |
"Authorization": f"Bearer {token}",
|
| 31 |
"Content-Type": "application/json",
|
| 32 |
+
"User-Agent": "Mozilla/5.0 (Standard Browser)"
|
|
|
|
| 33 |
}
|
| 34 |
|
| 35 |
+
# Retry Loop
|
| 36 |
+
for attempt in range(retries):
|
| 37 |
+
try:
|
| 38 |
+
response = requests.post(AZURE_URL, headers=headers, json=payload, timeout=15)
|
| 39 |
+
|
| 40 |
+
if response.status_code == 200:
|
| 41 |
+
return response
|
| 42 |
+
|
| 43 |
+
elif response.status_code == 429:
|
| 44 |
+
# If rate limited, wait and try again (Exponential backoff)
|
| 45 |
+
wait_time = (attempt + 1) * 2 # Wait 2s, then 4s, then 6s
|
| 46 |
+
print(f"Rate limited (429). Retrying in {wait_time}s...")
|
| 47 |
+
time.sleep(wait_time)
|
| 48 |
+
continue
|
| 49 |
+
|
| 50 |
+
else:
|
| 51 |
+
# Other errors (401, 500), don't retry, just return
|
| 52 |
+
return response
|
| 53 |
+
|
| 54 |
+
except Exception as e:
|
| 55 |
+
print(f"Connection error: {e}")
|
| 56 |
+
time.sleep(1)
|
| 57 |
+
|
| 58 |
+
return None # Failed after all retries
|
| 59 |
|
| 60 |
@app.route('/')
|
| 61 |
def health_check():
|
| 62 |
return jsonify({
|
| 63 |
"status": "active",
|
| 64 |
+
"platform": "Hugging Face Space (AWS)",
|
| 65 |
"tokens_loaded": len(TOKENS)
|
| 66 |
})
|
| 67 |
|
|
|
|
| 75 |
|
| 76 |
last_error = ""
|
| 77 |
|
| 78 |
+
for token in TOKENS:
|
| 79 |
+
# Use the retry function
|
| 80 |
+
resp = call_azure_openai_with_retry(filename, token)
|
| 81 |
+
|
| 82 |
+
if resp and resp.status_code == 200:
|
| 83 |
+
try:
|
| 84 |
result = resp.json()
|
| 85 |
content = result['choices'][0]['message']['content']
|
| 86 |
+
# Clean Markdown
|
| 87 |
+
clean = content.replace("```json", "").replace("```", "").strip()
|
| 88 |
+
import json
|
| 89 |
+
return jsonify(json.loads(clean))
|
| 90 |
+
except Exception as e:
|
| 91 |
+
return jsonify({"raw_result": content, "error": "JSON Parse Error"})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
+
elif resp:
|
| 94 |
+
last_error = f"Status {resp.status_code}: {resp.text}"
|
| 95 |
+
else:
|
| 96 |
+
last_error = "Connection Timeout/Error"
|
| 97 |
|
| 98 |
return jsonify({"error": "All tokens failed", "last_details": last_error}), 500
|
| 99 |
|
| 100 |
@app.route('/check-limit')
|
| 101 |
def check_limit():
|
| 102 |
results = []
|
|
|
|
| 103 |
|
| 104 |
+
# FIX: Uses the clean constant variable defined at the top
|
| 105 |
for i, token in enumerate(TOKENS):
|
| 106 |
try:
|
| 107 |
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
|
| 108 |
+
resp = requests.post(AZURE_URL, headers=headers, json={
|
| 109 |
"model": "gpt-4o-mini",
|
| 110 |
"messages": [{"role":"user", "content":"ping"}],
|
| 111 |
"max_tokens": 1
|
| 112 |
+
}, timeout=5)
|
| 113 |
+
|
| 114 |
results.append({
|
| 115 |
"token_index": i,
|
| 116 |
"status": resp.status_code,
|