BehradB2 commited on
Commit
7f8799e
·
verified ·
1 Parent(s): a8adfb3

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +41 -95
main.py CHANGED
@@ -7,128 +7,74 @@ import itertools
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,
26
- "error_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=["*"],
35
- allow_methods=["*"],
36
- allow_headers=["*"],
37
- )
38
 
39
  class AIRequest(BaseModel):
40
  prompt: str
41
 
42
  # ==========================================
43
- # ۲. ظاهر اپل (Public UI) - آدرس /
44
  # ==========================================
45
  @app.get("/", response_class=HTMLResponse)
46
  async def public_ui():
47
- return """
48
- <!DOCTYPE html>
49
- <html lang="fa" dir="rtl">
50
- <head>
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():
73
- return f"""
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
-
121
- stats["success_count"] += 1
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
- }
 
7
  import datetime
8
 
9
  # ==========================================
10
+ # ۱. تنظیمات و لیست کلیدها
11
  # ==========================================
12
+ # بهراد جان، حتماً تمام کلیدهای این لیست را با کلیدهای واقعی جایگزین کن
13
  API_KEYS = [
14
+ "AIzaSyBBRlkT-86gGwY9UvrYMSBmIetZK5Uu9xI",
 
15
  "AIzaSyD-7i8Y2iAiqNXJY1jklcGZNk6GdUsymOE",
16
+ "AIzaSyB2_RkZdEaVqiq7Qu3oMB1ya86cJHe0lLM"
17
  ]
18
 
19
  key_cycle = itertools.cycle(API_KEYS)
20
+ MY_SECRET_PASS = "Behrad_King_2026"
21
 
22
  stats = {
23
+ "total": 0, "ok": 0, "err": 0,
24
+ "start": datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
 
 
25
  }
26
 
27
+ app = FastAPI()
28
+ app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"])
 
 
 
 
 
 
29
 
30
  class AIRequest(BaseModel):
31
  prompt: str
32
 
33
  # ==========================================
34
+ # ۲. ظاهر سایت (بدون تغییر)
35
  # ==========================================
36
  @app.get("/", response_class=HTMLResponse)
37
  async def public_ui():
38
+ return '<body style="background:#f5f5f7;font-family:sans-serif;display:flex;align-items:center;justify-content:center;height:100vh;">' \
39
+ '<div style="background:white;padding:50px;border-radius:30px;box-shadow:0 10px 30px rgba(0,0,0,0.05);text-align:center;">' \
40
+ '<h1>Behrad AI Core</h1><p style="color:#888;">سیستم آماده پردازش است.</p></div></body>'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
 
 
 
42
  @app.get("/admin", response_class=HTMLResponse)
43
  async def admin_ui():
44
+ return f'<body style="background:#000;color:#0f0;font-family:monospace;padding:30px;">' \
45
+ f'<h2>[ ADMIN PANEL ]</h2><p>REQS: {stats["total"]} | OK: {stats["ok"]} | ERR: {stats["err"]}</p>' \
46
+ f'<p>Keys: {len(API_KEYS)}</p></body>'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
  # ==========================================
49
+ # ۳. هسته هوشمند (با قابلیت پرش از کلید خراب)
50
  # ==========================================
51
  @app.post("/ask")
52
  async def ask_ai(request: AIRequest, x_api_token: str = Header(...)):
53
  if x_api_token != MY_SECRET_PASS:
54
+ stats["err"] += 1
55
  raise HTTPException(status_code=403, detail="Access Denied")
56
 
57
+ stats["total"] += 1
 
58
 
59
+ # تلاش برای گرفتن جواب با چرخیدن بین کلیدها (تا ۳ بار تلاش)
60
+ for _ in range(len(API_KEYS)):
61
+ current_key = next(key_cycle)
62
+ try:
63
+ genai.configure(api_key=current_key)
64
+ # استفاده از مدل Pro به عنوان پایدارترین مدل
65
+ model = genai.GenerativeModel('gemini-pro')
66
+ response = model.generate_content(request.prompt)
67
+
68
+ stats["ok"] += 1
69
+ return {"status": "success", "answer": response.text, "key_used": current_key[:8]}
70
+
71
+ except Exception as e:
72
+ print(f"Key failed: {current_key[:8]} - Error: {str(e)}")
73
+ continue # اگر این کلید خطا داد، برو سراغ کلید بعدی
74
+
75
+ stats["err"] += 1
76
+ return {
77
+ "status": "error",
78
+ "message": "تمام کلیدهای شما با خطا مواجه شدند. لطفاً کلیدها را در Google AI Studio چک کنید.",
79
+ "debug": "All keys exhausted"
80
+ }