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ponse
|
| 4 |
+
import requests, os
|
| 5 |
+
from datetime import datetime, date
|
| 6 |
+
|
| 7 |
+
# =========================
|
| 8 |
+
# CONFIG
|
| 9 |
+
# =========================
|
| 10 |
+
HF_MODEL = "google/mt5-small"
|
| 11 |
+
HF_API_URL = f"https://api-inference.huggingface.co/models/{HF_MODEL}"
|
| 12 |
+
HF_TOKEN = os.getenv("HF_TOKEN", "")
|
| 13 |
+
|
| 14 |
+
SHOPIER_LINK = "https://shopier.com/42153760"
|
| 15 |
+
FREE_DAILY_LIMIT = 30
|
| 16 |
+
|
| 17 |
+
# =========================
|
| 18 |
+
# APP
|
| 19 |
+
# =========================
|
| 20 |
+
app = FastAPI(title="ZenkaMind API")
|
| 21 |
+
|
| 22 |
+
app.add_middleware(
|
| 23 |
+
CORSMiddleware,
|
| 24 |
+
allow_origins=["*"],
|
| 25 |
+
allow_methods=["*"],
|
| 26 |
+
allow_headers=["*"],
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
# =========================
|
| 30 |
+
# SIMPLE MEMORY (RAM)
|
| 31 |
+
# =========================
|
| 32 |
+
USERS = {}
|
| 33 |
+
|
| 34 |
+
def get_user(email):
|
| 35 |
+
today = date.today().isoformat()
|
| 36 |
+
if email not in USERS:
|
| 37 |
+
USERS[email] = {
|
| 38 |
+
"count": 0,
|
| 39 |
+
"date": today,
|
| 40 |
+
"premium": False
|
| 41 |
+
}
|
| 42 |
+
if USERS[email]["date"] != today:
|
| 43 |
+
USERS[email]["count"] = 0
|
| 44 |
+
USERS[email]["date"] = today
|
| 45 |
+
return USERS[email]
|
| 46 |
+
|
| 47 |
+
# =========================
|
| 48 |
+
# AI CALL
|
| 49 |
+
# =========================
|
| 50 |
+
def ask_ai(prompt: str):
|
| 51 |
+
headers = {}
|
| 52 |
+
if HF_TOKEN:
|
| 53 |
+
headers["Authorization"] = f"Bearer {HF_TOKEN}"
|
| 54 |
+
|
| 55 |
+
payload = {
|
| 56 |
+
"inputs": prompt,
|
| 57 |
+
"parameters": {
|
| 58 |
+
"max_length": 300,
|
| 59 |
+
"temperature": 0.6
|
| 60 |
+
}
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
r = requests.post(
|
| 64 |
+
HF_API_URL,
|
| 65 |
+
headers=headers,
|
| 66 |
+
json=payload,
|
| 67 |
+
timeout=20
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
if r.status_code != 200:
|
| 71 |
+
return None
|
| 72 |
+
|
| 73 |
+
data = r.json()
|
| 74 |
+
if isinstance(data, list) and len(data) > 0:
|
| 75 |
+
return data[0].get("generated_text", "").strip()
|
| 76 |
+
|
| 77 |
+
return None
|
| 78 |
+
|
| 79 |
+
# =========================
|
| 80 |
+
# PROMPT
|
| 81 |
+
# =========================
|
| 82 |
+
def build_prompt(user_message: str):
|
| 83 |
+
return f"""
|
| 84 |
+
Sen ZenkaMind’sin.
|
| 85 |
+
|
| 86 |
+
- Sadece Türkçe konuş.
|
| 87 |
+
- Net, açık ve kararlı cevaplar ver.
|
| 88 |
+
- Eğlence botu gibi davranma.
|
| 89 |
+
- Kullanıcıyı geliştirmeyi hedefle.
|
| 90 |
+
- Gereksiz uzatma yapma.
|
| 91 |
+
- Yanlış bir şey sorulursa açıkça yanlış olduğunu söyle.
|
| 92 |
+
|
| 93 |
+
Kullanıcı: {user_message}
|
| 94 |
+
|
| 95 |
+
ZenkaMind:
|
| 96 |
+
"""
|
| 97 |
+
|
| 98 |
+
# =========================
|
| 99 |
+
# ROUTES
|
| 100 |
+
# =========================
|
| 101 |
+
@app.get("/")
|
| 102 |
+
async def root():
|
| 103 |
+
return {"status": "ZenkaMind API çalışıyor"}
|
| 104 |
+
|
| 105 |
+
@app.get("/health")
|
| 106 |
+
async def health():
|
| 107 |
+
return {
|
| 108 |
+
"status": "ok",
|
| 109 |
+
"model": HF_MODEL,
|
| 110 |
+
"time": datetime.now().isoformat()
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
@app.post("/api/chat")
|
| 114 |
+
async def chat(req: Request):
|
| 115 |
+
data = await req.json()
|
| 116 |
+
email = data.get("email", "").lower()
|
| 117 |
+
message = data.get("message", "").strip()
|
| 118 |
+
|
| 119 |
+
if not email or "@" not in email:
|
| 120 |
+
return JSONResponse(400, {"error": "Email gerekli"})
|
| 121 |
+
|
| 122 |
+
if not message:
|
| 123 |
+
return JSONResponse(400, {"error": "Mesaj boş"})
|
| 124 |
+
|
| 125 |
+
user = get_user(email)
|
| 126 |
+
|
| 127 |
+
if not user["premium"] and user["count"] >= FREE_DAILY_LIMIT:
|
| 128 |
+
return {
|
| 129 |
+
"response": f"Günlük limit doldu.\n💎 Premium: {SHOPIER_LINK}",
|
| 130 |
+
"status": "limit"
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
prompt = build_prompt(message)
|
| 134 |
+
answer = ask_ai(prompt)
|
| 135 |
+
|
| 136 |
+
if not answer:
|
| 137 |
+
return {
|
| 138 |
+
"response": "Sunucu yoğun. Lütfen biraz sonra tekrar deneyin.",
|
| 139 |
+
"status": "error"
|
| 140 |
+
}
|
| 141 |
+
|
| 142 |
+
user["count"] += 1
|
| 143 |
+
|
| 144 |
+
if not user["premium"]:
|
| 145 |
+
answer += f"\n\n💎 Premium ile limitsiz kullanım: {SHOPIER_LINK}"
|
| 146 |
+
|
| 147 |
+
return {
|
| 148 |
+
"response": answer,
|
| 149 |
+
"remaining": "unlimited" if user["premium"] else max(0, FREE_DAILY_LIMIT - user["count"])
|
| 150 |
+
}
|