Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,22 @@
|
|
| 1 |
from fastapi import FastAPI, Request
|
| 2 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
from fastapi.responses import JSONResponse
|
| 4 |
-
import
|
| 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):
|
|
@@ -23,48 +28,51 @@ def get_user(email: str):
|
|
| 23 |
USERS[email]["date"] = today
|
| 24 |
return USERS[email]
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
prompt = f"""
|
| 31 |
Sen ZenkaMind'sin.
|
| 32 |
-
|
| 33 |
-
|
| 34 |
|
| 35 |
Kullanıcı: {message}
|
| 36 |
ZenkaMind:
|
| 37 |
"""
|
| 38 |
-
|
| 39 |
try:
|
| 40 |
-
|
| 41 |
HF_API_URL,
|
| 42 |
headers={"Authorization": f"Bearer {HF_TOKEN}"},
|
| 43 |
json={
|
| 44 |
"inputs": prompt,
|
| 45 |
"parameters": {
|
| 46 |
-
"max_new_tokens":
|
| 47 |
"temperature": 0.7,
|
| 48 |
"top_p": 0.9,
|
| 49 |
-
"
|
| 50 |
}
|
| 51 |
},
|
| 52 |
-
timeout=
|
| 53 |
)
|
| 54 |
|
| 55 |
-
if
|
| 56 |
-
return "⏳ Model yükleniyor
|
| 57 |
|
| 58 |
-
data =
|
| 59 |
-
if isinstance(data, list) and
|
| 60 |
-
return data[0]
|
| 61 |
|
| 62 |
-
return "Yanıt
|
| 63 |
|
| 64 |
-
except Exception:
|
| 65 |
-
|
|
|
|
| 66 |
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
app.add_middleware(
|
| 70 |
CORSMiddleware,
|
|
@@ -80,22 +88,20 @@ def root():
|
|
| 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, {"
|
| 88 |
|
| 89 |
if not message:
|
| 90 |
-
return JSONResponse(400, {"
|
| 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":
|
|
|
|
| 1 |
from fastapi import FastAPI, Request
|
|
|
|
| 2 |
from fastapi.responses import JSONResponse
|
| 3 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
import requests
|
| 5 |
+
import os
|
| 6 |
from datetime import date
|
| 7 |
|
| 8 |
+
# ==============================
|
| 9 |
+
# CONFIG
|
| 10 |
+
# ==============================
|
| 11 |
HF_MODEL = "Qwen/Qwen2.5-1.5B-Instruct"
|
| 12 |
HF_API_URL = f"https://api-inference.huggingface.co/models/{HF_MODEL}"
|
| 13 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 14 |
|
| 15 |
FREE_DAILY_LIMIT = 30
|
|
|
|
| 16 |
|
| 17 |
+
# ==============================
|
| 18 |
+
# RAM USER DB
|
| 19 |
+
# ==============================
|
| 20 |
USERS = {}
|
| 21 |
|
| 22 |
def get_user(email: str):
|
|
|
|
| 28 |
USERS[email]["date"] = today
|
| 29 |
return USERS[email]
|
| 30 |
|
| 31 |
+
# ==============================
|
| 32 |
+
# AI CALL
|
| 33 |
+
# ==============================
|
| 34 |
+
def ask_ai(message: str):
|
| 35 |
prompt = f"""
|
| 36 |
Sen ZenkaMind'sin.
|
| 37 |
+
Sadece Türkçe konuş.
|
| 38 |
+
Kısa, net ve insan gibi cevap ver.
|
| 39 |
|
| 40 |
Kullanıcı: {message}
|
| 41 |
ZenkaMind:
|
| 42 |
"""
|
|
|
|
| 43 |
try:
|
| 44 |
+
res = requests.post(
|
| 45 |
HF_API_URL,
|
| 46 |
headers={"Authorization": f"Bearer {HF_TOKEN}"},
|
| 47 |
json={
|
| 48 |
"inputs": prompt,
|
| 49 |
"parameters": {
|
| 50 |
+
"max_new_tokens": 200,
|
| 51 |
"temperature": 0.7,
|
| 52 |
"top_p": 0.9,
|
| 53 |
+
"return_full_text": False
|
| 54 |
}
|
| 55 |
},
|
| 56 |
+
timeout=60
|
| 57 |
)
|
| 58 |
|
| 59 |
+
if res.status_code == 503:
|
| 60 |
+
return "⏳ Model yükleniyor, 10 saniye sonra tekrar dene."
|
| 61 |
|
| 62 |
+
data = res.json()
|
| 63 |
+
if isinstance(data, list) and "generated_text" in data[0]:
|
| 64 |
+
return data[0]["generated_text"].strip()
|
| 65 |
|
| 66 |
+
return "⚠️ Yanıt alınamadı."
|
| 67 |
|
| 68 |
+
except Exception as e:
|
| 69 |
+
print(e)
|
| 70 |
+
return "❌ Sunucu hatası."
|
| 71 |
|
| 72 |
+
# ==============================
|
| 73 |
+
# FASTAPI
|
| 74 |
+
# ==============================
|
| 75 |
+
app = FastAPI()
|
| 76 |
|
| 77 |
app.add_middleware(
|
| 78 |
CORSMiddleware,
|
|
|
|
| 88 |
@app.post("/api/chat")
|
| 89 |
async def chat(req: Request):
|
| 90 |
data = await req.json()
|
| 91 |
+
email = data.get("email", "").lower()
|
| 92 |
+
message = data.get("message", "").strip()
|
| 93 |
|
| 94 |
if not email or "@" not in email:
|
| 95 |
+
return JSONResponse(status_code=400, content={"response": "Geçerli email gerekli."})
|
| 96 |
|
| 97 |
if not message:
|
| 98 |
+
return JSONResponse(status_code=400, content={"response": "Mesaj boş olamaz."})
|
| 99 |
|
| 100 |
user = get_user(email)
|
| 101 |
if user["count"] >= FREE_DAILY_LIMIT:
|
| 102 |
+
return {"response": "Günlük limit doldu."}
|
|
|
|
|
|
|
| 103 |
|
| 104 |
+
answer = ask_ai(message)
|
| 105 |
user["count"] += 1
|
|
|
|
| 106 |
|
| 107 |
+
return {"response": answer}
|