File size: 12,129 Bytes
8791d59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from typing import List, Dict, Any
from mcp import Resource
from datetime import datetime
import logging

logger = logging.getLogger(__name__)

class ModelResource(Resource):
    """Resource representing a loaded model"""
    def __init__(self, model_name: str, model_info: Dict[str, Any]):
        self.model_name = model_name
        self.model_info = model_info
        super().__init__(
            uri=f"model://{model_name}",
            name=model_name,
            description=f"{model_name} model information and status",
            mime_type="application/json"
        )
    
    async def get_content(self) -> str:
        """Get model information as JSON"""
        import json
        return json.dumps({
            **self.model_info,
            "timestamp": datetime.now().isoformat(),
            "uri": self.uri
        })

class StanceDetectionResource(ModelResource):
    """Resource for stance detection model"""
    def __init__(self):
        from services.stance_model_manager import stance_model_manager
        
        model_info = {
            "type": "stance_detection",
            "description": "Detects PRO/CON stance for topic-argument pairs",
            "capabilities": ["single_prediction", "batch_prediction"],
            "input_format": {"topic": "string", "argument": "string"},
            "output_format": {
                "predicted_stance": "PRO/CON",
                "confidence": "float",
                "probabilities": {"PRO": "float", "CON": "float"}
            }
        }
        
        if stance_model_manager and stance_model_manager.model_loaded:
            model_info.update({
                "loaded": True,
                "device": str(stance_model_manager.device),
                "model_id": getattr(stance_model_manager, 'model_id', 'unknown')
            })
        else:
            model_info["loaded"] = False
            
        super().__init__("stance_detection", model_info)

class KPAResource(ModelResource):
    """Resource for Keypoint-Argument matching model"""
    def __init__(self):
        from services.label_model_manager import kpa_model_manager
        
        model_info = {
            "type": "keypoint_argument_matching",
            "description": "Matches arguments with key points (apparie/non_apparie)",
            "capabilities": ["single_prediction", "batch_prediction"],
            "input_format": {"argument": "string", "key_point": "string"},
            "output_format": {
                "prediction": "0/1",
                "label": "apparie/non_apparie",
                "confidence": "float",
                "probabilities": {"non_apparie": "float", "apparie": "float"}
            }
        }
        
        if kpa_model_manager and kpa_model_manager.model_loaded:
            model_info.update({
                "loaded": True,
                "device": str(kpa_model_manager.device),
                "model_id": getattr(kpa_model_manager, 'model_id', 'unknown'),
                "max_length": getattr(kpa_model_manager, 'max_length', 256)
            })
        else:
            model_info["loaded"] = False
            
        super().__init__("kpa_matching", model_info)

class STTResource(Resource):
    """Resource for Speech-to-Text capabilities"""
    def __init__(self):
        from config import GROQ_API_KEY, GROQ_STT_MODEL
        
        super().__init__(
            uri="service://speech-to-text",
            name="speech_to_text",
            description="Speech-to-Text service using Groq Whisper API",
            mime_type="application/json"
        )
        
        self.config = {
            "provider": "Groq",
            "model": GROQ_STT_MODEL,
            "enabled": bool(GROQ_API_KEY),
            "language": "English only",
            "max_audio_size": "10MB",
            "supported_formats": ["wav", "mp3", "m4a", "mp4"]
        }
    
    async def get_content(self) -> str:
        """Get STT service information"""
        import json
        return json.dumps({
            **self.config,
            "timestamp": datetime.now().isoformat(),
            "uri": self.uri
        })

class TTSResource(Resource):
    """Resource for Text-to-Speech capabilities"""
    def __init__(self):
        from config import GROQ_API_KEY, GROQ_TTS_MODEL, GROQ_TTS_VOICE
        
        super().__init__(
            uri="service://text-to-speech",
            name="text_to_speech",
            description="Text-to-Speech service using Groq PlayAI TTS",
            mime_type="application/json"
        )
        
        self.config = {
            "provider": "Groq",
            "model": GROQ_TTS_MODEL,
            "voice": GROQ_TTS_VOICE,
            "enabled": bool(GROQ_API_KEY),
            "language": "English only",
            "format": "wav/mp3",
            "voices_available": ["Aaliyah-PlayAI", "Aria-PlayAI", "Dexter-PlayAI", "Fiona-PlayAI"]
        }
    
    async def get_content(self) -> str:
        """Get TTS service information"""
        import json
        return json.dumps({
            **self.config,
            "timestamp": datetime.now().isoformat(),
            "uri": self.uri
        })

class ChatbotResource(Resource):
    """Resource for Chatbot capabilities"""
    def __init__(self):
        from config import GROQ_API_KEY, GROQ_CHAT_MODEL
        
        super().__init__(
            uri="service://chatbot",
            name="chatbot",
            description="Chatbot service using Groq LLM API",
            mime_type="application/json"
        )
        
        self.config = {
            "provider": "Groq",
            "model": GROQ_CHAT_MODEL,
            "enabled": bool(GROQ_API_KEY),
            "language": "English only",
            "features": ["conversation", "context_awareness", "voice_chat"],
            "max_context_length": 8192
        }
    
    async def get_content(self) -> str:
        """Get chatbot service information"""
        import json
        return json.dumps({
            **self.config,
            "timestamp": datetime.now().isoformat(),
            "uri": self.uri
        })

class ArgumentGenerationResource(Resource):
    """Resource for Argument Generation model (à compléter)"""
    def __init__(self):
        super().__init__(
            uri="model://argument-generation",
            name="argument_generation",
            description="Persuasive argument generation model",
            mime_type="application/json"
        )
        
        self.config = {
            "type": "argument_generation",
            "status": "not_implemented",
            "description": "TODO: Implement your argument generation model",
            "planned_capabilities": [
                "single_argument_generation",
                "batch_generation",
                "stance_controlled_generation",
                "counter_argument_generation"
            ]
        }
    
    async def get_content(self) -> str:
        """Get argument generation model information"""
        import json
        return json.dumps({
            **self.config,
            "timestamp": datetime.now().isoformat(),
            "uri": self.uri,
            "note": "This is a placeholder. Implement your model in services/argument_generation.py"
        })

class SystemHealthResource(Resource):
    """Resource for system health and status"""
    def __init__(self):
        super().__init__(
            uri="system://health",
            name="system_health",
            description="System health and service status",
            mime_type="application/json"
        )
    
    async def get_content(self) -> str:
        """Get system health information"""
        import json
        from datetime import datetime
        
        # Collect model status
        model_status = {}
        try:
            from services.stance_model_manager import stance_model_manager
            model_status["stance_detection"] = {
                "loaded": stance_model_manager.model_loaded if stance_model_manager else False
            }
        except:
            model_status["stance_detection"] = {"loaded": False}
        
        try:
            from services.label_model_manager import kpa_model_manager
            model_status["kpa_matching"] = {
                "loaded": kpa_model_manager.model_loaded if kpa_model_manager else False
            }
        except:
            model_status["kpa_matching"] = {"loaded": False}
        
        # Service status
        from config import GROQ_API_KEY
        service_status = {
            "stt": bool(GROQ_API_KEY),
            "tts": bool(GROQ_API_KEY),
            "chatbot": bool(GROQ_API_KEY),
            "argument_generation": False  # À implémenter
        }
        
        return json.dumps({
            "timestamp": datetime.now().isoformat(),
            "status": "operational",
            "models": model_status,
            "services": service_status,
            "api_version": "1.0.0",
            "mcp_version": "1.0.0"
        })

class APIDocumentationResource(Resource):
    """Resource for API documentation"""
    def __init__(self):
        super().__init__(
            uri="documentation://api",
            name="api_documentation",
            description="API endpoints documentation",
            mime_type="application/json"
        )
        
        self.documentation = {
            "endpoints": {
                "mcp": {
                    "/mcp/health": "GET - Health check",
                    "/mcp/resources": "GET - List all resources",
                    "/mcp/tools": "GET - List all tools",
                    "/mcp/tools/call": "POST - Call a tool"
                },
                "models": {
                    "/api/v1/kpa/predict": "POST - KPA prediction",
                    "/api/v1/stance/predict": "POST - Stance prediction",
                    "/api/v1/stance/batch-predict": "POST - Batch stance prediction"
                },
                "voice": {
                    "/api/v1/stt/": "POST - Speech to text",
                    "/api/v1/tts/": "POST - Text to speech",
                    "/voice-chat/voice": "POST - Voice chat",
                    "/voice-chat/text": "POST - Text chat"
                }
            },
            "authentication": "Currently none (add JWT or API key based auth)",
            "rate_limits": "None configured",
            "version": "2.0.0"
        }
    
    async def get_content(self) -> str:
        """Get API documentation"""
        import json
        return json.dumps({
            **self.documentation,
            "timestamp": datetime.now().isoformat(),
            "uri": self.uri
        })

def get_resources() -> List[Resource]:
    """Return all available MCP resources"""
    resources = []
    
    try:
        resources.append(StanceDetectionResource())
    except Exception as e:
        logger.warning(f"Failed to create StanceDetectionResource: {e}")
    
    try:
        resources.append(KPAResource())
    except Exception as e:
        logger.warning(f"Failed to create KPAResource: {e}")
    
    try:
        resources.append(STTResource())
    except Exception as e:
        logger.warning(f"Failed to create STTResource: {e}")
    
    try:
        resources.append(TTSResource())
    except Exception as e:
        logger.warning(f"Failed to create TTSResource: {e}")
    
    try:
        resources.append(ChatbotResource())
    except Exception as e:
        logger.warning(f"Failed to create ChatbotResource: {e}")
    
    try:
        resources.append(ArgumentGenerationResource())
    except Exception as e:
        logger.warning(f"Failed to create ArgumentGenerationResource: {e}")
    
    try:
        resources.append(SystemHealthResource())
    except Exception as e:
        logger.warning(f"Failed to create SystemHealthResource: {e}")
    
    try:
        resources.append(APIDocumentationResource())
    except Exception as e:
        logger.warning(f"Failed to create APIDocumentationResource: {e}")
    
    logger.info(f"Created {len(resources)} MCP resources")
    return resources