Update app.py
Browse files
app.py
CHANGED
|
@@ -1,99 +1,38 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from fastapi import FastAPI, Request
|
| 3 |
-
from fastapi.responses import JSONResponse
|
| 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 |
-
# Modelin küçültüldüğü yer (Hata vermemesi için 7B'ye çekildi)
|
| 23 |
-
REPO_ID = "Qwen/Qwen2.5-7B-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 |
-
# Admin Bilgileri
|
| 31 |
-
ADMIN_USERNAME = "berkay"
|
| 32 |
-
ADMIN_PASSWORD = "123"
|
| 33 |
-
|
| 34 |
-
DB_FILE = "users.json"
|
| 35 |
-
|
| 36 |
-
# ==========================================
|
| 37 |
-
# 2. VERİTABANI YÖNETİMİ
|
| 38 |
-
# ==========================================
|
| 39 |
-
def load_db():
|
| 40 |
-
if not os.path.exists(DB_FILE): return {}
|
| 41 |
-
try:
|
| 42 |
-
with open(DB_FILE, "r") as f: return json.load(f)
|
| 43 |
-
except: return {}
|
| 44 |
-
def save_db(data):
|
| 45 |
-
with open(DB_FILE, "w") as f: json.dump(data, f)
|
| 46 |
-
|
| 47 |
-
# ==========================================
|
| 48 |
-
# 3. API VE SUNUCU KURULUMU
|
| 49 |
-
# ==========================================
|
| 50 |
app = FastAPI()
|
| 51 |
-
security = HTTPBasic()
|
| 52 |
app.add_middleware(
|
| 53 |
-
CORSMiddleware,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
)
|
| 55 |
-
def verify_admin(credentials: HTTPBasicCredentials = Depends(security)):
|
| 56 |
-
if credentials.username != ADMIN_USERNAME or credentials.password != ADMIN_PASSWORD:
|
| 57 |
-
raise HTTPException(status_code=401, detail="Hatalı Şifre Patron!", headers={"WWW-Authenticate": "Basic"})
|
| 58 |
-
return credentials.username
|
| 59 |
|
| 60 |
-
|
| 61 |
-
# 4. YAPAY ZEKA SOHBETİ (CHAT API)
|
| 62 |
-
# ==========================================
|
| 63 |
|
| 64 |
@app.post("/api/chat")
|
| 65 |
async def chat_api(request: Request):
|
| 66 |
try:
|
| 67 |
data = await request.json()
|
| 68 |
-
user_msg = data.get("message")
|
| 69 |
-
history = data.get("history", [])
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
KİMLİK: Mahallenin ağır abisi, saygılı ama duruşu çelik gibi sağlam.
|
| 75 |
-
KURALLAR:
|
| 76 |
-
1. SADECE TÜRKÇE KONUŞ. Yabancı dil konuşana "Hayırdır koçum? Burası Türkiye." de ve reddet.
|
| 77 |
-
2. ASLA ÖZÜR DİLEME.
|
| 78 |
-
3. KÜFÜR YASAK. Küfür edeni uyar ama tekrar etme.
|
| 79 |
-
4. NEREDE DOĞDUN? "Manisa Akhisar, Efendi Mahallesi'nde..." de.
|
| 80 |
-
"""
|
| 81 |
-
|
| 82 |
-
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 83 |
-
for msg in history:
|
| 84 |
-
if len(msg) >= 2:
|
| 85 |
-
messages.append({"role": "user", "content": str(msg[0])})
|
| 86 |
-
messages.append({"role": "assistant", "content": str(msg[1])})
|
| 87 |
-
messages.append({"role": "user", "content": user_msg})
|
| 88 |
|
| 89 |
full_response = ""
|
| 90 |
stream = client.chat_completion(
|
| 91 |
-
model=
|
| 92 |
-
messages=messages,
|
| 93 |
-
max_tokens=
|
| 94 |
stream=True,
|
| 95 |
-
temperature=0.7
|
| 96 |
-
top_p=0.9
|
| 97 |
)
|
| 98 |
|
| 99 |
for chunk in stream:
|
|
@@ -103,24 +42,19 @@ async def chat_api(request: Request):
|
|
| 103 |
return JSONResponse({"response": full_response})
|
| 104 |
|
| 105 |
except Exception as e:
|
| 106 |
-
return JSONResponse({"response": f"
|
| 107 |
-
|
| 108 |
-
# ==========================================
|
| 109 |
-
# 5. GRAFİKLİ ADMİN PANELİ VE BAŞLATMA
|
| 110 |
-
# ==========================================
|
| 111 |
|
| 112 |
-
#
|
|
|
|
|
|
|
| 113 |
|
| 114 |
-
|
| 115 |
-
async def admin_panel(username: str = Depends(verify_admin)):
|
| 116 |
-
return HTMLResponse("Admin paneli şu an devre dışı (Hafıza tasarrufu).")
|
| 117 |
-
|
| 118 |
-
@app.post("/api/register")
|
| 119 |
-
async def register_placeholder(): return JSONResponse({"status": "error", "message": "Üyelik sistemi şu an kapalı."})
|
| 120 |
-
|
| 121 |
-
# Uygulama Başlatma
|
| 122 |
-
io = gr.ChatInterface(fn=lambda x,y: "Backend Çalışıyor", title="ZenkaMind Backend")
|
| 123 |
app = gr.mount_gradio_app(app, io, path="/")
|
| 124 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
if __name__ == "__main__":
|
|
|
|
| 126 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from fastapi import FastAPI, Request
|
| 3 |
+
from fastapi.responses import JSONResponse
|
|
|
|
| 4 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
| 5 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
from huggingface_hub import InferenceClient
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
app = FastAPI()
|
|
|
|
| 9 |
app.add_middleware(
|
| 10 |
+
CORSMiddleware,
|
| 11 |
+
allow_origins=["*"],
|
| 12 |
+
allow_credentials=True,
|
| 13 |
+
allow_methods=["*"],
|
| 14 |
+
allow_headers=["*"],
|
| 15 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
client = InferenceClient(token=os.getenv("HF_TOKEN"))
|
|
|
|
|
|
|
| 18 |
|
| 19 |
@app.post("/api/chat")
|
| 20 |
async def chat_api(request: Request):
|
| 21 |
try:
|
| 22 |
data = await request.json()
|
| 23 |
+
user_msg = data.get("message", "")
|
|
|
|
| 24 |
|
| 25 |
+
messages = [
|
| 26 |
+
{"role": "user", "content": user_msg}
|
| 27 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
full_response = ""
|
| 30 |
stream = client.chat_completion(
|
| 31 |
+
model="Qwen/Qwen2.5-7B-Instruct",
|
| 32 |
+
messages=messages,
|
| 33 |
+
max_tokens=500,
|
| 34 |
stream=True,
|
| 35 |
+
temperature=0.7
|
|
|
|
| 36 |
)
|
| 37 |
|
| 38 |
for chunk in stream:
|
|
|
|
| 42 |
return JSONResponse({"response": full_response})
|
| 43 |
|
| 44 |
except Exception as e:
|
| 45 |
+
return JSONResponse({"response": f"Hata: {str(e)}"})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
# Gradio arayüzü
|
| 48 |
+
def chat_fn(message, history):
|
| 49 |
+
return "Gradio arayüzü aktif. API endpoint'ini kullanın: /api/chat"
|
| 50 |
|
| 51 |
+
io = gr.ChatInterface(fn=chat_fn, title="ZenkaMind")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
app = gr.mount_gradio_app(app, io, path="/")
|
| 53 |
|
| 54 |
+
@app.get("/")
|
| 55 |
+
async def home():
|
| 56 |
+
return {"status": "ZenkaMind API çalışıyor"}
|
| 57 |
+
|
| 58 |
if __name__ == "__main__":
|
| 59 |
+
import uvicorn
|
| 60 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|