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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
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
import os
os.environ['CUDA_LAUNCH_BLOCKING'] = '1'

web_url ="https://api.quantumgrove.tech"

from fastapi import FastAPI, UploadFile, File, BackgroundTasks

import sqlite3

def increment_video_hits(video_id):
    conn = sqlite3.connect('content.db')
    cursor = conn.cursor()
    cursor.execute('''
        UPDATE videos
        SET hits = hits + 1
        WHERE id = ?
    ''', (video_id,))
    
    conn.commit()
    conn.close()

def create_database():
    conn = sqlite3.connect('api.db')
    cursor = conn.cursor()
    cursor.execute('''CREATE TABLE IF NOT EXISTS ban_list (
                        id INTEGER PRIMARY KEY,
                        ip TEXT NOT NULL,
                        reason TEXT NOT NULL)''')
    
    cursor.execute('''CREATE TABLE IF NOT EXISTS request_log (
                        id INTEGER PRIMARY KEY,
                        ip TEXT NOT NULL,
                        url TEXT NOT NULL,
                        method TEXT NOT NULL,
                        endpoint TEXT NOT NULL,
                        timestamp DATETIME DEFAULT CURRENT_TIMESTAMP)''')
    
    cursor.execute('''CREATE TABLE IF NOT EXISTS output (
                        id INTEGER PRIMARY KEY AUTOINCREMENT,
                        uuid TEXT NOT NULL,
                        timestamp DATETIME NOT NULL
                    )''')
    conn.commit()
    conn.close()


def output_entry_id(output_id):
    conn = sqlite3.connect('api.db')
    cursor = conn.cursor()
    timestamp = datetime.now()
    cursor.execute("INSERT INTO output (uuid, timestamp) VALUES (?, ?)", (output_id, timestamp))
    conn.commit()
    conn.close()

from datetime import datetime, timedelta

def delete_uuid(uuid_to_delete):
    path = "./outputs"
    directories = []
    for root, dirs, files in os.walk(path):
        for dir in dirs:
            directories.append(os.path.join(root, dir))
    for i in directories:
        temp = i.split('/')
        if temp[-1] == uuid_to_delete:
            shutil.rmtree(i)
            return True
    return False

def get_old_out_ids():
    conn = sqlite3.connect('api.db')
    cursor = conn.cursor()
    ten_minutes_ago = datetime.now() - timedelta(minutes=5)
    cursor.execute("SELECT uuid FROM output WHERE timestamp < ?", (ten_minutes_ago,))
    rows = cursor.fetchall()
    if len(rows) == 0:
        return None
    
    for entry in rows:
        if delete_uuid(entry[0]):
            conn.execute("DELETE FROM output WHERE uuid = ?", (entry[0],))
            conn.commit()
    conn.close()
    return True

def insert_ban(ip, reason):
    conn = sqlite3.connect('api.db')
    cursor = conn.cursor()
    cursor.execute('''INSERT INTO ban_list (ip, reason) VALUES (?, ?)''', (ip, reason))
    conn.commit()
    conn.close()

def search_ban(ip):
    conn = sqlite3.connect('api.db')
    cursor = conn.cursor()
    cursor.execute('''SELECT * FROM ban_list WHERE ip = ?''', (ip,))
    rows = cursor.fetchall()
    conn.close()
    return rows

def log_request(ip, url, method, endpoint):
    conn = sqlite3.connect('api.db')
    cursor = conn.cursor()
    cursor.execute('''INSERT INTO request_log (ip, url, method, endpoint) VALUES (?, ?, ?, ?)''', (ip, url, method, endpoint))
    conn.commit()
    conn.close()

def create_database_port():
    conn = sqlite3.connect('./subprocess/port.db', check_same_thread=False)
    cursor = conn.cursor()
    cursor.execute('''DROP TABLE IF EXISTS ports''')
    conn.commit()
    cursor.execute('''CREATE TABLE IF NOT EXISTS ports
                    (port INTEGER)''')
    conn.commit()
    conn.close()

def search_port(port):
    conn = sqlite3.connect('./subprocess/port.db')
    cursor = conn.cursor()
    cursor.execute('''SELECT * FROM ports WHERE port = ?''', (port,))
    rows = cursor.fetchall()
    conn.close()
    if len(rows) > 0:
        return True
    return False

def is_table_empty():
    return False #test remove
    conn = sqlite3.connect('./subprocess/port.db')
    cursor = conn.cursor()
    cursor.execute('''SELECT COUNT(*) FROM ports''')
    count = cursor.fetchone()[0]
    conn.close()
    if count == 0:
        for item in os.listdir('/tmp/gradio'):
            item_path = os.path.join('/tmp/gradio', item)
            if os.path.isdir(item_path):
                shutil.rmtree(item_path)

def remove_port(port):
    if(search_port(port)):
        conn = sqlite3.connect('./subprocess/port.db')
        cursor = conn.cursor()
        cursor.execute('''DELETE FROM ports WHERE port = ?''', (port,))
        conn.commit()
        conn.close()
        return True
    return False

def insert_port(port):
    if( not search_port(port)):
        conn = sqlite3.connect('./subprocess/port.db')
        cursor = conn.cursor()
        cursor.execute('''INSERT INTO ports (port) VALUES (?)''', (port,))
        conn.commit()
        conn.close()
        return True
    else:
        return False
    


def generate_response(response_message , response_status ,uuid_code , age , gender , metadata):

    response_dict = {
                "response_message" : response_message,
                "response_status" : response_status,
                "data" :{
                    "UUID" : uuid_code,
                    "info" : {"age" : age , "gender" : gender},
                    "metadata" : metadata
                }
            }

    return response_dict


import numpy as np
import cv2
import time
import io
import gc
import shutil
import uuid
import torch
import subprocess
import torchvision.transforms as transforms
from scripts.psp import pSp
from argparse import Namespace
import dlib
from scripts.align_all_parallel import align_face
from scripts.augmentations import AgeTransformer
from scripts.common import tensor2im
from torchvision.transforms.functional import normalize
from scripts.basicsr.utils import  img2tensor, tensor2img
from scripts.basicsr.utils.misc import get_device
from scripts.facelib.utils.face_restoration_helper import FaceRestoreHelper
from scripts.basicsr.utils.registry import ARCH_REGISTRY
from scripts.basicsr.archs.rrdbnet_arch import RRDBNet
from scripts.basicsr.utils.realesrgan_utils import RealESRGANer
from scripts.facelib.utils.misc import is_gray
import PIL.Image
from scripts.erasescratches.models import Pix2PixHDModel_Mapping
from scripts.erasescratches.options import Options
from scripts.maskscratches import ScratchesDetector
from scripts.util import irregular_hole_synthesize, tensor_to_ndarray
import insightface
from insightface.app import FaceAnalysis
from pydub import AudioSegment
import scripts.RRDBNet_arch as arch
from RealESRGAN import RealESRGAN
from PIL import Image
import socket
import subprocess
from gradio_client import Client, handle_file

device = ("cuda" if torch.cuda.is_available() else "cpu")

#age model
EXPERIMENT_TYPE = 'ffhq_aging'
model_path = "./models/sam_ffhq_aging.pt"
model_age_slider = None

EXPERIMENT_DATA_ARGS = {
    "ffhq_aging": {
        "transform": transforms.Compose([
            transforms.Resize((256, 256)),
            transforms.ToTensor(),
            transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])])
    }
}
EXPERIMENT_ARGS = EXPERIMENT_DATA_ARGS[EXPERIMENT_TYPE]


def load_model_age():
    try:
        if not get_model_state_age():
            ckpt = torch.load(model_path, map_location='cpu')
            opts = ckpt['opts']
            del ckpt
            torch.cuda.empty_cache()
            opts['checkpoint_path'] = model_path
            opts = Namespace(**opts)
            global model_age_slider
            model_age_slider = pSp(opts)
            del opts
            torch.cuda.empty_cache()
            model_age_slider.eval()
            model_age_slider.cuda()
            torch.cuda.empty_cache()
        return True
    except:
        return False

def unload_model_age():
    try:
        global model_age_slider
        if model_age_slider is not None:
            model_age_slider = None
            torch.cuda.empty_cache()
        return True
    except:
        return False
    
def get_model_state_age():
    global model_age_slider
    if model_age_slider is None:
        return False
    return True
#model bg
from scripts.BG import BG
bg_model = None

def load_model_bg():
    try:
        global bg_model
        if bg_model is None:
            bg_model = BG()
        return True
    except:
        return False

def unload_model_bg():
    try:
        global bg_model
        if bg_model is not None:
            bg_model = None
        return True
    except:
        return False
    
def get_model_state_bg():
    global bg_model
    if bg_model is None:
        return False
    return True
#model code_former
model_upsampler = None
model_code_former = None


def set_realesrgan_cf():
    use_half = True if torch.cuda.is_available() else False
    model = RRDBNet(
        num_in_ch=3,
        num_out_ch=3,
        num_feat=64,
        num_block=23,
        num_grow_ch=32,
        scale=2,
    )
    upsampler = RealESRGANer(
        scale=2,
        model_path="./models/realesrgan/RealESRGAN_x2plus.pth",
        model=model,
        tile=400,
        tile_pad=40,
        pre_pad=0,
        half=use_half
    )
    return upsampler



def load_model_cf():
    try:
        device = get_device()
        global model_code_former
        model_code_former = ARCH_REGISTRY.get('CodeFormer')(dim_embd=512, codebook_size=1024, n_head=8, n_layers=9,
                                                connect_list=['32', '64', '128', '256']).to(device)
        ckpt_path = 'models/CodeFormer/codeformer.pth'
        checkpoint = torch.load(ckpt_path)['params_ema']
        model_code_former.load_state_dict(checkpoint)
        model_code_former.eval()

        global model_upsampler
        model_upsampler = set_realesrgan_cf()
        return True
    except:
        return False


def get_model_state_cf():
    global model_code_former
    if model_code_former is None:
        return False
    else:
        return True


def unload_model_cf():
    if get_model_state_cf() == False:
        return True
    try:
        global model_code_former
        model_code_former = None
        torch.cuda.empty_cache()

        global model_upsampler
        model_upsampler = None
        torch.cuda.empty_cache()
        return True
    except:
        return False
#model swap
swap_model = None
app_model = None

def load_model_swap():
    try:
        global swap_model
        global app_model
        if swap_model is None:
            swap_model = insightface.model_zoo.get_model("./models/inswapper_128.onnx",download=False,download_zip=False)
            app_model = FaceAnalysis(name="buffalo_l")
            app_model.prepare(ctx_id=0,det_size=(640,640))
        return True
    except:
        return False

def unload_model_swap():
    try:
        global swap_model
        global app_model
        if swap_model is not None:
            swap_model = None
        if app_model is not None:
            app_model = None
        return True
    except:
        return False
    
def get_model_state_swap():
    global swap_model

    if swap_model is None:
        return False
    return True
#model rest
model_scratches_remove = None
model_scratches_remove_detector = None
model_scratches_remove_options = None


def load_model_rest():
    try:

        model_path = "./models/zeroscratches/restoration"
        global model_scratches_remove_detector
        model_scratches_remove_detector = ScratchesDetector('./models/zeroscratches')
        gpu_ids = []
        if torch.cuda.is_available():
            gpu_ids = [d for d in range(torch.cuda.device_count())]
        global model_scratches_remove_options
        model_scratches_remove_options = Options(model_path, gpu_ids)
        model_scratches = Pix2PixHDModel_Mapping()
        global model_scratches_remove
        model_scratches_remove = Pix2PixHDModel_Mapping()
        model_scratches_remove.initialize(model_scratches_remove_options)
        model_scratches_remove.eval()
        return True
    except:
        return False


def get_model_state_rest():
    global model_scratches_remove
    if model_scratches_remove is None:
        return False
    else:
        return True


def unload_model_rest():
    if get_model_state_rest() == False:
        return True
    try:
        global model_scratches_remove
        model_scratches_remove = None
        global model_scratches_remove_detector
        model_scratches_remove_detector = None
        global model_scratches_remove_options
        model_scratches_remove_options = None
        torch.cuda.empty_cache()
        return True
    except:
        return False
#model talk
from scripts.talker import sad_talker
sad_talker_model = None

def load_model_talk():
    try:
        global sad_talker_model
        if sad_talker_model is None:
            sad_talker_model = sad_talker()
        return True
    except:
        return False

def unload_model_talk():
    try:
        global sad_talker_model
        if sad_talker_model is not None:
            sad_talker_model = None
        return True
    except:
        return False
    
def get_model_state_talk():
    global sad_talker_model
    if sad_talker_model is None:
        return False
    return True
#model sky
from scripts.SKY import SKY
sky_model = None

def load_model_sky():
    try:
        global sky_model
        if sky_model is None:
            sky_model = SKY()
        return True
    except:
        return False

def unload_model_sky():
    try:
        global sky_model
        if sky_model is not None:
            sky_model = None
        return True
    except:
        return False
    
def get_model_state_sky():
    global sky_model
    if sky_model is None:
        return False
    return True
#model up 
model_upscale = None

def load_model_up():
    try:
        model_path_upscale = './models/RRDB_ESRGAN_x4.pth'
        global model_upscale
        model_upscale = arch.RRDBNet(3, 3, 64, 23, gc=32)
        model_upscale.load_state_dict(torch.load(model_path_upscale), strict=True)

        model_upscale = model_upscale.half()
        model_upscale.eval()
        model_upscale = model_upscale.to(device)
        return True
    except:
        return False


def get_model_state_up():
    global model_upscale
    if model_upscale is None:
        return False
    else:
        return True


def unload_model_up():
    if get_model_state_up() == False:
        return True
    try:
        global model_upscale
        model_upscale = None
        torch.cuda.empty_cache()
        return True
    except:
        return False
#agr slider helper
def run_alignment(image_path):
    predictor = dlib.shape_predictor("./models/shape_predictor_68_face_landmarks.dat")
    aligned_image = align_face(filepath=image_path, predictor=predictor)
    return aligned_image

def run_on_batch(inputs, model_age_slider):
    result_batch = model_age_slider(inputs.to("cuda").float(), randomize_noise=False, resize=False)
    return result_batch


#model model_esrgan 
model_esrgan = None

def load_model_esrgan():
    try:
        global model_esrgan
        model_esrgan = RealESRGAN( torch.device(device), scale=2)
        model_esrgan.load_weights('./models/realesrgan/RealESRGAN_x2plus.pth', download=False)

        return True
    except:
        return False


def get_model_state_esrgan():
    global model_esrgan
    if model_esrgan is None:
        return False
    else:
        return True


def unload_model_esrgan():
    if get_model_state_esrgan() == False:
        return True
    try:
        global model_esrgan
        model_esrgan = None
        torch.cuda.empty_cache()
        return True
    except:
        return False

#model lp 
def is_port_in_use(port, host='localhost'):
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.settimeout(1)
        try:
            s.bind((host, port))
        except socket.error:
            return True  
        return False  
    
import requests
def is_server_ready(url, timeout=30):
    start_time = time.time()
    while True:
        try:
            response = requests.get(url)
            if response.status_code == 200:
                return True
        except requests.RequestException:
            pass
        
        if time.time() - start_time > timeout:
            return False
        time.sleep(1) 

model_port_lp = None

def load_port_lp():
    try:
        if not get_model_port_lp():
            port_to_check = 60000
            con_count = 0
            while(True):
                if con_count > 100:
                    break
                if is_port_in_use(port_to_check) or (search_port(port_to_check)):
                    port_to_check = port_to_check + 1
                    con_count = con_count + 1
                else:
                    break
            port_to_check = str(port_to_check)
            script_dir = "./subprocess/LivePortrait"
            script_dir = os.path.abspath(script_dir)

            command = ["/home/qtech/miniconda3/envs/LivePortrait/bin/python", script_dir+"/app.py", "--server_port", port_to_check]
            process = subprocess.Popen(command, cwd=script_dir)

            global model_port_lp
            model_port_lp = {"port":port_to_check , "pid":process}

            insert_port(port_to_check)

            is_server_ready(f'http://127.0.0.1:{port_to_check}/')


        return True
    except:
        return False

def unload_port_lp():
    try:
        if get_model_port_lp():
            global model_port_lp
            port = model_port_lp['port']
            remove_port(port)
            pid = model_port_lp['pid']
            pid.terminate()
            pid.kill()
            model_port_lp = None
            return True
        else:
            return True
    except:
        return False
    
def get_model_port_lp():
    global model_port_lp
    if model_port_lp is None:
        return False
    return True

def unload_model_all():
    try:
        unload_model_age()
        unload_model_bg()
        unload_model_cf()
        unload_model_up()
        unload_model_swap()
        unload_model_rest()
        unload_model_talk()
        unload_model_sky()
        unload_model_esrgan()
        unload_port_lp()
        try:
            is_table_empty()
        except:
            pass
        return True
    except:
        return False

    
def start_http_file_server():
    directory = './outputs'
    port = 8010
    if not os.path.exists(directory):
        os.makedirs(directory)
    cmd = f"python -m http.server {port} --directory {directory}"
    return subprocess.Popen(cmd, shell=True)

import asyncio
from datetime import datetime


time_last_call = None

async def continuous_function():
    while True:
        if get_old_out_ids():#test remove
            print(f"Old Data Deleted")

        if torch.cuda.is_available():
            torch.cuda.empty_cache()
        global time_last_call
        if time_last_call is not None:
            current_time = datetime.now()
            time_difference = current_time - time_last_call
            time_elapsed_minutes = time_difference.total_seconds() / 60

            if time_elapsed_minutes > 1:
                gc.collect()

            if time_elapsed_minutes > 5:
                model_delete_status = unload_model_all()
                

                print(f"model delete status = {model_delete_status}")

                time_last_call = None

        await asyncio.sleep(60*1)

async def start_continuous_function():
    await continuous_function()


app = FastAPI()

from fastapi.responses import HTMLResponse


from fastapi import Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse

app = FastAPI()

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

from api_analytics.fastapi import Analytics
app.add_middleware(Analytics, api_key="87686a97-20e4-42c9-99ab-9410c2ef2e65") #test remove

# async def custom_middleware(request: Request, call_next):
#     client_ip = request.client.host
#     request_url = str(request.url)
#     request_method = request.method
#     endpoint = request.url.path

#     log_request(client_ip, request_url, request_method, endpoint)
#     print("\n\n\n\n\n")
#     print("request:")
#     headers = request.headers
#     query_params = request.query_params
#     path_params = request.path_params
#     method = request.method
#     print("\n\n\n\n\n")
#     print("Request Method:", method)
#     print("Request Path Params:", path_params)
#     print("Request Query Params:", query_params)
#     print("Request Headers:", headers)
#     print("\n\n\n\n\n")
#     print("\n\n\n\n\n")
#     if len(search_ban(client_ip)) > 0:
#         return JSONResponse(status_code=401, content={"detail": "Unauthorized"})
    
#     # user_agent = request.headers.get("user-agent", "").lower()
#     # print(user_agent)
#     # if "android" not in user_agent:
#     #     insert_data(client_ip, "Access using unapproved device type")
#     #     return JSONResponse(status_code=401, content={"detail": "Unauthorized"})
    
#     response = await call_next(request)
    
#     if (response.status_code == 404) or (response.status_code == 405):
#         insert_ban(client_ip, "Access using unapproved method/endpoint")
#         return JSONResponse(status_code=401, content={"detail": "Unauthorized"})

#     return response

from fastapi import Request
from fastapi.responses import JSONResponse, Response, StreamingResponse

from fastapi import Request, Response
from fastapi.responses import StreamingResponse
import io

async def custom_middleware(request: Request, call_next):
    client_ip = request.client.host
    request_url = str(request.url)
    request_method = request.method
    endpoint = request.url.path

    log_request(client_ip, request_url, request_method, endpoint)

    print("\n\n\n\n\n")
    print("Request Details:")
    headers = request.headers
    query_params = request.query_params
    path_params = request.path_params
    method = request.method
    print("Request Method:", method)
    print("Request Path Params:", path_params)
    print("Request Query Params:", query_params)
    print("Request Headers:", headers)
    print("\n\n\n\n\n")

    if len(search_ban(client_ip)) > 0:
        return JSONResponse(status_code=401, content={"detail": "Unauthorized"})

    response = await call_next(request)

    if isinstance(response, StreamingResponse):
        # Buffer the streaming content for inspection (be cautious of performance impacts)
        content = io.BytesIO()
        async for chunk in response.body_iterator:
            content.write(chunk)
        content.seek(0)
        response = StreamingResponse(content, media_type=response.media_type)

        # Now you can log the content if needed
        print("Response Content:", content.getvalue().decode('utf-8', errors='replace'))
    else:
        # Handle non-streaming responses as before
        response_body = b"".join([chunk async for chunk in response.body_iterator]).decode('utf-8', errors='replace')
        print("Response Content:", response_body)

    if (response.status_code == 404) or (response.status_code == 405):
        insert_ban(client_ip, "Access using unapproved method/endpoint")
        return JSONResponse(status_code=401, content={"detail": "Unauthorized"})

    return response



app.middleware('http')(custom_middleware)


@app.get("/", response_class=HTMLResponse)
def read_index():
    index_path = './assests/index.html'
    with open(index_path, 'r') as file:
        content = file.read()
    return HTMLResponse(content=content)

from fastapi.responses import FileResponse

@app.get("/favicon.ico")
async def get_image():
    image_path = "./assests/logo.png" 
    return FileResponse(image_path, media_type="image/png")

@app.on_event("startup")
async def startup_event():
    asyncio.create_task(start_continuous_function())

@app.on_event("shutdown")
async def shutdown_event():
    print("Clearing Models")
    model_status = unload_model_all()
    print("Model clear status = "+str(model_status))
    asyncio.get_event_loop().stop()
    print("Stopping Server")

ALLOWED_IMAGE_FORMATS = ["jpg", "jpeg", "png"]
ALLOWED_AUDIO_FORMATS = ["wav", "mp3"]

def delete_uuid(uuid_to_delete):
    path = "./outputs"
    directories = []
    for root, dirs, files in os.walk(path):
        for dir in dirs:
            directories.append(os.path.join(root, dir))
    for i in directories:
        temp = i.split('/')
        if temp[-1] == uuid_to_delete:
            shutil.rmtree(i)
            return True
    return False

@app.delete("/Delete/{uuid}")
async def delete_item(uuid: str):
    gc.collect()
    try:
        result = delete_uuid(uuid)
        if result:
           return {"error": f"UUID '{uuid}' does not exist"}

        return {"message": f"UUID '{uuid}' deleted"}

    except :
        return {"error": "unexpected error"}
    


@app.put("/ClearModel_ALL")
async def clearModel_ALL():
    try:
        model_delete_status = unload_model_all()


        if model_delete_status:
            global time_last_call
            time_last_call = None
            return {"message": "vram cleared"}
        else :
            return {"message": "vram not cleared"}

    except :
        return {"error": "unexpected error"}

@app.post("/AgeSlider")
async def age_slider(background_tasks: BackgroundTasks , image: UploadFile = File(...)):

#################
#################
    #get time
    global time_last_call
    time_last_call = datetime.now()
##################
    try:

###################################################################################################################################################################################
############################################################################### Test Input Image ##################################################################################
###################################################################################################################################################################################
        image_format = image.filename.split(".")[-1].lower()
        if image_format not in ALLOWED_IMAGE_FORMATS:
            response_message = f"Unsupported image format. Supported formats: {', '.join(ALLOWED_IMAGE_FORMATS)}"
            response_status = "Failure"
            return generate_response(response_message , response_status ,'' , '' , '' , [])

        contents = await image.read()
        nparr = np.frombuffer(contents, np.uint8)
        image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)

        if image is None:
            response_message = "Image is empty"
            response_status = "Failure"
            return generate_response(response_message , response_status ,'' , '' , '' , [])
        
        app_face = FaceAnalysis(name="buffalo_l")
        app_face.prepare(ctx_id=0,det_size=(640,640))

        faces = app_face.get(image)
        if len(faces)==0:
            response_message = "Error no face detected"
            response_status = "Failure"
            return generate_response(response_message , response_status ,'' , '' , '' , [])
        elif len(faces)!=1:
            response_message = "Error more than 1 face detected"
            response_status = "Failure"
            return generate_response(response_message , response_status ,'' , '' , '' , [])

        info_age = faces[0]['age']
        info_gender = faces[0]['gender']
###################################################################################################################################################################################
############################################################################### Image Processing ##################################################################################
###################################################################################################################################################################################

        unique_id = str(uuid.uuid4())
        output_path = f"./outputs/AGE/{unique_id}"
        os.makedirs(output_path)

        # loading model
        if not get_model_state_age():
            unload_model_all()
            model_status = load_model_age()
            print(f"model status =========={model_status}")


        cv2.imwrite(output_path+'/input.png',image)
        aligned_image = run_alignment(output_path+'/input.png')
        #aligned_image.save(output_path+"/input_aligned_cropped.png")
        copy_aligned_image = aligned_image.copy()

        aligned_image.resize((256, 256))
        img_transforms = EXPERIMENT_ARGS['transform']
        input_image = img_transforms(aligned_image)

        target_ages = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
        age_transformers = [AgeTransformer(target_age=age) for age in target_ages]

        images = []


        for age_transformer in age_transformers:
            with torch.no_grad():
                input_image_age = [age_transformer(input_image.cpu()).to('cuda')]
                input_image_age = torch.stack(input_image_age)
                result_tensor = run_on_batch(input_image_age, model_age_slider)[0]
                result_image = tensor2im(result_tensor)
                images.append(result_image)

        image_paths_temp = []

        result_paths_list = []
        for idx,i in enumerate(images):
            image_temp = np.array(i)
            image_temp = cv2.cvtColor(np.array(image_temp), cv2.COLOR_RGB2BGR)
            result_path_temp = output_path+"/output_"+str(idx)+".png"
            cv2.imwrite(result_path_temp,image_temp)

            image_url_temp = '/'.join(result_path_temp.split('/')[-3:])
            image_url_temp = f"{web_url}:8001/{image_url_temp}"
            result_paths_list.append({"age" :str(target_ages[idx]), "image_url" : image_url_temp})

            image_paths_temp.append(result_path_temp)
        
        closest_age = min(target_ages, key=lambda x: abs(x - info_age))
        closest_age = target_ages.index(closest_age)

        os.remove(f"{output_path}/output_{closest_age}.png")
        copy_aligned_image = copy_aligned_image.resize((1024, 1024))
        copy_aligned_image.save(f"{output_path}/output_{closest_age}.png")

        background_tasks.add_task(Age_Slider_Video,unique_id , image_paths_temp)

        os.remove(output_path+'/input.png')

        response_message = "AgeSlider Ran Sucessfully"
        response_status = "Successful"
        return generate_response(response_message , response_status ,unique_id , info_age , ["Female","Male"][info_gender] , result_paths_list)

    except Exception as e:
        shutil.rmtree(output_path)
        return generate_response(f"Unknown Internal Error : {e}" , "Failure" ,'' , '' , '' , [])

def Age_Slider_Video(uuid_input , image_files):
    temp_file_uuid = str(uuid.uuid4())
    directory_path = f'./outputs/{temp_file_uuid}'
    if os.path.exists(directory_path):
        shutil.rmtree(directory_path)
    os.mkdir(directory_path)

    from scripts.morph_video import doMorphing
    doMorphing(image_files, 0.3, 20, f"{directory_path}/output")
    video_path = f"{directory_path}/output_combined.mp4"
    gif_path = f"{directory_path}/output.gif"

    shutil.copy(video_path, f"./outputs/AGE/{uuid_input}/output_combined.mp4")
    shutil.copy(gif_path, f"./outputs/AGE/{uuid_input}/output.gif")

    shutil.rmtree(directory_path)
    output_entry_id(uuid_input) 

@app.post("/AgeSliderVideo/{uuid_input}")
async def ageslidervideo(uuid_input: str):
##################
    try:
        input_path = f"./outputs/AGE/{uuid_input}"
###################################################################################################################################################################################
############################################################################### Test Input UUID  ##################################################################################
###################################################################################################################################################################################
        if not os.path.exists(input_path):
            response_message = f"Invalid UUID , file does not exist"
            response_status = "Failure"
            return generate_response(response_message , response_status ,'' , '' , '' , [])
###################################################################################################################################################################################
###############################################################################    Processing    ##################################################################################
###################################################################################################################################################################################

        output_video_path = ''
        output_gif_path = ''
        duration = 120
        start_time = time.time()
        while time.time() - start_time < duration:

            if output_video_path == '':
                if os.path.exists(input_path+"/output_combined.mp4"):
                    output_video_path = f'{web_url}:8001/AGE/{uuid_input}'+"/output_combined.mp4"
                     
            if output_gif_path == '':       
                if os.path.exists(input_path+"/output.gif"):
                    output_gif_path = f'{web_url}:8001/AGE/{uuid_input}'+"/output.gif"
            
            if output_video_path != '':
                if output_gif_path != '':
                    #output_entry_id(uuid_input) #test remove
                    response_message = "Video Genrated Sucessfully"
                    response_status = "Successful"
                    return generate_response(response_message , response_status ,uuid_input , '' , '', [{"video":output_video_path , "gif":output_gif_path}])
                

        if output_video_path != '': 
            if output_gif_path == '':
                response_message = "Only Video Genrated"
                response_status = "Failure"
                return generate_response(response_message , response_status ,uuid_input , '' , '', [{"video":output_video_path , "gif":output_gif_path}])
                    

        if output_gif_path != '': 
            if output_video_path == '':
                response_message = "Only GIF Genrated"
                response_status = "Failure"
                return generate_response(response_message , response_status ,uuid_input , '' , '', [{"video":output_video_path , "gif":output_gif_path}])
                    

        
        if output_gif_path == '': 
            if output_video_path == '':
                response_message = "Timeout Reached , Retry Later"
                response_status = "Failure"
                return generate_response(response_message , response_status ,uuid_input , '' , '', [{"video":output_video_path , "gif":output_gif_path}])




    except Exception as e:
        return generate_response(f"Unknown Internal Error : {e}" , "Failure" ,'' , '' , '' , [])



@app.post("/BG_remove")
async def bg(image: UploadFile = File(...)):

#################
#################
    #get time
    global time_last_call
    time_last_call = datetime.now()
##################
    try:

###################################################################################################################################################################################
############################################################################### Test Input Image ##################################################################################
###################################################################################################################################################################################
        image_format = image.filename.split(".")[-1].lower()
        if image_format not in ALLOWED_IMAGE_FORMATS:
            response_message = f"Unsupported image format. Supported formats: {', '.join(ALLOWED_IMAGE_FORMATS)}"
            response_status = "Failure"
            return generate_response(response_message , response_status ,'' , '' , '' , [])

        contents = await image.read()
        nparr = np.frombuffer(contents, np.uint8)
        image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)

        if image is None:
            response_message = "Image is empty"
            response_status = "Failure"
            return generate_response(response_message , response_status ,'' , '' , '' , [])
###################################################################################################################################################################################
############################################################################### Image Processing ##################################################################################
###################################################################################################################################################################################





        unique_id = str(uuid.uuid4())
        output_path = f"./outputs/BG/{unique_id}"

        


        # loading model
        if not get_model_state_bg():
            unload_model_all()
            model_status = load_model_bg()
            print(f"model status =========={model_status}")

        
        output_image = bg_model.BG_remove(image)

        os.makedirs(output_path)
        output_image_path = output_path+'/output.png'
        cv2.imwrite(output_image_path,output_image)


        output_entry_id(unique_id) #test remove
        response_message = "BG Removed Sucessfully"
        response_status = "Successful"
        return generate_response(response_message , response_status ,unique_id , '' , '', [f"{web_url}:8001/BG/{unique_id}/output.png"])





    except Exception as e:
        shutil.rmtree(output_path)
        return generate_response(f"Unknown Internal Error : {e}" , "Failure" ,'' , '' , '' , [])


@app.post("/CodeFormer")
async def codeformer(image: UploadFile = File(...)):

#################
#################
    #get time
    global time_last_call
    time_last_call = datetime.now()
##################
    try:

###################################################################################################################################################################################
############################################################################### Test Input Image ##################################################################################
###################################################################################################################################################################################
        image_format = image.filename.split(".")[-1].lower()
        if image_format not in ALLOWED_IMAGE_FORMATS:
            response_message = f"Unsupported image format. Supported formats: {', '.join(ALLOWED_IMAGE_FORMATS)}"
            response_status = "Failure"
            return generate_response(response_message , response_status ,'' , '' , '' , [])

        contents = await image.read()
        nparr = np.frombuffer(contents, np.uint8)
        image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)

        if image is None:
            response_message = "Image is empty"
            response_status = "Failure"
            return generate_response(response_message , response_status ,'' , '' , '' , [])
        
        height = image.shape[0]
        width = image.shape[1]

        if (height*width)>(500*500) :
            aspect_ratio = width / height
            new_height = int(((500*500) / aspect_ratio) ** 0.5)
            new_width = int(aspect_ratio * new_height)
            image = cv2.resize(image, (new_width, new_height), interpolation=cv2.INTER_AREA)
###################################################################################################################################################################################
############################################################################### Image Processing ##################################################################################
###################################################################################################################################################################################





        unique_id = str(uuid.uuid4())
        output_path = f"./outputs/CF/{unique_id}"
        

        # loading model
        if not get_model_state_cf():
            unload_model_all()
            model_status = load_model_cf()
            print(f"model status =========={model_status}")

        
        #image = cv2.imread(output_path+'/input.png')


        has_aligned = False
        only_center_face = False
        draw_box = False
        detection_model = "retinaface_resnet50"
        background_enhance = True
        face_upsample = True
        upscale = 2
        codeformer_fidelity = 0.5
        img = image#cv2.imread(str(img_path), cv2.IMREAD_COLOR)
        upscale = int(upscale) # convert type to int
        if upscale > 4: # avoid memory exceeded due to too large upscale
            upscale = 4
        if upscale > 2 and max(img.shape[:2])>1000: # avoid memory exceeded due to too large img resolution
            upscale = 2
        if max(img.shape[:2]) > 1500: # avoid memory exceeded due to too large img resolution
            upscale = 1
            background_enhance = False
            face_upsample = False
        face_helper = FaceRestoreHelper(
            upscale,
            face_size=512,
            crop_ratio=(1, 1),
            det_model=detection_model,
            save_ext="png",
            use_parse=True,
            device=device,
        )
        bg_upsampler = model_upsampler if background_enhance else None
        face_upsampler = model_upsampler if face_upsample else None
        if has_aligned:
            # the input faces are already cropped and aligned
            img = cv2.resize(img, (512, 512), interpolation=cv2.INTER_LINEAR)
            face_helper.is_gray = is_gray(img, threshold=5)
            if face_helper.is_gray:
                print('\tgrayscale input: True')
                face_helper.cropped_faces = [img]
        else:
            face_helper.read_image(img)
            # get face landmarks for each face
            num_det_faces = face_helper.get_face_landmarks_5(
            only_center_face=only_center_face, resize=640, eye_dist_threshold=5
            )
            print(f'\tdetect {num_det_faces} faces')
            # align and warp each face
            face_helper.align_warp_face()

        # face restoration for each cropped face
        for idx, cropped_face in enumerate(face_helper.cropped_faces):
            # prepare data
            cropped_face_t = img2tensor(
                cropped_face / 255.0, bgr2rgb=True, float32=True
            )
            normalize(cropped_face_t, (0.5, 0.5, 0.5), (0.5, 0.5, 0.5), inplace=True)
            cropped_face_t = cropped_face_t.unsqueeze(0).to(device)
            try:
                with torch.no_grad():
                    output = model_code_former(cropped_face_t, w=codeformer_fidelity, adain=True)[0]
                    restored_face = tensor2img(output, rgb2bgr=True, min_max=(-1, 1))
                del output
                torch.cuda.empty_cache()
            except RuntimeError as error:
                print(f"Failed inference for CodeFormer: {error}")
                restored_face = tensor2img(
                    cropped_face_t, rgb2bgr=True, min_max=(-1, 1)
                )
            restored_face = restored_face.astype("uint8")
            face_helper.add_restored_face(restored_face)

        # paste_back
        if not has_aligned:
            # upsample the background
            if bg_upsampler is not None:
                # Now only support RealESRGAN for upsampling background
                bg_img = bg_upsampler.enhance(img, outscale=upscale)[0]
            else:
                bg_img = None
            face_helper.get_inverse_affine(None)
            # paste each restored face to the input image
        if face_upsample and face_upsampler is not None:
            restored_img = face_helper.paste_faces_to_input_image(
                    upsample_img=bg_img,
                    draw_box=draw_box,
                    face_upsampler=face_upsampler,
                )
        else:
            restored_img = face_helper.paste_faces_to_input_image(
                    upsample_img=bg_img, draw_box=draw_box
                )
            




        output_image_path = output_path+'/output.png'
        os.makedirs(output_path)
        cv2.imwrite(output_image_path,restored_img)


        #cv2.imwrite(output_path+'/output.png',restored_img)


       


        output_entry_id(unique_id) #test remove
        response_message = "CodeFormer Image Sucessfully"
        response_status = "Successful"
        return generate_response(response_message , response_status ,unique_id , '' , '', [f"{web_url}:8001/CF/{unique_id}/output.png"])


    except Exception as e:
        shutil.rmtree(output_path)
        return generate_response(f"Unknown Internal Error : {e}" , "Failure" ,'' , '' , '' , [])

@app.post("/FaceSwap_single_image")
async def Swap_single_image(image: UploadFile = File(...)):

#################
#################
    #get time
    global time_last_call
    time_last_call = datetime.now()
##################
    try:

###################################################################################################################################################################################
############################################################################### Test Input Image ##################################################################################
###################################################################################################################################################################################
        image_format = image.filename.split(".")[-1].lower()
        if image_format not in ALLOWED_IMAGE_FORMATS:
            response_message = f"Unsupported image format. Supported formats: {', '.join(ALLOWED_IMAGE_FORMATS)}"
            response_status = "Failure"
            return generate_response(response_message , response_status ,'' , '' , '' , [])

        contents = await image.read()
        nparr = np.frombuffer(contents, np.uint8)
        image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)

        if image is None:
            response_message = "Image is empty"
            response_status = "Failure"
            return generate_response(response_message , response_status ,'' , '' , '' , [])
###################################################################################################################################################################################
############################################################################### Image Processing ##################################################################################
###################################################################################################################################################################################





        unique_id = str(uuid.uuid4())
        output_path = f"./outputs/SWAP/{unique_id}"




        # loading model
        if not get_model_state_swap():
            unload_model_all()
            model_status = load_model_swap()
            print(f"model status =========={model_status}")

        faces = app_model.get(image)

        if len(faces)==0:
            response_message = "Error no face detected"
            response_status = "Failure"
            return generate_response(response_message,response_status,'' , '' , '' , [])
        elif len(faces)!=2:
            response_message = "Error more than 2 face detected"
            response_status = "Failure"
            return generate_response(response_message,response_status,'' , '' , '' , [])
        
        face1 = faces[0]
        face2 = faces[1]

        image = swap_model.get(image,face1,face2,paste_back=True)
        image = swap_model.get(image,face2,face1,paste_back=True)


        os.makedirs(output_path)
        output_image_path = output_path+'/output.png'
        cv2.imwrite(output_image_path,image)


        output_entry_id(unique_id) #test remove
        response_message = "Face Swapped Sucessfully"
        response_status = "Successful"
        return generate_response(response_message , response_status ,unique_id , '' , '', [f"{web_url}:8001/SWAP/{unique_id}/output.png"])


    except Exception as e:
        shutil.rmtree(output_path)
        return generate_response(f"Unknown Internal Error : {e}" , "Failure" ,'' , '' , '' , [])



@app.post("/FaceSwap_two_images")
async def swap_two_image(image1: UploadFile = File(...), image2: UploadFile = File(...)):

#################
#################
    #get time
    global time_last_call
    time_last_call = datetime.now()
##################
    try:

###################################################################################################################################################################################
############################################################################### Test Input Image ##################################################################################
###################################################################################################################################################################################
        images = []
        for idx ,image in enumerate([image1,image2]):
            image_format = image.filename.split(".")[-1].lower()
            print(image.filename,image.filename.split(".")[-1].lower())
            if image_format not in ALLOWED_IMAGE_FORMATS:
                response_message = f"Unsupported image{str(idx+1)} format. Supported formats: {', '.join(ALLOWED_IMAGE_FORMATS)}"
                response_status = "Failure"
                return generate_response(response_message , response_status ,'' , '' , '' , [])

            contents = await image.read()
            nparr = np.frombuffer(contents, np.uint8)
            image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)

            images.append(image)

            if image is None:
                response_message = f"Image{str(idx+1)} is empty"
                response_status = "Failure"
                return generate_response(response_message , response_status ,'' , '' , '' , [])
###################################################################################################################################################################################
############################################################################### Image Processing ##################################################################################
###################################################################################################################################################################################





        unique_id = str(uuid.uuid4())
        output_path = f"./outputs/SWAP/{unique_id}"




        # loading model
        if not get_model_state_swap():
            unload_model_all()
            model_status = load_model_swap()
            print(f"model status =========={model_status}")

        faces = []
        faces.append(app_model.get(images[0]))
        faces.append(app_model.get(images[1]))

        for idx ,face in enumerate(faces):
            if len(face)==0:
                response_message = f"Error no face detected in image{idx}"
                response_status = "Failure"
                return generate_response(response_message,response_status,'' , '' , '' , [])
            elif len(face) >1:
                response_message = f"Error more than 1 face detected in image{idx}"
                response_status = "Failure"
                return generate_response(response_message,response_status,'' , '' , '' , [])
        
        face1 = faces[0][0]
        face2 = faces[1][0]

        image = swap_model.get(images[0],face1,face2,paste_back=True)


        os.makedirs(output_path)
        output_image_path = output_path+'/output.png'
        cv2.imwrite(output_image_path,image)


        output_entry_id(unique_id) #test remove
        response_message = "Face Swapped Sucessfully"
        response_status = "Successful"
        return generate_response(response_message , response_status ,unique_id , '' , '', [f"{web_url}:8001/SWAP/{unique_id}/output.png"])


    except Exception as e:
        return generate_response(f"Unknown Internal Error : {e}" , "Failure" ,'' , '' , '' , [])


@app.post("/Restore_Images")
async def restore(image: UploadFile = File(...)):

#################
#################
    #get time
    global time_last_call
    time_last_call = datetime.now()
##################
    try:

###################################################################################################################################################################################
############################################################################### Test Input Image ##################################################################################
###################################################################################################################################################################################
        image_format = image.filename.split(".")[-1].lower()
        if image_format not in ALLOWED_IMAGE_FORMATS:
            response_message = f"Unsupported image format. Supported formats: {', '.join(ALLOWED_IMAGE_FORMATS)}"
            response_status = "Failure"
            return generate_response(response_message , response_status ,'' , '' , '' , [])

        contents = await image.read()
        nparr = np.frombuffer(contents, np.uint8)
        image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)

        if image is None:
            response_message = "Image is empty"
            response_status = "Failure"
            return generate_response(response_message , response_status ,'' , '' , '' , [])
###################################################################################################################################################################################
############################################################################### Image Processing ##################################################################################
###################################################################################################################################################################################





        unique_id = str(uuid.uuid4())
        output_path = f"./outputs/Restore/{unique_id}"
        os.makedirs(output_path)

        cv2.imwrite(output_path+'/input.png',image)

        


        # loading model
        if not get_model_state_rest():
            unload_model_all()
            model_status = load_model_rest()
            print(f"model status =========={model_status}")

        image = PIL.Image.open(output_path+'/input.png')
        os.remove(output_path+'/input.png')


        transformed, mask = model_scratches_remove_detector.process(image)
        img_transform = transforms.Compose(
            [transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]
        )
        mask_transform = transforms.ToTensor()
        if model_scratches_remove_options.mask_dilation != 0:
            kernel = np.ones((3, 3), np.uint8)
            mask = np.array(mask)
            mask = cv2.dilate(mask, kernel, iterations=model_scratches_remove_options.mask_dilation)
            mask = PIL.Image.fromarray(mask.astype('uint8'))
        transformed = irregular_hole_synthesize(transformed, mask)
        mask = mask_transform(mask)
        mask = mask[:1, :, :]
        mask = mask.unsqueeze(0)
        transformed = img_transform(transformed)
        transformed = transformed.unsqueeze(0)
        generated = model_scratches_remove.inference(transformed, mask)
        tensor_restored = (generated.data.cpu() + 1.0) / 2.0
        image_to_show = tensor_restored.squeeze().cpu().numpy().transpose((1, 2, 0))

        image_to_show = (image_to_show * 255).astype(np.uint8)[:, :, ::-1]


        from scripts.colorizer import colorize_image
        image_to_show = colorize_image(image_to_show)

        output_image_path = output_path+'/output.png'
        cv2.imwrite(output_image_path,image_to_show)


        output_entry_id(unique_id) #test remove
        response_message = "Restred Image Sucessfully"
        response_status = "Successful"
        return generate_response(response_message , response_status ,unique_id , '' , '', [f"{web_url}:8001/Restore/{unique_id}/output.png"])





    except Exception as e:
        shutil.rmtree(output_path)
        return generate_response(f"Unknown Internal Error : {e}" , "Failure" ,'' , '' , '' , [])


@app.post("/Sad_Talker")
async def sadtalker(image: UploadFile = File(...), audio: UploadFile = File(...)):

#################
#################
    #get time
    global time_last_call
    time_last_call = datetime.now()
##################
    try:

###################################################################################################################################################################################
############################################################################### Test Input Image ##################################################################################
###################################################################################################################################################################################
        image_format = image.filename.split(".")[-1].lower()
        if image_format not in ALLOWED_IMAGE_FORMATS:
            response_message = f"Unsupported image format. Supported formats: {', '.join(ALLOWED_IMAGE_FORMATS)}"
            response_status = "Failure"
            return generate_response(response_message , response_status ,'' , '' , '' , [])

        contents = await image.read()
        nparr = np.frombuffer(contents, np.uint8)
        image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)

        if image is None:
            response_message = "Image is empty"
            response_status = "Failure"
            return generate_response(response_message , response_status ,'' , '' , '' , [])
###################################################################################################################################################################################
############################################################################### Test Input Audio  ##################################################################################
###################################################################################################################################################################################
        audio_format = audio.filename.split(".")[-1].lower()
        if audio_format not in ALLOWED_AUDIO_FORMATS:
            response_message = f"Unsupported audio format. Supported formats: {', '.join(ALLOWED_AUDIO_FORMATS)}"
            response_status = "Failure"
            return generate_response(response_message, response_status, '', '', '', [])

        audio_contents = await audio.read()
        if len(audio_contents) == 0:
            response_message = "Audio file is empty"
            response_status = "Failure"
            return generate_response(response_message, response_status, '', '', '', [])
###################################################################################################################################################################################
############################################################################### Image Processing ##################################################################################
###################################################################################################################################################################################





        unique_id = str(uuid.uuid4())


        output_path = f"./outputs/Sad_Talker/{unique_id}"
        
        os.makedirs(output_path)

        image_input_path = output_path+'/input.png'
        cv2.imwrite(image_input_path,image)

        audio_segment = AudioSegment.from_file(io.BytesIO(audio_contents), format=audio_format)
        audio_input_path = os.path.join(output_path, f"audio.{audio_format}")
        audio_segment.export(audio_input_path, format=audio_format)

        app_face = FaceAnalysis(name="buffalo_l")
        app_face.prepare(ctx_id=0,det_size=(640,640))

        faces = app_face.get(image)
        if len(faces)==0:
            response_message = "Error no face detected"
            response_status = "Failure"
            return generate_response(response_message , response_status ,'' , '' , '' , [])
        
        elif len(faces)!=1:
            response_message = "Error more than 1 face detected"
            response_status = "Failure"

            return generate_response(response_message , response_status ,'' , '' , '' , [])

        info_age = faces[0]['age']
        info_gender = faces[0]['gender']

        
        # loading model
        if not get_model_state_talk():
            unload_model_all()
            model_status = load_model_talk()
            print(f"model status =========={model_status}")




        response = sad_talker_model.genrate_video(image_input_path,audio_input_path,output_path,True)

        # input_file = f"{output_path}/output.mp4 "
        # subprocess.run(['ffmpeg', '-i', input_file, '-c:v', 'libx264', '-preset', 'slow', '-crf', '23', '-c:a', 'aac', '-b:a', '192k', '-y', input_file], check=True)
        
        os.remove(image_input_path)
        os.remove(audio_input_path)

        if response:

            input_file = f"outputs/Sad_Talker/{unique_id}/output.mp4"
            output_file = f"outputs/Sad_Talker/{unique_id}/final.mp4"

            subprocess.run(['ffmpeg', '-i', input_file, '-c:v', 'libx264', '-preset', 'slow', '-crf', '23', '-c:a', 'aac', '-b:a', '192k', output_file], check=True)

            os.remove(input_file)

            output_entry_id(unique_id) #test remove
            response_message = "Video Generated Sucessfully"
            response_status = "Successful"
            return generate_response(response_message , response_status ,unique_id , info_age , ["Female","Male"][info_gender] , [f"{web_url}:8001/Sad_Talker/{unique_id}/final.mp4"])

        else:
            response_message = "Error Genrating Video, try another image"
            response_status = "Failure"
            #shutil.rmtree(output_path)
            return generate_response(response_message , response_status ,'','','',[])



    except Exception as e:
        shutil.rmtree(output_path)
        return generate_response(f"Unknown Internal Error : {e}" , "Failure" ,'' , '' , '' , [])


@app.post("/SKY_remove")
async def sky(image: UploadFile = File(...)):

#################
#################
    #get time
    global time_last_call
    time_last_call = datetime.now()
##################
    try:

###################################################################################################################################################################################
############################################################################### Test Input Image ##################################################################################
###################################################################################################################################################################################
        image_format = image.filename.split(".")[-1].lower()
        if image_format not in ALLOWED_IMAGE_FORMATS:
            response_message = f"Unsupported image format. Supported formats: {', '.join(ALLOWED_IMAGE_FORMATS)}"
            response_status = "Failure"
            return generate_response(response_message , response_status ,'' , '' , '' , [])

        contents = await image.read()
        nparr = np.frombuffer(contents, np.uint8)
        image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)

        if image is None:
            response_message = "Image is empty"
            response_status = "Failure"
            return generate_response(response_message , response_status ,'' , '' , '' , [])
###################################################################################################################################################################################
############################################################################### Image Processing ##################################################################################
###################################################################################################################################################################################





        unique_id = str(uuid.uuid4())
        output_path = f"./outputs/SKY/{unique_id}"

        


        # loading model
        if not get_model_state_sky():
            unload_model_all()
            model_status = load_model_sky()
            print(f"model status =========={model_status}")

        
        output_image = sky_model.SKY_remove(image)

        os.makedirs(output_path)
        output_image_path = output_path+'/output.png'
        cv2.imwrite(output_image_path,output_image)


        output_entry_id(unique_id) #test remove
        response_message = "SKY Removed Sucessfully"
        response_status = "Successful"
        return generate_response(response_message , response_status ,unique_id , '' , '', [f"{web_url}:8001/SKY/{unique_id}/output.png"])






    except Exception as e:
        shutil.rmtree(output_path)
        return generate_response(f"Unknown Internal Error : {e}" , "Failure" ,'' , '' , '' , [])


@app.post("/SKY_replace_image")
async def sky_replace_image(image1: UploadFile = File(...), image2: UploadFile = File(...)):

#################
#################
    #get time
    global time_last_call
    time_last_call = datetime.now()
##################
    try:

###################################################################################################################################################################################
############################################################################### Test Input Image ##################################################################################
###################################################################################################################################################################################
        images = []
        for idx ,image in enumerate([image1,image2]):
            image_format = image.filename.split(".")[-1].lower()
            print(image.filename,image.filename.split(".")[-1].lower())
            if image_format not in ALLOWED_IMAGE_FORMATS:
                response_message = f"Unsupported image{str(idx+1)} format. Supported formats: {', '.join(ALLOWED_IMAGE_FORMATS)}"
                response_status = "Failure"
                return generate_response(response_message , response_status ,'' , '' , '' , [])

            contents = await image.read()
            nparr = np.frombuffer(contents, np.uint8)
            image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)

            images.append(image)

            if image is None:
                response_message = f"Image{str(idx+1)} is empty"
                response_status = "Failure"
                return generate_response(response_message , response_status ,'' , '' , '' , [])
###################################################################################################################################################################################
############################################################################### Image Processing ##################################################################################
###################################################################################################################################################################################





        unique_id = str(uuid.uuid4())
        output_path = f"./outputs/SKY/{unique_id}"




        # loading model
        if not get_model_state_sky():
            unload_model_all()
            model_status = load_model_sky()
            print(f"model status =========={model_status}")

        


        output_image = sky_model.SKY_replace_image(images[0],images[1])

        os.makedirs(output_path)
        output_image_path = output_path+'/output.png'
        cv2.imwrite(output_image_path,output_image)


        output_entry_id(unique_id) #test remove
        response_message = "SKY Removed Sucessfully"
        response_status = "Successful"
        return generate_response(response_message , response_status ,unique_id , '' , '', [f"{web_url}:8001/SKY/{unique_id}/output.png"])



    except Exception as e:
        return generate_response(f"Unknown Internal Error : {e}" , "Failure" ,'' , '' , '' , [])

@app.post("/ImageUpscale")
async def upscale(image: UploadFile = File(...)):

#################
#################
    #get time
    global time_last_call
    time_last_call = datetime.now()
##################
    try:

###################################################################################################################################################################################
############################################################################### Test Input Image ##################################################################################
###################################################################################################################################################################################
        image_format = image.filename.split(".")[-1].lower()
        if image_format not in ALLOWED_IMAGE_FORMATS:
            response_message = f"Unsupported image format. Supported formats: {', '.join(ALLOWED_IMAGE_FORMATS)}"
            response_status = "Failure"
            return generate_response(response_message , response_status ,'' , '' , '' , [])

        contents = await image.read()
        nparr = np.frombuffer(contents, np.uint8)
        image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)

        if image is None:
            response_message = "Image is empty"
            response_status = "Failure"
            return generate_response(response_message , response_status ,'' , '' , '' , [])

        height = image.shape[0]
        width = image.shape[1]
        
        if (height*width)>(200*200) :
            aspect_ratio = width / height
            new_height = int(((200*200) / aspect_ratio) ** 0.5)
            new_width = int(aspect_ratio * new_height)
            image = cv2.resize(image, (new_width, new_height), interpolation=cv2.INTER_AREA)
    
        
###################################################################################################################################################################################
############################################################################### Image Processing ##################################################################################
###################################################################################################################################################################################


        unique_id = str(uuid.uuid4())
        output_path = f"./outputs/Upscale/{unique_id}"
        


        # loading model
        if not get_model_state_up():
            unload_model_all()
            model_status = load_model_up()
            print(f"model status =========={model_status}")

        
        #image = cv2.imread(output_path+'/input.png')

        image = image * 1.0 / 255
        image = torch.from_numpy(np.transpose(image[:, :, [2, 1, 0]], (2, 0, 1))).float()
        img_LR = image.unsqueeze(0)
        img_LR = img_LR.to(device).half()

        output = model_upscale(img_LR).data.squeeze().float().cpu().clamp_(0, 1).numpy()

        output = np.transpose(output[[2, 1, 0], :, :], (1, 2, 0))
        output = (output * 255.0).round()
        image = output.astype(np.uint8)
        
        output_image_path = output_path+'/output.png'
        os.makedirs(output_path)
        cv2.imwrite(output_image_path,image)


        output_entry_id(unique_id) #test remove
        response_message = "Upscaled Image Sucessfully"
        response_status = "Successful"
        return generate_response(response_message , response_status ,unique_id , '' , '', [f"{web_url}:8001/Upscale/{unique_id}/output.png"])


    except Exception as e:
        shutil.rmtree(output_path)
        return generate_response(f"Unknown Internal Error : {e}" , "Failure" ,'' , '' , '' , [])

@app.post("/ImageDenoise")
async def deniose(image: UploadFile = File(...)):

#################
#################
    #get time
    global time_last_call
    time_last_call = datetime.now()
##################
    try:

###################################################################################################################################################################################
############################################################################### Test Input Image ##################################################################################
###################################################################################################################################################################################
        image_format = image.filename.split(".")[-1].lower()
        if image_format not in ALLOWED_IMAGE_FORMATS:
            response_message = f"Unsupported image format. Supported formats: {', '.join(ALLOWED_IMAGE_FORMATS)}"
            response_status = "Failure"
            return generate_response(response_message , response_status ,'' , '' , '' , [])

        contents = await image.read()
        nparr = np.frombuffer(contents, np.uint8)
        image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)

        if image is None:
            response_message = "Image is empty"
            response_status = "Failure"
            return generate_response(response_message , response_status ,'' , '' , '' , [])

        height = image.shape[0]
        width = image.shape[1]

        if (height*width)>(1500*1500) :
            aspect_ratio = width / height
            new_height = int(((1500*1500) / aspect_ratio) ** 0.5)
            new_width = int(aspect_ratio * new_height)
            image = cv2.resize(image, (new_width, new_height), interpolation=cv2.INTER_AREA)
        
###################################################################################################################################################################################
############################################################################### Image Processing ##################################################################################
###################################################################################################################################################################################





        unique_id = str(uuid.uuid4())
        output_path = f"./outputs/Denoise/{unique_id}"
        


        # loading model
        if not get_model_state_esrgan():
            unload_model_all()
            model_status = load_model_esrgan()
            print(f"model status =========={model_status}")

        
        #image = cv2.imread(output_path+'/input.png')

        image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        image_pil = Image.fromarray(image_rgb)
        global model_esrgan
        result_pil = model_esrgan.predict(image_pil)

        result_rgb = result_pil.convert('RGB')
        result_np = np.array(result_rgb)
        image = cv2.cvtColor(result_np, cv2.COLOR_RGB2BGR)

        output_image_path = output_path+'/output.png'
        os.makedirs(output_path)
        cv2.imwrite(output_image_path,image)


        output_entry_id(unique_id) #test remove
        response_message = "Upscaled Image Sucessfully"
        response_status = "Successful"
        return generate_response(response_message , response_status ,unique_id , '' , '', [f"{web_url}:8001/Denoise/{unique_id}/output.png"])


    except Exception as e:
        shutil.rmtree(output_path)
        return generate_response(f"Unknown Internal Error : {e}" , "Failure" ,'' , '' , '' , [])


  

video_url = [{'id': '6a025d6c-5cf9-4de0-b323-6a628563327f',
            'url': 'https://api.quantumgrove.tech:8001/Videos/d13.mp4',
            'title': '6a025d6c-5cf9-4de0-b323-6a628563327f',
            'info': '6a025d6c-5cf9-4de0-b323-6a628563327f',
            'path': './subprocess/LivePortrait/assets/examples/driving/d13.mp4',
            "cat" : "Trending"},
            {'id': '70153a58-0b49-44f2-8c81-816aff6ee2fe',
            'url': 'https://api.quantumgrove.tech:8001/Videos/d10.mp4',
            'title': '70153a58-0b49-44f2-8c81-816aff6ee2fe',
            'info': '70153a58-0b49-44f2-8c81-816aff6ee2fe',
            'path': './subprocess/LivePortrait/assets/examples/driving/d10.mp4',
            "cat" : "Kids"},
            {'id': '5df41b72-f7e6-4aba-aa19-fc970d310dc7',
            'url': 'https://api.quantumgrove.tech:8001/Videos/d19.mp4',
            'title': '5df41b72-f7e6-4aba-aa19-fc970d310dc7',
            'info': '5df41b72-f7e6-4aba-aa19-fc970d310dc7',
            'path': './subprocess/LivePortrait/assets/examples/driving/d19.mp4',
            "cat" : "Kids"},
            {'id': '3ca89a2a-145d-4d7c-88c3-f7e0bc3c2dea',
            'url': 'https://api.quantumgrove.tech:8001/Videos/d9.mp4',
            'title': '3ca89a2a-145d-4d7c-88c3-f7e0bc3c2dea',
            'info': '3ca89a2a-145d-4d7c-88c3-f7e0bc3c2dea',
            'path': './subprocess/LivePortrait/assets/examples/driving/d9.mp4',
            "cat" : "Trending"},
            {'id': '93116106-7c25-41fa-8343-35c866ce9954',
            'url': 'https://api.quantumgrove.tech:8001/Videos/d3.mp4',
            'title': '93116106-7c25-41fa-8343-35c866ce9954',
            'info': '93116106-7c25-41fa-8343-35c866ce9954',
            'path': './subprocess/LivePortrait/assets/examples/driving/d3.mp4',
            "cat" : "Trending"},
            {'id': '15576fab-3125-4bf2-a26b-2a3f69120516',
            'url': 'https://api.quantumgrove.tech:8001/Videos/d14.mp4',
            'title': '15576fab-3125-4bf2-a26b-2a3f69120516',
            'info': '15576fab-3125-4bf2-a26b-2a3f69120516',
            'path': './subprocess/LivePortrait/assets/examples/driving/d14.mp4',
            "cat" : "Trending"},
            {'id': '8ff558b3-5437-4b0c-a1f0-a02758eb7c7e',
            'url': 'https://api.quantumgrove.tech:8001/Videos/d11.mp4',
            'title': '8ff558b3-5437-4b0c-a1f0-a02758eb7c7e',
            'info': '8ff558b3-5437-4b0c-a1f0-a02758eb7c7e',
            'path': './subprocess/LivePortrait/assets/examples/driving/d11.mp4',
            "cat" : "Top"},
            {'id': 'd528d624-546a-4a98-8a8d-810c604178de',
            'url': 'https://api.quantumgrove.tech:8001/Videos/d6.mp4',
            'title': 'd528d624-546a-4a98-8a8d-810c604178de',
            'info': 'd528d624-546a-4a98-8a8d-810c604178de',
            'path': './subprocess/LivePortrait/assets/examples/driving/d6.mp4',
            "cat" : "Top"},
            {'id': 'fd8785af-deed-40cd-89b2-978c3658d1eb',
            'url': 'https://api.quantumgrove.tech:8001/Videos/d0.mp4',
            'title': 'fd8785af-deed-40cd-89b2-978c3658d1eb',
            'info': 'fd8785af-deed-40cd-89b2-978c3658d1eb',
            'path': './subprocess/LivePortrait/assets/examples/driving/d0.mp4',
            "cat" : "Trending"},
            {'id': '946bf5ad-6612-4aab-8551-22e77762df1c',
            'url': 'https://api.quantumgrove.tech:8001/Videos/d12.mp4',
            'title': '946bf5ad-6612-4aab-8551-22e77762df1c',
            'info': '946bf5ad-6612-4aab-8551-22e77762df1c',
            'path': './subprocess/LivePortrait/assets/examples/driving/d12.mp4',
            "cat" : "Trending"},
            {'id': '13286ebb-8bda-4560-8521-9455a5f7188a',
            'url': 'https://api.quantumgrove.tech:8001/Videos/d18.mp4',
            'title': '13286ebb-8bda-4560-8521-9455a5f7188a',
            'info': '13286ebb-8bda-4560-8521-9455a5f7188a',
            'path': './subprocess/LivePortrait/assets/examples/driving/d18.mp4',
            "cat" : "Top"}]

@app.post("/LivePortrait/{video_id}")
async def lp(video_id: str,image: UploadFile = File(...)):

#################
#################
    #get time
    global time_last_call
    time_last_call = datetime.now()
##################
    try:

###################################################################################################################################################################################
############################################################################### Test Input Image ##################################################################################
###################################################################################################################################################################################
        image_format = image.filename.split(".")[-1].lower()
        print(image_format)
        print(video_id)
        # if image_format not in ALLOWED_IMAGE_FORMATS:
        #     response_message = f"Unsupported image format. Supported formats: {', '.join(ALLOWED_IMAGE_FORMATS)}"
        #     response_status = "Failure"
        #     return generate_response(response_message , response_status ,'' , '' , '' , [])

        contents = await image.read()
        nparr = np.frombuffer(contents, np.uint8)
        image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)

        if image is None:
            response_message = "Image is empty"
            response_status = "Failure"
            return generate_response(response_message , response_status ,'' , '' , '' , [])

        height = image.shape[0]
        width = image.shape[1]

        if (height*width)>(1500*1500) :
            aspect_ratio = width / height
            new_height = int(((500*500) / aspect_ratio) ** 0.5)
            new_width = int(aspect_ratio * new_height)
            image = cv2.resize(image, (new_width, new_height), interpolation=cv2.INTER_AREA)
        

        app_face = FaceAnalysis(name="buffalo_l")
        app_face.prepare(ctx_id=0,det_size=(640,640))

        faces = app_face.get(image)
        if len(faces)==0:
            response_message = "Error no face detected"
            response_status = "Failure"
            return generate_response(response_message , response_status ,'' , '' , '' , [])
        elif len(faces)!=1:
            response_message = "Error more than 1 face detected"
            response_status = "Failure"
            return generate_response(response_message , response_status ,'' , '' , '' , [])
        
###################################################################################################################################################################################
############################################################################### Image Processing ##################################################################################
###################################################################################################################################################################################





        unique_id = str(uuid.uuid4())
        output_path = f"./outputs/LP/{unique_id}"
        os.makedirs(output_path)

        image_input_path = output_path+'/input.png'
        cv2.imwrite(image_input_path,image)
        

        # import glob
        # directory_path = "./subprocess/LivePortrait/assets/examples/driving"
        # mp4_files = glob.glob(os.path.join(directory_path, "*.mp4"))

        
        

        # loading model
        if not get_model_port_lp():
            unload_model_all()
            model_status = load_port_lp()
            print(f"model status =========={model_status}")

        con = 0
        for i in video_url:
            #id = "7aaa2b05-5207-4c84-b2f9-926b0219cae1"
            video_path = f"./outputs/videos/{video_id}.mp4"
            if os.path.exists(video_path):
                increment_video_hits(video_id)
            # if video_id == i['id']:
            #     video_path = i['path']
                con = 1
                break
        if con == 0:
            return generate_response(f"Incorrect Video ID" , "Failure" ,'' , '' , '' , [])
            
        global model_port_lp
        client = Client(f"http://127.0.0.1:{model_port_lp['port']}/")

        result = client.predict(
            param_0=handle_file(os.path.abspath(output_path+'/input.png')),
            param_1={"video": handle_file(video_path)},
            param_2=True,
            param_3=True,
            param_4=True,
            param_5=False,
            api_name="/gpu_wrapped_execute_video"
        )
        
        shutil.copy(result[0]['video'], output_path+'/output.mp4')
        
        for i in result:
            root = os.path.dirname(i['video'])

            if os.path.isdir(root):
                shutil.rmtree(root)

        os.remove(output_path+'/input.png')
        output_entry_id(unique_id) #test remove
        response_message = "LP genrated Sucessfully"
        response_status = "Successful"
        return generate_response(response_message , response_status ,unique_id , '' , '', [f"{web_url}:8001/LP/{unique_id}/output.mp4"])


    except Exception as e:
        shutil.rmtree(output_path)
        return generate_response(f"Unknown Internal Error : {e}" , "Failure" ,'' , '' , '' , [])


@app.post("/ImageEnhancer")
async def imageenhance(image: UploadFile = File(...)):

#################
#################
    #get time
    global time_last_call
    time_last_call = datetime.now()
##################
    try:

###################################################################################################################################################################################
############################################################################### Test Input Image ##################################################################################
###################################################################################################################################################################################
        image_format = image.filename.split(".")[-1].lower()
        if image_format not in ALLOWED_IMAGE_FORMATS:
            response_message = f"Unsupported image format. Supported formats: {', '.join(ALLOWED_IMAGE_FORMATS)}"
            response_status = "Failure"
            return generate_response(response_message , response_status ,'' , '' , '' , [])

        contents = await image.read()
        nparr = np.frombuffer(contents, np.uint8)
        image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)

        if image is None:
            response_message = "Image is empty"
            response_status = "Failure"
            return generate_response(response_message , response_status ,'' , '' , '' , [])
###################################################################################################################################################################################
############################################################################### Image Processing ##################################################################################
###################################################################################################################################################################################
        # loading model
        
        upscale_image = True
       

        unique_id = str(uuid.uuid4())
        output_path = f"./outputs/Enhance/{unique_id}"

        height = image.shape[0]
        width = image.shape[1]

        if (height*width)>(1500*1500) :
                aspect_ratio = width / height
                new_height = int(((1500*1500) / aspect_ratio) ** 0.5)
                new_width = int(aspect_ratio * new_height)
                image = cv2.resize(image, (new_width, new_height), interpolation=cv2.INTER_AREA)

        # loading model
        if not get_model_state_cf():
            unload_model_all()
            model_status = load_model_cf()
            print(f"model status =========={model_status}")

        if upscale_image:

            has_aligned = False
            only_center_face = False
            draw_box = False
            detection_model = "retinaface_resnet50"
            background_enhance = True
            face_upsample = True
            upscale = 2
            codeformer_fidelity = 0.5
            img = image#cv2.imread(str(img_path), cv2.IMREAD_COLOR)
            upscale = int(upscale) # convert type to int
            if upscale > 4: # avoid memory exceeded due to too large upscale
                upscale = 4
            if upscale > 2 and max(img.shape[:2])>1000: # avoid memory exceeded due to too large img resolution
                upscale = 2
            if max(img.shape[:2]) > 1500: # avoid memory exceeded due to too large img resolution
                upscale = 1
                background_enhance = False
                face_upsample = False
            face_helper = FaceRestoreHelper(
                upscale,
                face_size=512,
                crop_ratio=(1, 1),
                det_model=detection_model,
                save_ext="png",
                use_parse=True,
                device=device,
            )
            bg_upsampler = model_upsampler if background_enhance else None
            face_upsampler = model_upsampler if face_upsample else None
            if has_aligned:
                # the input faces are already cropped and aligned
                img = cv2.resize(img, (512, 512), interpolation=cv2.INTER_LINEAR)
                face_helper.is_gray = is_gray(img, threshold=5)
                if face_helper.is_gray:
                    print('\tgrayscale input: True')
                    face_helper.cropped_faces = [img]
            else:
                face_helper.read_image(img)
                # get face landmarks for each face
                num_det_faces = face_helper.get_face_landmarks_5(
                only_center_face=only_center_face, resize=640, eye_dist_threshold=5
                )
                print(f'\tdetect {num_det_faces} faces')
                # align and warp each face
                face_helper.align_warp_face()

            # face restoration for each cropped face
            for idx, cropped_face in enumerate(face_helper.cropped_faces):
                # prepare data
                cropped_face_t = img2tensor(
                    cropped_face / 255.0, bgr2rgb=True, float32=True
                )
                normalize(cropped_face_t, (0.5, 0.5, 0.5), (0.5, 0.5, 0.5), inplace=True)
                cropped_face_t = cropped_face_t.unsqueeze(0).to(device)
                try:
                    with torch.no_grad():
                        output = model_code_former(cropped_face_t, w=codeformer_fidelity, adain=True)[0]
                        restored_face = tensor2img(output, rgb2bgr=True, min_max=(-1, 1))
                    del output
                    torch.cuda.empty_cache()
                except RuntimeError as error:
                    print(f"Failed inference for CodeFormer: {error}")
                    restored_face = tensor2img(
                        cropped_face_t, rgb2bgr=True, min_max=(-1, 1)
                    )
                restored_face = restored_face.astype("uint8")
                face_helper.add_restored_face(restored_face)

            # paste_back
            if not has_aligned:
                # upsample the background
                if bg_upsampler is not None:
                    # Now only support RealESRGAN for upsampling background
                    bg_img = bg_upsampler.enhance(img, outscale=upscale)[0]
                else:
                    bg_img = None
                face_helper.get_inverse_affine(None)
                # paste each restored face to the input image
            if face_upsample and face_upsampler is not None:
                restored_img = face_helper.paste_faces_to_input_image(
                        upsample_img=bg_img,
                        draw_box=draw_box,
                        face_upsampler=face_upsampler,
                    )
            else:
                restored_img = face_helper.paste_faces_to_input_image(
                        upsample_img=bg_img, draw_box=draw_box
                    )
                




            output_image_path = output_path+'/output.png'
            os.makedirs(output_path)
            cv2.imwrite(output_image_path,restored_img)


            #cv2.imwrite(output_path+'/output.png',restored_img)


        


        output_entry_id(unique_id) #test remove
        response_message = "CodeFormer Image Sucessfully"
        response_status = "Successful"
        return generate_response(response_message , response_status ,unique_id , '' , '', [f"{web_url}:8001/Enhance/{unique_id}/output.png"])


    except Exception as e:
        shutil.rmtree(output_path)
        return generate_response(f"Unknown Internal Error : {e}" , "Failure" ,'' , '' , '' , [])

# if __name__ == '__main__':

#     import uvicorn
#     try:
#         http_file_server_process = start_http_file_server()
#         uvicorn.run("main:app", host='0.0.0.0', port=8080, workers=1 , limit_max_requests=20)
#     except KeyboardInterrupt:
#         print("Received KeyboardInterrupt, shutting down gracefully...")
#         http_file_server_process.kill()
#         shutdown_event()

if __name__ == '__main__':


    from cf_api import start_cf_api
    import subprocess
    api_process = subprocess.Popen(["python", "cf_api.py"])
    get_server = subprocess.Popen(["/home/qtech/miniconda3/envs/api/bin/python","./get_req.py"])
    report_server = subprocess.Popen(["/home/qtech/miniconda3/envs/api/bin/python","./report/report.py"])

    print("on")
    import uvicorn
    import os
    try:
        if not os.path.exists('api.db'):
            create_database()

        if not os.path.exists('./subprocess/port.db'):
            os.remove('./subprocess/port.db')
        create_database_port()

        http_file_server_process = start_http_file_server()
        uvicorn.run("main:app", host='0.0.0.0', port=8080, workers=4 , limit_max_requests=200 )#, timeout_keep_alive=5)
        print("Received KeyboardInterrupt, shutting down gracefully...")
        http_file_server_process.kill()
        shutdown_event()
    except KeyboardInterrupt:
        print("off")
        print("Received KeyboardInterrupt, shutting down gracefully...")
        http_file_server_process.kill()
        shutdown_event()

        api_process.kill()
        get_server.kill()
        report_server.kill()