File size: 8,225 Bytes
a94ab76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
from peft import PeftModel, PeftConfig
import gc
import torch
import os
from typing import Optional
from dotenv import load_dotenv

# Load environment variables
load_dotenv()

app = FastAPI()

# CORS ayarları - React uygulamanızın çalıştığı port'a izin verin
app.add_middleware(
    CORSMiddleware,
    allow_origins=["http://localhost:5173", "http://localhost:3000"],  # Vite ve CRA portları
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Global variables
current_model = None
current_pipe = None
current_model_name = None

class GenerateRequest(BaseModel):
    model_name: str
    prompt: str
    system_prompt: str
    max_tokens: int = 512
    temperature: float = 0.75
    top_p: float = 0.95
    top_k: int = 64
    image: Optional[str] = None  # Base64 encoded image

class GenerateResponse(BaseModel):
    generated_text: str
    model_used: str

def load_model(model_path: str):
    global current_model, current_pipe, current_model_name
    
    # Return existing pipeline if the model is already loaded
    if current_pipe is not None and current_model_name == model_path:
        return current_pipe

    print(f"Unloading previous model to load: {model_path}")
    
    # Cleanup previous model
    if current_model is not None:
        del current_model
    if current_pipe is not None:
        del current_pipe
    
    gc.collect()
    torch.cuda.empty_cache()
    
    try:
        if "Gemma3-1B" in model_path:
            print("Loading Gemma 3 1B with PEFT...")
            base_model_name = "unsloth/gemma-3-1b-it"
            tokenizer = AutoTokenizer.from_pretrained(base_model_name)
            base_model = AutoModelForCausalLM.from_pretrained(
                base_model_name,
                device_map="auto",
                dtype="auto"
            )
            model = PeftModel.from_pretrained(base_model, model_path)
            pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
            current_model = model

        elif "Gemma3-12B" in model_path:
            print("Loading Gemma 3 12B with PEFT (Image Support)...")
            base_model_name = "unsloth/gemma-3-12b-it"
            tokenizer = AutoTokenizer.from_pretrained(base_model_name)
            base_model = AutoModelForCausalLM.from_pretrained(
                base_model_name,
                device_map="auto",
                dtype="auto"
            )
            model = PeftModel.from_pretrained(base_model, model_path)
            pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
            current_model = model

        elif "Qwen2.5-3B" in model_path:
            print("Loading Qwen 2.5 3B...")
            pipe = pipeline("text-generation", model=model_path, device=0)
            current_model = pipe.model # Keep reference for simple consistency

        elif "Llama3.1-8B" in model_path:
            print("Loading Llama 3.1 8B...")
            pipe = pipeline("text-generation", model=model_path, device=0)
            current_model = pipe.model

        else:
            print(f"Unknown model pattern for {model_path}, trying default pipeline loading...")
            pipe = pipeline("text-generation", model=model_path, device=0)
            current_model = pipe.model

        current_pipe = pipe
        current_model_name = model_path
        return pipe

    except Exception as e:
        print(f"Error loading model {model_path}: {str(e)}")
        raise HTTPException(status_code=500, detail=f"Model loading failed: {str(e)}")

# Initialize with default 1B model
default_model = "Chan-Y/TurkishReasoner-Gemma3-1B"
try:
    load_model(default_model)
except Exception as e:
    print(f"Initial model loading failed (might be expected in dev env): {e}")

@app.get("/")
def read_root():
    return {"message": "Turkish AI Backend API is running"}

@app.get("/models")
def get_models():
    """Return available models"""
    return {
        "models": [
            {
                "name": "Gemma 3 1B Turkish Reasoning",
                "path": "Chan-Y/TurkishReasoner-Gemma3-1B",
                "supportsImages": False
            },
            {
                "name": "Gemma 3 12B Turkish (Supports Images)",
                "path": "Chan-Y/TurkishReasoner-Gemma3-12B",
                "supportsImages": True
            },
            {
                "name": "Qwen 2.5 3B Turkish Reasoning",
                "path": "Chan-Y/TurkishReasoner-Qwen2.5-3B",
                "supportsImages": False
            },
            {
                "name": "Llama 3.1 8B Turkish Reasoning",
                "path": "Chan-Y/TurkishReasoner-Llama3.1-8B",
                "supportsImages": False
            }
        ]
    }

@app.post("/generate", response_model=GenerateResponse)
async def generate_text(request: GenerateRequest):
    """Generate text using the model"""
    global current_pipe
    
    try:
        # Load requested model if different
        pipe = load_model(request.model_name)
        
        # Prepare message content
        user_content = [{"type": "text", "text": request.prompt}]
        
        # Add image if provided and supported
        if request.image and "Gemma3-12B" in request.model_name:
            # Assuming the image string is a data:image/jpeg;base64,... URI
            # Pipeline might expect a PIL image or a URL or strictly formatted dict
            # Standard transformers pipeline behavior for image: 
            # {"type": "image", "image": "base64_string_or_url"}
            user_content.insert(0, {"type": "image", "image": request.image})
        
        messages = [
            {
                "role": "system",
                "content": [{"type": "text", "text": request.system_prompt}]
            },
            {
                "role": "user",
                "content": user_content
            },
        ]
        
        # Clean up system prompt if empty or not supported by some models?
        # Standard chat templates usually handle system prompts.
        
        print(f"Generating with {request.model_name}, temp={request.temperature}")
        
        output = pipe(
            messages, 
            max_new_tokens=request.max_tokens, 
            temperature=request.temperature,
            top_p=request.top_p,
            top_k=request.top_k
        )
        
        generated_text = output[0]["generated_text"][-1]["content"]
        
        return GenerateResponse(
            generated_text=generated_text,
            model_used=request.model_name
        )
        
    except Exception as e:
        print(f"Error during generation: {str(e)}")
        raise HTTPException(status_code=500, detail=str(e))

@app.post("/generate/stream")
async def generate_text_stream(request: GenerateRequest):
    """
    Streaming endpoint for real-time generation
    (Not implemented in this version - would use Server-Sent Events)
    """
    raise HTTPException(status_code=501, detail="Streaming not yet implemented")

# --- Static Files Serving (for Deployment) ---
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse

# Check if static directory exists (it will in Docker)
static_dir = "static"
if os.path.exists(static_dir):
    app.mount("/assets", StaticFiles(directory=f"{static_dir}/assets"), name="assets")
    
    # Catch-all for SPA (serve index.html)
    @app.get("/{full_path:path}")
    async def serve_spa(full_path: str):
        # Allow API routes to pass through (though they match specifically defined routes first)
        if full_path.startswith("api") or full_path.startswith("generate") or full_path.startswith("models"):
             raise HTTPException(status_code=404, detail="Not found")
        
        # Serve index.html for everything else
        return FileResponse(f"{static_dir}/index.html")
else:
    print("Static directory not found. Running in API-only mode.")

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=7860)