File size: 43,706 Bytes
d28afad
 
 
 
d919708
1b86d8a
5da2025
2ee9112
8a9ebcc
 
1b86d8a
 
493a4a6
 
5e980fd
87b09dd
1b86d8a
f53960f
 
1b86d8a
fa42c97
87b09dd
 
5e980fd
509531f
 
dcad397
 
d919708
a94def8
 
87b09dd
d919708
87b09dd
d919708
87b09dd
 
 
 
2ee9112
87b09dd
 
 
01237fb
d28afad
f2cc606
dcad397
87b09dd
 
fa42c97
 
 
 
d919708
dcad397
 
 
 
87b09dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5e980fd
 
 
 
 
 
 
 
 
87b09dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d919708
fa42c97
d919708
87b09dd
 
 
 
 
 
 
 
 
f2cc606
7fa30a6
d919708
 
 
 
 
 
 
 
dcad397
d919708
 
f53960f
d919708
1b86d8a
 
 
7372212
493a4a6
1b86d8a
87b09dd
 
1b86d8a
87b09dd
 
 
 
 
493a4a6
 
 
 
87b09dd
 
 
 
 
 
1b86d8a
87b09dd
493a4a6
 
87b09dd
 
 
493a4a6
87b09dd
 
 
5e980fd
 
 
7f03ffe
d28afad
7f03ffe
 
 
 
5e980fd
 
87b09dd
d919708
5e980fd
 
d919708
87b09dd
d919708
 
 
87b09dd
d919708
7fa30a6
bb3c951
d919708
5e980fd
bb3c951
87b09dd
5e980fd
 
d28afad
5e980fd
 
 
 
 
 
7f03ffe
5e980fd
 
 
 
7f03ffe
5e980fd
f53960f
 
 
7f03ffe
d28afad
7f03ffe
f53960f
 
7f03ffe
f53960f
 
7f03ffe
 
f53960f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d28afad
f53960f
dcad397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f841fdd
1b86d8a
 
 
7372212
493a4a6
1b86d8a
87b09dd
 
1b86d8a
87b09dd
 
 
 
 
493a4a6
 
 
 
87b09dd
 
 
 
 
 
1b86d8a
87b09dd
493a4a6
 
87b09dd
 
 
493a4a6
87b09dd
f841fdd
5e980fd
 
 
 
 
 
f841fdd
7fa30a6
5e980fd
 
f841fdd
5e980fd
f841fdd
 
87b09dd
f841fdd
 
bb3c951
7fa30a6
87b09dd
5e980fd
 
d28afad
5e980fd
 
 
 
 
 
 
 
 
 
 
 
87b09dd
 
 
 
493a4a6
87b09dd
493a4a6
87b09dd
 
 
 
 
 
 
f841fdd
 
 
1b86d8a
 
 
7372212
493a4a6
1b86d8a
 
87b09dd
 
f841fdd
5e980fd
 
 
7fa30a6
5e980fd
 
 
 
 
 
bb3c951
5e980fd
 
f841fdd
5e980fd
f841fdd
 
87b09dd
7fa30a6
bb3c951
f841fdd
87b09dd
5e980fd
 
d28afad
5e980fd
 
 
 
 
 
 
 
 
 
 
 
f841fdd
d919708
1b86d8a
 
 
7372212
493a4a6
1b86d8a
 
87b09dd
 
d919708
 
 
bb3c951
5e980fd
 
 
d919708
87b09dd
 
5e980fd
 
 
 
 
 
 
 
 
 
87b09dd
bb3c951
5e980fd
 
d919708
87b09dd
d919708
 
87b09dd
7fa30a6
bb3c951
 
 
87b09dd
5e980fd
 
d28afad
5e980fd
 
 
 
 
 
 
 
 
 
 
 
 
87b09dd
5e980fd
 
d28afad
 
5e980fd
 
d28afad
5e980fd
 
 
 
 
 
 
 
d919708
 
1b86d8a
 
 
7372212
493a4a6
1b86d8a
 
87b09dd
 
d919708
bb3c951
5e980fd
 
 
87b09dd
 
5e980fd
 
 
 
 
 
 
 
 
 
87b09dd
bb3c951
5e980fd
 
d919708
87b09dd
d919708
 
87b09dd
7fa30a6
bb3c951
 
 
87b09dd
5e980fd
 
d28afad
5e980fd
 
 
 
 
 
 
 
 
 
 
 
 
87b09dd
5e980fd
 
d28afad
 
5e980fd
 
d28afad
5e980fd
 
 
 
 
 
 
 
d919708
f841fdd
1b86d8a
 
 
 
7372212
493a4a6
1b86d8a
 
87b09dd
 
 
 
 
493a4a6
 
 
 
87b09dd
 
 
 
 
 
1b86d8a
87b09dd
493a4a6
 
87b09dd
 
 
493a4a6
87b09dd
 
 
5e980fd
 
 
 
 
 
2ee9112
5e980fd
 
 
 
87b09dd
bb3c951
5e980fd
 
2ee9112
87b09dd
f841fdd
 
87b09dd
2ee9112
 
bb3c951
 
 
87b09dd
5e980fd
 
d28afad
5e980fd
 
 
 
 
 
 
 
 
 
 
 
87b09dd
5e980fd
 
 
d28afad
 
5e980fd
 
d28afad
5e980fd
 
 
 
 
 
 
 
493a4a6
5e980fd
493a4a6
5e980fd
 
 
 
 
 
 
 
 
 
 
f841fdd
d919708
f2cc606
d919708
5e980fd
 
 
 
 
01237fb
d919708
 
 
 
 
d28afad
d919708
 
5e980fd
 
87b09dd
 
 
 
 
493a4a6
87b09dd
 
 
 
 
 
 
 
 
 
493a4a6
 
87b09dd
 
 
 
 
 
493a4a6
87b09dd
 
 
493a4a6
 
 
 
 
 
 
87b09dd
 
 
 
 
 
 
493a4a6
87b09dd
 
 
493a4a6
 
 
 
 
87b09dd
 
 
 
 
 
493a4a6
87b09dd
 
 
493a4a6
 
 
 
 
 
 
87b09dd
 
 
 
 
493a4a6
87b09dd
 
 
 
 
 
493a4a6
87b09dd
 
 
493a4a6
 
 
 
 
 
 
87b09dd
 
 
493a4a6
 
 
87b09dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f46462b
5da2025
f46462b
 
 
 
5da2025
 
 
 
 
 
 
 
dcad397
87b09dd
 
 
 
493a4a6
87b09dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
493a4a6
 
87b09dd
 
 
 
 
 
 
 
 
 
 
 
 
f53960f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
# api/endpoints.py
# SPDX-FileCopyrightText: Hadad <hadad@linuxmail.org>
# SPDX-License-Identifier: Apache-2.0

import os
import uuid
from fastapi import APIRouter, Depends, HTTPException, Request, status, UploadFile, File , Body
from fastapi.responses import StreamingResponse
from api.database import User, Conversation, Message
from api.models import QueryRequest, ConversationOut, ConversationCreate, UserUpdate
from api.auth import current_active_user
from api.database import get_db
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select, delete
from utils.generation import request_generation, select_model, check_model_availability
from utils.web_search import web_search
import io
import asyncio
import json
from openai import OpenAI
from motor.motor_asyncio import AsyncIOMotorClient
from datetime import datetime
import logging
from typing import List, Optional
# from utils.constants import MODEL_ALIASES, MODEL_NAME, SECONDARY_MODEL_NAME, TERTIARY_MODEL_NAME, CLIP_BASE_MODEL, CLIP_LARGE_MODEL, ASR_MODEL, TTS_MODEL, IMAGE_GEN_MODEL, SECONDARY_IMAGE_GEN_MODEL
from utils.constants import MODEL_ALIASES, MODEL_NAME, SECONDARY_MODEL_NAME, TERTIARY_MODEL_NAME, CLIP_BASE_MODEL, CLIP_LARGE_MODEL, ASR_MODEL, TTS_MODEL, IMAGE_GEN_MODEL, SECONDARY_IMAGE_GEN_MODEL, IMAGE_INFERENCE_API
import psutil
import time
router = APIRouter()
from pydantic import BaseModel

logger = logging.getLogger(__name__)

# Check HF_TOKEN and BACKUP_HF_TOKEN
HF_TOKEN = os.getenv("HF_TOKEN")
if not HF_TOKEN:
    logger.error("HF_TOKEN is not set in environment variables.")
    raise ValueError("HF_TOKEN is required for Inference API.")

BACKUP_HF_TOKEN = os.getenv("BACKUP_HF_TOKEN")
if not BACKUP_HF_TOKEN:
    logger.warning("BACKUP_HF_TOKEN is not set. Fallback to secondary model will not work if primary token fails.")

ROUTER_API_URL = os.getenv("ROUTER_API_URL", "https://router.huggingface.co")
API_ENDPOINT = os.getenv("API_ENDPOINT", "https://router.huggingface.co/v1")
FALLBACK_API_ENDPOINT = os.getenv("FALLBACK_API_ENDPOINT", "https://api-inference.huggingface.co/v1")


# MongoDB setup
MONGO_URI = os.getenv("MONGODB_URI")
client = AsyncIOMotorClient(MONGO_URI)
db = client["hager"]
session_message_counts = db["session_message_counts"]

class ImageGenRequest(BaseModel):
    prompt: str
    output_format: str = "image"

# Helper function to handle sessions for non-logged-in users
async def handle_session(request: Request):
    if not hasattr(request, "session"):
        raise HTTPException(status_code=500, detail="Session middleware not configured")
    session_id = request.session.get("session_id")
    if not session_id:
        session_id = str(uuid.uuid4())
        request.session["session_id"] = session_id
        await session_message_counts.insert_one({"session_id": session_id, "message_count": 0})
    
    session_doc = await session_message_counts.find_one({"session_id": session_id})
    if not session_doc:
        session_doc = {"session_id": session_id, "message_count": 0}
        await session_message_counts.insert_one(session_doc)
    
    message_count = session_doc["message_count"] + 1
    await session_message_counts.update_one(
        {"session_id": session_id},
        {"$set": {"message_count": message_count}}
    )
    if message_count > 4:
        raise HTTPException(
            status_code=status.HTTP_403_FORBIDDEN,
            detail="Message limit reached. Please log in to continue."
        )
    return session_id

# Helper function to enhance system prompt for Arabic language
def enhance_system_prompt(system_prompt: str, message: str, user: Optional[User] = None) -> str:
    enhanced_prompt = system_prompt
    if any(0x0600 <= ord(char) <= 0x06FF for char in message):
        enhanced_prompt += "\nRespond in Arabic with clear, concise, and accurate information tailored to the user's query."
    if user and user.additional_info:
        enhanced_prompt += f"\nUser Profile: {user.additional_info}\nConversation Style: {user.conversation_style or 'default'}"
    return enhanced_prompt

@router.get("/api/settings")
async def get_settings(user: User = Depends(current_active_user)):
    if not user:
        raise HTTPException(status_code=401, detail="Login required")
    return {
        "available_models": [
            {"alias": "advanced", "description": "High-performance model for complex queries"},
            {"alias": "standard", "description": "Balanced model for general use"},
            {"alias": "light", "description": "Lightweight model for quick responses"}
        ],
        "conversation_styles": ["default", "concise", "analytical", "creative"],
        "user_settings": {
            "display_name": user.display_name,
            "preferred_model": user.preferred_model,
            "job_title": user.job_title,
            "education": user.education,
            "interests": user.interests,
            "additional_info": user.additional_info,
            "conversation_style": user.conversation_style
        }
    }

@router.get("/api/model-info")
async def model_info():
    return {
        "available_models": [
            {"alias": "advanced", "description": "High-performance model for complex queries"},
            {"alias": "standard", "description": "Balanced model for general use"},
            {"alias": "light", "description": "Lightweight model for quick responses"},
            {"alias": "image_base", "description": "Basic image analysis model"},
            {"alias": "image_advanced", "description": "Advanced image analysis model"},
            {"alias": "audio", "description": "Audio transcription model (default)"},
            {"alias": "tts", "description": "Text-to-speech model (default)"}
        ],
        "api_base": API_ENDPOINT,
        "fallback_api_base": FALLBACK_API_ENDPOINT,
        "status": "online"
    }

@router.get("/api/performance")
async def performance_stats():
    return {
        "queue_size": int(os.getenv("QUEUE_SIZE", 80)),
        "concurrency_limit": int(os.getenv("CONCURRENCY_LIMIT", 20)),
        "uptime": time.time() - psutil.boot_time()  # مدة تشغيل النظام بالثواني
    }


@router.post("/api/chat")
async def chat_endpoint(
    request: Request,
    req: QueryRequest,
    user: User = Depends(current_active_user),
    db: AsyncSession = Depends(get_db)
):
    logger.info(f"Received chat request: {req}")
    
    if not user:
        await handle_session(request)
    
    conversation = None
    if user:
        title = req.title or (req.message[:50] + "..." if len(req.message) > 50 else req.message or "Untitled Conversation")
        result = await db.execute(
            select(Conversation).filter(Conversation.user_id == user.id).order_by(Conversation.updated_at.desc())
        )
        conversation = result.scalar_one_or_none()
        if not conversation:
            conversation_id = str(uuid.uuid4())
            conversation = Conversation(
                conversation_id=conversation_id,
                user_id=user.id,
                title=title
            )
            db.add(conversation)
            await db.commit()
            await db.refresh(conversation)
        
        user_msg = Message(role="user", content=req.message, conversation_id=conversation.id)
        db.add(user_msg)
        await db.commit()
    
    preferred_model = user.preferred_model if user else None
    model_name, api_endpoint = select_model(req.message, input_type="text", preferred_model=preferred_model)
    
    is_available, api_key, selected_endpoint = check_model_availability(model_name, HF_TOKEN)
    if not is_available:
        logger.warning(f"Model {model_name} is not available at {api_endpoint}, trying fallback model.")
        model_name = SECONDARY_MODEL_NAME
        is_available, api_key, selected_endpoint = check_model_availability(model_name, HF_TOKEN)
        if not is_available:
            logger.error(f"Fallback model {model_name} is not available at {selected_endpoint}")
            raise HTTPException(status_code=503, detail=f"No available models. Tried {MODEL_NAME} and {SECONDARY_MODEL_NAME}.")
    
    system_prompt = enhance_system_prompt(req.system_prompt, req.message, user)
    
    stream = request_generation(
        api_key=api_key,
        api_base=selected_endpoint,
        message=req.message,
        system_prompt=system_prompt,
        model_name=model_name,
        chat_history=req.history,
        temperature=req.temperature,
        max_new_tokens=req.max_new_tokens or 2048,
        deep_search=req.enable_browsing,
        input_type="text",
        output_format=req.output_format
    )
    
    if req.output_format == "audio":
        audio_chunks = []
        try:
            for chunk in stream:
                logger.debug(f"Processing audio chunk: {chunk[:100] if isinstance(chunk, str) else 'bytes'}")
                if isinstance(chunk, bytes):
                    audio_chunks.append(chunk)
                else:
                    logger.warning(f"Unexpected non-bytes chunk in audio stream: {chunk}")
            if not audio_chunks:
                logger.error("No audio data generated.")
                raise HTTPException(status_code=502, detail="No audio data generated. Model may be unavailable.")
            audio_data = b"".join(audio_chunks)
            return StreamingResponse(io.BytesIO(audio_data), media_type="audio/wav")
        except Exception as e:
            logger.error(f"Audio generation failed: {e}")
            raise HTTPException(status_code=502, detail=f"Audio generation failed: {str(e)}")
    
    async def stream_response():
        response_chunks = []
        try:
            for chunk in stream:
                if isinstance(chunk, str) and chunk.strip() and chunk not in ["analysis", "assistantfinal"]:
                    response_chunks.append(chunk)
                    yield chunk.encode('utf-8')  # إرسال الـ chunk مباشرةً
                    await asyncio.sleep(0.05)  # تأخير بسيط لمحاكاة الكتابة
                else:
                    logger.warning(f"Skipping chunk: {chunk}")
            
            response = "".join(response_chunks)
            if not response.strip():
                logger.warning(f"Empty response from {model_name}. Trying fallback model {SECONDARY_MODEL_NAME}.")
                model_name = SECONDARY_MODEL_NAME
                is_available, api_key, selected_endpoint = check_model_availability(model_name, HF_TOKEN)
                if not is_available:
                    logger.error(f"Fallback model {model_name} is not available at {selected_endpoint}")
                    yield f"Error: No available models. Tried {MODEL_NAME} and {SECONDARY_MODEL_NAME}.".encode('utf-8')
                    return
                
                stream = request_generation(
                    api_key=api_key,
                    api_base=selected_endpoint,
                    message=req.message,
                    system_prompt=system_prompt,
                    model_name=model_name,
                    chat_history=req.history,
                    temperature=req.temperature,
                    max_new_tokens=req.max_new_tokens or 2048,
                    deep_search=req.enable_browsing,
                    input_type="text",
                    output_format=req.output_format
                )
                response_chunks = []
                for chunk in stream:
                    if isinstance(chunk, str) and chunk.strip() and chunk not in ["analysis", "assistantfinal"]:
                        response_chunks.append(chunk)
                        yield chunk.encode('utf-8')
                        await asyncio.sleep(0.05)
                    else:
                        logger.warning(f"Skipping fallback chunk: {chunk}")
                
                response = "".join(response_chunks)
                if not response.strip():
                    logger.error(f"Empty response from fallback model {model_name}.")
                    yield f"Error: Empty response from both {MODEL_NAME} and {SECONDARY_MODEL_NAME}.".encode('utf-8')
                    return
            
            if user and conversation:
                assistant_msg = Message(role="assistant", content=response, conversation_id=conversation.id)
                db.add(assistant_msg)
                await db.commit()
                conversation.updated_at = datetime.utcnow()
                await db.commit()
                yield json.dumps({
                    "conversation_id": conversation.conversation_id,
                    "conversation_url": f"https://mgzon-mgzon-app.hf.space/chat/{conversation.conversation_id}",
                    "conversation_title": conversation.title
                }, ensure_ascii=False).encode('utf-8')
        except Exception as e:
            logger.error(f"Chat generation failed: {e}")
            yield f"Error: Chat generation failed: {str(e)}".encode('utf-8')

    return StreamingResponse(stream_response(), media_type="text/plain")

@router.post("/api/image-generation")
async def image_generation_endpoint(
    request: Request,
    req: dict,
    file: Optional[UploadFile] = File(None),
    user: User = Depends(current_active_user),
    db: AsyncSession = Depends(get_db)
):
    if not user:
        await handle_session(request)
    
    prompt = req.get("prompt", "")
    output_format = req.get("output_format", "image")
    if not prompt.strip():
        raise HTTPException(status_code=400, detail="Prompt is required for image generation.")
    
    model_name, api_endpoint = select_model(prompt, input_type="image_gen")
    
    is_available, api_key, selected_endpoint = check_model_availability(model_name, HF_TOKEN)
    if not is_available:
        logger.error(f"Model {model_name} is not available at {api_endpoint}")
        raise HTTPException(status_code=503, detail=f"Model {model_name} is not available. Please try another model.")
    
    image_data = None
    if file:
        image_data = await file.read()
    
    system_prompt = enhance_system_prompt(
        "You are an expert in generating high-quality images based on detailed prompts. Ensure the output is visually appealing and matches the user's description.",
        prompt, user
    )
    
    stream = request_generation(
        api_key=api_key,
        api_base=selected_endpoint,
        message=prompt,
        system_prompt=system_prompt,
        model_name=model_name,
        temperature=0.7,
        max_new_tokens=2048,
        input_type="image_gen",
        image_data=image_data,
        output_format=output_format
    )
    
    if output_format == "image":
        image_chunks = []
        try:
            for chunk in stream:
                logger.debug(f"Processing image chunk: {chunk[:100] if isinstance(chunk, str) else 'bytes'}")
                if isinstance(chunk, bytes):
                    image_chunks.append(chunk)
                else:
                    logger.warning(f"Unexpected non-bytes chunk in image stream: {chunk}")
            if not image_chunks:
                logger.error("No image data generated.")
                raise HTTPException(status_code=500, detail="No image data generated for image generation.")
            image_data = b"".join(image_chunks)
            return StreamingResponse(io.BytesIO(image_data), media_type="image/png")
        except Exception as e:
            logger.error(f"Image generation failed: {e}")
            raise HTTPException(status_code=500, detail=f"Image generation failed: {str(e)}")
    
    response_chunks = []
    try:
        for chunk in stream:
            logger.debug(f"Processing text chunk: {chunk[:100]}...")
            if isinstance(chunk, str) and chunk.strip() and chunk not in ["analysis", "assistantfinal"]:
                response_chunks.append(chunk)
            else:
                logger.warning(f"Skipping chunk: {chunk}")
        response = "".join(response_chunks)
        if not response.strip():
            logger.error("Empty response generated.")
            raise HTTPException(status_code=500, detail="Empty response generated from model.")
        return {"response": response}
    except Exception as e:
        logger.error(f"Image generation failed: {e}")
        raise HTTPException(status_code=500, detail=f"Image generation failed: {str(e)}")


@router.post("/api/audio-transcription")
async def audio_transcription_endpoint(
    request: Request,
    file: UploadFile = File(...),
    user: User = Depends(current_active_user),
    db: AsyncSession = Depends(get_db)
):
    logger.info(f"Received audio transcription request for file: {file.filename}")
    
    if not user:
        await handle_session(request)
    
    conversation = None
    if user:
        title = "Audio Transcription"
        result = await db.execute(
            select(Conversation).filter(Conversation.user_id == user.id).order_by(Conversation.updated_at.desc())
        )
        conversation = result.scalar_one_or_none()
        if not conversation:
            conversation_id = str(uuid.uuid4())
            conversation = Conversation(
                conversation_id=conversation_id,
                user_id=user.id,
                title=title
            )
            db.add(conversation)
            await db.commit()
            await db.refresh(conversation)
        
        user_msg = Message(role="user", content="Audio message", conversation_id=conversation.id)
        db.add(user_msg)
        await db.commit()
    
    model_name, api_endpoint = select_model("transcribe audio", input_type="audio")
    
    is_available, api_key, selected_endpoint = check_model_availability(model_name, HF_TOKEN)
    if not is_available:
        logger.error(f"Model {model_name} is not available at {api_endpoint}")
        raise HTTPException(status_code=503, detail=f"Model {model_name} is not available. Please try another model.")
    
    audio_data = await file.read()
    stream = request_generation(
        api_key=api_key,
        api_base=selected_endpoint,
        message="Transcribe audio",
        system_prompt="Transcribe the provided audio using Whisper. Ensure accurate transcription in the detected language.",
        model_name=model_name,
        temperature=0.7,
        max_new_tokens=2048,
        input_type="audio",
        audio_data=audio_data,
        output_format="text"
    )
    response_chunks = []
    try:
        for chunk in stream:
            logger.debug(f"Processing transcription chunk: {chunk[:100]}...")
            if isinstance(chunk, str):
                response_chunks.append(chunk)
            else:
                logger.warning(f"Unexpected non-string chunk in transcription stream: {chunk}")
        response = "".join(response_chunks)
        if not response.strip():
            logger.error("Empty transcription generated.")
            raise HTTPException(status_code=500, detail="Empty transcription generated from model.")
        logger.info(f"Audio transcription response: {response[:100]}...")
    except Exception as e:
        logger.error(f"Audio transcription failed: {e}")
        raise HTTPException(status_code=500, detail=f"Audio transcription failed: {str(e)}")
    
    if user and conversation:
        assistant_msg = Message(role="assistant", content=response, conversation_id=conversation.id)
        db.add(assistant_msg)
        await db.commit()
        conversation.updated_at = datetime.utcnow()
        await db.commit()
        return {
            "transcription": response,
            "conversation_id": conversation.conversation_id,
            "conversation_url": f"https://mgzon-mgzon-app.hf.space/chat/{conversation.conversation_id}",
            "conversation_title": conversation.title
        }
    
    return {"transcription": response}

@router.post("/api/text-to-speech")
async def text_to_speech_endpoint(
    request: Request,
    req: dict,
    user: User = Depends(current_active_user),
    db: AsyncSession = Depends(get_db)
):
    if not user:
        await handle_session(request)
    
    text = req.get("text", "")
    if not text.strip():
        raise HTTPException(status_code=400, detail="Text input is required for text-to-speech.")
    
    model_name, api_endpoint = select_model("text to speech", input_type="tts")
    
    is_available, api_key, selected_endpoint = check_model_availability(model_name, HF_TOKEN)
    if not is_available:
        logger.error(f"Model {model_name} is not available at {api_endpoint}")
        raise HTTPException(status_code=503, detail=f"Model {model_name} is not available. Please try another model.")
    
    stream = request_generation(
        api_key=api_key,
        api_base=selected_endpoint,
        message=text,
        system_prompt="Convert the provided text to speech using a text-to-speech model. Ensure clear and natural pronunciation, especially for Arabic text.",
        model_name=model_name,
        temperature=0.7,
        max_new_tokens=2048,
        input_type="tts",
        output_format="audio"
    )
    audio_chunks = []
    try:
        for chunk in stream:
            logger.debug(f"Processing TTS chunk: {chunk[:100] if isinstance(chunk, str) else 'bytes'}")
            if isinstance(chunk, bytes):
                audio_chunks.append(chunk)
            else:
                logger.warning(f"Unexpected non-bytes chunk in TTS stream: {chunk}")
        if not audio_chunks:
            logger.error("No audio data generated for TTS.")
            raise HTTPException(status_code=500, detail="No audio data generated for text-to-speech.")
        audio_data = b"".join(audio_chunks)
        return StreamingResponse(io.BytesIO(audio_data), media_type="audio/wav")
    except Exception as e:
        logger.error(f"Text-to-speech generation failed: {e}")
        raise HTTPException(status_code=500, detail=f"Text-to-speech generation failed: {str(e)}")

@router.post("/api/code")
async def code_endpoint(
    request: Request,
    req: dict,
    user: User = Depends(current_active_user),
    db: AsyncSession = Depends(get_db)
):
    if not user:
        await handle_session(request)
    
    framework = req.get("framework")
    task = req.get("task")
    code = req.get("code", "")
    output_format = req.get("output_format", "text")
    if not task:
        raise HTTPException(status_code=400, detail="Task description is required.")
    
    prompt = f"Generate code for task: {task} using {framework}. Existing code: {code}"
    preferred_model = user.preferred_model if user else None
    model_name, api_endpoint = select_model(prompt, input_type="text", preferred_model=preferred_model)
    
    is_available, api_key, selected_endpoint = check_model_availability(model_name, HF_TOKEN)
    if not is_available:
        logger.error(f"Model {model_name} is not available at {api_endpoint}")
        raise HTTPException(status_code=503, detail=f"Model {model_name} is not available. Please try another model.")
    
    system_prompt = enhance_system_prompt(
        "You are a coding expert. Provide detailed, well-commented code with examples and explanations.",
        prompt, user
    )
    
    stream = request_generation(
        api_key=api_key,
        api_base=selected_endpoint,
        message=prompt,
        system_prompt=system_prompt,
        model_name=model_name,
        temperature=0.7,
        max_new_tokens=2048,
        input_type="text",
        output_format=output_format
    )
    if output_format == "audio":
        audio_chunks = []
        try:
            for chunk in stream:
                logger.debug(f"Processing code audio chunk: {chunk[:100] if isinstance(chunk, str) else 'bytes'}")
                if isinstance(chunk, bytes):
                    audio_chunks.append(chunk)
                else:
                    logger.warning(f"Unexpected non-bytes chunk in code audio stream: {chunk}")
            if not audio_chunks:
                logger.error("No audio data generated for code.")
                raise HTTPException(status_code=500, detail="No audio data generated for code.")
            audio_data = b"".join(audio_chunks)
            return StreamingResponse(io.BytesIO(audio_data), media_type="audio/wav")
        except Exception as e:
            logger.error(f"Code audio generation failed: {e}")
            raise HTTPException(status_code=500, detail=f"Code audio generation failed: {str(e)}")
    
    response_chunks = []
    try:
        for chunk in stream:
            logger.debug(f"Processing code text chunk: {chunk[:100]}...")
            if isinstance(chunk, str) and chunk.strip() and chunk not in ["analysis", "assistantfinal"]:
                response_chunks.append(chunk)
            else:
                logger.warning(f"Skipping code chunk: {chunk}")
        response = "".join(response_chunks)
        if not response.strip():
            logger.error("Empty code response generated.")
            raise HTTPException(status_code=500, detail="Empty code response generated from model.")
        return {"generated_code": response}
    except Exception as e:
        logger.error(f"Code generation failed: {e}")
        raise HTTPException(status_code=500, detail=f"Code generation failed: {str(e)}")

@router.post("/api/analysis")
async def analysis_endpoint(
    request: Request,
    req: dict,
    user: User = Depends(current_active_user),
    db: AsyncSession = Depends(get_db)
):
    if not user:
        await handle_session(request)
    
    message = req.get("text", "")
    output_format = req.get("output_format", "text")
    if not message.strip():
        raise HTTPException(status_code=400, detail="Text input is required for analysis.")
    
    preferred_model = user.preferred_model if user else None
    model_name, api_endpoint = select_model(message, input_type="text", preferred_model=preferred_model)
    
    is_available, api_key, selected_endpoint = check_model_availability(model_name, HF_TOKEN)
    if not is_available:
        logger.error(f"Model {model_name} is not available at {api_endpoint}")
        raise HTTPException(status_code=503, detail=f"Model {model_name} is not available. Please try another model.")
    
    system_prompt = enhance_system_prompt(
        "You are an expert analyst. Provide detailed analysis with step-by-step reasoning and examples.",
        message, user
    )
    
    stream = request_generation(
        api_key=api_key,
        api_base=selected_endpoint,
        message=message,
        system_prompt=system_prompt,
        model_name=model_name,
        temperature=0.7,
        max_new_tokens=2048,
        input_type="text",
        output_format=output_format
    )
    if output_format == "audio":
        audio_chunks = []
        try:
            for chunk in stream:
                logger.debug(f"Processing analysis audio chunk: {chunk[:100] if isinstance(chunk, str) else 'bytes'}")
                if isinstance(chunk, bytes):
                    audio_chunks.append(chunk)
                else:
                    logger.warning(f"Unexpected non-bytes chunk in analysis audio stream: {chunk}")
            if not audio_chunks:
                logger.error("No audio data generated for analysis.")
                raise HTTPException(status_code=500, detail="No audio data generated for analysis.")
            audio_data = b"".join(audio_chunks)
            return StreamingResponse(io.BytesIO(audio_data), media_type="audio/wav")
        except Exception as e:
            logger.error(f"Analysis audio generation failed: {e}")
            raise HTTPException(status_code=500, detail=f"Analysis audio generation failed: {str(e)}")
    
    response_chunks = []
    try:
        for chunk in stream:
            logger.debug(f"Processing analysis text chunk: {chunk[:100]}...")
            if isinstance(chunk, str) and chunk.strip() and chunk not in ["analysis", "assistantfinal"]:
                response_chunks.append(chunk)
            else:
                logger.warning(f"Skipping analysis chunk: {chunk}")
        response = "".join(response_chunks)
        if not response.strip():
            logger.error("Empty analysis response generated.")
            raise HTTPException(status_code=500, detail="Empty analysis response generated from model.")
        return {"analysis": response}
    except Exception as e:
        logger.error(f"Analysis generation failed: {e}")
        raise HTTPException(status_code=500, detail=f"Analysis generation failed: {str(e)}")

@router.post("/api/image-analysis")
async def image_analysis_endpoint(
    request: Request,
    file: UploadFile = File(...),
    output_format: str = "text",
    user: User = Depends(current_active_user),
    db: AsyncSession = Depends(get_db)
):
    if not user:
        await handle_session(request)
    
    conversation = None
    if user:
        title = "Image Analysis"
        result = await db.execute(
            select(Conversation).filter(Conversation.user_id == user.id).order_by(Conversation.updated_at.desc())
        )
        conversation = result.scalar_one_or_none()
        if not conversation:
            conversation_id = str(uuid.uuid4())
            conversation = Conversation(
                conversation_id=conversation_id,
                user_id=user.id,
                title=title
            )
            db.add(conversation)
            await db.commit()
            await db.refresh(conversation)
        
        user_msg = Message(role="user", content="Image analysis request", conversation_id=conversation.id)
        db.add(user_msg)
        await db.commit()
    
    preferred_model = user.preferred_model if user else None
    model_name, api_endpoint = select_model("analyze image", input_type="image", preferred_model=preferred_model)
    
    is_available, api_key, selected_endpoint = check_model_availability(model_name, HF_TOKEN)
    if not is_available:
        logger.error(f"Model {model_name} is not available at {api_endpoint}")
        raise HTTPException(status_code=503, detail=f"Model {model_name} is not available. Please try another model.")
    
    image_data = await file.read()
    system_prompt = enhance_system_prompt(
        "You are an expert in image analysis. Provide detailed descriptions or classifications based on the query.",
        "Analyze this image", user
    )
    
    stream = request_generation(
        api_key=api_key,
        api_base=selected_endpoint,
        message="Analyze this image",
        system_prompt=system_prompt,
        model_name=model_name,
        temperature=0.7,
        max_new_tokens=2048,
        input_type="image",
        image_data=image_data,
        output_format=output_format
    )
    if output_format == "audio":
        audio_chunks = []
        try:
            for chunk in stream:
                logger.debug(f"Processing image analysis audio chunk: {chunk[:100] if isinstance(chunk, str) else 'bytes'}")
                if isinstance(chunk, bytes):
                    audio_chunks.append(chunk)
                else:
                    logger.warning(f"Unexpected non-bytes chunk in image analysis audio stream: {chunk}")
            if not audio_chunks:
                logger.error("No audio data generated for image analysis.")
                raise HTTPException(status_code=500, detail="No audio data generated for image analysis.")
            audio_data = b"".join(audio_chunks)
            return StreamingResponse(io.BytesIO(audio_data), media_type="audio/wav")
        except Exception as e:
            logger.error(f"Image analysis audio generation failed: {e}")
            raise HTTPException(status_code=500, detail=f"Image analysis audio generation failed: {str(e)}")
    
    response_chunks = []
    try:
        for chunk in stream:
            logger.debug(f"Processing image analysis text chunk: {chunk[:100]}...")
            if isinstance(chunk, str) and chunk.strip() and chunk not in ["analysis", "assistantfinal"]:
                response_chunks.append(chunk)
            else:
                logger.warning(f"Skipping image analysis chunk: {chunk}")
        response = "".join(response_chunks)
        if not response.strip():
            logger.error("Empty image analysis response generated.")
            raise HTTPException(status_code=500, detail="Empty image analysis response generated from model.")
        
        if user and conversation:
            assistant_msg = Message(role="assistant", content=response, conversation_id=conversation.id)
            db.add(assistant_msg)
            await db.commit()
            conversation.updated_at = datetime.utcnow()
            await db.commit()
            return {
                "image_analysis": response,
                "conversation_id": conversation.conversation_id,
                "conversation_url": f"https://mgzon-mgzon-app.hf.space/chat/{conversation.conversation_id}",
                "conversation_title": conversation.title
            }
        
        return {"image_analysis": response}
    except Exception as e:
        logger.error(f"Image analysis failed: {e}")
        raise HTTPException(status_code=500, detail=f"Image analysis failed: {str(e)}")

@router.get("/api/test-model")
async def test_model(model: str = MODEL_NAME, endpoint: str = API_ENDPOINT):
    try:
        is_available, api_key, selected_endpoint = check_model_availability(model, HF_TOKEN)
        if not is_available:
            logger.error(f"Model {model} is not available at {endpoint}")
            raise HTTPException(status_code=503, detail=f"Model {model} is not available.")
        
        client = OpenAI(api_key=api_key, base_url=selected_endpoint, timeout=60.0)
        response = client.chat.completions.create(
            model=model,
            messages=[{"role": "user", "content": "Test"}],
            max_tokens=50
        )
        logger.debug(f"Test model response: {response.choices[0].message.content}")
        return {"status": "success", "response": response.choices[0].message.content}
    except Exception as e:
        logger.error(f"Test model failed: {e}")
        raise HTTPException(status_code=500, detail=f"Test model failed: {str(e)}")

@router.post("/api/conversations", response_model=ConversationOut)
async def create_conversation(
    req: ConversationCreate,
    user: User = Depends(current_active_user),
    db: AsyncSession = Depends(get_db)
):
    if not user:
        raise HTTPException(status_code=401, detail="Login required")
    conversation_id = str(uuid.uuid4())
    conversation = Conversation(
        conversation_id=conversation_id,
        title=req.title or "Untitled Conversation",
        user_id=user.id
    )
    db.add(conversation)
    await db.commit()
    await db.refresh(conversation)
    return ConversationOut.from_orm(conversation)

@router.get("/api/conversations/{conversation_id}", response_model=ConversationOut)
async def get_conversation(
    conversation_id: str,
    user: User = Depends(current_active_user),
    db: AsyncSession = Depends(get_db)
):
    if not user:
        raise HTTPException(status_code=401, detail="Login required")
    result = await db.execute(
        select(Conversation).filter(
            Conversation.conversation_id == conversation_id,
            Conversation.user_id == user.id
        )
    )
    conversation = result.scalar_one_or_none()
    if not conversation:
        raise HTTPException(status_code=404, detail="Conversation not found")
    return ConversationOut.from_orm(conversation)

@router.get("/api/conversations", response_model=List[ConversationOut])
async def list_conversations(
    user: User = Depends(current_active_user),
    db: AsyncSession = Depends(get_db)
):
    if not user:
        raise HTTPException(status_code=401, detail="Login required")
    result = await db.execute(
        select(Conversation).filter(Conversation.user_id == user.id).order_by(Conversation.created_at.desc())
    )
    conversations = result.scalars().all()
    return [ConversationOut.from_orm(conv) for conv in conversations]

@router.put("/api/conversations/{conversation_id}/title")
async def update_conversation_title(
    conversation_id: str,
    title: str,
    user: User = Depends(current_active_user),
    db: AsyncSession = Depends(get_db)
):
    if not user:
        raise HTTPException(status_code=401, detail="Login required")
    result = await db.execute(
        select(Conversation).filter(
            Conversation.conversation_id == conversation_id,
            Conversation.user_id == user.id
        )
    )
    conversation = result.scalar_one_or_none()
    if not conversation:
        raise HTTPException(status_code=404, detail="Conversation not found")
    
    conversation.title = title
    conversation.updated_at = datetime.utcnow()
    await db.commit()
    return {"message": "Conversation title updated", "title": conversation.title}

@router.delete("/api/conversations/{conversation_id}")
async def delete_conversation(
    conversation_id: str,
    user: User = Depends(current_active_user),
    db: AsyncSession = Depends(get_db)
):
    if not user:
        raise HTTPException(status_code=401, detail="Login required")
    result = await db.execute(
        select(Conversation).filter(
            Conversation.conversation_id == conversation_id,
            Conversation.user_id == user.id
        )
    )
    conversation = result.scalar_one_or_none()
    if not conversation:
        raise HTTPException(status_code=404, detail="Conversation not found")
    
    await db.execute(delete(Message).filter(Message.conversation_id == conversation.id))
    await db.delete(conversation)
    await db.commit()
    return {"message": "Conversation deleted successfully"}

@router.get("/users/me")
async def get_user_settings(user: User = Depends(current_active_user)):
    if not user:
        raise HTTPException(status_code=401, detail="Login required")
    return {
        "id": user.id,
        "email": user.email,
        "display_name": user.display_name,
        "preferred_model": user.preferred_model,
        "job_title": user.job_title,
        "education": user.education,
        "interests": user.interests,
        "additional_info": user.additional_info,
        "conversation_style": user.conversation_style,
        "is_active": user.is_active,
        "is_superuser": user.is_superuser
    }



@router.get("/api/verify-token")
async def verify_token(user: User = Depends(current_active_user)):
    if not user:
        raise HTTPException(status_code=401, detail="Invalid or expired token")
    return {
        "status": "valid",
        "user": {
            "id": user.id,
            "email": user.email,
            "is_active": user.is_active
        }
    }

@router.put("/users/me")
async def update_user_settings(
    settings: UserUpdate,
    user: User = Depends(current_active_user),
    db: AsyncSession = Depends(get_db)
):
    if not user:
        raise HTTPException(status_code=401, detail="Login required")
    
    if settings.preferred_model and settings.preferred_model not in MODEL_ALIASES:
        raise HTTPException(status_code=400, detail="Invalid model alias")
    
    if settings.display_name is not None:
        user.display_name = settings.display_name
    if settings.preferred_model is not None:
        user.preferred_model = settings.preferred_model
    if settings.job_title is not None:
        user.job_title = settings.job_title
    if settings.education is not None:
        user.education = settings.education
    if settings.interests is not None:
        user.interests = settings.interests
    if settings.additional_info is not None:
        user.additional_info = settings.additional_info
    if settings.conversation_style is not None:
        user.conversation_style = settings.conversation_style
    
    await db.commit()
    await db.refresh(user)
    return {"message": "Settings updated successfully", "user": {
        "id": user.id,
        "email": user.email,
        "display_name": user.display_name,
        "preferred_model": user.preferred_model,
        "job_title": user.job_title,
        "education": user.education,
        "interests": user.interests,
        "additional_info": user.additional_info,
        "conversation_style": user.conversation_style,
        "is_active": user.is_active,
        "is_superuser": user.is_superuser
    }}
    


@router.post("/api/conversations/sync", response_model=ConversationOut)
async def sync_conversation(
    request: Request,
    payload: dict = Body(...),
    user: User = Depends(current_active_user),
    db: AsyncSession = Depends(get_db)
):
    if not user:
        raise HTTPException(status_code=401, detail="Login required")
    
    messages = payload.get("messages", [])
    title = payload.get("title", "Untitled Conversation")
    conversation_id = payload.get("conversation_id")
    
    logger.info(f"Syncing conversation for user {user.email}, conversation_id: {conversation_id}")

    try:
        # Check if conversation exists
        if conversation_id:
            result = await db.execute(
                select(Conversation).filter(
                    Conversation.conversation_id == conversation_id,
                    Conversation.user_id == user.id
                )
            )
            conversation = result.scalar_one_or_none()
            if not conversation:
                raise HTTPException(status_code=404, detail="Conversation not found")
            
            # Update existing conversation
            conversation.title = title
            conversation.updated_at = datetime.utcnow()
            
            # Delete old messages
            await db.execute(
                delete(Message).filter(Message.conversation_id == conversation.id)
            )
            
            # Add new messages
            for msg in messages:
                new_message = Message(
                    conversation_id=conversation.id,
                    role=msg.get("role", "user"),
                    content=msg.get("content", ""),
                    created_at=datetime.utcnow()
                )
                db.add(new_message)
            
            await db.commit()
            await db.refresh(conversation)
            logger.info(f"Updated conversation {conversation_id} for user {user.email}")
        else:
            # Create new conversation
            conversation_id = str(uuid.uuid4())
            conversation = Conversation(
                conversation_id=conversation_id,
                user_id=user.id,
                title=title,
                created_at=datetime.utcnow(),
                updated_at=datetime.utcnow()
            )
            db.add(conversation)
            await db.commit()
            await db.refresh(conversation)
            
            # Add messages
            for msg in messages:
                new_message = Message(
                    conversation_id=conversation.id,
                    role=msg.get("role", "user"),
                    content=msg.get("content", ""),
                    created_at=datetime.utcnow()
                )
                db.add(new_message)
            
            await db.commit()
            logger.info(f"Created new conversation {conversation_id} for user {user.email}")

        return ConversationOut.from_orm(conversation)
    except Exception as e:
        logger.error(f"Error syncing conversation: {str(e)}")
        raise HTTPException(status_code=500, detail=f"Failed to sync conversation: {str(e)}")