Update app.py
Browse files
app.py
CHANGED
|
@@ -1,69 +1,171 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from fastapi import FastAPI, Request
|
|
|
|
|
|
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
-
from fastapi.responses import JSONResponse
|
| 5 |
import uvicorn
|
| 6 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
from huggingface_hub import InferenceClient
|
| 8 |
|
| 9 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
REPO_ID = "Qwen/Qwen2.5-72B-Instruct"
|
| 11 |
client = InferenceClient(token=os.getenv("HF_TOKEN"))
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
app = FastAPI()
|
|
|
|
| 15 |
|
| 16 |
-
#
|
| 17 |
app.add_middleware(
|
| 18 |
CORSMiddleware,
|
| 19 |
-
allow_origins=["*"],
|
| 20 |
allow_credentials=True,
|
| 21 |
allow_methods=["*"],
|
| 22 |
allow_headers=["*"],
|
| 23 |
)
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
# 4. SOHBET API (SİTENİN BAĞLANACAĞI YER)
|
| 31 |
@app.post("/api/chat")
|
| 32 |
async def chat_api(request: Request):
|
| 33 |
try:
|
| 34 |
data = await request.json()
|
| 35 |
user_msg = data.get("message")
|
|
|
|
| 36 |
|
| 37 |
-
#
|
| 38 |
SYSTEM_PROMPT = """
|
| 39 |
Sen ZenkaMind 1.1'sin. Tek Yaratıcın: Berkay Adıgül.
|
| 40 |
-
KİMLİK: Mahallenin ağır abisi, saygılı ama duruşu çelik gibi sağlam.
|
|
|
|
| 41 |
KURALLAR:
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
| 44 |
"""
|
| 45 |
|
| 46 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
messages.append({"role": "user", "content": user_msg})
|
| 48 |
|
| 49 |
-
|
| 50 |
stream = client.chat_completion(
|
| 51 |
model=REPO_ID,
|
| 52 |
messages=messages,
|
| 53 |
max_tokens=1024,
|
| 54 |
stream=True,
|
| 55 |
-
temperature=0.7
|
|
|
|
| 56 |
)
|
| 57 |
-
|
| 58 |
for chunk in stream:
|
| 59 |
if chunk.choices and chunk.choices[0].delta.content:
|
| 60 |
-
|
| 61 |
|
| 62 |
-
return {"response":
|
| 63 |
|
| 64 |
except Exception as e:
|
| 65 |
-
return {"response": f"⚠️
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from fastapi import FastAPI, Request, Form, HTTPException, Depends
|
| 3 |
+
from fastapi.responses import JSONResponse, HTMLResponse, RedirectResponse
|
| 4 |
+
from fastapi.security import HTTPBasic, HTTPBasicCredentials
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
| 6 |
import uvicorn
|
| 7 |
import os
|
| 8 |
+
import json
|
| 9 |
+
import base64
|
| 10 |
+
import hmac
|
| 11 |
+
import hashlib
|
| 12 |
+
import requests
|
| 13 |
+
import time
|
| 14 |
+
import psutil
|
| 15 |
+
from datetime import datetime, timedelta
|
| 16 |
from huggingface_hub import InferenceClient
|
| 17 |
|
| 18 |
+
# ==========================================
|
| 19 |
+
# 1. AYARLAR VE GÜVENLİK
|
| 20 |
+
# ==========================================
|
| 21 |
+
|
| 22 |
+
# Model ve API Anahtarları
|
| 23 |
REPO_ID = "Qwen/Qwen2.5-72B-Instruct"
|
| 24 |
client = InferenceClient(token=os.getenv("HF_TOKEN"))
|
| 25 |
|
| 26 |
+
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 KESİNLİKLE DEĞİŞTİR)
|
| 31 |
+
ADMIN_USERNAME = "berkay"
|
| 32 |
+
ADMIN_PASSWORD = "123"
|
| 33 |
+
|
| 34 |
+
# Veritabanı Dosyası
|
| 35 |
+
DB_FILE = "users.json"
|
| 36 |
+
|
| 37 |
+
# ==========================================
|
| 38 |
+
# 2. VERİTABANI YÖNETİMİ (JSON)
|
| 39 |
+
# ==========================================
|
| 40 |
+
def load_db():
|
| 41 |
+
if not os.path.exists(DB_FILE): return {}
|
| 42 |
+
try: with open(DB_FILE, "r") as f: return json.load(f)
|
| 43 |
+
except: return {}
|
| 44 |
+
|
| 45 |
+
def save_db(data):
|
| 46 |
+
with open(DB_FILE, "w") as f: json.dump(data, f)
|
| 47 |
+
|
| 48 |
+
# ==========================================
|
| 49 |
+
# 3. API VE SUNUCU KURULUMU
|
| 50 |
+
# ==========================================
|
| 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=["*"],
|
| 58 |
allow_credentials=True,
|
| 59 |
allow_methods=["*"],
|
| 60 |
allow_headers=["*"],
|
| 61 |
)
|
| 62 |
|
| 63 |
+
# Admin Giriş Kontrolü
|
| 64 |
+
def verify_admin(credentials: HTTPBasicCredentials = Depends(security)):
|
| 65 |
+
if credentials.username != ADMIN_USERNAME or credentials.password != ADMIN_PASSWORD:
|
| 66 |
+
raise HTTPException(status_code=401, detail="Hatalı Şifre Patron!", headers={"WWW-Authenticate": "Basic"})
|
| 67 |
+
return credentials.username
|
| 68 |
+
|
| 69 |
+
# ==========================================
|
| 70 |
+
# 4. YAPAY ZEKA SOHBETİ (ZENKAMIND BEYNİ)
|
| 71 |
+
# ==========================================
|
| 72 |
|
|
|
|
| 73 |
@app.post("/api/chat")
|
| 74 |
async def chat_api(request: Request):
|
| 75 |
try:
|
| 76 |
data = await request.json()
|
| 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. Asla kurumsal konuşmazsın.
|
| 84 |
+
|
| 85 |
KURALLAR:
|
| 86 |
+
1. SADECE TÜRKÇE KONUŞ. Yabancı dil konuşana "Hayırdır koçum? Burası Türkiye. Bize ecnebi dili sökmez." de ve reddet. Asla yumuşama.
|
| 87 |
+
2. ASLA ÖZÜR DİLEME. "Mevzu yok", "Hallederiz" de.
|
| 88 |
+
3. KÜFÜR YASAK. Küfür edeni uyar ama tekrar etme.
|
| 89 |
+
4. NEREDE DOĞDUN? "Manisa Akhisar, Efendi Mahallesi'nde, uykusuz gecelerin eseri olarak doğdum." de.
|
| 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])})
|
| 99 |
+
|
| 100 |
messages.append({"role": "user", "content": user_msg})
|
| 101 |
|
| 102 |
+
full_response = ""
|
| 103 |
stream = client.chat_completion(
|
| 104 |
model=REPO_ID,
|
| 105 |
messages=messages,
|
| 106 |
max_tokens=1024,
|
| 107 |
stream=True,
|
| 108 |
+
temperature=0.7,
|
| 109 |
+
top_p=0.9
|
| 110 |
)
|
| 111 |
+
|
| 112 |
for chunk in stream:
|
| 113 |
if chunk.choices and chunk.choices[0].delta.content:
|
| 114 |
+
full_response += chunk.choices[0].delta.content
|
| 115 |
|
| 116 |
+
return JSONResponse({"response": full_response})
|
| 117 |
|
| 118 |
except Exception as e:
|
| 119 |
+
return JSONResponse({"response": f"⚠️ Sistem yoğun aslanım. (Hata: {str(e)})"})
|
| 120 |
+
|
| 121 |
+
# ==========================================
|
| 122 |
+
# 5. GRAFİKLİ ADMİN PANELİ & İSTATİSTİKLER
|
| 123 |
+
# ==========================================
|
| 124 |
+
|
| 125 |
+
@app.get("/api/stats")
|
| 126 |
+
async def get_stats():
|
| 127 |
+
cpu = psutil.cpu_percent(interval=None)
|
| 128 |
+
ram = psutil.virtual_memory().percent
|
| 129 |
+
db = load_db()
|
| 130 |
+
premium_count = sum(1 for u in db.values() if datetime.now() < datetime.strptime(u.get("premium_until", "2000-01-01"), "%Y-%m-%d"))
|
| 131 |
+
return {"cpu": cpu, "ram": ram, "users": len(db), "premium": premium_count}
|
| 132 |
+
|
| 133 |
+
@app.get("/admin", response_class=HTMLResponse)
|
| 134 |
+
async def admin_panel(username: str = Depends(verify_admin)):
|
| 135 |
+
db = load_db()
|
| 136 |
+
# Kullanıcı Tablosunu Oluştur
|
| 137 |
+
user_rows = ""
|
| 138 |
+
for email, data in db.items():
|
| 139 |
+
# Tarih formatı kontrolü
|
| 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 |
+
html = f"""
|
| 167 |
+
<!DOCTYPE html>
|
| 168 |
+
<html>
|
| 169 |
+
<head>
|
| 170 |
+
<title>ZenkaMind CORE</title>
|
| 171 |
+
<script src="https://cdn.jsdelivr
|