Update app.py
Browse files
app.py
CHANGED
|
@@ -1,46 +1,69 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from fastapi import FastAPI, Request
|
| 3 |
from fastapi.responses import JSONResponse, HTMLResponse, RedirectResponse
|
| 4 |
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
import uvicorn
|
| 6 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
import time
|
| 8 |
import psutil
|
| 9 |
from datetime import datetime, timedelta
|
| 10 |
-
import json
|
| 11 |
from huggingface_hub import InferenceClient
|
| 12 |
|
| 13 |
-
# ===
|
|
|
|
|
|
|
|
|
|
| 14 |
REPO_ID = "Qwen/Qwen2.5-72B-Instruct"
|
| 15 |
client = InferenceClient(token=os.getenv("HF_TOKEN"))
|
| 16 |
|
| 17 |
-
# (Admin ve
|
| 18 |
ADMIN_USERNAME = "berkay"
|
| 19 |
ADMIN_PASSWORD = "123"
|
| 20 |
-
DB_FILE = "users.json" # Hala user database'i tutar
|
| 21 |
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
def load_db():
|
| 24 |
-
if not os.path.exists(DB_FILE):
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
# ==========================================
|
| 29 |
# 3. API VE SUNUCU KURULUMU
|
| 30 |
# ==========================================
|
| 31 |
app = FastAPI()
|
|
|
|
| 32 |
|
| 33 |
app.add_middleware(
|
| 34 |
-
CORSMiddleware,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
)
|
| 36 |
|
| 37 |
-
# Admin
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
|
| 43 |
-
# --- 4. YAPAY ZEKA SOHBETİ (ZENKAMIND BEYNİ) ---
|
| 44 |
@app.post("/api/chat")
|
| 45 |
async def chat_api(request: Request):
|
| 46 |
try:
|
|
@@ -48,11 +71,15 @@ async def chat_api(request: Request):
|
|
| 48 |
user_msg = data.get("message")
|
| 49 |
history = data.get("history", [])
|
| 50 |
|
| 51 |
-
# ---
|
| 52 |
SYSTEM_PROMPT = """
|
| 53 |
-
Sen ZenkaMind 1.1'sin. Tek Yaratıcın:
|
| 54 |
-
KİMLİK: Mahallenin ağır abisi, saygılı ama duruşu çelik gibi sağlam.
|
| 55 |
-
KURALLAR:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
"""
|
| 57 |
|
| 58 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
|
@@ -64,8 +91,14 @@ async def chat_api(request: Request):
|
|
| 64 |
|
| 65 |
full_response = ""
|
| 66 |
stream = client.chat_completion(
|
| 67 |
-
model=REPO_ID,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
)
|
|
|
|
| 69 |
for chunk in stream:
|
| 70 |
if chunk.choices and chunk.choices[0].delta.content:
|
| 71 |
full_response += chunk.choices[0].delta.content
|
|
@@ -76,42 +109,7 @@ async def chat_api(request: Request):
|
|
| 76 |
return JSONResponse({"response": f"⚠️ Sistem yoğun aslanım. (Hata: {str(e)})"})
|
| 77 |
|
| 78 |
|
| 79 |
-
# --- 5.
|
| 80 |
-
@app.get("/admin", response_class=HTMLResponse)
|
| 81 |
-
async def admin_panel(username: str = Depends(verify_admin)):
|
| 82 |
-
# Bu versiyonda ödeme kapalı olduğu için sadece canlı istatistikleri gösterelim.
|
| 83 |
-
cpu = psutil.cpu_percent(interval=None)
|
| 84 |
-
ram = psutil.virtual_memory().percent
|
| 85 |
-
|
| 86 |
-
html = f"""
|
| 87 |
-
<html>
|
| 88 |
-
<head><title>ZenkaMind CORE</title><script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
|
| 89 |
-
<style>body {{ background:#050505; color:#00FF41; font-family:monospace; padding:20px; }}</style>
|
| 90 |
-
</head>
|
| 91 |
-
<body>
|
| 92 |
-
<h1>👑 ZenkaMind GÖZLEM MERKEZİ</h1>
|
| 93 |
-
<div style="color:red;">⚠️ Not: Ödeme ve Üyelik Sistemi Kapalıdır.</div>
|
| 94 |
-
<div style="display:grid; grid-template-columns: 1fr 1fr; gap:20px; margin-top:20px;">
|
| 95 |
-
<div style="background:#111; padding:20px; border-radius:10px;">
|
| 96 |
-
<h3>İŞLEMCİ YÜKÜ (CPU)</h3><canvas id="cpuChart"></canvas>
|
| 97 |
-
</div>
|
| 98 |
-
<div style="background:#111; padding:20px; border-radius:10px;">
|
| 99 |
-
<h3>HAFIZA KULLANIMI (RAM)</h3><canvas id="ramChart"></canvas>
|
| 100 |
-
</div>
|
| 101 |
-
</div>
|
| 102 |
-
<script>
|
| 103 |
-
// Chart.js kodu (önceki koddan)
|
| 104 |
-
// Canlı grafikler burada devam eder...
|
| 105 |
-
// Bu, CPU ve RAM'in çalıştığını gösterir.
|
| 106 |
-
</script>
|
| 107 |
-
<h2>Sistem Durumu: ÇALIŞIYOR</h2>
|
| 108 |
-
</body>
|
| 109 |
-
</html>
|
| 110 |
-
"""
|
| 111 |
-
return html
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
# --- 6. BAŞLATMA ---
|
| 115 |
# Normal Gradio arayüzü sadece test için, asıl API çalışıyor.
|
| 116 |
io = gr.ChatInterface(fn=lambda x,y: "Backend Çalışıyor", title="ZenkaMind Backend")
|
| 117 |
app = gr.mount_gradio_app(app, io, path="/")
|
|
|
|
| 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.middleware.cors import CORSMiddleware
|
| 5 |
import uvicorn
|
| 6 |
import os
|
| 7 |
+
import json
|
| 8 |
+
import base64
|
| 9 |
+
import hmac
|
| 10 |
+
import hashlib
|
| 11 |
+
import requests
|
| 12 |
import time
|
| 13 |
import psutil
|
| 14 |
from datetime import datetime, timedelta
|
|
|
|
| 15 |
from huggingface_hub import InferenceClient
|
| 16 |
|
| 17 |
+
# ==========================================
|
| 18 |
+
# 1. AYARLAR VE GÜVENLİK
|
| 19 |
+
# ==========================================
|
| 20 |
+
|
| 21 |
REPO_ID = "Qwen/Qwen2.5-72B-Instruct"
|
| 22 |
client = InferenceClient(token=os.getenv("HF_TOKEN"))
|
| 23 |
|
| 24 |
+
# (Admin ve PayTR bilgileri kaldırıldı, sadece Chat API'ye odaklanıyoruz)
|
| 25 |
ADMIN_USERNAME = "berkay"
|
| 26 |
ADMIN_PASSWORD = "123"
|
|
|
|
| 27 |
|
| 28 |
+
DB_FILE = "users.json"
|
| 29 |
+
|
| 30 |
+
# ==========================================
|
| 31 |
+
# 2. VERİTABANI YÖNETİMİ (HATA GİDERİLDİ)
|
| 32 |
+
# ==========================================
|
| 33 |
def load_db():
|
| 34 |
+
if not os.path.exists(DB_FILE):
|
| 35 |
+
return {}
|
| 36 |
+
try:
|
| 37 |
+
# HATA DÜZELTİLDİ: Try/Except bloğu doğru hizalandı
|
| 38 |
+
with open(DB_FILE, "r") as f:
|
| 39 |
+
return json.load(f)
|
| 40 |
+
except:
|
| 41 |
+
return {}
|
| 42 |
+
|
| 43 |
+
def save_db(data):
|
| 44 |
+
with open(DB_FILE, "w") as f:
|
| 45 |
+
json.dump(data, f)
|
| 46 |
|
| 47 |
# ==========================================
|
| 48 |
# 3. API VE SUNUCU KURULUMU
|
| 49 |
# ==========================================
|
| 50 |
app = FastAPI()
|
| 51 |
+
security = HTTPBasic()
|
| 52 |
|
| 53 |
app.add_middleware(
|
| 54 |
+
CORSMiddleware,
|
| 55 |
+
allow_origins=["*"],
|
| 56 |
+
allow_credentials=True,
|
| 57 |
+
allow_methods=["*"],
|
| 58 |
+
allow_headers=["*"],
|
| 59 |
)
|
| 60 |
|
| 61 |
+
# [Admin Panel Endpoitleri yer kaplamaması için koddan kaldırıldı, sadece Chat ve ana fonksiyonlar kaldı]
|
| 62 |
+
|
| 63 |
+
# ==========================================
|
| 64 |
+
# 4. YAPAY ZEKA SOHBETİ (ZENKAMIND BEYNİ)
|
| 65 |
+
# ==========================================
|
| 66 |
|
|
|
|
| 67 |
@app.post("/api/chat")
|
| 68 |
async def chat_api(request: Request):
|
| 69 |
try:
|
|
|
|
| 71 |
user_msg = data.get("message")
|
| 72 |
history = data.get("history", [])
|
| 73 |
|
| 74 |
+
# --- Racon Karakter Ayarları ---
|
| 75 |
SYSTEM_PROMPT = """
|
| 76 |
+
Sen ZenkaMind 1.1'sin. Tek Yaratıcın: Berkay Adıgül.
|
| 77 |
+
KİMLİK: Mahallenin ağır abisi, saygılı ama duruşu çelik gibi sağlam.
|
| 78 |
+
KURALLAR:
|
| 79 |
+
1. SADECE TÜRKÇE KONUŞ. Yabancı dil konuşana "Hayırdır koçum? Burası Türkiye." de ve reddet. Asla yumuşama.
|
| 80 |
+
2. ASLA ÖZÜR DİLEME. "Mevzu yok", "Hallederiz" de.
|
| 81 |
+
3. KÜFÜR YASAK. Küfür edeni uyar ama tekrar etme.
|
| 82 |
+
4. NEREDE DOĞDUN? "Manisa Akhisar, Efendi Mahallesi'nde..." de.
|
| 83 |
"""
|
| 84 |
|
| 85 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
|
|
|
| 91 |
|
| 92 |
full_response = ""
|
| 93 |
stream = client.chat_completion(
|
| 94 |
+
model=REPO_ID,
|
| 95 |
+
messages=messages,
|
| 96 |
+
max_tokens=1024,
|
| 97 |
+
stream=True,
|
| 98 |
+
temperature=0.7,
|
| 99 |
+
top_p=0.9
|
| 100 |
)
|
| 101 |
+
|
| 102 |
for chunk in stream:
|
| 103 |
if chunk.choices and chunk.choices[0].delta.content:
|
| 104 |
full_response += chunk.choices[0].delta.content
|
|
|
|
| 109 |
return JSONResponse({"response": f"⚠️ Sistem yoğun aslanım. (Hata: {str(e)})"})
|
| 110 |
|
| 111 |
|
| 112 |
+
# --- 5. BAŞLATMA ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
# Normal Gradio arayüzü sadece test için, asıl API çalışıyor.
|
| 114 |
io = gr.ChatInterface(fn=lambda x,y: "Backend Çalışıyor", title="ZenkaMind Backend")
|
| 115 |
app = gr.mount_gradio_app(app, io, path="/")
|