Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,14 +3,12 @@ from fastapi.responses import JSONResponse
|
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
import json
|
| 5 |
import os
|
| 6 |
-
import requests
|
| 7 |
from datetime import datetime, date
|
| 8 |
|
| 9 |
-
app = FastAPI(
|
| 10 |
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"])
|
| 11 |
|
| 12 |
DB_PATH = "users.json"
|
| 13 |
-
HF_TOKEN = os.getenv("HF_TOKEN", "")
|
| 14 |
|
| 15 |
def load_db():
|
| 16 |
try:
|
|
@@ -28,49 +26,13 @@ def save_db(data):
|
|
| 28 |
except:
|
| 29 |
pass
|
| 30 |
|
| 31 |
-
def get_ai_response(message):
|
| 32 |
-
"""GERÇEK AI YANITI"""
|
| 33 |
-
if not HF_TOKEN:
|
| 34 |
-
return "⚠️ AI Model yüklenemedi. HF_TOKEN gerekli."
|
| 35 |
-
|
| 36 |
-
try:
|
| 37 |
-
response = requests.post(
|
| 38 |
-
"https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2",
|
| 39 |
-
headers={"Authorization": f"Bearer {HF_TOKEN}"},
|
| 40 |
-
json={
|
| 41 |
-
"inputs": f"Kullanıcı: {message}\nAI:",
|
| 42 |
-
"parameters": {"max_new_tokens": 200}
|
| 43 |
-
},
|
| 44 |
-
timeout=15
|
| 45 |
-
)
|
| 46 |
-
|
| 47 |
-
if response.status_code == 200:
|
| 48 |
-
result = response.json()
|
| 49 |
-
if isinstance(result, list):
|
| 50 |
-
text = result[0].get('generated_text', '')
|
| 51 |
-
if text:
|
| 52 |
-
return text
|
| 53 |
-
|
| 54 |
-
return f"🤖 '{message}' hakkında düşünüyorum..."
|
| 55 |
-
|
| 56 |
-
except:
|
| 57 |
-
return f"🤖 '{message}' hakkında düşünüyorum..."
|
| 58 |
-
|
| 59 |
@app.get("/")
|
| 60 |
def home():
|
| 61 |
-
return {
|
| 62 |
-
"service": "ZenkaMind AI",
|
| 63 |
-
"ai_mode": "real" if HF_TOKEN else "demo",
|
| 64 |
-
"status": "running"
|
| 65 |
-
}
|
| 66 |
|
| 67 |
@app.get("/health")
|
| 68 |
def health():
|
| 69 |
-
return {
|
| 70 |
-
"status": "healthy",
|
| 71 |
-
"ai_available": bool(HF_TOKEN),
|
| 72 |
-
"timestamp": datetime.now().isoformat()
|
| 73 |
-
}
|
| 74 |
|
| 75 |
@app.post("/api/chat")
|
| 76 |
async def chat(request: Request):
|
|
@@ -103,8 +65,8 @@ async def chat(request: Request):
|
|
| 103 |
if user["count"] >= 100:
|
| 104 |
return JSONResponse(429, {"error": "Daily limit reached"})
|
| 105 |
|
| 106 |
-
#
|
| 107 |
-
ai_response =
|
| 108 |
|
| 109 |
user["count"] += 1
|
| 110 |
user["total"] += 1
|
|
@@ -113,7 +75,6 @@ async def chat(request: Request):
|
|
| 113 |
return {
|
| 114 |
"response": ai_response,
|
| 115 |
"status": "success",
|
| 116 |
-
"ai_mode": "real" if HF_TOKEN else "demo",
|
| 117 |
"remaining": 100 - user["count"]
|
| 118 |
}
|
| 119 |
|
|
@@ -125,6 +86,6 @@ def stats():
|
|
| 125 |
db = load_db()
|
| 126 |
return {
|
| 127 |
"users": len(db),
|
| 128 |
-
"
|
| 129 |
-
"
|
| 130 |
}
|
|
|
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
import json
|
| 5 |
import os
|
|
|
|
| 6 |
from datetime import datetime, date
|
| 7 |
|
| 8 |
+
app = FastAPI()
|
| 9 |
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"])
|
| 10 |
|
| 11 |
DB_PATH = "users.json"
|
|
|
|
| 12 |
|
| 13 |
def load_db():
|
| 14 |
try:
|
|
|
|
| 26 |
except:
|
| 27 |
pass
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
@app.get("/")
|
| 30 |
def home():
|
| 31 |
+
return {"status": "ok", "service": "ZenkaMind"}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
@app.get("/health")
|
| 34 |
def health():
|
| 35 |
+
return {"status": "healthy", "time": datetime.now().isoformat()}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
@app.post("/api/chat")
|
| 38 |
async def chat(request: Request):
|
|
|
|
| 65 |
if user["count"] >= 100:
|
| 66 |
return JSONResponse(429, {"error": "Daily limit reached"})
|
| 67 |
|
| 68 |
+
# MODEL YANITI - SADECE MESAJI EKO YAP
|
| 69 |
+
ai_response = message
|
| 70 |
|
| 71 |
user["count"] += 1
|
| 72 |
user["total"] += 1
|
|
|
|
| 75 |
return {
|
| 76 |
"response": ai_response,
|
| 77 |
"status": "success",
|
|
|
|
| 78 |
"remaining": 100 - user["count"]
|
| 79 |
}
|
| 80 |
|
|
|
|
| 86 |
db = load_db()
|
| 87 |
return {
|
| 88 |
"users": len(db),
|
| 89 |
+
"total_messages": sum(u.get("total", 0) for u in db.values()),
|
| 90 |
+
"status": "ok"
|
| 91 |
}
|