Zenkad commited on
Commit
38d047e
·
verified ·
1 Parent(s): baa1b25

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -6
app.py CHANGED
@@ -3,12 +3,14 @@ from fastapi.responses import JSONResponse
3
  from fastapi.middleware.cors import CORSMiddleware
4
  import json
5
  import os
 
6
  from datetime import datetime, date
7
 
8
- app = FastAPI(title="ZenkaMind", version="1.0")
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,13 +28,49 @@ def save_db(data):
26
  except:
27
  pass
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  @app.get("/")
30
  def home():
31
- return {"status": "running", "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,7 +103,8 @@ async def chat(request: Request):
65
  if user["count"] >= 100:
66
  return JSONResponse(429, {"error": "Daily limit reached"})
67
 
68
- ai_response = f"AI: '{message}' hakkında düşünüyorum..."
 
69
 
70
  user["count"] += 1
71
  user["total"] += 1
@@ -74,6 +113,7 @@ async def chat(request: Request):
74
  return {
75
  "response": ai_response,
76
  "status": "success",
 
77
  "remaining": 100 - user["count"]
78
  }
79
 
@@ -85,6 +125,6 @@ def stats():
85
  db = load_db()
86
  return {
87
  "users": len(db),
88
- "total_messages": sum(u.get("total", 0) for u in db.values()),
89
- "status": "ok"
90
  }
 
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(title="ZenkaMind AI", version="2.0")
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
  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
  if user["count"] >= 100:
104
  return JSONResponse(429, {"error": "Daily limit reached"})
105
 
106
+ # GERÇEK AI YANITI
107
+ ai_response = get_ai_response(message)
108
 
109
  user["count"] += 1
110
  user["total"] += 1
 
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
  db = load_db()
126
  return {
127
  "users": len(db),
128
+ "ai_mode": "real" if HF_TOKEN else "demo",
129
+ "token_set": bool(HF_TOKEN)
130
  }