Behrad-AI-Core / main.py
BehradB2's picture
Update main.py
3cace07 verified
Raw History Blame Contribute Delete
3.53 kB
from fastapi import FastAPI, HTTPException, Header, Depends
from fastapi.responses import HTMLResponse
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
import google.generativeai as genai
import os
# ==========================================
# تنظیمات امنیتی و هوش مصنوعی
# ==========================================
# 1. کلید جمنای (این رو بعداً تو تنظیمات سرور وارد میکنیم که امن بمونه)
# اگر خواستی تست کنی، موقتاً کلیدت رو جای os.environ... بذار
# کلیدت رو مستقیم بذار اینجا (حواست باشه گیومه " " بذاری)
API_KEY = "AIzaSyD-7i8Y2iAiqNXJY1jklcGZNk6GdUsymOE"
# 2. رمز عبور API تو (که فقط خودت و پروژه‌هات بتونن استفاده کنن)
MY_SECRET_PASS = "Behrad_King_2026" # این رمز رو عوض کن!
genai.configure(api_key=GEMINI_API_KEY)
MODEL_NAME = "models/gemini-flash-latest"
app = FastAPI(title="Behrad AI Core", docs_url="/docs")
# تنظیمات CORS (آزادسازی برای همه پروژه‌ها)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# مدل ورودی
class AIRequest(BaseModel):
prompt: str
# تابع چک کردن رمز (Security Check)
async def verify_token(x_api_token: str = Header(...)):
if x_api_token != MY_SECRET_PASS:
raise HTTPException(status_code=400, detail="⛔ Access Denied: رمز اشتباه است!")
# ==========================================
# ظاهر گرافیکی (UI) - HTML/CSS
# ==========================================
@app.get("/", response_class=HTMLResponse)
async def read_root():
return """
<!DOCTYPE html>
<html>
<head>
<title>Behrad AI Server</title>
<style>
body { background-color: #0d1117; color: #00ff41; font-family: 'Courier New', monospace; text-align: center; padding-top: 50px; }
h1 { font-size: 3em; text-shadow: 0 0 10px #00ff41; }
.status { border: 2px solid #00ff41; padding: 20px; display: inline-block; border-radius: 10px; box-shadow: 0 0 20px #00ff41; }
.blink { animation: blinker 1.5s linear infinite; }
@keyframes blinker { 50% { opacity: 0; } }
p { font-size: 1.2em; }
</style>
</head>
<body>
<div class="status">
<h1>SYSTEM ONLINE</h1>
<p>Server Status: <span class="blink">ACTIVE</span></p>
<p>Owner: Behrad Besharati</p>
<p>Location: Secure Cloud (Anti-Sanction)</p>
<hr style="border-color: #00ff41;">
<p>برای استفاده از API به آدرس /docs بروید</p>
</div>
</body>
</html>
"""
# ==========================================
# اندپوینت اصلی (مغز هوش مصنوعی)
# ==========================================
@app.post("/ask", dependencies=[Depends(verify_token)])
async def ask_ai(request: AIRequest):
try:
if not GEMINI_API_KEY:
return {"error": "کلید جمنای تنظیم نشده است!"}
response = model.generate_content(request.prompt)
return {
"status": "success",
"answer": response.text,
"credits": "Generated by Behrad AI"
}
except Exception as e:
return {"status": "error", "message": str(e)}