Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from fastapi.responses import JSONResponse
|
| 4 |
+
import os
|
| 5 |
+
import requests
|
| 6 |
+
from datetime import date
|
| 7 |
+
|
| 8 |
+
HF_MODEL = "Qwen/Qwen2.5-1.5B-Instruct"
|
| 9 |
+
HF_API_URL = f"https://api-inference.huggingface.co/models/{HF_MODEL}"
|
| 10 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 11 |
+
|
| 12 |
+
FREE_DAILY_LIMIT = 30
|
| 13 |
+
SHOPIER_LINK = "https://shopier.com/42153760"
|
| 14 |
+
|
| 15 |
+
USERS = {}
|
| 16 |
+
|
| 17 |
+
def get_user(email: str):
|
| 18 |
+
today = date.today().isoformat()
|
| 19 |
+
if email not in USERS:
|
| 20 |
+
USERS[email] = {"count": 0, "date": today}
|
| 21 |
+
if USERS[email]["date"] != today:
|
| 22 |
+
USERS[email]["count"] = 0
|
| 23 |
+
USERS[email]["date"] = today
|
| 24 |
+
return USERS[email]
|
| 25 |
+
|
| 26 |
+
def generate_ai(message: str):
|
| 27 |
+
if not HF_TOKEN:
|
| 28 |
+
return "⚠️ Sistem yapılandırılıyor. Lütfen biraz sonra tekrar deneyin."
|
| 29 |
+
|
| 30 |
+
prompt = f"""
|
| 31 |
+
Sen ZenkaMind'sin.
|
| 32 |
+
Türkiye merkezli, Türkçe düşünen bir yapay zekâsın.
|
| 33 |
+
Net, kısa, öğretici cevaplar ver.
|
| 34 |
+
|
| 35 |
+
Kullanıcı: {message}
|
| 36 |
+
ZenkaMind:
|
| 37 |
+
"""
|
| 38 |
+
|
| 39 |
+
try:
|
| 40 |
+
r = requests.post(
|
| 41 |
+
HF_API_URL,
|
| 42 |
+
headers={"Authorization": f"Bearer {HF_TOKEN}"},
|
| 43 |
+
json={
|
| 44 |
+
"inputs": prompt,
|
| 45 |
+
"parameters": {
|
| 46 |
+
"max_new_tokens": 256,
|
| 47 |
+
"temperature": 0.7,
|
| 48 |
+
"top_p": 0.9,
|
| 49 |
+
"wait_for_model": True
|
| 50 |
+
}
|
| 51 |
+
},
|
| 52 |
+
timeout=120
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
if r.status_code != 200:
|
| 56 |
+
return "⏳ Model yükleniyor veya yoğun. Biraz sonra tekrar dene."
|
| 57 |
+
|
| 58 |
+
data = r.json()
|
| 59 |
+
if isinstance(data, list) and len(data) > 0:
|
| 60 |
+
return data[0].get("generated_text", "").strip()
|
| 61 |
+
|
| 62 |
+
return "Yanıt üretilemedi."
|
| 63 |
+
|
| 64 |
+
except Exception:
|
| 65 |
+
return "Sunucu hatası oluştu."
|
| 66 |
+
|
| 67 |
+
app = FastAPI(title="ZenkaMind API")
|
| 68 |
+
|
| 69 |
+
app.add_middleware(
|
| 70 |
+
CORSMiddleware,
|
| 71 |
+
allow_origins=["*"],
|
| 72 |
+
allow_methods=["*"],
|
| 73 |
+
allow_headers=["*"],
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
@app.get("/")
|
| 77 |
+
def root():
|
| 78 |
+
return {"status": "ZenkaMind API çalışıyor"}
|
| 79 |
+
|
| 80 |
+
@app.post("/api/chat")
|
| 81 |
+
async def chat(req: Request):
|
| 82 |
+
data = await req.json()
|
| 83 |
+
email = data.get("email", "")
|
| 84 |
+
message = data.get("message", "")
|
| 85 |
+
|
| 86 |
+
if not email or "@" not in email:
|
| 87 |
+
return JSONResponse(400, {"error": "Email gerekli"})
|
| 88 |
+
|
| 89 |
+
if not message:
|
| 90 |
+
return JSONResponse(400, {"error": "Mesaj boş"})
|
| 91 |
+
|
| 92 |
+
user = get_user(email)
|
| 93 |
+
if user["count"] >= FREE_DAILY_LIMIT:
|
| 94 |
+
return {
|
| 95 |
+
"response": f"Günlük limit doldu.\n💎 Premium: {SHOPIER_LINK}"
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
user["count"] += 1
|
| 99 |
+
response = generate_ai(message)
|
| 100 |
+
|
| 101 |
+
return {"response": response}
|