File size: 11,228 Bytes
25e927f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
CRANE AI - Ana Sistem (yeniden oluşturuldu)
"""

import asyncio
import logging
import socket
from typing import Dict, Any, List
from datetime import datetime
from contextlib import asynccontextmanager

from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
import uvicorn
import gradio as gr

from config.settings import (
    MODELS,
    ROUTER_CONFIG,
    SYSTEM_LIMITS,
    MEMORY_CONFIG,
    DEVICE,
    HF_TOKEN,
    API_CONFIG,
)
from modules.code_module import CodeModule
from modules.chat_module import ChatModule  
from modules.reason_module import ReasonModule
from modules.fast_module import FastModule
from router.intelligent_router import IntelligentRouter
from core.token_capsule import TokenCapsuleLayer
from memory.local_memory import LocalMemoryManager

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

# -------------------------------------------------
# Yardımcı Fonksiyonlar
# -------------------------------------------------

def find_free_port(start_port: int = 7860, max_port: int = 7870) -> int:
    """Belirtilen aralıkta boş port bulur"""
    for port in range(start_port, max_port + 1):
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
            if sock.connect_ex(("localhost", port)) != 0:
                return port
    raise RuntimeError("Boş port bulunamadı")

# -------------------------------------------------
# Pydantic Modeller
# -------------------------------------------------

class QueryRequest(BaseModel):
    query: str
    context: Dict[str, Any] = {}
    user_id: str = "default"

class QueryResponse(BaseModel):
    response: str
    module_used: str
    confidence: float
    execution_time: float
    routing_info: Dict[str, Any] = {}

# -------------------------------------------------
# Ana Sistem Sınıfı
# -------------------------------------------------

class CRANEAISystem:
    """CRANE AI ana orkestratör sınıfı"""
    
    def __init__(self):
        self.modules: Dict[str, Any] = {}
        self.router: IntelligentRouter | None = None
        self.token_layer: TokenCapsuleLayer | None = None
        self.memory_manager: LocalMemoryManager | None = None
        self.is_initialized: bool = False
        
        self.stats = {
            "total_queries": 0,
            "successful_queries": 0,
            "failed_queries": 0,
            "avg_response_time": 0.0,
            "uptime_start": datetime.now(),
        }
        
    # ------------- Başlat / Durdur -------------

    async def initialize(self):
        """Sistemdeki tüm alt bileşenleri başlatır"""
            logger.info("🚀 CRANE AI sistemi başlatılıyor...")
            await self._load_modules()
            await self._initialize_router()
            await self._initialize_token_layer()
            await self._initialize_memory()
            self.is_initialized = True
        logger.info("✅ CRANE AI sistemi hazır!")

    async def shutdown(self):
        """Sistemi kapatır ve kaynakları temizler"""
        logger.info("🔄 Sistem kapatılıyor...")
        if self.memory_manager:
            await self.memory_manager.stop()
        for module in self.modules.values():
            if hasattr(module, "unload_model"):
                module.unload_model()
        logger.info("👋 Sistem kapatıldı")

    # ------------- Alt Bileşen Başlatma -------------
    
    async def _load_modules(self):
        logger.info("📦 MicroModule'lar yükleniyor...")
        for name, cfg in MODELS.items():
            try:
                config = {**cfg, "device": DEVICE, "hf_token": HF_TOKEN}
                if name == "code_module":
                    module = CodeModule(config)
                elif name == "chat_module":
                    module = ChatModule(config)
                elif name == "reason_module":
                    module = ReasonModule(config)
                elif name == "fast_module":
                    module = FastModule(config)
                else:
                    continue
                self.modules[name] = module
                logger.info(f"  ✅ {name} yüklendi")
            except Exception as e:
                logger.error(f"  ❌ {name} yükleme hatası: {e}")
        logger.info(f"📦 {len(self.modules)} modül yüklendi")
    
    async def _initialize_router(self):
        logger.info("🎯 Router başlatılıyor...")
        self.router = IntelligentRouter(self.modules, ROUTER_CONFIG)
        logger.info("🎯 Router başlatıldı")
    
    async def _initialize_token_layer(self):
        logger.info("🔤 Token Capsule Layer başlatılıyor...")
        self.token_layer = TokenCapsuleLayer({
            "max_length": SYSTEM_LIMITS["max_input_length"],
            "device": DEVICE,
            "hf_token": HF_TOKEN,
            "cache_size": 500,
        })
        logger.info("🔤 Token Capsule Layer başlatıldı")
    
    async def _initialize_memory(self):
        logger.info("🧠 Memory Manager başlatılıyor...")
        self.memory_manager = LocalMemoryManager(MEMORY_CONFIG)
        await self.memory_manager.start()
        logger.info("🧠 Memory Manager başlatıldı")
    
    # ------------- Sorgu İşleme -------------
        
    async def process_query(self, query: str, context: Dict[str, Any] | None = None, user_id: str = "default") -> Dict[str, Any]:
        start = datetime.now()
        context = context or {}
        try:
            if not self.is_initialized:
                raise RuntimeError("Sistem henüz başlatılmadı")
            
            # Geçmişi al
            user_history = await self.memory_manager.retrieve(f"history_{user_id}") if self.memory_manager else None
            if user_history:
                context["history"] = user_history
            
            result = await self.router.route_query(query, context) if self.router else {"error": "Router yok"}

            # Geçmişi güncelle
            if self.memory_manager:
                updated_history: List[str] = user_history or []
                updated_history.append(query)
                if len(updated_history) > 10:
                    updated_history = updated_history[-10:]
                await self.memory_manager.store(f"history_{user_id}", updated_history, ttl=86_400)
            
            exec_time = (datetime.now() - start).total_seconds()
            self._update_stats(exec_time, "error" not in result)

            if "error" in result:
                raise RuntimeError(result["error"])

            return {
                "response": result.get("response", "Yanıt alınamadı"),
                "module_used": result.get("module", "unknown"),
                "confidence": result.get("confidence", 0.0),
                "execution_time": exec_time,
                "routing_info": result.get("routing_info", {}),
            }
        except Exception as e:
            exec_time = (datetime.now() - start).total_seconds()
            self._update_stats(exec_time, False)
            logger.error(f"Sorgu hatası: {e}")
            return {"error": str(e), "execution_time": exec_time}

    # ------------- Yardımcılar -------------
    
    def _update_stats(self, exec_time: float, success: bool):
        self.stats["total_queries"] += 1
        if success:
            self.stats["successful_queries"] += 1
        else:
            self.stats["failed_queries"] += 1
        total_time = self.stats["avg_response_time"] * (self.stats["total_queries"] - 1)
        self.stats["avg_response_time"] = (total_time + exec_time) / self.stats["total_queries"]
    
    def get_system_stats(self) -> Dict[str, Any]:
        uptime = datetime.now() - self.stats["uptime_start"]
        return {
            "system_info": {
                "uptime": str(uptime),
                "is_initialized": self.is_initialized,
                "device": DEVICE,
            },
            "query_stats": self.stats,
            "router": self.router.get_stats() if self.router else {},
            "token": self.token_layer.get_stats() if self.token_layer else {},
        }

# -------------------------------------------------
# CRANE Sistemi Global Örnek
# -------------------------------------------------

crane_system = CRANEAISystem()

# -------------------------------------------------
# FastAPI Uygulaması (lifespan)
# -------------------------------------------------

@asynccontextmanager
async def lifespan(app: FastAPI):
    await crane_system.initialize()
    yield
    await crane_system.shutdown()

app = FastAPI(title="CRANE AI API", version="1.0.0", lifespan=lifespan)

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# ------ API Endpoints ------

@app.post("/query", response_model=QueryResponse)
async def query_endpoint(req: QueryRequest):
    result = await crane_system.process_query(req.query, req.context, req.user_id)
        if "error" in result:
            raise HTTPException(status_code=500, detail=result["error"])
        return QueryResponse(**result)

@app.get("/stats")
async def stats_endpoint():
    return crane_system.get_system_stats()

@app.get("/health")
async def health():
    return {"status": "initialized" if crane_system.is_initialized else "initializing"}

# -------------------------------------------------
# Gradio Arayüzü
# -------------------------------------------------

async def gradio_interface(message: str, history: List[Dict[str, str]] | None = None):
    res = await crane_system.process_query(message, {"history": history or []})
    if "error" in res:
        return f"❌ Hata: {res['error']}"
    return f"**Yanıt:**\n{res['response']}\n\n**Sistem Bilgileri:**\n- Kullanılan Modül: {res['module_used']}\n- Güven Skoru: {res['confidence']:.2f}\n- Süre: {res['execution_time']:.2f}s"
        
demo = gr.ChatInterface(
    fn=gradio_interface,
    type="messages",
    title="🏗️ CRANE AI - Hibrit Yapay Zeka Sistemi",
    description="Compressed Routing and Neural Embedding ile güçlendirilmiş AI sistemi",
    theme=gr.themes.Soft(),
    examples=[
        "Merhaba! Nasılsın?",
        "Python'da bir hesap makinesi yaz",
        "Yapay zeka nedir?",
        "Bir web sitesi tasarla",
        "Bugün hava nasıl?",
    ],
)

# -------------------------------------------------
# Ana Async Fonksiyon (API + Gradio)
# -------------------------------------------------

async def main():
    # Boş port bularak Gradio'yu başlat
    gradio_port = find_free_port(7860, 7870)
    logger.info(f"🌐 Gradio port: {gradio_port}")
        
    # Gradio'yu background thread'de başlat
    demo.launch(server_name="0.0.0.0", server_port=gradio_port, share=False, inbrowser=False)

    # FastAPI sunucusunu başlat
    config = uvicorn.Config(app=app, host=API_CONFIG["host"], port=API_CONFIG["port"], workers=API_CONFIG["workers"])
        server = uvicorn.Server(config)
        await server.serve()

if __name__ == "__main__":
    asyncio.run(main())