Zenkad commited on
Commit
ef509a8
·
verified ·
1 Parent(s): 06acd43

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -14
app.py CHANGED
@@ -4,11 +4,13 @@ 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,13 +28,43 @@ def save_db(data):
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,8 +97,8 @@ 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
@@ -79,13 +111,4 @@ async def chat(request: Request):
79
  }
80
 
81
  except Exception as e:
82
- return JSONResponse(500, {"error": str(e)})
83
-
84
- @app.get("/api/stats")
85
- def stats():
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
- }
 
4
  import json
5
  import os
6
  from datetime import datetime, date
7
+ import requests
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
  except:
29
  pass
30
 
31
+ def get_model_response(message):
32
+ """Model yanıtı"""
33
+ if not HF_TOKEN:
34
+ return "Model hazır değil."
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": message,
42
+ "parameters": {"max_new_tokens": 100}
43
+ },
44
+ timeout=30
45
+ )
46
+
47
+ if response.status_code == 200:
48
+ result = response.json()
49
+ if isinstance(result, list):
50
+ return result[0].get('generated_text', 'Yanıt alınamadı')
51
+
52
+ return "Model yanıt veremedi."
53
+
54
+ except:
55
+ return "Model hatası."
56
+
57
  @app.get("/")
58
  def home():
59
+ return {"status": "ok"}
60
 
61
  @app.get("/health")
62
  def health():
63
+ return {
64
+ "status": "healthy",
65
+ "model": "ready" if HF_TOKEN else "no_token",
66
+ "time": datetime.now().isoformat()
67
+ }
68
 
69
  @app.post("/api/chat")
70
  async def chat(request: Request):
 
97
  if user["count"] >= 100:
98
  return JSONResponse(429, {"error": "Daily limit reached"})
99
 
100
+ # Model yanıtı
101
+ ai_response = get_model_response(message)
102
 
103
  user["count"] += 1
104
  user["total"] += 1
 
111
  }
112
 
113
  except Exception as e:
114
+ return JSONResponse(500, {"error": str(e)})