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

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +32 -41
main.py CHANGED
@@ -3,25 +3,27 @@ from fastapi.responses import HTMLResponse
3
  from fastapi.middleware.cors import CORSMiddleware
4
  from pydantic import BaseModel
5
  import google.generativeai as genai
6
- import os
7
 
8
  # ==========================================
9
- # تنظیمات امنیتی و هوش مصنوعی
10
  # ==========================================
 
 
 
 
 
11
 
12
- # 1. کلید جمنای (این رو بعداً تو تنظیمات سرور وارد میکنیم که امن بمونه)
13
- # اگر خواستی تست کنی، موقتاً کلیدت رو جای os.environ... بذار
14
- GEMINI_API_KEY = "AIzaSyAQ3YmLkTJKinO6Tbg9Hsi3883HBgR7j1Q"
15
 
16
- # 2. رمز عبور API تو (که فقط خودت و پروژه‌هات بتونن استفاده کنن)
17
- MY_SECRET_PASS = "Behrad_King_2026" # این رمز رو عوض کن!
18
-
19
- genai.configure(api_key=GEMINI_API_KEY)
20
- model = genai.GenerativeModel('models/gemini-flash-latest')
21
 
22
- app = FastAPI(title="Behrad AI Core", docs_url="/docs")
23
 
24
- # تنظیمات CORS (آزادسازی برای همه پروژه‌ها)
25
  app.add_middleware(
26
  CORSMiddleware,
27
  allow_origins=["*"],
@@ -30,61 +32,50 @@ app.add_middleware(
30
  allow_headers=["*"],
31
  )
32
 
33
- # مدل ورودی
34
  class AIRequest(BaseModel):
35
  prompt: str
36
 
37
- # تابع چک کردن رمز (Security Check)
38
  async def verify_token(x_api_token: str = Header(...)):
39
  if x_api_token != MY_SECRET_PASS:
40
  raise HTTPException(status_code=400, detail="⛔ Access Denied: رمز اشتباه است!")
41
 
42
  # ==========================================
43
- # ظاهر گرافیکی (UI) - HTML/CSS
44
  # ==========================================
45
  @app.get("/", response_class=HTMLResponse)
46
  async def read_root():
47
  return """
48
- <!DOCTYPE html>
49
  <html>
50
- <head>
51
- <title>Behrad AI Server</title>
52
- <style>
53
- body { background-color: #0d1117; color: #00ff41; font-family: 'Courier New', monospace; text-align: center; padding-top: 50px; }
54
- h1 { font-size: 3em; text-shadow: 0 0 10px #00ff41; }
55
- .status { border: 2px solid #00ff41; padding: 20px; display: inline-block; border-radius: 10px; box-shadow: 0 0 20px #00ff41; }
56
- .blink { animation: blinker 1.5s linear infinite; }
57
- @keyframes blinker { 50% { opacity: 0; } }
58
- p { font-size: 1.2em; }
59
- </style>
60
- </head>
61
- <body>
62
- <div class="status">
63
- <h1>SYSTEM ONLINE</h1>
64
- <p>Server Status: <span class="blink">ACTIVE</span></p>
65
- <p>Owner: Behrad Besharati</p>
66
- <p>Location: Secure Cloud (Anti-Sanction)</p>
67
- <hr style="border-color: #00ff41;">
68
- <p>برای استفاده از API به آدرس /docs بروید</p>
69
- </div>
70
  </body>
71
  </html>
72
  """
73
 
74
  # ==========================================
75
- # اندپوینت اصلی (مغز هوش مصنوعی)
76
  # ==========================================
77
  @app.post("/ask", dependencies=[Depends(verify_token)])
78
  async def ask_ai(request: AIRequest):
79
  try:
80
- if not GEMINI_API_KEY:
81
- return {"error": "کلید جمنای تنظیم نشده است!"}
82
-
 
 
 
 
 
 
83
  response = model.generate_content(request.prompt)
 
84
  return {
85
  "status": "success",
86
  "answer": response.text,
87
- "credits": "Generated by Behrad AI"
88
  }
89
  except Exception as e:
90
  return {"status": "error", "message": str(e)}
 
3
  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=["*"],
 
32
  allow_headers=["*"],
33
  )
34
 
 
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)}