File size: 15,258 Bytes
90e814c
 
0a411fa
90e814c
c8d8745
0a411fa
90e814c
c8d8745
ef509a8
5051bf7
90e814c
d472182
5051bf7
90e814c
 
 
 
5051bf7
c4facf3
90e814c
 
 
 
 
 
5051bf7
90e814c
5051bf7
 
90e814c
 
5051bf7
 
90e814c
5051bf7
 
90e814c
c8d8745
ef509a8
5051bf7
 
 
90e814c
5051bf7
90e814c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c4facf3
c8d8745
5051bf7
c8d8745
 
5051bf7
c8d8745
5051bf7
 
baa1b25
c8d8745
c4facf3
c8d8745
5051bf7
c8d8745
5051bf7
 
 
 
 
 
775dd9d
90e814c
 
ef509a8
90e814c
ef509a8
 
90e814c
 
5051bf7
ef509a8
5051bf7
 
 
 
 
ef509a8
90e814c
5051bf7
 
 
90e814c
5051bf7
 
 
ef509a8
90e814c
ef509a8
 
90e814c
5051bf7
ef509a8
 
5051bf7
90e814c
ef509a8
90e814c
 
 
 
 
5051bf7
90e814c
 
 
 
5051bf7
90e814c
 
 
 
 
 
5051bf7
90e814c
5051bf7
 
90e814c
5051bf7
 
90e814c
ef509a8
5051bf7
90e814c
5051bf7
90e814c
 
5051bf7
 
90e814c
 
5051bf7
90e814c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5051bf7
90e814c
 
 
5051bf7
90e814c
 
 
 
 
 
 
 
5051bf7
90e814c
 
 
 
 
 
5051bf7
90e814c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c4facf3
c8d8745
90e814c
 
5051bf7
90e814c
5051bf7
ef509a8
90e814c
5051bf7
90e814c
 
5051bf7
90e814c
 
 
 
 
ef509a8
ac04e02
90e814c
 
 
 
 
 
 
 
 
 
 
 
 
 
c8d8745
 
5051bf7
92bb2fe
c8d8745
 
 
 
90e814c
5051bf7
 
c8d8745
90e814c
c8d8745
 
90e814c
5051bf7
90e814c
 
c8d8745
90e814c
c8d8745
5051bf7
c8d8745
 
 
0912d9c
 
5051bf7
90e814c
92bb2fe
c8d8745
 
 
90e814c
5051bf7
0912d9c
5051bf7
c8d8745
5051bf7
90e814c
5051bf7
90e814c
 
0912d9c
 
5051bf7
c8d8745
 
 
 
baa1b25
5051bf7
 
 
90e814c
5051bf7
 
90e814c
 
5051bf7
90e814c
 
5051bf7
 
90e814c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5051bf7
90e814c
 
 
 
 
 
 
 
 
 
5051bf7
90e814c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5051bf7
 
 
 
90e814c
 
 
 
 
 
 
 
 
 
 
 
 
5051bf7
90e814c
5051bf7
90e814c
 
 
5051bf7
90e814c
 
5051bf7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
from fastapi import FastAPI, Request, HTTPException
from fastapi.responses import JSONResponse, HTMLResponse
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
import json
import os
import socket
from datetime import datetime, date
import requests
import logging
import threading

# Logging ayarı
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)

app = FastAPI(
    title="AI Chat API",
    version="2.0.0",
    docs_url="/docs",
    redoc_url="/redoc"
)

# TÜM CORS izinleri - Telefondan erişim için
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # Tüm domain'lere izin ver
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
    expose_headers=["*"]
)

# Dosya yolları
DB_PATH = "users.json"
HF_TOKEN = os.getenv("HF_TOKEN", "")
MODEL_NAME = "mistralai/Mistral-7B-Instruct-v0.2"
API_URL = f"https://api-inference.huggingface.co/models/{MODEL_NAME}"

# Limitler
DAILY_LIMIT = 100
MAX_TOKENS = 150

def get_local_ip():
    """Bilgisayarın IP adresini bul"""
    try:
        # İnternet bağlantısı olan bir socket oluştur
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.connect(("8.8.8.8", 80))  # Google DNS
        ip = s.getsockname()[0]
        s.close()
        return ip
    except:
        return "127.0.0.1"

# Sunucu bilgileri
SERVER_IP = get_local_ip()
SERVER_PORT = 8000
BASE_URL = f"http://{SERVER_IP}:{SERVER_PORT}"

# QR kodu oluşturmak için
def generate_qr_code_url():
    """Telefonda açmak için QR kodu URL'si"""
    import urllib.parse
    html_url = f"{BASE_URL}/test"
    qr_url = f"https://api.qrserver.com/v1/create-qr-code/?size=200x200&data={urllib.parse.quote(html_url)}"
    return qr_url

def load_db():
    """Kullanıcı veritabanını yükle"""
    try:
        if os.path.exists(DB_PATH):
            with open(DB_PATH, "r", encoding="utf-8") as f:
                return json.load(f)
    except Exception as e:
        logger.error(f"Veritabanı yükleme hatası: {e}")
        return {}
    return {}

def save_db(data):
    """Kullanıcı veritabanını kaydet"""
    try:
        with open(DB_PATH, "w", encoding="utf-8") as f:
            json.dump(data, f, indent=2, ensure_ascii=False)
        return True
    except Exception as e:
        logger.error(f"Veritabanı kaydetme hatası: {e}")
        return False

def get_model_response_simple(message):
    """Basit model yanıtı - Hata toleranslı"""
    if not HF_TOKEN:
        return "🔑 API token bulunamadı. Lütfen HF_TOKEN ayarlayın."
    
    try:
        # Daha kısa ve basit prompt
        prompt = f"Kullanıcı soruyor: {message}\nYanıt:"
        
        response = requests.post(
            API_URL,
            headers={
                "Authorization": f"Bearer {HF_TOKEN}",
                "Content-Type": "application/json"
            },
            json={
                "inputs": prompt,
                "parameters": {
                    "max_new_tokens": MAX_TOKENS,
                    "temperature": 0.7,
                    "top_p": 0.9,
                    "do_sample": True,
                    "return_full_text": False
                }
            },
            timeout=15
        )
        
        logger.info(f"API Status: {response.status_code}")
        
        if response.status_code == 200:
            result = response.json()
            
            # Farklı formatları işle
            if isinstance(result, list):
                for item in result:
                    if isinstance(item, dict):
                        # Format: [{'generated_text': '...'}]
                        if 'generated_text' in item:
                            text = item['generated_text']
                            if isinstance(text, str):
                                # Prompt'u temizle
                                text = text.replace(prompt, "").strip()
                                if text:
                                    return text
                        
                        # Diğer formatlar
                        for key, value in item.items():
                            if isinstance(value, str) and len(value) > 10:
                                cleaned = value.replace(prompt, "").strip()
                                if cleaned:
                                    return cleaned
            
            return "🤖 Yanıt alındı ancak işlenemedi."
        
        elif response.status_code == 503:
            return "⏳ Model yükleniyor, lütfen 15 saniye bekleyin..."
        
        elif response.status_code == 429:
            return "🚫 Çok fazla istek, lütfen bekleyin."
        
        else:
            return f"❌ API hatası: {response.status_code}"
    
    except requests.exceptions.Timeout:
        return "⏱️ Zaman aşımı, lütfen daha kısa soru sorun."
    
    except Exception as e:
        logger.error(f"Model hatası: {e}")
        return f"⚠️ Teknik hata: {str(e)[:50]}"

@app.get("/", response_class=HTMLResponse)
async def home():
    """Ana sayfa - QR kodu ve bağlantılar"""
    html_content = f"""
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>AI Chat API - Telefon Erişimi</title>
        <style>
            body {{
                font-family: Arial, sans-serif;
                max-width: 800px;
                margin: 0 auto;
                padding: 20px;
                background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
                min-height: 100vh;
                color: white;
            }}
            .container {{
                background: rgba(255,255,255,0.95);
                border-radius: 20px;
                padding: 30px;
                color: #333;
                box-shadow: 0 10px 40px rgba(0,0,0,0.3);
            }}
            h1 {{ color: #4f46e5; }}
            .qr-container {{
                text-align: center;
                margin: 30px 0;
                padding: 20px;
                background: #f8fafc;
                border-radius: 15px;
            }}
            .qr-code {{
                width: 200px;
                height: 200px;
                margin: 0 auto;
                background: white;
                padding: 10px;
                border-radius: 10px;
            }}
            .link-box {{
                background: #e0f2fe;
                padding: 15px;
                border-radius: 10px;
                margin: 10px 0;
                word-break: break-all;
            }}
            .btn {{
                display: inline-block;
                background: #4f46e5;
                color: white;
                padding: 12px 24px;
                text-decoration: none;
                border-radius: 10px;
                margin: 5px;
                font-weight: bold;
            }}
            .status {{
                padding: 10px;
                border-radius: 8px;
                margin: 10px 0;
            }}
            .success {{ background: #d1fae5; color: #065f46; }}
            .warning {{ background: #fef3c7; color: #92400e; }}
            .error {{ background: #fee2e2; color: #dc2626; }}
        </style>
    </head>
    <body>
        <div class="container">
            <h1>🤖 AI Chat API</h1>
            <p>Telefonunuzdan bağlanmak için:</p>
            
            <div class="status {'success' if HF_TOKEN else 'warning'}">
                <strong>Model Durumu:</strong> {'✅ Hazır' if HF_TOKEN else '⚠️ Token Eksik'}
            </div>
            
            <div class="qr-container">
                <h3>📱 Telefon ile Bağlan</h3>
                <p>Telefon kameranızla bu QR kodu tarayın:</p>
                <div class="qr-code">
                    <img src="{generate_qr_code_url()}" width="200" height="200">
                </div>
                <p style="font-size: 14px; color: #666;">veya bağlantıya tıklayın</p>
            </div>
            
            <h3>🔗 Bağlantılar:</h3>
            <div class="link-box">
                <strong>Test Arayüzü:</strong><br>
                <a href="/test" class="btn">📲 Aç</a>
                <a href="{BASE_URL}/test" target="_blank">{BASE_URL}/test</a>
            </div>
            
            <div class="link-box">
                <strong>API Dokümantasyon:</strong><br>
                <a href="/docs" class="btn">📚 Aç</a>
                <a href="{BASE_URL}/docs" target="_blank">{BASE_URL}/docs</a>
            </div>
            
            <div class="link-box">
                <strong>Health Check:</strong><br>
                <a href="/health" class="btn">🩺 Aç</a>
                <a href="{BASE_URL}/health" target="_blank">{BASE_URL}/health</a>
            </div>
            
            <div style="margin-top: 30px; padding-top: 20px; border-top: 2px solid #e5e7eb;">
                <h4>📋 Bilgiler:</h4>
                <p><strong>IP Adresi:</strong> {SERVER_IP}</p>
                <p><strong>Port:</strong> {SERVER_PORT}</p>
                <p><strong>Model:</strong> {MODEL_NAME}</p>
                <p><strong>Günlük Limit:</strong> {DAILY_LIMIT} sorgu/kullanıcı</p>
            </div>
        </div>
    </body>
    </html>
    """
    return HTMLResponse(content=html_content)

@app.get("/health")
async def health():
    """Sağlık kontrolü - JSON formatında"""
    db_status = "exists" if os.path.exists(DB_PATH) else "missing"
    token_exists = bool(HF_TOKEN)
    
    return {
        "status": "healthy" if token_exists else "no_token",
        "timestamp": datetime.now().isoformat(),
        "server_ip": SERVER_IP,
        "port": SERVER_PORT,
        "model": MODEL_NAME,
        "token": "valid" if token_exists else "missing",
        "database": db_status,
        "daily_limit": DAILY_LIMIT,
        "api_url": f"http://{SERVER_IP}:{SERVER_PORT}",
        "test_url": f"http://{SERVER_IP}:{SERVER_PORT}/test"
    }

@app.get("/test", response_class=HTMLResponse)
async def test_page():
    """Test arayüzü sayfası"""
    with open("test.html", "r", encoding="utf-8") as f:
        html_content = f.read()
    
    # API URL'sini dinamik olarak ayarla
    html_content = html_content.replace(
        'const API_BASE_URL = \'http://localhost:8000\';',
        f'const API_BASE_URL = \'http://{SERVER_IP}:{SERVER_PORT}\';'
    )
    
    return HTMLResponse(content=html_content)

@app.post("/api/chat")
async def chat(request: Request):
    """Chat endpoint'i"""
    try:
        data = await request.json()
        email = data.get("email", "").strip().lower()
        message = data.get("message", "").strip()
        
        logger.info(f"Chat isteği: {email} - {message[:30]}...")
        
        # Validasyon
        if not email or "@" not in email:
            raise HTTPException(400, "Geçerli email gerekli")
        
        if not message:
            raise HTTPException(400, "Mesaj boş olamaz")
        
        if len(message) > 500:
            raise HTTPException(400, "Mesaj çok uzun (max 500 karakter)")
        
        # Veritabanı
        db = load_db()
        today = date.today().isoformat()
        
        if email not in db:
            db[email] = {
                "count": 0,
                "total": 0,
                "last_reset": today,
                "created": datetime.now().isoformat()
            }
        
        user = db[email]
        
        # Limit kontrol
        if user["last_reset"] != today:
            user["count"] = 0
            user["last_reset"] = today
        
        if user["count"] >= DAILY_LIMIT:
            raise HTTPException(429, f"Günlük limit ({DAILY_LIMIT}) doldu")
        
        # Model yanıtı
        ai_response = get_model_response_simple(message)
        user["count"] += 1
        user["total"] += 1
        
        save_db(db)
        
        return {
            "response": ai_response,
            "status": "success",
            "remaining": DAILY_LIMIT - user["count"],
            "used_today": user["count"],
            "total_used": user["total"],
            "timestamp": datetime.now().isoformat()
        }
        
    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"Chat hatası: {e}")
        raise HTTPException(500, f"Sunucu hatası: {str(e)}")

@app.get("/api/stats/{email}")
async def get_stats(email: str):
    """İstatistikler"""
    email = email.strip().lower()
    db = load_db()
    
    if email not in db:
        raise HTTPException(404, "Kullanıcı bulunamadı")
    
    user = db[email]
    today = date.today().isoformat()
    
    if user["last_reset"] != today:
        user["count"] = 0
        user["last_reset"] = today
        save_db(db)
    
    return {
        "email": email,
        "used_today": user["count"],
        "remaining_today": DAILY_LIMIT - user["count"],
        "total_used": user["total"],
        "last_reset": user["last_reset"],
        "created": user.get("created", "unknown")
    }

@app.get("/api/reset/{email}")
async def reset_limit(email: str):
    """Limit sıfırlama (test için)"""
    email = email.strip().lower()
    db = load_db()
    
    if email in db:
        db[email]["count"] = 0
        db[email]["last_reset"] = date.today().isoformat()
        save_db(db)
        
        return {"status": "success", "message": f"{email} limiti sıfırlandı"}
    else:
        raise HTTPException(404, "Kullanıcı bulunamadı")

@app.get("/api/info")
async def api_info():
    """API bilgileri"""
    return {
        "api_name": "AI Chat API",
        "version": "2.0.0",
        "model": MODEL_NAME,
        "server_ip": SERVER_IP,
        "port": SERVER_PORT,
        "base_url": f"http://{SERVER_IP}:{SERVER_PORT}",
        "endpoints": {
            "home": "/",
            "test_ui": "/test",
            "chat": "POST /api/chat",
            "health": "GET /health",
            "stats": "GET /api/stats/{email}",
            "reset": "GET /api/reset/{email}",
            "docs": "/docs"
        }
    }

if __name__ == "__main__":
    import uvicorn
    
    logger.info("=" * 60)
    logger.info("🤖 AI Chat API Başlatılıyor...")
    logger.info(f"📡 Sunucu IP: {SERVER_IP}")
    logger.info(f"🔌 Port: {SERVER_PORT}")
    logger.info(f"🧠 Model: {MODEL_NAME}")
    logger.info(f"🔑 Token: {'✅ Var' if HF_TOKEN else '❌ Yok'}")
    logger.info(f"📊 Günlük Limit: {DAILY_LIMIT}")
    logger.info("=" * 60)
    logger.info("📱 Telefon bağlantıları:")
    logger.info(f"   • Test Arayüzü: http://{SERVER_IP}:{SERVER_PORT}/test")
    logger.info(f"   • API Doküman: http://{SERVER_IP}:{SERVER_PORT}/docs")
    logger.info(f"   • Health Check: http://{SERVER_IP}:{SERVER_PORT}/health")
    logger.info("=" * 60)
    
    # Uvicorn'u başlat
    uvicorn.run(
        app,
        host="0.0.0.0",  # Tüm ağlardan erişime izin ver
        port=SERVER_PORT,
        reload=True,
        log_level="info",
        access_log=True
    )