BehradB2 commited on
Commit
b3387f5
·
verified ·
1 Parent(s): 047cb6c

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +89 -89
main.py CHANGED
@@ -1,90 +1,90 @@
1
- from fastapi import FastAPI, HTTPException, Header, Depends
2
- 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 = os.environ.get("GEMINI_API_KEY")
15
-
16
- # 2. رمز عبور API تو (که فقط خودت و پروژه‌هات بتونن استفاده کنن)
17
- MY_SECRET_PASS = "Behrad_King_2026" # این رمز رو عوض کن!
18
-
19
- genai.configure(api_key=GEMINI_API_KEY)
20
- model = genai.GenerativeModel('gemini-1.5-flash')
21
-
22
- app = FastAPI(title="Behrad AI Core", docs_url="/docs")
23
-
24
- # تنظیمات CORS (آزادسازی برای همه پروژه‌ها)
25
- app.add_middleware(
26
- CORSMiddleware,
27
- allow_origins=["*"],
28
- allow_credentials=True,
29
- allow_methods=["*"],
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)}
 
1
+ from fastapi import FastAPI, HTTPException, Header, Depends
2
+ 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 = "AIzaSyDaFoAOqHlWu9nFdKISz6HifupnmtmZZUs"
15
+
16
+ # 2. رمز عبور API تو (که فقط خودت و پروژه‌هات بتونن استفاده کنن)
17
+ MY_SECRET_PASS = "Behrad_King_2026" # این رمز رو عوض کن!
18
+
19
+ genai.configure(api_key=GEMINI_API_KEY)
20
+ model = genai.GenerativeModel('gemini-1.5-flash')
21
+
22
+ app = FastAPI(title="Behrad AI Core", docs_url="/docs")
23
+
24
+ # تنظیمات CORS (آزادسازی برای همه پروژه‌ها)
25
+ app.add_middleware(
26
+ CORSMiddleware,
27
+ allow_origins=["*"],
28
+ allow_credentials=True,
29
+ allow_methods=["*"],
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)}