File size: 10,778 Bytes
f74bbd3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import json
import asyncio
from typing import Dict, List, Any, Optional, Union
from fastapi import FastAPI, HTTPException, UploadFile, File
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import StreamingResponse
from pydantic import BaseModel
import uvicorn

# Model imports
try:
    from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
    import torch
except ImportError:
    pipeline = None

# Initialize FastAPI app
app = FastAPI(
    title="AI Model Runner API",
    description="Multi-purpose AI API for code understanding, dialogue, and reasoning",
    version="1.0.0"
)

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

# Global model storage
models = {}

class ChatMessage(BaseModel):
    role: str
    content: str

class ChatRequest(BaseModel):
    messages: List[ChatMessage]
    model: Optional[str] = "microsoft/DialoGPT-medium"
    max_length: Optional[int] = 100
    temperature: Optional[float] = 0.7

class CodeRequest(BaseModel):
    code: str
    task: str  # "explain", "refactor", "debug", "optimize"
    language: Optional[str] = "python"

class ReasoningRequest(BaseModel):
    problem: str
    context: Optional[str] = ""
    steps: Optional[int] = 5

class ModelInfo(BaseModel):
    name: str
    type: str
    description: str
    loaded: bool

@app.on_event("startup")
async def startup_event():
    """Initialize models on startup"""
    await load_models()

async def load_models():
    """Load commonly used models"""
    global models
    
    if pipeline is None:
        print("Transformers not available, running in mock mode")
        return
    
    try:
        # Load dialogue model
        print("Loading dialogue model...")
        models["dialogue"] = pipeline(
            "conversational", 
            model="microsoft/DialoGPT-medium"
        )
        
        # Load text generation model
        print("Loading text generation model...")
        models["text_gen"] = pipeline(
            "text-generation",
            model="gpt2",
            do_sample=True,
            max_length=200
        )
        
        # Load sentiment analysis
        print("Loading sentiment model...")
        models["sentiment"] = pipeline(
            "sentiment-analysis",
            model="distilbert-base-uncased-finetuned-sst-2-english"
        )
        
        print("Models loaded successfully!")
        
    except Exception as e:
        print(f"Error loading models: {e}")
        print("Running in mock mode")

@app.get("/")
async def root():
    """Root endpoint with API information"""
    return {
        "message": "AI Model Runner API",
        "version": "1.0.0",
        "status": "running",
        "endpoints": {
            "chat": "/chat",
            "code": "/code",
            "reasoning": "/reasoning",
            "models": "/models",
            "health": "/health"
        }
    }

@app.get("/health")
async def health_check():
    """Health check endpoint"""
    return {"status": "healthy", "models_loaded": len(models)}

@app.get("/models")
async def list_models():
    """List available models"""
    model_list = [
        ModelInfo(
            name="microsoft/DialoGPT-medium",
            type="conversational",
            description="Multi-turn dialogue model",
            loaded="dialogue" in models
        ),
        ModelInfo(
            name="gpt2",
            type="text-generation",
            description="Text generation model",
            loaded="text_gen" in models
        ),
        ModelInfo(
            name="distilbert-base-uncased-finetuned-sst-2-english",
            type="sentiment-analysis",
            description="Sentiment analysis model",
            loaded="sentiment" in models
        )
    ]
    return {"models": [model.dict() for model in model_list]}

@app.post("/chat")
async def chat_completion(request: ChatRequest):
    """Multi-turn dialogue endpoint"""
    try:
        if "dialogue" not in models:
            # Mock response if model not loaded
            return {
                "response": f"Mock response to: {request.messages[-1].content if request.messages else 'Hello'}",
                "model": request.model,
                "usage": {"tokens": 10}
            }
        
        # Convert chat format to single input
        conversation = "\n".join([f"{msg.role}: {msg.content}" for msg in request.messages])
        
        # Generate response
        response = models["dialogue"](
            conversation,
            max_length=request.max_length,
            temperature=request.temperature
        )
        
        return {
            "response": response[0]["generated_text"],
            "model": request.model,
            "usage": {"tokens": len(response[0]["generated_text"].split())}
        }
        
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

@app.post("/code")
async def code_analysis(request: CodeRequest):
    """Code understanding and analysis endpoint"""
    try:
        if request.task == "explain":
            return await explain_code(request.code, request.language)
        elif request.task == "refactor":
            return await refactor_code(request.code, request.language)
        elif request.task == "debug":
            return await debug_code(request.code, request.language)
        elif request.task == "optimize":
            return await optimize_code(request.code, request.language)
        else:
            raise HTTPException(status_code=400, detail="Unsupported task")
            
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

async def explain_code(code: str, language: str) -> Dict[str, Any]:
    """Explain what the code does"""
    explanation = f"""
**Code Analysis for {language}:**

```python
{code}
```

**Explanation:**
- This appears to be {language} code
- The main functionality involves [analysis would be performed here]
- Key components include functions, variables, and control structures
- The code flow follows a typical pattern for this type of application

**Complexity:** Medium
**Readability:** Good
**Suggestions:** Consider adding more comments and error handling
"""
    return {"task": "explain", "result": explanation.strip()}

async def refactor_code(code: str, language: str) -> Dict[str, Any]:
    """Refactor the code for better performance/readability"""
    refactored = f"""
# Refactored {language} Code

Original: {len(code)} lines
Refactored version with:
- Improved naming conventions
- Better error handling
- Enhanced readability
- Performance optimizations

```python
# Refactored code would appear here
# Using modern {language} best practices
```

**Improvements Made:**
- Better variable names
- Added error handling
- Improved code structure
- Performance optimizations
"""
    return {"task": "refactor", "result": refactored.strip()}

async def debug_code(code: str, language: str) -> Dict[str, Any]:
    """Debug and find issues in the code"""
    analysis = f"""
**Debug Analysis for {language} Code:**

```python
{code}
```

**Potential Issues Found:**
- [Specific issues would be identified here]
- Consider adding input validation
- Check for edge cases
- Review error handling

**Suggestions:**
- Add try-catch blocks where needed
- Validate inputs
- Check for null/empty values
- Review logic flow

**Fixed Version:**
```python
# Debugged code would appear here
```
"""
    return {"task": "debug", "result": analysis.strip()}

async def optimize_code(code: str, language: str) -> Dict[str, Any]:
    """Optimize code for performance"""
    optimized = f"""
**Performance Optimization for {language}:**

**Current Performance:** Analysis of complexity
**Optimization Opportunities:**
- Algorithm improvements
- Memory usage optimization
- I/O efficiency gains
- Caching opportunities

**Optimized Code:**
```python
# Optimized implementation would appear here
```

**Performance Gains:**
- Estimated speed improvement: 20-40%
- Memory usage reduction: 15-25%
- Better scalability
"""
    return {"task": "optimize", "result": optimized.strip()}

@app.post("/reasoning")
async def reasoning_analysis(request: ReasoningRequest):
    """Reasoning and problem-solving endpoint"""
    try:
        steps = []
        for i in range(request.steps or 5):
            steps.append(f"Step {i+1}: {request.problem[:50]}...")
        
        reasoning = f"""
**Problem:** {request.problem}
**Context:** {request.context or "No additional context provided"}

**Reasoning Process:**
{chr(10).join(steps)}

**Conclusion:**
Based on the analysis above, the solution involves [reasoned conclusion would appear here].

**Confidence:** High
**Alternative Approaches:** 2-3 alternative methods could be considered
"""
        return {"reasoning": reasoning.strip(), "steps": request.steps or 5}
        
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

@app.post("/analyze-sentiment")
async def analyze_sentiment(text: str):
    """Sentiment analysis endpoint"""
    try:
        if "sentiment" not in models:
            # Mock response
            return {
                "text": text,
                "sentiment": "NEUTRAL",
                "confidence": 0.85,
                "model": "mock-sentiment"
            }
        
        result = models["sentiment"](text)
        return {
            "text": text,
            "sentiment": result[0]["label"],
            "confidence": result[0]["score"],
            "model": "distilbert-base-uncased-finetuned-sst-2-english"
        }
        
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

@app.post("/upload")
async def upload_file(file: UploadFile = File(...)):
    """Upload and analyze files"""
    try:
        content = await file.read()
        
        # For text files
        if file.content_type.startswith("text/"):
            return {
                "filename": file.filename,
                "content_type": file.content_type,
                "size": len(content),
                "content": content.decode("utf-8")[:1000] + "..." if len(content) > 1000 else content.decode("utf-8")
            }
        
        # For binary files
        return {
            "filename": file.filename,
            "content_type": file.content_type,
            "size": len(content),
            "status": "File uploaded successfully"
        }
        
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

if __name__ == "__main__":
    port = int(os.environ.get("PORT", 8000))
    uvicorn.run(app, host="0.0.0.0", port=port)