Spaces:
Runtime error
Runtime error
Update main.py
Browse files
main.py
CHANGED
|
@@ -7,22 +7,19 @@ import itertools
|
|
| 7 |
import datetime
|
| 8 |
|
| 9 |
# ==========================================
|
| 10 |
-
# ۱.
|
| 11 |
# ==========================================
|
| 12 |
-
# کلیدهای واقعی خودت را اینجا
|
| 13 |
API_KEYS = [
|
|
|
|
| 14 |
"AIzaSyB2_RkZdEaVqiq7Qu3oMB1ya86cJHe0lLM",
|
| 15 |
"AIzaSyD-7i8Y2iAiqNXJY1jklcGZNk6GdUsymOE",
|
| 16 |
"AIzaSyBBRlkT-86gGwY9UvrYMSBmIetZK5Uu9xI"
|
| 17 |
]
|
| 18 |
|
| 19 |
-
# ایجاد چرخه برای استفاده نوبتی از کلیدها
|
| 20 |
key_cycle = itertools.cycle(API_KEYS)
|
|
|
|
| 21 |
|
| 22 |
-
# رمز عبور اختصاصی بهراد
|
| 23 |
-
MY_SECRET_PASS = "Behrad_King_2026"
|
| 24 |
-
|
| 25 |
-
# آمارهای زنده برای پنل مدیریت
|
| 26 |
stats = {
|
| 27 |
"total_requests": 0,
|
| 28 |
"success_count": 0,
|
|
@@ -30,9 +27,8 @@ stats = {
|
|
| 30 |
"start_time": datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
|
| 31 |
}
|
| 32 |
|
| 33 |
-
app = FastAPI(title="Behrad AI Core
|
| 34 |
|
| 35 |
-
# آزاد کردن دسترسی برای تمام پروژههای تو (CORS)
|
| 36 |
app.add_middleware(
|
| 37 |
CORSMiddleware,
|
| 38 |
allow_origins=["*"],
|
|
@@ -44,7 +40,7 @@ class AIRequest(BaseModel):
|
|
| 44 |
prompt: str
|
| 45 |
|
| 46 |
# ==========================================
|
| 47 |
-
# ۲. ظاهر
|
| 48 |
# ==========================================
|
| 49 |
@app.get("/", response_class=HTMLResponse)
|
| 50 |
async def public_ui():
|
|
@@ -55,24 +51,22 @@ async def public_ui():
|
|
| 55 |
<meta charset="UTF-8">
|
| 56 |
<style>
|
| 57 |
body { background: #f5f5f7; font-family: -apple-system, sans-serif; display: flex; align-items: center; justify-content: center; height: 100vh; margin: 0; }
|
| 58 |
-
.glass
|
| 59 |
-
h1 { font-size:
|
| 60 |
-
p { color: #86868b; font-size: 20px;
|
| 61 |
-
.btn { display: inline-block; margin-top: 40px; padding: 12px 30px; background: #0071e3; color: white; border-radius: 25px; text-decoration: none; font-size: 15px; font-weight: 500; }
|
| 62 |
</style>
|
| 63 |
</head>
|
| 64 |
<body>
|
| 65 |
-
<div class="glass
|
| 66 |
<h1>Behrad AI</h1>
|
| 67 |
-
<p>
|
| 68 |
-
<a href="/docs" class="btn">مشاهده مستندات فنی</a>
|
| 69 |
</div>
|
| 70 |
</body>
|
| 71 |
</html>
|
| 72 |
"""
|
| 73 |
|
| 74 |
# ==========================================
|
| 75 |
-
# ۳. پنل
|
| 76 |
# ==========================================
|
| 77 |
@app.get("/admin", response_class=HTMLResponse)
|
| 78 |
async def admin_ui():
|
|
@@ -80,57 +74,47 @@ async def admin_ui():
|
|
| 80 |
<!DOCTYPE html>
|
| 81 |
<html>
|
| 82 |
<head>
|
| 83 |
-
<title>
|
| 84 |
<style>
|
| 85 |
-
body {{ background: #
|
| 86 |
-
.
|
| 87 |
-
.
|
| 88 |
-
.
|
| 89 |
-
.stat-box div {{ font-size: 32px; font-weight: bold; margin-top: 10px; }}
|
| 90 |
-
.badge {{ color: black; background: #4af626; padding: 2px 10px; border-radius: 4px; font-weight: bold; }}
|
| 91 |
</style>
|
| 92 |
</head>
|
| 93 |
<body>
|
| 94 |
-
<div class="
|
| 95 |
-
<h2>[
|
| 96 |
-
<
|
| 97 |
-
|
| 98 |
-
<div class="
|
| 99 |
-
<div class="
|
| 100 |
-
<div class="
|
| 101 |
-
<div class="stat-box">KEYS<div>{len(API_KEYS)}</div></div>
|
| 102 |
-
</div>
|
| 103 |
-
<div style="margin-top: 20px; font-size: 13px; opacity: 0.7;">
|
| 104 |
-
> Start Time: {stats['start_time']}<br>
|
| 105 |
-
> Auto-rotation: ENABLED<br>
|
| 106 |
-
> Security Layer: x-api-token active
|
| 107 |
</div>
|
|
|
|
| 108 |
</div>
|
| 109 |
</body>
|
| 110 |
</html>
|
| 111 |
"""
|
| 112 |
|
| 113 |
# ==========================================
|
| 114 |
-
# ۴. هسته
|
| 115 |
# ==========================================
|
| 116 |
@app.post("/ask")
|
| 117 |
async def ask_ai(request: AIRequest, x_api_token: str = Header(...)):
|
| 118 |
-
# بررسی امنیت
|
| 119 |
if x_api_token != MY_SECRET_PASS:
|
| 120 |
stats["error_count"] += 1
|
| 121 |
-
raise HTTPException(status_code=403, detail="
|
| 122 |
|
| 123 |
stats["total_requests"] += 1
|
| 124 |
-
|
| 125 |
-
# انتخاب کلید بعدی از چرخه برای دور زدن محدودیت
|
| 126 |
current_key = next(key_cycle)
|
| 127 |
|
| 128 |
try:
|
| 129 |
-
# پیکربندی
|
| 130 |
genai.configure(api_key=current_key)
|
| 131 |
|
| 132 |
-
# استفاده از م
|
| 133 |
-
model = genai.GenerativeModel('
|
| 134 |
|
| 135 |
response = model.generate_content(request.prompt)
|
| 136 |
|
|
@@ -138,13 +122,13 @@ async def ask_ai(request: AIRequest, x_api_token: str = Header(...)):
|
|
| 138 |
return {
|
| 139 |
"status": "success",
|
| 140 |
"answer": response.text,
|
| 141 |
-
"
|
| 142 |
}
|
| 143 |
|
| 144 |
except Exception as e:
|
| 145 |
stats["error_count"] += 1
|
| 146 |
return {
|
| 147 |
"status": "error",
|
| 148 |
-
"message": "سیستم
|
| 149 |
"debug": str(e)
|
| 150 |
}
|
|
|
|
| 7 |
import datetime
|
| 8 |
|
| 9 |
# ==========================================
|
| 10 |
+
# ۱. سیستم چرخشی کلیدها (Multi-Key)
|
| 11 |
# ==========================================
|
| 12 |
+
# کلیدهای واقعی خودت را اینجا بگذار
|
| 13 |
API_KEYS = [
|
| 14 |
+
|
| 15 |
"AIzaSyB2_RkZdEaVqiq7Qu3oMB1ya86cJHe0lLM",
|
| 16 |
"AIzaSyD-7i8Y2iAiqNXJY1jklcGZNk6GdUsymOE",
|
| 17 |
"AIzaSyBBRlkT-86gGwY9UvrYMSBmIetZK5Uu9xI"
|
| 18 |
]
|
| 19 |
|
|
|
|
| 20 |
key_cycle = itertools.cycle(API_KEYS)
|
| 21 |
+
MY_SECRET_PASS = "Behrad_King_2026" # رمز اختصاصی بهراد
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
stats = {
|
| 24 |
"total_requests": 0,
|
| 25 |
"success_count": 0,
|
|
|
|
| 27 |
"start_time": datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
|
| 28 |
}
|
| 29 |
|
| 30 |
+
app = FastAPI(title="Behrad AI Core - Stable Flash")
|
| 31 |
|
|
|
|
| 32 |
app.add_middleware(
|
| 33 |
CORSMiddleware,
|
| 34 |
allow_origins=["*"],
|
|
|
|
| 40 |
prompt: str
|
| 41 |
|
| 42 |
# ==========================================
|
| 43 |
+
# ۲. ظاهر اپل (Public UI) - آدرس /
|
| 44 |
# ==========================================
|
| 45 |
@app.get("/", response_class=HTMLResponse)
|
| 46 |
async def public_ui():
|
|
|
|
| 51 |
<meta charset="UTF-8">
|
| 52 |
<style>
|
| 53 |
body { background: #f5f5f7; font-family: -apple-system, sans-serif; display: flex; align-items: center; justify-content: center; height: 100vh; margin: 0; }
|
| 54 |
+
.glass { background: rgba(255, 255, 255, 0.7); backdrop-filter: blur(20px); border-radius: 40px; padding: 60px; box-shadow: 0 20px 50px rgba(0,0,0,0.05); text-align: center; max-width: 500px; }
|
| 55 |
+
h1 { font-size: 52px; font-weight: 700; color: #1d1d1f; letter-spacing: -2px; }
|
| 56 |
+
p { color: #86868b; font-size: 20px; margin-top: 15px; }
|
|
|
|
| 57 |
</style>
|
| 58 |
</head>
|
| 59 |
<body>
|
| 60 |
+
<div class="glass">
|
| 61 |
<h1>Behrad AI</h1>
|
| 62 |
+
<p>سرویس هوشمند بهراد با مدل Gemini 1.5 Flash. پرقدرت و سریع.</p>
|
|
|
|
| 63 |
</div>
|
| 64 |
</body>
|
| 65 |
</html>
|
| 66 |
"""
|
| 67 |
|
| 68 |
# ==========================================
|
| 69 |
+
# ۳. پنل ادمین (Engineering UI) - آدرس /admin
|
| 70 |
# ==========================================
|
| 71 |
@app.get("/admin", response_class=HTMLResponse)
|
| 72 |
async def admin_ui():
|
|
|
|
| 74 |
<!DOCTYPE html>
|
| 75 |
<html>
|
| 76 |
<head>
|
| 77 |
+
<title>Admin Panel</title>
|
| 78 |
<style>
|
| 79 |
+
body {{ background: #0a0a0a; color: #4af626; font-family: monospace; padding: 40px; }}
|
| 80 |
+
.terminal {{ border: 1px solid #4af626; padding: 25px; border-radius: 10px; }}
|
| 81 |
+
.stat-grid {{ display: grid; grid-template-columns: repeat(4, 1fr); gap: 20px; margin-top: 20px; }}
|
| 82 |
+
.box {{ border: 1px dashed #4af626; padding: 15px; text-align: center; }}
|
|
|
|
|
|
|
| 83 |
</style>
|
| 84 |
</head>
|
| 85 |
<body>
|
| 86 |
+
<div class="terminal">
|
| 87 |
+
<h2>[ BEHRAD_SYSTEM_MONITOR ]</h2>
|
| 88 |
+
<div class="stat-grid">
|
| 89 |
+
<div class="box">REQS<div>{stats['total_requests']}</div></div>
|
| 90 |
+
<div class="box">OK<div>{stats['success_count']}</div></div>
|
| 91 |
+
<div class="box">ERR<div>{stats['error_count']}</div></div>
|
| 92 |
+
<div class="box">KEYS<div>{len(API_KEYS)}</div></div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
</div>
|
| 94 |
+
<p style="font-size: 12px; margin-top: 20px;">> Running: gemini-1.5-flash</p>
|
| 95 |
</div>
|
| 96 |
</body>
|
| 97 |
</html>
|
| 98 |
"""
|
| 99 |
|
| 100 |
# ==========================================
|
| 101 |
+
# ۴. هسته اصلی (بدون ارور 404)
|
| 102 |
# ==========================================
|
| 103 |
@app.post("/ask")
|
| 104 |
async def ask_ai(request: AIRequest, x_api_token: str = Header(...)):
|
|
|
|
| 105 |
if x_api_token != MY_SECRET_PASS:
|
| 106 |
stats["error_count"] += 1
|
| 107 |
+
raise HTTPException(status_code=403, detail="Access Denied")
|
| 108 |
|
| 109 |
stats["total_requests"] += 1
|
|
|
|
|
|
|
| 110 |
current_key = next(key_cycle)
|
| 111 |
|
| 112 |
try:
|
| 113 |
+
# پیکربندی با کلید انتخابی
|
| 114 |
genai.configure(api_key=current_key)
|
| 115 |
|
| 116 |
+
# استفاده از نام استاندارد برای جلوگیری از 404
|
| 117 |
+
model = genai.GenerativeModel('gemini-1.5-flash')
|
| 118 |
|
| 119 |
response = model.generate_content(request.prompt)
|
| 120 |
|
|
|
|
| 122 |
return {
|
| 123 |
"status": "success",
|
| 124 |
"answer": response.text,
|
| 125 |
+
"info": "Model: Gemini 1.5 Flash"
|
| 126 |
}
|
| 127 |
|
| 128 |
except Exception as e:
|
| 129 |
stats["error_count"] += 1
|
| 130 |
return {
|
| 131 |
"status": "error",
|
| 132 |
+
"message": "خطای سیستمی رخ داد. لطفاً دوباره تلاش کنید.",
|
| 133 |
"debug": str(e)
|
| 134 |
}
|