File size: 35,982 Bytes
73f5316
99fb93e
73f5316
 
 
4fb8f72
73f5316
 
 
 
bf6b77c
73f5316
 
 
 
428a486
4fb8f72
99fb93e
73f5316
322b93f
 
 
73f5316
 
 
 
 
4fb8f72
428a486
 
 
4fb8f72
 
 
322b93f
 
ed9fb93
4fb8f72
73f5316
4fb8f72
73f5316
 
4fb8f72
428a486
 
 
73f5316
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f893731
4fb8f72
73f5316
f893731
73f5316
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b6f3f77
73f5316
4fb8f72
b6f3f77
73f5316
4fb8f72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73f5316
 
 
322b93f
73f5316
322b93f
 
 
 
 
 
 
 
 
 
 
73f5316
 
322b93f
 
 
 
73f5316
322b93f
 
 
 
 
 
 
 
73f5316
322b93f
 
 
73f5316
ecf0feb
ed9fb93
322b93f
 
 
 
ed9fb93
428a486
73f5316
 
 
322b93f
 
73f5316
4fb8f72
73f5316
322b93f
73f5316
 
 
322b93f
73f5316
322b93f
 
 
 
 
 
 
 
 
 
 
 
 
4fb8f72
322b93f
 
 
 
 
 
 
4fb8f72
322b93f
4fb8f72
322b93f
 
 
 
 
 
 
 
 
4fb8f72
322b93f
 
 
 
 
 
4fb8f72
322b93f
 
 
 
f893731
322b93f
 
 
 
4fb8f72
322b93f
4fb8f72
322b93f
 
 
 
 
 
428a486
322b93f
 
 
428a486
f893731
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73f5316
f893731
322b93f
 
73f5316
 
 
 
 
 
 
 
 
 
322b93f
73f5316
 
322b93f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f893731
 
 
 
 
 
322b93f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73f5316
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4fb8f72
73f5316
 
 
 
 
 
 
 
 
 
 
 
 
 
4fb8f72
73f5316
 
 
 
 
 
 
 
 
322b93f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73f5316
 
 
 
 
322b93f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73f5316
 
 
 
 
 
 
4fb8f72
73f5316
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
428a486
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4fb8f72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f893731
4fb8f72
 
 
 
 
f893731
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73f5316
 
428a486
 
 
ee02959
73f5316
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
322b93f
73f5316
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f893731
73f5316
 
 
 
322b93f
73f5316
 
428a486
 
 
4fb8f72
 
 
 
 
428a486
 
 
73f5316
4fb8f72
73f5316
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
322b93f
73f5316
 
 
 
428a486
322b93f
428a486
73f5316
 
 
 
428a486
322b93f
428a486
73f5316
 
322b93f
73f5316
 
 
 
 
 
 
322b93f
f893731
 
 
 
73f5316
428a486
 
 
 
 
 
 
 
 
 
73f5316
 
 
 
 
4fb8f72
 
 
 
73f5316
4fb8f72
 
 
 
 
 
 
 
 
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
import g4f
import gradio as gr
import tempfile
import os
from pathlib import Path
from g4f.client import Client
from PIL import Image
import io
import requests
import numpy as np
from ultralytics import YOLO
import subprocess
import sys
import time
from typing import Dict, Any, Optional
import easyocr
from web_engine import search_info, get_page_text  # Импортируем функции из web_engine.py

AVAILABLE_MODELS = {
    # "GPT-4": "gpt-4",
    "O3 (NEW)": "openai",

}
 
SYSTEM_PROMPT = """
1. Всегда используйте кодировку UTF-8
2. Используйте обработку ошибок
3. Если пользователь отправляет изображение или пишет что-то на подобие 'посмотри на изображение':
   - Автоматически проанализируйте изображение
   - Опишите что вы видите на русском языке
   - Тебе дается текстовая информация, которая может быть полезна для анализа
4. Если пользователь просит найти информацию, например, "найди что-то про Адольфа Гитлера":
   - Ответь с текстом [SEARCH: {prompt}]
   - Затем используй полученную информацию для ответа на вопрос
4.1 Если ты не знаешь информации которая пользователь запрашивает то напиши:
    [SEARCH: {PROMPT}] 

5. Если пользователь просит нарисовать или сгенерировать изображение:
   - Начните ответ с [GENERATE_IMAGE]
   - Напишите детальный промпт на английском языке
   - Завершите промпт строкой [/GENERATE_IMAGE]
   - Продолжите обычный ответ на русском языке
6. В режиме размышления:
   - Используйте [THINKING_STEP] для обозначения шага размышления
   - Используйте [FINAL_ANSWER] для итогового ответа
   - Старайтесь подробно описывать ход мыслей
"""

def test_code(code: str, file_type: str = ".py") -> Dict[str, Any]:
    """Тестирование кода в безопасной среде"""
    start_time = time.time()
    result = {
        "success": False,
        "output": "",
        "error": "",
        "execution_time": 0
    }
    
    try:
        code = "# -*- coding: utf-8 -*-\n" + code
        
        with tempfile.NamedTemporaryFile(suffix=file_type, mode='w', encoding='utf-8', delete=False) as tmp:
            tmp.write(code)
            tmp_path = tmp.name
        
        process = subprocess.Popen(
            [sys.executable, tmp_path],
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            text=True,
            encoding='utf-8'
        )
        
        try:
            stdout, stderr = process.communicate(timeout=10)
            result["success"] = process.returncode == 0
            result["output"] = stdout
            result["error"] = stderr
            
        except subprocess.TimeoutExpired:
            process.kill()
            result["error"] = "Превышено время выполнения (10 сек)"
            
        finally:
            os.unlink(tmp_path)
            
    except Exception as e:
        result["error"] = str(e)
        
    result["execution_time"] = time.time() - start_time
    return result

def process_file(file):
    """Обработка загруженного файла"""
    if not file:
        return None
        
    try:
        # print()
        # Получаем расширение файла
        file_extension = file.name.lower().split('.')[-1] if hasattr(file, 'name') else ''
        print(file_extension)
        # Список поддерживаемых форматов изображений
        image_extensions = {'png', 'jpg', 'jpeg', 'gif', 'bmp', 'webp'}
        
        # Если это изображение
        if file_extension in image_extensions:
            try:
                # Открываем изображение
                if isinstance(file, str):
                    image = Image.open(file)
                else:
                    image = Image.open(io.BytesIO(file.read()))
                
                # Анализируем изображение
                return process_image(image)
            except Exception as e:
                return f"[Ошибка обработки изображения: {str(e)}]"
        
        # Если это текстовый файл
        if isinstance(file, str):
            with open(file, 'r', encoding='utf-8') as f:
                return f.read()
        else:
            return file.read().decode('utf-8')
            
    except UnicodeDecodeError:
        return "[Бинарный файл]"
    except Exception as e:
        return f"[Ошибка чтения файла: {str(e)}]"
def process_audio(file_path: str, operation: str) -> Optional[str]:
    """Обработка аудио файлов"""
    try:
        from pydub import AudioSegment
        audio = AudioSegment.from_file(file_path)
        
        if "громкость" in operation.lower():
            audio = audio + 10
            
        output_path = os.path.join(tempfile.gettempdir(), f"processed_{int(time.time())}.mp3")
        audio.export(output_path, format="mp3")
        return output_path
        
    except Exception as e:
        return f"Ошибка обработки аудио: {str(e)}"

def generate_image(prompt: str) -> str:
    """Генерация изображения через g4f"""
    try:
        client = Client()
        response = client.images.generate(
            model="flux-pro",
            prompt=prompt,
            response_format="url",
            provider='PollinationsAI'
        )
        
        image_url = response.data[0].url
        
        # Проверяем доступность изображения
        timeout = 30  # Максимальное время ожидания в секундах
        interval = 0.1  # Интервал между проверками в секундах
        elapsed_time = 0
        
        while elapsed_time < timeout:
            # Проверяем доступность изображения
            head_response = requests.head(image_url)
            if head_response.status_code == 200:
                # Дополнительная проверка на наличие изображения
                get_response = requests.get(image_url)
                if get_response.status_code == 200:
                    return image_url  # Изображение доступно
                else:
                    return "❌ Ошибка: изображение недоступно."
            time.sleep(interval)
            elapsed_time += interval
        
        return "❌ Ошибка: время ожидания изображения истекло."  # Время ожидания истекло

    except Exception as e:
        return f"Ошибка при генерации изобраения: {str(e)}"
def process_image(image_input) -> str:
    """Обработка изображения через провайдера Blackbox."""
    try:
        # Инициализация клиента GPT с провайдером Blackbox
        client = Client()

        # Определяем, как было передано изображение (URL или объект PIL.Image)
        if isinstance(image_input, str):  # Если передан URL изображения
            response = requests.get(image_input, stream=True)
            if response.status_code == 200:
                image = Image.open(response.raw)  # Преобразуем поток в объект PIL.Image
            else:
                return "❌ Ошибка загрузки изображения по URL."
        elif isinstance(image_input, Image.Image):  # Если передано изображение PIL
            image = image_input
        else:
            return "❌ Неподдерживаемый формат изображения."

        # Конвертируем изображение в режим RGB, если оно в RGBA или другом формате
        if image.mode != 'RGB':
            image = image.convert('RGB')

        # Сохраняем изображение во временный файл
        with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as tmp:
            image.save(tmp.name, format="JPEG")  # Сохраняем изображение как JPEG
            image_file = open(tmp.name, "rb")  # Открываем файл для передачи в запрос

        # Запрос на анализ изображения через Blackbox
        response = client.chat.completions.create(
            model="gpt-4o",
            messages=[{"role": "user", "content": "Опишите, что изображено на этом изображении."}],
            image=image_file,
            provider="Blackbox"
        )
        print("MADEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE")
        print(response)
        # Закрываем временный файл
        image_file.close()

        # Возвращаем результат анализа
        return response

    except Exception as e:
        return f"❌ Ошибка при обработке изображения: {str(e)}"
    finally:
        # Удаляем временный файл после использования
        if "tmp" in locals():
            Path(tmp.name).unlink(missing_ok=True)
def chat_response(message, history, model_name, direct_mode, thinking_depth=1.0, uploaded_file=None):
    messages = [{"role": "system", "content": SYSTEM_PROMPT}]
    partial_message = ""  # Инициализация переменной
    history = history or []
    history.append({"role": "user", "content": message})
    history.append({"role": "assistant", "content": ""})
    
    try:
        # Обычный поток обработки сообщений
        for msg in history[:-2]:
            messages.append({"role": msg["role"], "content": msg["content"]})
        messages.append({"role": "user", "content": str(message)})

        partial_message = ""
        response = None  # Инициализация переменной response
        response = g4f.ChatCompletion.create(
            model=AVAILABLE_MODELS.get(model_name, "O3"),
            messages=messages,
            stream=False,
            provider="PollinationsAI"
        )
        
        if response and isinstance(response, str):
            partial_message += response
            history[-1]["content"] = f"""
            <div class="text-fade-in">
                {partial_message}
            </div>
            """
            
            yield history, *[gr.update(visible=False) for _ in range(6)]

            # Если в ответе от модели есть [SEARCH:], выполняем поиск
            if "[SEARCH:" in partial_message:
                history[-1]["content"] = """
                <div class="search-animation">
                    <div class="search-text">🔍 Выполняется поиск...</div>
                </div>
                """
                yield history, *[gr.update(visible=False) for _ in range(6)]
                prompt = partial_message.split("[SEARCH:")[1].split("]")[0].strip()

                # Выполняем поиск
                results = search_info(prompt)
                if not results:
                    history[-1]["content"] = "Не удалось найти информацию."
                    yield history, *[gr.update(visible=False) for _ in range(6)]
                    return

                info_text = ""
                for link in results:
                    info_text += f"Ссылка: {link}\n"
                    page_text = get_page_text(link)
                    info_text += f"Текст: {page_text[:500]}...\n\n"
                
                search_response_prompt = f"""Вот информация по запросу "{prompt}":
                
{info_text}

На основе этой информации сформируй краткий и понятный ответ на русском языке.
"""
                search_response = g4f.ChatCompletion.create(
                    model="gpt-4",
                    messages=[{"role": "user", "content": search_response_prompt}],
                    stream=False,
                    provider="Mhystical"
                )

                final_response = search_response if isinstance(search_response, str) else "Не удалось получить ответ."
                final_response = final_response.replace(f"[SEARCH: {prompt}]", "").strip()
                history.append({"role": "assistant", "content": final_response})
                yield history, *[gr.update(visible=False) for _ in range(6)]
                
                if response and isinstance(response, str):
                    steps = response.split("[THINKING_STEP]")
                    final_answer = ""

                    for i, step in enumerate(steps):
                        if "[FINAL_ANSWER]" in step:
                            final_answer = step.split("[FINAL_ANSWER]")[1].strip()
                            step = step.split("[FINAL_ANSWER]")[0]

                        step_html = f"""
                        <div class="thinking-step-block">
                            <span class="thinking-step">Шаг {i + 1}: {step.strip()}</span>
                        </div>
                        """
                        history[-1]["content"] += step_html

                        yield history, gr.update(visible=False)

                    # Добавляем финальный блок с заключением
                    final_html = f"""
                    <div class="final-answer-block">
                        <span class="thinking-conclusion">Ответ: {final_answer}</span>
                    </div>
                    """
                    history[-1]["content"] += final_html
                    yield history, gr.update(visible=True)
                    return

    
        history[-1]["content"] = partial_message.strip()
        yield history, *[gr.update(visible=True) for _ in range(6)]

    except Exception as e:
        history[-1]["content"] = f"❌ Произошла ошибка: {str(e)}"
        yield (
            history,
            gr.update(visible=False),
            gr.update(visible=True, value=str(e)),
            gr.update(visible=True),
            "",
            gr.update(visible=False),
            gr.update(value=None)
        )

    if direct_mode:
        image_url = generate_image(message)
        if not image_url.startswith("Ошибка"):
            history[-1]["content"] = f"![Generated Image]({image_url})"
        else:
            history[-1]["content"] = f"❌ {image_url}"
        yield history, *[gr.update(visible=False) for _ in range(6)]
        return

    yield (
        history,
        gr.update(visible=False),
        gr.update(visible=False),
        gr.update(visible=False),
        "",
        gr.update(visible=False),
        gr.update(visible=False)
    )
    
    if uploaded_file:
        file_content = process_file(uploaded_file)
        if file_content:
            message = f"Файл содержит:\n```\n{file_content}\n```\n\n{message}"

    for msg in history[:-2]:
        messages.append({"role": msg["role"], "content": msg["content"]})
    messages.append({"role": "user", "content": str(message)})

    partial_message = ""
    code_block = None
    # response = g4f.ChatCompletion.create(
    #     model=AVAILABLE_MODELS.get(model_name, "O3"),
    #     messages=messages,
    #     stream=False,
    #     provider="PollinationsAI"
    # )
    
    if response and isinstance(response, str):
        partial_message += response
        if "[GENERATE_IMAGE]" in partial_message:
            history[-1]["content"] = """
            <div class="generating-animation">
                <div class="generating-text">Генерация изображения...</div>
            </div>
            """
        
        if "[GENERATE_IMAGE]" in partial_message and "[/GENERATE_IMAGE]" in partial_message:
            start_idx = partial_message.find("[GENERATE_IMAGE]") + len("[GENERATE_IMAGE]")
            end_idx = partial_message.find("[/GENERATE_IMAGE]")
            image_prompt = partial_message[start_idx:end_idx].strip()
            yield history, *[gr.update(visible=False) for _ in range(6)]
            
            image_url = generate_image(image_prompt)
            
            if not image_url.startswith("Ошибка"):
                explanation_text = partial_message[end_idx + len("[/GENERATE_IMAGE]"):].strip()
                partial_message = f"![Generated Image]({image_url})\n\n{explanation_text}"
            else:
                partial_message = f"❌ {image_url}"
        
        history[-1]["content"] = partial_message + "|"
        yield history, *[gr.update(visible=False) for _ in range(6)]
        
        if "```" in partial_message:
            code_start = partial_message.rfind("```") + 3
            code_end = partial_message.find("```", code_start)
            if code_end != -1:
                code_block = partial_message[code_start:code_end].strip()
    
    history[-1]["content"] = partial_message
    yield (
        history, 
        *[gr.update(visible=True if code_block else False) for _ in range(5)],
        gr.update(value=None)
    )


def analyze_code(code):
    """Анализ кода и получение объяснения"""
    if not code:
        return [{"role": "assistant", "content": "❌ Нет кода для анализа"}]
        
    prompt = f"""Проанализируй этот код и объясни:
1. Что он делает
2. Возможные проблемы или ошибки
3. Как можно улучшить
4. Примеры использования

Код:
```
{code}
```"""
    
    try:
        response = g4f.ChatCompletion.create(
            model="gpt-4o",
            messages=[{"role": "user", "content": prompt}],
            stream=False
        )
        return [{"role": "assistant", "content": response if isinstance(response, str) else "Не удалось получить анализ"}]
    except Exception as e:
        return [{"role": "assistant", "content": f"❌ Ошибка при анализе: {str(e)}"}]

def create_interface():
    # Обновляем тему на темую
    theme = gr.themes.Soft(
        primary_hue="slate",
        secondary_hue="gray",
    ).set(
        body_background_fill="#1a1a1a",
        body_text_color="#ffffff",
        button_primary_background_fill="#2d2d2d",
        button_primary_background_fill_hover="#3d3d3d",
        button_primary_text_color="white",
        block_label_background_fill="#2d2d2d",
        block_title_text_color="#ffffff",
        input_background_fill="#2d2d2d",
    )

    # Обновляем CSS стили для управления цветом кода
    css = """
    .gradio-container { 
        background-color: #1a1a1a !important;
    }
    .chatbot {
        background-color: #2d2d2d !important;
        border-radius: 10px;
        box-shadow: 0 2px 6px rgba(0,0,0,0.15);
    }
    .text-fade-in {
        opacity: 0;  /* Исходная прозрачность */
        transform: translateY(10px);  /* Исходное смещение */
        animation: fadeInText 1s ease-in-out forwards;  /* Анимация при показе */
    }

    @keyframes fadeInText {
        0% {
            opacity: 0;
            transform: translateY(10px);  /* Немного ниже */
        }
        100% {
            opacity: 1;
            transform: translateY(0);  /* Возвращается на место */
        }
    }

    .message.user {
        background-color: #3d3d3d !important;
        color: white !important;
        border-radius: 15px;
    }
    .search-animation {
        position: relative;
        width: 100%;
        min-height: 80px;
        background: linear-gradient(45deg, #2d2d2d, #3d3d3d);
        border-radius: 15px;
        overflow: hidden;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .search-text {
        color: white;
        font-size: 18px;
        z-index: 2;
        animation: search-pulse 1.5s infinite;
    }

    .search-animation::before {
        content: '';
        position: absolute;
        width: 200%;
        height: 200%;
        top: -50%;
        left: -50%;
        background: 
            radial-gradient(2px 2px at 20% 30%, rgba(255,255,255,0.3), rgba(0,0,0,0)),
            radial-gradient(2px 2px at 40% 70%, rgba(255,255,255,0.3), rgba(0,0,0,0));
        background-repeat: repeat;
        animation: search-rotate 6s linear infinite;
    }

    @keyframes search-pulse {
        0% { opacity: 0.6; }
        50% { opacity: 1; }
        100% { opacity: 0.6; }
    }

    @keyframes search-rotate {
        from {
            transform: rotate(0deg);
        }
        to {
            transform: rotate(360deg);
        }
    }
    .message.bot {
        background-color: #2d2d2d !important;
        color: white !important;
        border-radius: 15px;
        position: relative;
        transition: all 0.3s ease;
    }
    /* Добавляем стиль ля блоков кода */
    .message.bot pre {
        background-color: #1a1a1a !important;
        border-radius: 8px;
        padding: 10px;
    }
    .message.bot.typing::after {
        content: '|';
        animation: cursor 1s infinite;
    }
    @keyframes cursor {
        0% { opacity: 0; }
        50% { opacity: 1; }
        100% { opacity: 0; }
    }
    
    .message.bot:last-child {
        animation: fadeIn 0.3s ease-in-out;
    }
    
    /* Убираем дублирующиеся стили курсора */
    .message.bot:last-child:not(:empty)::after {
        content: '';
    }
    
    /* Анимация появления текста */
    @keyframes fadeIn {
        from {
            opacity: 0;
            transform: translateY(10px);
        }
        to {
            opacity: 1;
            transform: translateY(0);
        }
    }
    
    @keyframes cursor {
        0% { opacity: 0; }
        50% { opacity: 1; }
        100% { opacity: 0; }
    }
    .footer {
        display: none !important;
    }
    
    .generating-animation {
        position: relative;
        width: 100%;
        height: 120px;
        background: linear-gradient(45deg, #1a1a1a, #2d2d2d);
        border-radius: 15px;
        overflow: hidden;
        display: flex;
        align-items: center;
        justify-content: center;
    }
    
    .generating-text {
        color: white;
        font-size: 16px;
        z-index: 2;
        animation: pulse 1.5s infinite;
    }
    
    .generating-animation::before {
        content: '';
        position: absolute;
        width: 200%;
        height: 200%;
        top: -50%;
        left: -50%;
        background: 
            radial-gradient(2px 2px at 20% 30%, rgba(255,255,255,0.8), rgba(0,0,0,0)),
            radial-gradient(2px 2px at 40% 70%, rgba(255,255,255,0.8), rgba(0,0,0,0)),
            radial-gradient(2px 2px at 50% 40%, rgba(255,255,255,0.8), rgba(0,0,0,0)),
            radial-gradient(2px 2px at 60% 60%, rgba(255,255,255,0.8), rgba(0,0,0,0));
        background-repeat: repeat;
        animation: rotate 8s linear infinite;
    }
    
    @keyframes rotate {
        from {
            transform: rotate(0deg);
        }
        to {
            transform: rotate(360deg);
        }
    }
    
    @keyframes pulse {
        0% { opacity: 0.6; }
        50% { opacity: 1; }
        100% { opacity: 0.6; }
    }
    
    .submit-btn {
        min-width: 100px !important;
        cursor: pointer !important;
        background-color: #4a4a4a !important;
        transition: all 0.2s ease;
    }

    .submit-btn:hover {
        background-color: #5a5a5a !important;
    }
    
    .input-row {
        gap: 8px;
        padding: 8px;
        background: #2d2d2d;
        border-radius: 8px;
        margin-top: 8px;
    }
    
    .footer {
        display: none !important;
    }
    
    /* Добавляем анимацию для просмотра изображения */
    .image-analysis-animation {
        position: relative;
        width: 100%;
        min-height: 100px;
        background: linear-gradient(45deg, #2d2d2d, #3d3d3d);
        border-radius: 15px;
        overflow: hidden;
        display: flex;
        align-items: center;
        justify-content: center;
        margin: 10px 0;
    }

    .analysis-text {
        color: white;
        font-size: 16px;
        z-index: 2;
        text-align: center;
        animation: pulse 1.5s infinite;
    }

    .image-analysis-animation::before {
        content: '';
        position: absolute;
        width: 100%;
        height: 100%;
        background: linear-gradient(90deg, 
            transparent 0%,
            rgba(255,255,255,0.1) 50%,
            transparent 100%);
        animation: scanning 2s linear infinite;
    }

    @keyframes scanning {
        0% { transform: translateX(-100%); }
        100% { transform: translateX(100%); }
    }

    .thinking-animation {
        position: relative;
        width: 100%;
        min-height: 100px;
        background: linear-gradient(45deg, #1a1a1a, #2d2d2d);
        border-radius: 15px;
        overflow: hidden;
        margin: 10px 0;
        padding: 20px;
        border-left: 4px solid #4a90e2;
        transition: all 0.3s ease;
    }

    .thinking-animation:hover {
        background: linear-gradient(45deg, #202020, #333333);
    }

    .thinking-step {
        color: #b0b0b0;
        font-style: italic;
        margin-bottom: 10px;
        padding-left: 20px;
        border-left: 2px solid #4a90e2;
        opacity: 0;
        transform: translateY(10px);
        animation: fadeInStep 0.5s ease-in-out forwards;
    }

    .thinking-conclusion {
        color: white;
        font-weight: bold;
        margin-top: 15px;
        padding: 10px;
        background: rgba(74, 144, 226, 0.1);
        border-radius: 8px;
        border-left: 4px solid #4a90e2;
    }

    @keyframes fadeInStep {
        to {
            opacity: 1;
            transform: translateY(0);
        }
    }

    .typing-cursor {
        display: inline-block;
        width: 2px;
        height: 15px;
        background: #4a90e2;
        margin-left: 5px;
        animation: blink 1s infinite;
    }

    @keyframes blink {
        50% { opacity: 0; }
    }
    
    /* Стили для слайдера глубины размышления */
    .thinking-depth-slider {
        margin-top: 10px;
        padding: 8px;
        background: rgba(74, 144, 226, 0.1);
        border-radius: 8px;
        border-left: 4px solid #4a90e2;
    }

    /* Индикатор глубины размышления в сообщении */
    .thinking-depth-indicator {
        font-size: 0.9em;
        color: #4a90e2;
        margin-bottom: 10px;
        font-style: italic;
    }
    
    .error-animation {
        position: relative;
        width: 100%;
        min-height: 80px;
        background: linear-gradient(45deg, #2d1a1a, #2d2d2d);
        border-radius: 15px;
        overflow: hidden;
        display: flex;
        align-items: center;
        justify-content: center;
        border-left: 4px solid #e24a4a;
        padding: 15px;
        margin: 10px 0;
    }

    .error-text {
        color: #ff9999;
        font-size: 16px;
        z-index: 2;
        text-align: center;
    }

    .thinking-step-block {
        background: linear-gradient(45deg, #1a1a1a, #2d2d2d);
        border-left: 4px solid #4a90e2;
        margin: 15px 0;
        padding: 15px;
        border-radius: 8px;
        animation: fadeIn 0.5s ease-in-out;
    }

    .final-answer-block {
        background: linear-gradient(45deg, #1a2a3a, #2d3d4d);
        border-left: 4px solid #50c878;
        margin: 20px 0;
        padding: 20px;
        border-radius: 8px;
        font-weight: bold;
        animation: slideIn 0.5s ease-in-out;
    }

    @keyframes fadeIn {
        from {
            opacity: 0;
            transform: translateY(10px);
        }
        to {
            opacity: 1;
            transform: translateY(0);
        }
    }

    @keyframes slideIn {
        from {
            opacity: 0;
            transform: translateX(-10px);
        }                  
        to {
            opacity: 1;
            transform: translateX(0);
        }
    }
    .thinking-animation {
    position: relative;
    width: 100%;
    min-height: 100px;
    background: linear-gradient(45deg, #1a1a1a, #2d2d2d);
    border-radius: 15px;
    overflow: hidden;
    margin: 10px 0;
    padding: 20px;
    border-left: 4px solid #4a90e2;
    transition: all 0.3s ease;
}

.thinking-step {
    color: #b0b0b0;
    font-style: italic;
    margin-bottom: 10px;
    padding-left: 20px;
    border-left: 2px solid #4a90e2;
    opacity: 0;
    transform: translateY(10px);
    animation: fadeInStep 0.5s ease-in-out forwards;
}

.thinking-conclusion {
    color: white;
    font-weight: bold;
    margin-top: 15px;
    padding: 10px;
    background: rgba(74, 144, 226, 0.1);
    border-radius: 8px;
    border-left: 4px solid #4a90e2;
}

@keyframes fadeInStep {
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

    """

    # Добавте этот скрипт в create_interface()
    

    
    with gr.Blocks(theme=theme, css=css) as demo:
        # gr.Markdown("# 💬 AI Chat Assistant")
        
        current_code = gr.State("")
        
        with gr.Row():
            with gr.Column(scale=4):
                chatbot = gr.Chatbot(
                    height="600px",
                    show_label=False,
                    container=True,
                    show_copy_button=True,
                    elem_classes="chatbot",
                    type="messages",
                    render_markdown=True
                )
                

                
                with gr.Row(elem_classes="input-row"):
                    msg = gr.Textbox(
                        placeholder="Спросите что-нибудь...",
                        container=False,
                        scale=4,
                        show_label=False,
                        lines=1,  # Явно указываем одну строку
                        elem_classes="input-textbox"
                    )
                    submit = gr.Button(
                        "Отправить",
                        variant="primary",
                        scale=1,
                        elem_classes="submit-btn"
                    )
                    # clear = gr.Button("Очистить", scale=1)
            
            with gr.Column(scale=1, visible=True) as sidebar:
                model = gr.Dropdown(
                    choices=list(AVAILABLE_MODELS.keys()),
                    value="O3 (NEW)",
                    label="Модель"
                )
                
                # Добавляем слайдер глубины размышления (изначально скрытый)
                thinking_depth = gr.Slider(
                    minimum=2.0,  # Минимум 3 шага
                    maximum=25.0,
                    value=3.0,
                    step=1.0,    # Шаг целочисленный
                    label="Глубина размышления (количество шагов анализа)",
                    visible=False
                )
                
                direct_mode = gr.Checkbox(
                    label="Прямая нерация (без обрботки прмпта)",
                    value=False
                )
                file_output = gr.File(label="Загрузить файл")

        # Добавляем примеры ипользования  в виде кнопок
       

        # Добавляем кнопки анализа и запуска кода
        analyze_btn = gr.Button(
            "Анализировать",
            visible=False,
            elem_classes="analyze-btn"
        )
        
        run_btn = gr.Button(
            "▶️ Запустить код",
            visible=False,
            elem_classes="run-btn"
        )
        

          
    
        # Обработчики событий
        msg.submit(
            fn=chat_response,
            inputs=[msg, chatbot, model, direct_mode, thinking_depth, file_output],
            outputs=[chatbot, current_code],
            api_name=None
        )

        submit.click(
            fn=chat_response,
            inputs=[msg, chatbot, model, direct_mode, thinking_depth, file_output],
            outputs=[chatbot, current_code],
            api_name=None
        )



        analyze_btn.click(
            fn=analyze_code,
            inputs=[current_code],
            outputs=[chatbot]
        )
        

        # clear.click(
        #     fn=lambda: (None, ""),
        #     outputs=[chatbot, current_code]
        # )

        # Добавляем обработчик изменения модели
        def update_thinking_depth_visibility(model_name):
            return gr.update(visible=model_name == "GPT-4o THINK")
        
        model.change(
            fn=update_thinking_depth_visibility,
            inputs=[model],
            outputs=[thinking_depth]
        )

    return demo


if __name__ == "__main__":
    demo = create_interface()
   
     
    # Запускем мониторинг изменений в тдельном потоке

    
    demo.launch(
        show_api=False,
        show_error=True,
        favicon_path=None,
        auth=None,
        # quiet=True,  # Добавим этот параметр
      

    )