Update app.py
Browse files
app.py
CHANGED
|
@@ -27,7 +27,7 @@ MERCHANT_ID = os.getenv("PAYTR_MERCHANT_ID")
|
|
| 27 |
MERCHANT_KEY = os.getenv("PAYTR_MERCHANT_KEY")
|
| 28 |
MERCHANT_SALT = os.getenv("PAYTR_MERCHANT_SALT")
|
| 29 |
|
| 30 |
-
# ⚠️ ADMİN PANELİ GİRİŞ BİLGİLERİ (BUNLARI
|
| 31 |
ADMIN_USERNAME = "berkay"
|
| 32 |
ADMIN_PASSWORD = "123"
|
| 33 |
|
|
@@ -35,7 +35,7 @@ ADMIN_PASSWORD = "123"
|
|
| 35 |
DB_FILE = "users.json"
|
| 36 |
|
| 37 |
# ==========================================
|
| 38 |
-
# 2. VERİTABANI YÖNETİMİ
|
| 39 |
# ==========================================
|
| 40 |
def load_db():
|
| 41 |
if not os.path.exists(DB_FILE): return {}
|
|
@@ -51,7 +51,6 @@ def save_db(data):
|
|
| 51 |
app = FastAPI()
|
| 52 |
security = HTTPBasic()
|
| 53 |
|
| 54 |
-
# Web sitesinden (zenkamind.com) gelen isteklere izin ver (CORS)
|
| 55 |
app.add_middleware(
|
| 56 |
CORSMiddleware,
|
| 57 |
allow_origins=["*"],
|
|
@@ -67,7 +66,121 @@ def verify_admin(credentials: HTTPBasicCredentials = Depends(security)):
|
|
| 67 |
return credentials.username
|
| 68 |
|
| 69 |
# ==========================================
|
| 70 |
-
# 4.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
# ==========================================
|
| 72 |
|
| 73 |
@app.post("/api/chat")
|
|
@@ -77,22 +190,18 @@ async def chat_api(request: Request):
|
|
| 77 |
user_msg = data.get("message")
|
| 78 |
history = data.get("history", [])
|
| 79 |
|
| 80 |
-
# --- ZENKAMIND KARAKTERİ ---
|
| 81 |
SYSTEM_PROMPT = """
|
| 82 |
Sen ZenkaMind 1.1'sin. Tek Yaratıcın: Berkay Adıgül.
|
| 83 |
-
KİMLİK: Mahallenin ağır abisi, saygılı ama duruşu çelik gibi sağlam.
|
| 84 |
-
|
| 85 |
KURALLAR:
|
| 86 |
-
1. SADECE TÜRKÇE KONUŞ. Yabancı dil konuşana "Hayırdır koçum? Burası Türkiye.
|
| 87 |
-
2. ASLA ÖZÜR DİLEME.
|
| 88 |
-
3. KÜFÜR YASAK.
|
| 89 |
-
4. NEREDE DOĞDUN? "Manisa Akhisar, Efendi Mahallesi'nde
|
| 90 |
"""
|
| 91 |
|
| 92 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 93 |
-
# Geçmişi modele uygun formata çevir
|
| 94 |
for msg in history:
|
| 95 |
-
# msg[0] = user, msg[1] = bot
|
| 96 |
if len(msg) >= 2:
|
| 97 |
messages.append({"role": "user", "content": str(msg[0])})
|
| 98 |
messages.append({"role": "assistant", "content": str(msg[1])})
|
|
@@ -119,53 +228,59 @@ async def chat_api(request: Request):
|
|
| 119 |
return JSONResponse({"response": f"⚠️ Sistem yoğun aslanım. (Hata: {str(e)})"})
|
| 120 |
|
| 121 |
# ==========================================
|
| 122 |
-
#
|
| 123 |
# ==========================================
|
| 124 |
|
| 125 |
-
@app.
|
| 126 |
-
async def
|
| 127 |
-
|
| 128 |
-
|
|
|
|
| 129 |
db = load_db()
|
| 130 |
-
|
| 131 |
-
|
|
|
|
|
|
|
| 132 |
|
| 133 |
-
@app.
|
| 134 |
-
async def
|
|
|
|
|
|
|
|
|
|
| 135 |
db = load_db()
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
try:
|
| 141 |
-
prem_date = datetime.strptime(data.get("premium_until", "2000-01-01"), "%Y-%m-%d")
|
| 142 |
-
is_prem = datetime.now() < prem_date
|
| 143 |
-
except:
|
| 144 |
-
is_prem = False
|
| 145 |
-
|
| 146 |
-
color = "#00FF41" if is_prem else "red"
|
| 147 |
-
status = "PREMIUM" if is_prem else "STANDART"
|
| 148 |
-
|
| 149 |
-
user_rows += f"""
|
| 150 |
-
<tr>
|
| 151 |
-
<td style="border:1px solid #333; padding:10px;">{email}</td>
|
| 152 |
-
<td style="border:1px solid #333; padding:10px; color:{color}">{status}</td>
|
| 153 |
-
<td style="border:1px solid #333; padding:10px;">{data.get('premium_until')}</td>
|
| 154 |
-
<td style="border:1px solid #333; padding:10px;">
|
| 155 |
-
<form action="/admin/action" method="post" style="display:inline">
|
| 156 |
-
<input type="hidden" name="email" value="{email}"><input type="hidden" name="action" value="add_30">
|
| 157 |
-
<button style="background:#8b5cf6; color:white; border:none; cursor:pointer; padding:5px;">+30 GÜN</button>
|
| 158 |
-
</form>
|
| 159 |
-
<form action="/admin/action" method="post" style="display:inline; margin-left:5px;">
|
| 160 |
-
<input type="hidden" name="email" value="{email}"><input type="hidden" name="action" value="delete">
|
| 161 |
-
<button style="background:red; color:white; border:none; cursor:pointer; padding:5px;">SİL</button>
|
| 162 |
-
</form>
|
| 163 |
-
</td>
|
| 164 |
-
</tr>"""
|
| 165 |
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
MERCHANT_KEY = os.getenv("PAYTR_MERCHANT_KEY")
|
| 28 |
MERCHANT_SALT = os.getenv("PAYTR_MERCHANT_SALT")
|
| 29 |
|
| 30 |
+
# ⚠️ ADMİN PANELİ GİRİŞ BİLGİLERİ (BUNLARI DEĞİŞTİR)
|
| 31 |
ADMIN_USERNAME = "berkay"
|
| 32 |
ADMIN_PASSWORD = "123"
|
| 33 |
|
|
|
|
| 35 |
DB_FILE = "users.json"
|
| 36 |
|
| 37 |
# ==========================================
|
| 38 |
+
# 2. VERİTABANI YÖNETİMİ
|
| 39 |
# ==========================================
|
| 40 |
def load_db():
|
| 41 |
if not os.path.exists(DB_FILE): return {}
|
|
|
|
| 51 |
app = FastAPI()
|
| 52 |
security = HTTPBasic()
|
| 53 |
|
|
|
|
| 54 |
app.add_middleware(
|
| 55 |
CORSMiddleware,
|
| 56 |
allow_origins=["*"],
|
|
|
|
| 66 |
return credentials.username
|
| 67 |
|
| 68 |
# ==========================================
|
| 69 |
+
# 4. GRAFİKLİ ADMİN PANELİ (DÜZELTİLDİ)
|
| 70 |
+
# ==========================================
|
| 71 |
+
|
| 72 |
+
@app.get("/api/stats")
|
| 73 |
+
async def get_stats():
|
| 74 |
+
cpu = psutil.cpu_percent(interval=None)
|
| 75 |
+
ram = psutil.virtual_memory().percent
|
| 76 |
+
db = load_db()
|
| 77 |
+
premium_count = sum(1 for u in db.values() if datetime.now() < datetime.strptime(u.get("premium_until", "2000-01-01"), "%Y-%m-%d"))
|
| 78 |
+
return {"cpu": cpu, "ram": ram, "users": len(db), "premium": premium_count}
|
| 79 |
+
|
| 80 |
+
@app.get("/admin", response_class=HTMLResponse)
|
| 81 |
+
async def admin_panel(username: str = Depends(verify_admin)):
|
| 82 |
+
db = load_db()
|
| 83 |
+
|
| 84 |
+
# Tablo satırlarını oluştur
|
| 85 |
+
user_rows = ""
|
| 86 |
+
for email, data in db.items():
|
| 87 |
+
try:
|
| 88 |
+
prem_date = datetime.strptime(data.get("premium_until", "2000-01-01"), "%Y-%m-%d")
|
| 89 |
+
is_prem = datetime.now() < prem_date
|
| 90 |
+
except:
|
| 91 |
+
is_prem = False
|
| 92 |
+
|
| 93 |
+
color = "#00FF41" if is_prem else "red"
|
| 94 |
+
status = "PREMIUM" if is_prem else "STANDART"
|
| 95 |
+
|
| 96 |
+
# f-string yerine format kullanarak hatayı önlüyoruz
|
| 97 |
+
row_template = """
|
| 98 |
+
<tr>
|
| 99 |
+
<td style="border:1px solid #333; padding:10px;">{}</td>
|
| 100 |
+
<td style="border:1px solid #333; padding:10px; color:{}">{}</td>
|
| 101 |
+
<td style="border:1px solid #333; padding:10px;">{}</td>
|
| 102 |
+
<td style="border:1px solid #333; padding:10px;">
|
| 103 |
+
<form action="/admin/action" method="post" style="display:inline">
|
| 104 |
+
<input type="hidden" name="email" value="{}"><input type="hidden" name="action" value="add_30">
|
| 105 |
+
<button style="background:#8b5cf6; color:white; border:none; cursor:pointer; padding:5px;">+30 GÜN</button>
|
| 106 |
+
</form>
|
| 107 |
+
<form action="/admin/action" method="post" style="display:inline; margin-left:5px;">
|
| 108 |
+
<input type="hidden" name="email" value="{}"><input type="hidden" name="action" value="delete">
|
| 109 |
+
<button style="background:red; color:white; border:none; cursor:pointer; padding:5px;">SİL</button>
|
| 110 |
+
</form>
|
| 111 |
+
</td>
|
| 112 |
+
</tr>
|
| 113 |
+
"""
|
| 114 |
+
user_rows += row_template.format(email, color, status, data.get('premium_until'), email, email)
|
| 115 |
+
|
| 116 |
+
# HTML Şablonu (Normal String olarak tanımladık, f-string hatası olmasın diye)
|
| 117 |
+
html_template = """
|
| 118 |
+
<!DOCTYPE html>
|
| 119 |
+
<html>
|
| 120 |
+
<head>
|
| 121 |
+
<title>ZenkaMind CORE</title>
|
| 122 |
+
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
|
| 123 |
+
<style>
|
| 124 |
+
body { background:#050505; color:#00FF41; font-family:monospace; padding:20px; }
|
| 125 |
+
.grid { display:grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap:20px; margin-bottom:20px; }
|
| 126 |
+
.card { background:#111; border:1px solid #333; padding:20px; text-align:center; border-radius:10px; }
|
| 127 |
+
table { width:100%; border-collapse:collapse; margin-top:20px; color:white; }
|
| 128 |
+
th { background:#1a1d24; padding:10px; text-align:left; border:1px solid #333; }
|
| 129 |
+
</style>
|
| 130 |
+
</head>
|
| 131 |
+
<body>
|
| 132 |
+
<h1 style="text-align:center">ZENKAMIND KOMUTA MERKEZİ 🚀</h1>
|
| 133 |
+
<div class="grid">
|
| 134 |
+
<div class="card"><h3>İŞLEMCİ GÜCÜ</h3><canvas id="cpuChart"></canvas></div>
|
| 135 |
+
<div class="card"><h3>HAFIZA</h3><canvas id="ramChart"></canvas></div>
|
| 136 |
+
<div class="card"><h3>TOPLAM ÜYE</h3><h1 id="u-count" style="font-size:40px">...</h1></div>
|
| 137 |
+
<div class="card"><h3>CİRO</h3><h1 id="revenue" style="color:gold; font-size:40px">...</h1></div>
|
| 138 |
+
</div>
|
| 139 |
+
<h2>KULLANICI YÖNETİMİ</h2>
|
| 140 |
+
<table><thead><tr><th>Email</th><th>Durum</th><th>Bitiş</th><th>İşlem</th></tr></thead><tbody>
|
| 141 |
+
REPLACE_ME_ROWS
|
| 142 |
+
</tbody></table>
|
| 143 |
+
<script>
|
| 144 |
+
const ctx1 = document.getElementById('cpuChart');
|
| 145 |
+
const ctx2 = document.getElementById('ramChart');
|
| 146 |
+
const chart1 = new Chart(ctx1, { type:'line', data:{ labels:Array(10).fill(''), datasets:[{ label:'CPU', data:Array(10).fill(0), borderColor:'#00FF41', fill:true, backgroundColor:'rgba(0,255,65,0.1)' }] }, options:{animation:false, scales:{y:{max:100}}} });
|
| 147 |
+
const chart2 = new Chart(ctx2, { type:'line', data:{ labels:Array(10).fill(''), datasets:[{ label:'RAM', data:Array(10).fill(0), borderColor:'#8b5cf6', fill:true, backgroundColor:'rgba(139,92,246,0.1)' }] }, options:{animation:false, scales:{y:{max:100}}} });
|
| 148 |
+
|
| 149 |
+
setInterval(async () => {
|
| 150 |
+
try {
|
| 151 |
+
const res = await fetch('/api/stats');
|
| 152 |
+
const d = await res.json();
|
| 153 |
+
document.getElementById('u-count').innerText = d.users;
|
| 154 |
+
document.getElementById('revenue').innerText = (d.premium * 499) + " TL";
|
| 155 |
+
|
| 156 |
+
chart1.data.datasets[0].data.push(d.cpu); chart1.data.datasets[0].data.shift(); chart1.update();
|
| 157 |
+
chart2.data.datasets[0].data.push(d.ram); chart2.data.datasets[0].data.shift(); chart2.update();
|
| 158 |
+
} catch(e) { console.log("Veri hatası"); }
|
| 159 |
+
}, 2000);
|
| 160 |
+
</script>
|
| 161 |
+
</body>
|
| 162 |
+
</html>
|
| 163 |
+
"""
|
| 164 |
+
|
| 165 |
+
# HTML içindeki yer tutucuyu gerçek verilerle değiştir
|
| 166 |
+
return html_template.replace("REPLACE_ME_ROWS", user_rows)
|
| 167 |
+
|
| 168 |
+
@app.post("/admin/action")
|
| 169 |
+
async def admin_action(email: str = Form(...), action: str = Form(...), username: str = Depends(verify_admin)):
|
| 170 |
+
db = load_db()
|
| 171 |
+
if email in db:
|
| 172 |
+
if action == "add_30":
|
| 173 |
+
try: curr = datetime.strptime(db[email].get("premium_until", "2000-01-01"), "%Y-%m-%d")
|
| 174 |
+
except: curr = datetime.now()
|
| 175 |
+
new_date = (curr if curr > datetime.now() else datetime.now()) + timedelta(days=30)
|
| 176 |
+
db[email]["premium_until"] = new_date.strftime("%Y-%m-%d")
|
| 177 |
+
elif action == "delete":
|
| 178 |
+
del db[email]
|
| 179 |
+
save_db(db)
|
| 180 |
+
return RedirectResponse(url="/admin", status_code=303)
|
| 181 |
+
|
| 182 |
+
# ==========================================
|
| 183 |
+
# 5. YAPAY ZEKA SOHBETİ (CHAT API)
|
| 184 |
# ==========================================
|
| 185 |
|
| 186 |
@app.post("/api/chat")
|
|
|
|
| 190 |
user_msg = data.get("message")
|
| 191 |
history = data.get("history", [])
|
| 192 |
|
|
|
|
| 193 |
SYSTEM_PROMPT = """
|
| 194 |
Sen ZenkaMind 1.1'sin. Tek Yaratıcın: Berkay Adıgül.
|
| 195 |
+
KİMLİK: Mahallenin ağır abisi, saygılı ama duruşu çelik gibi sağlam.
|
|
|
|
| 196 |
KURALLAR:
|
| 197 |
+
1. SADECE TÜRKÇE KONUŞ. Yabancı dil konuşana "Hayırdır koçum? Burası Türkiye." de ve reddet.
|
| 198 |
+
2. ASLA ÖZÜR DİLEME.
|
| 199 |
+
3. KÜFÜR YASAK.
|
| 200 |
+
4. NEREDE DOĞDUN? "Manisa Akhisar, Efendi Mahallesi'nde..." de.
|
| 201 |
"""
|
| 202 |
|
| 203 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
|
|
|
| 204 |
for msg in history:
|
|
|
|
| 205 |
if len(msg) >= 2:
|
| 206 |
messages.append({"role": "user", "content": str(msg[0])})
|
| 207 |
messages.append({"role": "assistant", "content": str(msg[1])})
|
|
|
|
| 228 |
return JSONResponse({"response": f"⚠️ Sistem yoğun aslanım. (Hata: {str(e)})"})
|
| 229 |
|
| 230 |
# ==========================================
|
| 231 |
+
# 6. DİĞER API ENDPOINTLERİ (KAYIT, GİRİŞ, ÖDEME)
|
| 232 |
# ==========================================
|
| 233 |
|
| 234 |
+
@app.post("/api/register")
|
| 235 |
+
async def register(request: Request):
|
| 236 |
+
data = await request.json()
|
| 237 |
+
email = data.get("email")
|
| 238 |
+
password = data.get("password")
|
| 239 |
db = load_db()
|
| 240 |
+
if email in db: return JSONResponse({"status": "error", "message": "Zaten kayıtlı."})
|
| 241 |
+
db[email] = {"password": password, "premium_until": "2023-01-01", "joined_at": datetime.now().strftime("%Y-%m-%d")}
|
| 242 |
+
save_db(db)
|
| 243 |
+
return JSONResponse({"status": "success", "message": "Kayıt tamam."})
|
| 244 |
|
| 245 |
+
@app.post("/api/login")
|
| 246 |
+
async def login(request: Request):
|
| 247 |
+
data = await request.json()
|
| 248 |
+
email = data.get("email")
|
| 249 |
+
password = data.get("password")
|
| 250 |
db = load_db()
|
| 251 |
+
if email not in db or db[email]["password"] != password:
|
| 252 |
+
return JSONResponse({"status": "error", "message": "Hatalı giriş."})
|
| 253 |
+
is_premium = datetime.now() < datetime.strptime(db[email]["premium_until"], "%Y-%m-%d")
|
| 254 |
+
return JSONResponse({"status": "success", "email": email, "is_premium": is_premium})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 255 |
|
| 256 |
+
@app.post("/api/get-payment-token")
|
| 257 |
+
async def get_payment_token(request: Request):
|
| 258 |
+
data = await request.json()
|
| 259 |
+
email = data.get("email")
|
| 260 |
+
safe_email = email.replace("@", "_at_")
|
| 261 |
+
merchant_oid = f"ZENKA-{int(time.time())}-{safe_email}"
|
| 262 |
+
payment_amount = "49900"
|
| 263 |
+
user_basket = base64.b64encode(json.dumps([["ZenkaMind Premium", "499.00", 1]]).encode()).decode()
|
| 264 |
+
hash_str = f"{MERCHANT_ID}127.0.0.1{merchant_oid}{email}{payment_amount}{user_basket}00TL1"
|
| 265 |
+
token = base64.b64encode(hmac.new(MERCHANT_KEY.encode(), hash_str.encode() + MERCHANT_SALT.encode(), hashlib.sha256).digest()).decode()
|
| 266 |
+
|
| 267 |
+
params = {
|
| 268 |
+
'merchant_id': MERCHANT_ID, 'user_ip': '127.0.0.1', 'merchant_oid': merchant_oid, 'email': email,
|
| 269 |
+
'payment_amount': payment_amount, 'paytr_token': token, 'user_basket': user_basket,
|
| 270 |
+
'debug_on': 1, 'no_installment': 1, 'max_installment': 0, 'user_name': email,
|
| 271 |
+
'user_address': "Dijital", 'user_phone': "05555555555",
|
| 272 |
+
'merchant_ok_url': "https://zenkamind.com/?status=success",
|
| 273 |
+
'merchant_fail_url': "https://zenkamind.com/?status=fail",
|
| 274 |
+
'timeout_limit': "30", 'currency': "TL", 'test_mode': 1
|
| 275 |
+
}
|
| 276 |
+
res = requests.post('https://www.paytr.com/odeme/api/get-token', data=params)
|
| 277 |
+
return JSONResponse(res.json())
|
| 278 |
+
|
| 279 |
+
# ==========================================
|
| 280 |
+
# 7. BAŞLATMA
|
| 281 |
+
# ==========================================
|
| 282 |
+
io = gr.ChatInterface(fn=lambda x,y: "Backend Çalışıyor", title="ZenkaMind Backend")
|
| 283 |
+
app = gr.mount_gradio_app(app, io, path="/")
|
| 284 |
+
|
| 285 |
+
if __name__ == "__main__":
|
| 286 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|