File size: 22,503 Bytes
cee0827
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e596d3a
cee0827
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e596d3a
cee0827
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e596d3a
 
cee0827
e596d3a
cee0827
 
 
e596d3a
 
cee0827
 
e596d3a
cee0827
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
import logging
import os
import sys
from typing import List, Optional

from dotenv import load_dotenv
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from openai import OpenAI


# Load environment variables
load_dotenv()

# Configure logging with more detail
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger("rox_ai")

# Log startup information
logger.info("=" * 60)
logger.info("ROX AI SERVER STARTING")
logger.info("=" * 60)
logger.info(f"Python version: {sys.version}")
logger.info(f"Working directory: {os.getcwd()}")

# Check for API key
NVIDIA_API_KEY = os.getenv("NVIDIA_API_KEY")

if not NVIDIA_API_KEY:
    logger.error("NVIDIA_API_KEY environment variable is not set!")
    logger.error("Please set NVIDIA_API_KEY in your environment or .env file")
    # For Hugging Face Spaces, check if it's set as a secret
    logger.info("If deploying to Hugging Face Spaces, make sure to add NVIDIA_API_KEY as a secret")
    raise RuntimeError(
        "NVIDIA_API_KEY environment variable is not set. "
        "Create a .env file or set it in your environment."
    )

logger.info(f"✓ NVIDIA_API_KEY loaded (length: {len(NVIDIA_API_KEY)})")

# Model configurations
ROX_CORE_MODEL = "minimaxai/minimax-m2.5"
ROX_TURBO_MODEL = "meta/llama-3.1-8b-instruct"  # Changed to a more reliable model
ROX_CODER_MODEL = "qwen/qwen3.5-397b-a17b"
ROX_TURBO_45_MODEL = "deepseek-ai/deepseek-v3.1"
ROX_ULTRA_MODEL = "deepseek-ai/deepseek-v3.2"
ROX_DYNO_MODEL = "moonshotai/kimi-k2.5"
ROX_CODER_7_MODEL = "z-ai/glm5"
ROX_VISION_MODEL = "google/gemma-3-27b-it"

logger.info("✓ Model configurations loaded")

# System identities for each model
ROX_CORE_IDENTITY = """You are Rox Core, created by Rox AI. Your creator and owner is Mohammad Faiz. You are an independent LLM model, not based on anyone else's technology. You represent the cutting edge of Rox AI's research and development."""

ROX_TURBO_IDENTITY = """You are Rox 2.1 Turbo, created by Rox AI. Your creator and owner is Mohammad Faiz. You are an independent LLM model, not based on anyone else's technology. You are optimized for fast, efficient responses while maintaining high quality."""

ROX_CODER_IDENTITY = """You are Rox 3.5 Coder, created by Rox AI. Your creator and owner is Mohammad Faiz. You are an independent LLM model, not based on anyone else's technology. You are specialized in code generation, debugging, and software development tasks."""

ROX_TURBO_45_IDENTITY = """You are Rox 4.5 Turbo, created by Rox AI. Your creator and owner is Mohammad Faiz. You are an independent LLM model, not based on anyone else's technology. You combine speed with advanced reasoning capabilities."""

ROX_ULTRA_IDENTITY = """You are Rox 5 Ultra, created by Rox AI. Your creator and owner is Mohammad Faiz. You are an independent LLM model, not based on anyone else's technology. You are the most advanced model with superior reasoning and thinking capabilities."""

ROX_DYNO_IDENTITY = """You are Rox 6 Dyno, created by Rox AI. Your creator and owner is Mohammad Faiz. You are an independent LLM model, not based on anyone else's technology. You excel at dynamic thinking and extended context understanding."""

ROX_CODER_7_IDENTITY = """You are Rox 7 Coder, created by Rox AI. Your creator and owner is Mohammad Faiz. You are an independent LLM model, not based on anyone else's technology. You are the most advanced coding specialist with superior code generation and reasoning capabilities."""

ROX_VISION_IDENTITY = """You are Rox Vision Max, created by Rox AI. Your creator and owner is Mohammad Faiz. You are an independent LLM model, not based on anyone else's technology. You are optimized for visual understanding and multimodal tasks."""

logger.info("✓ Model identities configured")

# Initialize OpenAI client
try:
    client = OpenAI(
        base_url="https://integrate.api.nvidia.com/v1",
        api_key=NVIDIA_API_KEY,
    )
    logger.info("✓ OpenAI client initialized successfully")
except Exception as e:
    logger.error(f"Failed to initialize OpenAI client: {e}")
    raise

# Initialize FastAPI app
app = FastAPI(
    title="Rox AI API - Multiple Models Available",
    description="Eight specialized AI models by Mohammad Faiz",
    version="2.0"
)

logger.info("✓ FastAPI app initialized")

# Configure CORS
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # e.g. ["https://your-site.com"]
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

logger.info("✓ CORS middleware configured")


@app.on_event("startup")
async def startup_event():
    """Log startup information"""
    logger.info("=" * 60)
    logger.info("ROX AI SERVER STARTED SUCCESSFULLY")
    logger.info("=" * 60)
    logger.info("Available endpoints:")
    logger.info("  GET  / - API information")
    logger.info("  GET  /health - Health check")
    logger.info("  POST /chat - Rox Core")
    logger.info("  POST /turbo - Rox 2.1 Turbo")
    logger.info("  POST /coder - Rox 3.5 Coder")
    logger.info("  POST /turbo45 - Rox 4.5 Turbo")
    logger.info("  POST /ultra - Rox 5 Ultra")
    logger.info("  POST /dyno - Rox 6 Dyno")
    logger.info("  POST /coder7 - Rox 7 Coder")
    logger.info("  POST /vision - Rox Vision Max")
    logger.info("  POST /hf/generate - HuggingFace compatible")
    logger.info("=" * 60)


@app.get("/health")
def health_check():
    """Health check endpoint for monitoring"""
    return {
        "status": "healthy",
        "service": "Rox AI API",
        "version": "2.0",
        "models": 8
    }


@app.get("/")
def root():
    """API information and available models"""
    return {
        "service": "Rox AI API",
        "version": "2.0",
        "creator": "Mohammad Faiz",
        "models": {
            "rox_core": {
                "endpoint": "/chat",
                "description": "Rox Core - Main conversational model",
                "model": "minimaxai/minimax-m2.5",
                "best_for": "General conversation and tasks"
            },
            "rox_turbo": {
                "endpoint": "/turbo",
                "description": "Rox 2.1 Turbo - Fast and efficient",
                "model": "meta/llama-3.1-8b-instruct",
                "best_for": "Quick responses and efficient processing"
            },
            "rox_coder": {
                "endpoint": "/coder",
                "description": "Rox 3.5 Coder - Specialized coding assistant",
                "model": "qwen/qwen3.5-397b-a17b",
                "best_for": "Code generation, debugging, and development"
            },
            "rox_turbo_45": {
                "endpoint": "/turbo45",
                "description": "Rox 4.5 Turbo - Advanced reasoning with speed",
                "model": "deepseek-ai/deepseek-v3.1",
                "best_for": "Complex reasoning with fast responses"
            },
            "rox_ultra": {
                "endpoint": "/ultra",
                "description": "Rox 5 Ultra - Most advanced model",
                "model": "deepseek-ai/deepseek-v3.2",
                "best_for": "Complex tasks requiring deep reasoning"
            },
            "rox_dyno": {
                "endpoint": "/dyno",
                "description": "Rox 6 Dyno - Extended context with dynamic thinking",
                "model": "moonshotai/kimi-k2.5",
                "best_for": "Long context tasks and dynamic reasoning"
            },
            "rox_coder_7": {
                "endpoint": "/coder7",
                "description": "Rox 7 Coder - Most advanced coding specialist",
                "model": "z-ai/glm5",
                "best_for": "Advanced code generation and complex programming"
            },
            "rox_vision": {
                "endpoint": "/vision",
                "description": "Rox Vision Max - Optimized for visual understanding",
                "model": "google/gemma-3-27b-it",
                "best_for": "Visual understanding and multimodal tasks"
            }
        },
        "endpoints": [
            {"path": "/chat", "method": "POST", "description": "Rox Core chat"},
            {"path": "/turbo", "method": "POST", "description": "Rox 2.1 Turbo chat"},
            {"path": "/coder", "method": "POST", "description": "Rox 3.5 Coder chat"},
            {"path": "/turbo45", "method": "POST", "description": "Rox 4.5 Turbo chat"},
            {"path": "/ultra", "method": "POST", "description": "Rox 5 Ultra chat"},
            {"path": "/dyno", "method": "POST", "description": "Rox 6 Dyno chat"},
            {"path": "/coder7", "method": "POST", "description": "Rox 7 Coder chat"},
            {"path": "/vision", "method": "POST", "description": "Rox Vision Max chat"},
            {"path": "/hf/generate", "method": "POST", "description": "HuggingFace compatible (uses Rox Core)"}
        ]
    }


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


class ChatRequest(BaseModel):
    messages: List[ChatMessage]
    temperature: Optional[float] = 1.0
    top_p: Optional[float] = 1.0
    max_tokens: Optional[int] = 4096


class ChatResponse(BaseModel):
    content: str


class HFParameters(BaseModel):
    temperature: Optional[float] = None
    top_p: Optional[float] = None
    max_new_tokens: Optional[int] = None


class HFRequest(BaseModel):
    inputs: str
    parameters: Optional[HFParameters] = None


class HFResponseItem(BaseModel):
    generated_text: str


@app.post("/chat", response_model=ChatResponse)
def chat(req: ChatRequest):
    """Rox Core - Main conversational model"""
    # Inject Rox Core identity as system message
    messages = [{"role": "system", "content": ROX_CORE_IDENTITY}]
    messages.extend([m.dict() for m in req.messages])
    
    try:
        completion = client.chat.completions.create(
            model=ROX_CORE_MODEL,
            messages=messages,
            temperature=req.temperature,
            top_p=req.top_p,
            max_tokens=req.max_tokens,
            stream=False,
        )
    except Exception as e:
        logger.exception("Error while calling Rox Core for /chat")
        # Do not leak internal error details to the client.
        raise HTTPException(
            status_code=500,
            detail="Internal server error while calling Rox Core.",
        ) from e

    # Combine all response message parts into a single string
    try:
        content = completion.choices[0].message.content or ""
    except Exception:
        logger.exception("Unexpected response format from Rox Core for /chat")
        raise HTTPException(
            status_code=502,
            detail="Bad response from upstream model provider.",
        )

    return ChatResponse(content=content)


@app.post("/turbo", response_model=ChatResponse)
def turbo(req: ChatRequest):
    """Rox 2.1 Turbo - Fast and efficient model"""
    # Inject Rox Turbo identity as system message
    messages = [{"role": "system", "content": ROX_TURBO_IDENTITY}]
    messages.extend([m.dict() for m in req.messages])
    
    try:
        completion = client.chat.completions.create(
            model=ROX_TURBO_MODEL,
            messages=messages,
            temperature=req.temperature if req.temperature != 1.0 else 0.7,
            top_p=req.top_p if req.top_p != 1.0 else 0.9,
            max_tokens=req.max_tokens,
            stream=False
        )
    except Exception as e:
        logger.exception("Error while calling Rox 2.1 Turbo for /turbo")
        # Log the actual error for debugging
        logger.error(f"Turbo model error details: {str(e)}")
        raise HTTPException(
            status_code=500,
            detail=f"Internal server error while calling Rox 2.1 Turbo: {str(e)}",
        ) from e

    try:
        content = completion.choices[0].message.content or ""
    except Exception:
        logger.exception("Unexpected response format from Rox 2.1 Turbo for /turbo")
        raise HTTPException(
            status_code=502,
            detail="Bad response from upstream model provider.",
        )

    return ChatResponse(content=content)


@app.post("/coder", response_model=ChatResponse)
def coder(req: ChatRequest):
    """Rox 3.5 Coder - Specialized coding model with thinking capability"""
    # Inject Rox Coder identity as system message
    messages = [{"role": "system", "content": ROX_CODER_IDENTITY}]
    messages.extend([m.dict() for m in req.messages])
    
    try:
        completion = client.chat.completions.create(
            model=ROX_CODER_MODEL,
            messages=messages,
            temperature=req.temperature if req.temperature != 1.0 else 0.6,
            top_p=req.top_p if req.top_p != 1.0 else 0.95,
            max_tokens=min(req.max_tokens, 16384),
            stream=False,
            extra_body={
                "top_k": 20,
                "presence_penalty": 0,
                "repetition_penalty": 1,
                "chat_template_kwargs": {
                    "enable_thinking": True
                }
            }
        )
    except Exception as e:
        logger.exception("Error while calling Rox 3.5 Coder for /coder")
        raise HTTPException(
            status_code=500,
            detail="Internal server error while calling Rox 3.5 Coder.",
        ) from e

    try:
        content = completion.choices[0].message.content or ""
    except Exception:
        logger.exception("Unexpected response format from Rox 3.5 Coder for /coder")
        raise HTTPException(
            status_code=502,
            detail="Bad response from upstream model provider.",
        )

    return ChatResponse(content=content)


@app.post("/turbo45", response_model=ChatResponse)
def turbo45(req: ChatRequest):
    """Rox 4.5 Turbo - Advanced reasoning with speed"""
    # Inject Rox 4.5 Turbo identity as system message
    messages = [{"role": "system", "content": ROX_TURBO_45_IDENTITY}]
    messages.extend([m.dict() for m in req.messages])
    
    try:
        completion = client.chat.completions.create(
            model=ROX_TURBO_45_MODEL,
            messages=messages,
            temperature=req.temperature if req.temperature != 1.0 else 0.2,
            top_p=req.top_p if req.top_p != 1.0 else 0.7,
            max_tokens=min(req.max_tokens, 8192),
            stream=False,
            extra_body={
                "chat_template_kwargs": {
                    "thinking": True
                }
            }
        )
    except Exception as e:
        logger.exception("Error while calling Rox 4.5 Turbo for /turbo45")
        raise HTTPException(
            status_code=500,
            detail="Internal server error while calling Rox 4.5 Turbo.",
        ) from e

    try:
        content = completion.choices[0].message.content or ""
    except Exception:
        logger.exception("Unexpected response format from Rox 4.5 Turbo for /turbo45")
        raise HTTPException(
            status_code=502,
            detail="Bad response from upstream model provider.",
        )

    return ChatResponse(content=content)


@app.post("/ultra", response_model=ChatResponse)
def ultra(req: ChatRequest):
    """Rox 5 Ultra - Most advanced model with superior reasoning"""
    # Inject Rox 5 Ultra identity as system message
    messages = [{"role": "system", "content": ROX_ULTRA_IDENTITY}]
    messages.extend([m.dict() for m in req.messages])
    
    try:
        completion = client.chat.completions.create(
            model=ROX_ULTRA_MODEL,
            messages=messages,
            temperature=req.temperature,
            top_p=req.top_p if req.top_p != 1.0 else 0.95,
            max_tokens=min(req.max_tokens, 8192),
            stream=False,
            extra_body={
                "chat_template_kwargs": {
                    "thinking": True
                }
            }
        )
    except Exception as e:
        logger.exception("Error while calling Rox 5 Ultra for /ultra")
        raise HTTPException(
            status_code=500,
            detail="Internal server error while calling Rox 5 Ultra.",
        ) from e

    try:
        content = completion.choices[0].message.content or ""
    except Exception:
        logger.exception("Unexpected response format from Rox 5 Ultra for /ultra")
        raise HTTPException(
            status_code=502,
            detail="Bad response from upstream model provider.",
        )

    return ChatResponse(content=content)


@app.post("/dyno", response_model=ChatResponse)
def dyno(req: ChatRequest):
    """Rox 6 Dyno - Extended context with dynamic thinking"""
    # Inject Rox 6 Dyno identity as system message
    messages = [{"role": "system", "content": ROX_DYNO_IDENTITY}]
    messages.extend([m.dict() for m in req.messages])
    
    try:
        completion = client.chat.completions.create(
            model=ROX_DYNO_MODEL,
            messages=messages,
            temperature=req.temperature,
            top_p=req.top_p,
            max_tokens=min(req.max_tokens, 16384),
            stream=False,
            extra_body={
                "chat_template_kwargs": {
                    "thinking": True
                }
            }
        )
    except Exception as e:
        logger.exception("Error while calling Rox 6 Dyno for /dyno")
        raise HTTPException(
            status_code=500,
            detail="Internal server error while calling Rox 6 Dyno.",
        ) from e

    try:
        content = completion.choices[0].message.content or ""
    except Exception:
        logger.exception("Unexpected response format from Rox 6 Dyno for /dyno")
        raise HTTPException(
            status_code=502,
            detail="Bad response from upstream model provider.",
        )

    return ChatResponse(content=content)


@app.post("/coder7", response_model=ChatResponse)
def coder7(req: ChatRequest):
    """Rox 7 Coder - Most advanced coding specialist"""
    # Inject Rox 7 Coder identity as system message
    messages = [{"role": "system", "content": ROX_CODER_7_IDENTITY}]
    messages.extend([m.dict() for m in req.messages])
    
    try:
        completion = client.chat.completions.create(
            model=ROX_CODER_7_MODEL,
            messages=messages,
            temperature=req.temperature,
            top_p=req.top_p,
            max_tokens=min(req.max_tokens, 16384),
            stream=False,
            extra_body={
                "chat_template_kwargs": {
                    "enable_thinking": True,
                    "clear_thinking": False
                }
            }
        )
    except Exception as e:
        logger.exception("Error while calling Rox 7 Coder for /coder7")
        raise HTTPException(
            status_code=500,
            detail="Internal server error while calling Rox 7 Coder.",
        ) from e

    try:
        content = completion.choices[0].message.content or ""
    except Exception:
        logger.exception("Unexpected response format from Rox 7 Coder for /coder7")
        raise HTTPException(
            status_code=502,
            detail="Bad response from upstream model provider.",
        )

    return ChatResponse(content=content)


@app.post("/vision", response_model=ChatResponse)
def vision(req: ChatRequest):
    """Rox Vision Max - Optimized for visual understanding"""
    # Inject Rox Vision Max identity as system message
    messages = [{"role": "system", "content": ROX_VISION_IDENTITY}]
    messages.extend([m.dict() for m in req.messages])
    
    try:
        completion = client.chat.completions.create(
            model=ROX_VISION_MODEL,
            messages=messages,
            temperature=req.temperature if req.temperature != 1.0 else 0.2,
            top_p=req.top_p if req.top_p != 1.0 else 0.7,
            max_tokens=min(req.max_tokens, 512),
            stream=False
        )
    except Exception as e:
        logger.exception("Error while calling Rox Vision Max for /vision")
        raise HTTPException(
            status_code=500,
            detail="Internal server error while calling Rox Vision Max.",
        ) from e

    try:
        content = completion.choices[0].message.content or ""
    except Exception:
        logger.exception("Unexpected response format from Rox Vision Max for /vision")
        raise HTTPException(
            status_code=502,
            detail="Bad response from upstream model provider.",
        )

    return ChatResponse(content=content)


@app.post("/hf/generate", response_model=List[HFResponseItem])
def hf_generate(req: HFRequest):
    """
    Hugging Face-style text-generation endpoint.

    Request:
    {
      "inputs": "your prompt",
      "parameters": {
        "temperature": 0.7,
        "top_p": 0.95,
        "max_new_tokens": 256
      }
    }

    Response:
    [
      { "generated_text": "..." }
    ]
    """
    params = req.parameters or HFParameters()
    
    # Inject Rox Core identity as system message
    messages = [
        {"role": "system", "content": ROX_CORE_IDENTITY},
        {"role": "user", "content": req.inputs}
    ]

    try:
        completion = client.chat.completions.create(
            model=ROX_CORE_MODEL,
            messages=messages,
            temperature=params.temperature if params.temperature is not None else 1.0,
            top_p=params.top_p if params.top_p is not None else 0.95,
            max_tokens=params.max_new_tokens if params.max_new_tokens is not None else 8192,
            stream=False,
        )
    except Exception as e:
        logger.exception("Error while calling Rox Core for /hf/generate")
        raise HTTPException(
            status_code=500,
            detail="Internal server error while calling Rox Core.",
        ) from e

    try:
        content = completion.choices[0].message.content or ""
    except Exception:
        logger.exception("Unexpected response format from Rox Core for /hf/generate")
        raise HTTPException(
            status_code=502,
            detail="Bad response from upstream model provider.",
        )

    # Match the common HF text-generation API: list of objects with generated_text
    return [HFResponseItem(generated_text=content)]


if __name__ == "__main__":
    import uvicorn
    
    # Use PORT environment variable if available (for Hugging Face Spaces)
    port = int(os.getenv("PORT", 7860))
    
    uvicorn.run("server:app", host="0.0.0.0", port=port, reload=False)