Spaces:
Runtime error
Runtime error
File size: 3,526 Bytes
01c1f99 b3387f5 01c1f99 b8c5653 3cace07 01c1f99 8044a63 01c1f99 3cace07 8ac314b 01c1f99 b3387f5 01c1f99 b3387f5 01c1f99 5fe8ee1 01c1f99 5fe8ee1 01c1f99 5fe8ee1 01c1f99 5fe8ee1 01c1f99 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | 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)} |