BehradB2 commited on
Commit
dd10f6e
·
verified ·
1 Parent(s): 96bb0a8

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +85 -31
main.py CHANGED
@@ -4,30 +4,33 @@ from fastapi.middleware.cors import CORSMiddleware
4
  from pydantic import BaseModel
5
  import google.generativeai as genai
6
  import itertools
 
7
 
8
  # ==========================================
9
- # 1. لیست کلیدهای جمنای (هر چند تا داری اینجا بذار)
10
  # ==========================================
 
11
  API_KEYS = [
12
- "AIzaSyCaUj_SFJ9IY_THJ8nci8UnIotfCklO_Yc",
13
  "AIzaSyD-BeqfkCjjFtHPsp9ckUxkQIz5M2QBzMg",
14
  "AIzaSyCYfXgkmuOgPGNW5Nw3uL_gkCimNbzLt-g"
15
  ]
16
 
17
- # این دستور باعث میشه کلیدها به نوبت استفاده بشن (چرخشی)
18
  key_cycle = itertools.cycle(API_KEYS)
 
 
 
 
 
 
19
 
20
- # ==========================================
21
- # 2. تنظیمات و امنیت
22
- # ==========================================
23
- MY_SECRET_PASS = "Behrad_King_2026" # رمز شخصی تو
24
 
25
- app = FastAPI(title="Behrad AI Core - Multi Key", docs_url="/docs")
26
 
27
  app.add_middleware(
28
  CORSMiddleware,
29
  allow_origins=["*"],
30
- allow_credentials=True,
31
  allow_methods=["*"],
32
  allow_headers=["*"],
33
  )
@@ -35,47 +38,98 @@ app.add_middleware(
35
  class AIRequest(BaseModel):
36
  prompt: str
37
 
38
- # تابع چک کردن رمز
39
- async def verify_token(x_api_token: str = Header(...)):
40
- if x_api_token != MY_SECRET_PASS:
41
- raise HTTPException(status_code=400, detail="⛔ Access Denied: رمز اشتباه است!")
42
-
43
  # ==========================================
44
- # 3. ظاهر گرافیکی (هکری)
45
  # ==========================================
46
  @app.get("/", response_class=HTMLResponse)
47
- async def read_root():
48
  return """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  <html>
50
- <body style="background-color:black; color:#00ff41; text-align:center; font-family:monospace; padding-top:50px;">
51
- <h1>SYSTEM ONLINE (Multi-Key Mode)</h1>
52
- <p>Status: ACTIVE</p>
53
- <p>Owner: Behrad Besharati</p>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  </body>
55
  </html>
56
  """
57
 
58
  # ==========================================
59
- # 4. مغز اصلی (با قابلیت تعویض کلید)
60
  # ==========================================
61
- @app.post("/ask", dependencies=[Depends(verify_token)])
62
- async def ask_ai(request: AIRequest):
 
 
 
 
 
 
 
63
  try:
64
- # انتخاب کلید بعدی از لیست
65
- current_key = next(key_cycle)
66
-
67
- # تنظیم جمنای با کلید جدید
68
  genai.configure(api_key=current_key)
69
 
70
- # استفاده از مدل Pro که مطمئنیم کار میکنه
71
- model = genai.GenerativeModel('gemini-pro')
72
 
73
  response = model.generate_content(request.prompt)
74
 
 
75
  return {
76
  "status": "success",
77
  "answer": response.text,
78
- "key_used": current_key[:10] + "..." # (فقط برای تست: نشون میده کدوم کلید استفاده شد)
79
  }
 
80
  except Exception as e:
81
- return {"status": "error", "message": str(e)}
 
 
 
 
 
4
  from pydantic import BaseModel
5
  import google.generativeai as genai
6
  import itertools
7
+ import datetime
8
 
9
  # ==========================================
10
+ # ۱. مدیریت کلیدها و آمار (Behrad AI Core)
11
  # ==========================================
12
+ # لیست کلیدهای خود را اینجا قرار دهید
13
  API_KEYS = [
14
+ "AIzaSyCaUj_SFJ9IY_THJ8nci8UnIotfCklO_Yc",
15
  "AIzaSyD-BeqfkCjjFtHPsp9ckUxkQIz5M2QBzMg",
16
  "AIzaSyCYfXgkmuOgPGNW5Nw3uL_gkCimNbzLt-g"
17
  ]
18
 
 
19
  key_cycle = itertools.cycle(API_KEYS)
20
+ stats = {
21
+ "total_requests": 0,
22
+ "success_count": 0,
23
+ "error_count": 0,
24
+ "start_time": datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
25
+ }
26
 
27
+ MY_SECRET_PASS = "Behrad_King_2026" # رمز اختصاصی شما
 
 
 
28
 
29
+ app = FastAPI(title="Behrad AI Core V3.1")
30
 
31
  app.add_middleware(
32
  CORSMiddleware,
33
  allow_origins=["*"],
 
34
  allow_methods=["*"],
35
  allow_headers=["*"],
36
  )
 
38
  class AIRequest(BaseModel):
39
  prompt: str
40
 
 
 
 
 
 
41
  # ==========================================
42
+ # ۲. بخش عمومی (Apple Style UI)
43
  # ==========================================
44
  @app.get("/", response_class=HTMLResponse)
45
+ async def public_ui():
46
  return """
47
+ <!DOCTYPE html>
48
+ <html lang="fa" dir="rtl">
49
+ <head>
50
+ <meta charset="UTF-8">
51
+ <style>
52
+ body { background: #f5f5f7; font-family: -apple-system, sans-serif; display: flex; align-items: center; justify-content: center; height: 100vh; margin: 0; }
53
+ .glass { background: rgba(255, 255, 255, 0.8); backdrop-filter: blur(20px); border-radius: 40px; padding: 60px; box-shadow: 0 20px 50px rgba(0,0,0,0.05); text-align: center; border: 1px solid rgba(255,255,255,0.3); max-width: 500px; }
54
+ h1 { font-size: 52px; font-weight: 700; color: #1d1d1f; letter-spacing: -2px; margin-bottom: 10px; }
55
+ p { color: #86868b; font-size: 20px; line-height: 1.5; }
56
+ .status { display: inline-block; margin-top: 30px; padding: 8px 20px; background: #0071e3; color: white; border-radius: 20px; font-size: 14px; }
57
+ </style>
58
+ </head>
59
+ <body>
60
+ <div class="glass">
61
+ <h1>Behrad AI</h1>
62
+ <p>قدرت گرفته از جدیدترین نسخه Gemini Flash. سرعت و دقت در پروژه‌های هوشمند شما.</p>
63
+ <div class="status">سیستم آنلاین است</div>
64
+ </div>
65
+ </body>
66
+ </html>
67
+ """
68
+
69
+ # ==========================================
70
+ # ۳. پنل مدیریت (Engineering Dashboard)
71
+ # ==========================================
72
+ @app.get("/admin", response_class=HTMLResponse)
73
+ async def admin_ui():
74
+ return f"""
75
+ <!DOCTYPE html>
76
  <html>
77
+ <head>
78
+ <title>Admin Panel | Behrad AI</title>
79
+ <style>
80
+ body {{ background: #0a0a0a; color: #4af626; font-family: 'Courier New', monospace; padding: 30px; }}
81
+ .terminal {{ border: 1px solid #4af626; padding: 25px; border-radius: 10px; }}
82
+ .grid {{ display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 20px; margin: 30px 0; }}
83
+ .stat-box {{ border: 1px dashed #4af626; padding: 20px; text-align: center; }}
84
+ .model-tag {{ color: #fff; background: #d4413d; padding: 2px 8px; border-radius: 4px; font-size: 12px; }}
85
+ </style>
86
+ </head>
87
+ <body>
88
+ <div class="terminal">
89
+ <h2>> BEHRAD_AI_CORE_V3.1 <span class="model-tag">GEMINI-FLASH-LATEST</span></h2>
90
+ <p>SERVER_STATUS: ACTIVE | START_TIME: {stats['start_time']}</p>
91
+ <div class="grid">
92
+ <div class="stat-box">REQUESTS<div>{stats['total_requests']}</div></div>
93
+ <div class="stat-box">SUCCESS<div>{stats['success_count']}</div></div>
94
+ <div class="stat-box">ERRORS<div>{stats['error_count']}</div></div>
95
+ <div class="stat-box">KEYS<div>{len(API_KEYS)}</div></div>
96
+ </div>
97
+ <div style="font-size: 12px; opacity: 0.6;">> Listening on /ask for secure tokens...</div>
98
+ </div>
99
  </body>
100
  </html>
101
  """
102
 
103
  # ==========================================
104
+ # ۴. هسته پردازشی با مدل جدید
105
  # ==========================================
106
+ @app.post("/ask")
107
+ async def ask_ai(request: AIRequest, x_api_token: str = Header(...)):
108
+ if x_api_token != MY_SECRET_PASS:
109
+ stats["error_count"] += 1
110
+ raise HTTPException(status_code=403, detail="Access Denied")
111
+
112
+ stats["total_requests"] += 1
113
+ current_key = next(key_cycle)
114
+
115
  try:
 
 
 
 
116
  genai.configure(api_key=current_key)
117
 
118
+ # --- جایگزینی مدل درخواستی شما ---
119
+ model = genai.GenerativeModel('gemini-1.5-flash-latest')
120
 
121
  response = model.generate_content(request.prompt)
122
 
123
+ stats["success_count"] += 1
124
  return {
125
  "status": "success",
126
  "answer": response.text,
127
+ "engine": "gemini-flash-latest"
128
  }
129
+
130
  except Exception as e:
131
+ stats["error_count"] += 1
132
+ return {
133
+ "status": "error",
134
+ "message": str(e)
135
+ }