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
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
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
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
# import tkinter as tk
# from tkinter import scrolledtext, ttk
# import threading
# import google.generativeai as genai
# from PIL import Image, ImageTk
# import os
# import time

# class GeminiChatbot:
#     def __init__(self, root):
#         self.root = root
#         self.root.title("Gemini AI Chatbot")
#         self.root.geometry("800x600")
#         self.root.configure(bg="#f0f0f0")
        
#         # Configure API key
#         genai.configure(api_key="AIzaSyCzHa6m60yJ6-gd81SABHUrNpK6m3Nl4uY")
        
#         # Load the model
#         self.model = genai.GenerativeModel('gemini-2.0-flash')
        
#         # Create chat instance
#         self.chat = self.model.start_chat(history=[])
        
#         self.create_widgets()
        
#     def create_widgets(self):
#         # Main frame
#         main_frame = tk.Frame(self.root, bg="#f0f0f0")
#         main_frame.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
        
#         # Chat history area
#         self.chat_history = scrolledtext.ScrolledText(
#             main_frame,
#             wrap=tk.WORD,
#             bg="white",
#             font=("Arial", 10),
#             state="disabled",
#             height=20
#         )
#         self.chat_history.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)
        
#         # Input area frame
#         input_frame = tk.Frame(main_frame, bg="#f0f0f0")
#         input_frame.pack(fill=tk.X, pady=5)
        
#         # Message input field
#         self.message_input = scrolledtext.ScrolledText(
#             input_frame,
#             wrap=tk.WORD,
#             height=3,
#             font=("Arial", 10)
#         )
#         self.message_input.pack(side=tk.LEFT, fill=tk.X, expand=True, padx=(0, 5))
#         self.message_input.bind("<Return>", self.send_on_enter)
        
#         # Send button
#         self.send_button = ttk.Button(
#             input_frame,
#             text="Send",
#             command=self.send_message,
#             style="Send.TButton"
#         )
#         self.send_button.pack(side=tk.RIGHT, padx=5)
        
#         # Status bar
#         self.status_var = tk.StringVar()
#         self.status_var.set("Ready")
#         self.status_bar = tk.Label(
#             self.root,
#             textvariable=self.status_var,
#             bd=1,
#             relief=tk.SUNKEN,
#             anchor=tk.W
#         )
#         self.status_bar.pack(side=tk.BOTTOM, fill=tk.X)
        
#         # Style configuration
#         style = ttk.Style()
#         style.configure("Send.TButton", font=("Arial", 10, "bold"))
        
#         # Welcome message
#         self.update_chat_history("Gemini AI: Hello! How can I assist you today?", "bot")
        
#     def update_chat_history(self, message, sender):
#         self.chat_history.config(state="normal")
        
#         # Format based on sender
#         if sender == "user":
#             self.chat_history.insert(tk.END, "You: " + message + "\n\n", "user_msg")
#             self.chat_history.tag_config("user_msg", foreground="#0078D7", font=("Arial", 10, "bold"))
#         elif sender == "bot":
#             self.chat_history.insert(tk.END, message + "\n\n", "bot_msg")
#             self.chat_history.tag_config("bot_msg", foreground="#4a4a4a", font=("Arial", 10))
        
#         self.chat_history.config(state="disabled")
#         self.chat_history.see(tk.END)
    
#     def send_on_enter(self, event):
#         if not event.state & 0x0001:  # Check if shift key is not pressed
#             self.send_message()
#             return "break"  # Prevents the default behavior (newline)
    
#     def send_message(self):
#         user_message = self.message_input.get("1.0", tk.END).strip()
#         if not user_message:
#             return
        
#         # Clear input field
#         self.message_input.delete("1.0", tk.END)
        
#         # Update chat history with user message
#         self.update_chat_history(user_message, "user")
        
#         # Disable send button and update status
#         self.send_button.config(state="disabled")
#         self.status_var.set("Thinking...")
#         self.root.update_idletasks()
        
#         # Process in a separate thread to keep UI responsive
#         threading.Thread(target=self.get_ai_response, args=(user_message,), daemon=True).start()
    
#     def get_ai_response(self, user_message):
#         try:
#             # Get response from Gemini
#             response = self.chat.send_message(user_message)
#             response_text = response.text
            
#             # Update the chat history with AI response
#             self.root.after(0, lambda: self.update_chat_history(f"Gemini AI: {response_text}", "bot"))
            
#         except Exception as e:
#             error_message = f"An error occurred: {str(e)}"
#             self.root.after(0, lambda: self.update_chat_history(f"Gemini AI: {error_message}", "bot"))
        
#         # Re-enable the send button and update status
#         self.root.after(0, lambda: self.send_button.config(state="normal"))
#         self.root.after(0, lambda: self.status_var.set("Ready"))

# def main():
#     root = tk.Tk()
#     app = GeminiChatbot(root)
#     root.mainloop()

# if __name__ == "__main__":
#     main()









# import requests
# import json
# import time
# import os

# # Simple StarryAI image generator based on your provided code

# def generate_image(api_key, prompt):
#     """Generate an image using the StarryAI API"""
#     url = "https://api.starryai.com/creations/"
    
#     payload = {
#         "prompt": prompt,
#         "model": "lyra",
#         "aspectRatio": "square",
#         "highResolution": False,
#         "images": 4,
#         "steps": 20,
#         "initialImageMode": "color"
#     }
    
#     headers = {
#         "accept": "application/json",
#         "content-type": "application/json",
#         "X-API-Key": api_key
#     }
    
#     print("Sending request to StarryAI API...")
#     response = requests.post(url, json=payload, headers=headers)
    
#     # Print the raw response for debugging
#     print(f"Response status code: {response.status_code}")
#     print(f"Response content: {response.text}")
    
#     if response.status_code != 200:
#         print(f"Error: API returned status code {response.status_code}")
#         return None
    
#     try:
#         return response.json()
#     except json.JSONDecodeError:
#         print("Error: Could not parse JSON response")
#         return None

# def check_creation_status(api_key, creation_id):
#     """Check the status of an image creation"""
#     url = f"https://api.starryai.com/creations/{creation_id}"
    
#     headers = {
#         "accept": "application/json",
#         "X-API-Key": api_key
#     }
    
#     response = requests.get(url, headers=headers)
    
#     if response.status_code != 200:
#         print(f"Error checking status: API returned status code {response.status_code}")
#         print(f"Response: {response.text}")
#         return None
    
#     try:
#         return response.json()
#     except json.JSONDecodeError:
#         print("Error: Could not parse JSON response")
#         return None

# def wait_for_creation(api_key, creation_id, max_wait_time=300, check_interval=5):
#     """Wait for an image creation to complete"""
#     start_time = time.time()
    
#     while time.time() - start_time < max_wait_time:
#         print(f"Checking status of creation {creation_id}...")
#         status_data = check_creation_status(api_key, creation_id)
        
#         if not status_data:
#             print("Failed to get status data")
#             return None
        
#         print(f"Status data: {json.dumps(status_data, indent=2)}")
        
#         # Extract status from response (adjust based on actual API response structure)
#         status = status_data.get("status")
#         if not status:
#             print("Could not find status in response")
#             return None
        
#         print(f"Current status: {status}")
        
#         if status == "completed":
#             return status_data
#         elif status == "failed":
#             print("Creation failed")
#             return status_data
        
#         print(f"Waiting {check_interval} seconds before checking again...")
#         time.sleep(check_interval)
    
#     print(f"Timed out after {max_wait_time} seconds")
#     return None

# def download_and_save_image(image_url, output_path="generated_image.png"):
#     """Download and save an image from a URL"""
#     try:
#         response = requests.get(image_url)
        
#         if response.status_code != 200:
#             print(f"Error downloading image: API returned status code {response.status_code}")
#             return False
        
#         with open(output_path, "wb") as f:
#             f.write(response.content)
        
#         print(f"Image saved to {output_path}")
#         return True
#     except Exception as e:
#         print(f"Error downloading image: {e}")
#         return False

# def main():
#     # Your API key
#     api_key = "dugMLMJ_GP-tVeyJE1UIq58ZNQ8-mQ"
    
#     # Get prompt from user
#     prompt = input("Enter a prompt for image generation: ")
    
#     # Generate image
#     print(f"Starting image generation with prompt: '{prompt}'")
#     creation_data = generate_image(api_key, prompt)
    
#     if not creation_data:
#         print("Failed to start generation")
#         return
    
#     print(f"API Response: {json.dumps(creation_data, indent=2)}")
    
#     # Extract creation ID (adjust based on actual API response structure)
#     creation_id = creation_data.get("id")
#     if not creation_id:
#         print("No creation ID in response")
#         return
    
#     print(f"Creation started with ID: {creation_id}")
    
#     # Wait for creation to complete
#     print("Waiting for image generation to complete...")
#     completed_data = wait_for_creation(api_key, creation_id)
    
#     if not completed_data:
#         print("Failed to get completed creation data")
#         return
    
#     # Get image URL (adjust based on actual API response structure)
#     # This might be different based on the API's actual response format
#     image_url = None
#     if "imageUrl" in completed_data:
#         image_url = completed_data["imageUrl"]
#     elif "output" in completed_data and "imageUrl" in completed_data["output"]:
#         image_url = completed_data["output"]["imageUrl"]
#     elif "images" in completed_data and len(completed_data["images"]) > 0:
#         image_url = completed_data["images"][0]["url"]
    
#     if not image_url:
#         print("Could not find image URL in response")
#         print(f"Response data: {json.dumps(completed_data, indent=2)}")
#         return
    
#     # Download and save the image
#     output_filename = f"starryai_{int(time.time())}.png"
#     if download_and_save_image(image_url, output_filename):
#         print(f"Successfully generated and saved image to {output_filename}")
        
#         # Try to open the image
#         try:
#             from PIL import Image
#             Image.open(output_filename).show()
#             print("Image opened in default viewer")
#         except ImportError:
#             print("PIL not installed, cannot display image")
#         except Exception as e:
#             print(f"Could not display image: {e}")
#     else:
#         print("Failed to download and save image")

# if __name__ == "__main__":
#     main()





# import tkinter as tk
# from tkinter import ttk, scrolledtext
# import requests
# import json
# import time
# import os
# import threading
# from PIL import Image, ImageTk
# from io import BytesIO
# import webbrowser

# class StarryAIGenerator:
#     def __init__(self, root):
#         self.root = root
#         self.root.title("StarryAI Image Generator")
#         self.root.geometry("1000x800")
#         self.root.configure(bg="#f0f0f0")
        
#         # Your API key - you might want to store this more securely in a real application
#         self.api_key = "dugMLMJ_GP-tVeyJE1UIq58ZNQ8-mQ"
        
#         # Create UI elements
#         self.create_widgets()
        
#         # Store image references to prevent garbage collection
#         self.image_references = []
        
#         # For storing generated image URLs
#         self.generated_image_urls = []
        
#     def create_widgets(self):
#         # Main frame
#         main_frame = ttk.Frame(self.root, padding="10")
#         main_frame.pack(fill=tk.BOTH, expand=True)
        
#         # Title label
#         title_label = ttk.Label(
#             main_frame, 
#             text="StarryAI Image Generator", 
#             font=("Arial", 20, "bold")
#         )
#         title_label.pack(pady=(0, 20))
        
#         # Input frame
#         input_frame = ttk.Frame(main_frame)
#         input_frame.pack(fill=tk.X, pady=10)
        
#         # Prompt label and entry
#         prompt_label = ttk.Label(input_frame, text="Enter your prompt:", font=("Arial", 12))
#         prompt_label.pack(side=tk.LEFT, padx=(0, 10))
        
#         self.prompt_entry = ttk.Entry(input_frame, width=50, font=("Arial", 12))
#         self.prompt_entry.pack(side=tk.LEFT, fill=tk.X, expand=True, padx=(0, 10))
        
#         # Generate button
#         self.generate_button = ttk.Button(
#             input_frame, 
#             text="Generate Images", 
#             command=self.start_generation
#         )
#         self.generate_button.pack(side=tk.RIGHT)
        
#         # Options frame
#         options_frame = ttk.LabelFrame(main_frame, text="Options", padding="10")
#         options_frame.pack(fill=tk.X, pady=10)
        
#         # Model selection
#         model_label = ttk.Label(options_frame, text="Model:")
#         model_label.grid(row=0, column=0, sticky=tk.W, padx=5, pady=5)
        
#         self.model_var = tk.StringVar(value="lyra")
#         model_combo = ttk.Combobox(
#             options_frame, 
#             textvariable=self.model_var, 
#             values=["lyra", "orion", "nebula"],
#             state="readonly",
#             width=15
#         )
#         model_combo.grid(row=0, column=1, sticky=tk.W, padx=5, pady=5)
        
#         # Aspect ratio
#         aspect_label = ttk.Label(options_frame, text="Aspect Ratio:")
#         aspect_label.grid(row=0, column=2, sticky=tk.W, padx=5, pady=5)
        
#         self.aspect_var = tk.StringVar(value="square")
#         aspect_combo = ttk.Combobox(
#             options_frame, 
#             textvariable=self.aspect_var, 
#             values=["square", "portrait", "landscape"],
#             state="readonly",
#             width=15
#         )
#         aspect_combo.grid(row=0, column=3, sticky=tk.W, padx=5, pady=5)
        
#         # High resolution
#         self.high_res_var = tk.BooleanVar(value=False)
#         high_res_check = ttk.Checkbutton(
#             options_frame, 
#             text="High Resolution", 
#             variable=self.high_res_var
#         )
#         high_res_check.grid(row=0, column=4, sticky=tk.W, padx=5, pady=5)
        
#         # Steps
#         steps_label = ttk.Label(options_frame, text="Steps:")
#         steps_label.grid(row=1, column=0, sticky=tk.W, padx=5, pady=5)
        
#         self.steps_var = tk.IntVar(value=20)
#         steps_spin = ttk.Spinbox(
#             options_frame, 
#             from_=5, 
#             to=50, 
#             textvariable=self.steps_var,
#             width=5
#         )
#         steps_spin.grid(row=1, column=1, sticky=tk.W, padx=5, pady=5)
        
#         # Number of images
#         images_label = ttk.Label(options_frame, text="Images:")
#         images_label.grid(row=1, column=2, sticky=tk.W, padx=5, pady=5)
        
#         self.images_var = tk.IntVar(value=4)
#         images_spin = ttk.Spinbox(
#             options_frame, 
#             from_=1, 
#             to=4, 
#             textvariable=self.images_var,
#             width=5
#         )
#         images_spin.grid(row=1, column=3, sticky=tk.W, padx=5, pady=5)
        
#         # Progress frame
#         progress_frame = ttk.LabelFrame(main_frame, text="Progress", padding="10")
#         progress_frame.pack(fill=tk.X, pady=10)
        
#         # Status label
#         self.status_var = tk.StringVar(value="Ready")
#         status_label = ttk.Label(progress_frame, textvariable=self.status_var)
#         status_label.pack(side=tk.LEFT, padx=(0, 10))
        
#         # Progress bar
#         self.progress = ttk.Progressbar(progress_frame, mode="indeterminate", length=300)
#         self.progress.pack(side=tk.LEFT, fill=tk.X, expand=True)
        
#         # Log frame
#         log_frame = ttk.LabelFrame(main_frame, text="Log", padding="10")
#         log_frame.pack(fill=tk.BOTH, expand=True, pady=10)
        
#         # Log text area
#         self.log_text = scrolledtext.ScrolledText(log_frame, height=5, wrap=tk.WORD)
#         self.log_text.pack(fill=tk.BOTH, expand=True)
        
#         # Images frame
#         self.images_frame = ttk.LabelFrame(main_frame, text="Generated Images", padding="10")
#         self.images_frame.pack(fill=tk.BOTH, expand=True, pady=10)
        
#         # Image display area
#         self.images_display = ttk.Frame(self.images_frame)
#         self.images_display.pack(fill=tk.BOTH, expand=True)
    
#     def log(self, message):
#         """Add message to log text area"""
#         self.log_text.configure(state="normal")
#         self.log_text.insert(tk.END, message + "\n")
#         self.log_text.see(tk.END)
#         self.log_text.configure(state="disabled")
#         self.root.update_idletasks()
    
#     def start_generation(self):
#         """Start image generation in a separate thread"""
#         prompt = self.prompt_entry.get().strip()
#         if not prompt:
#             self.log("Please enter a prompt")
#             return
        
#         # Disable the generate button and start progress bar
#         self.generate_button.configure(state="disabled")
#         self.progress.start()
#         self.status_var.set("Generating...")
        
#         # Clear previous images
#         for widget in self.images_display.winfo_children():
#             widget.destroy()
#         self.image_references.clear()
#         self.generated_image_urls.clear()
        
#         # Start generation in a new thread
#         thread = threading.Thread(target=self.generate_images, args=(prompt,))
#         thread.daemon = True
#         thread.start()
    
#     def generate_images(self, prompt):
#         """Generate images in a background thread"""
#         try:
#             self.log(f"Starting image generation with prompt: '{prompt}'")
            
#             # Get parameters from UI
#             model = self.model_var.get()
#             aspect_ratio = self.aspect_var.get()
#             high_res = self.high_res_var.get()
#             steps = self.steps_var.get()
#             num_images = self.images_var.get()
            
#             # API call parameters
#             url = "https://api.starryai.com/creations/"
#             payload = {
#                 "prompt": prompt,
#                 "model": model,
#                 "aspectRatio": aspect_ratio,
#                 "highResolution": high_res,
#                 "images": num_images,
#                 "steps": steps,
#                 "initialImageMode": "color"
#             }
#             headers = {
#                 "accept": "application/json",
#                 "content-type": "application/json",
#                 "X-API-Key": self.api_key
#             }
            
#             # Make API request
#             self.log("Sending request to StarryAI API...")
#             response = requests.post(url, json=payload, headers=headers)
            
#             self.log(f"Response status code: {response.status_code}")
            
#             if response.status_code != 200:
#                 self.log(f"Error: API returned status code {response.status_code}")
#                 self.log(f"Response content: {response.text}")
#                 self.root.after(0, self.end_generation)
#                 return
            
#             # Parse response
#             try:
#                 creation_data = response.json()
#                 self.log(f"Creation started with ID: {creation_data.get('id')}")
                
#                 # Wait for creation to complete
#                 self.wait_for_creation(creation_data.get('id'))
                
#             except json.JSONDecodeError:
#                 self.log("Error: Could not parse JSON response")
#                 self.root.after(0, self.end_generation)
        
#         except Exception as e:
#             self.log(f"Error during generation: {str(e)}")
#             self.root.after(0, self.end_generation)
    
#     def wait_for_creation(self, creation_id, max_wait_time=300, check_interval=5):
#         """Wait for an image creation to complete"""
#         start_time = time.time()
        
#         while time.time() - start_time < max_wait_time:
#             self.log(f"Checking status of creation {creation_id}...")
            
#             # Check status
#             url = f"https://api.starryai.com/creations/{creation_id}"
#             headers = {
#                 "accept": "application/json",
#                 "X-API-Key": self.api_key
#             }
            
#             response = requests.get(url, headers=headers)
            
#             if response.status_code != 200:
#                 self.log(f"Error checking status: API returned status code {response.status_code}")
#                 self.log(f"Response: {response.text}")
#                 self.root.after(0, self.end_generation)
#                 return
            
#             try:
#                 status_data = response.json()
#                 status = status_data.get("status")
#                 self.log(f"Current status: {status}")
                
#                 if status == "completed":
#                     # Process completed images
#                     self.process_completed_images(status_data)
#                     return
#                 elif status == "failed":
#                     self.log("Creation failed")
#                     self.root.after(0, self.end_generation)
#                     return
                
#                 self.log(f"Waiting {check_interval} seconds before checking again...")
#                 time.sleep(check_interval)
                
#             except json.JSONDecodeError:
#                 self.log("Error: Could not parse JSON response")
#                 self.root.after(0, self.end_generation)
#                 return
        
#         self.log(f"Timed out after {max_wait_time} seconds")
#         self.root.after(0, self.end_generation)
    
#     def process_completed_images(self, completed_data):
#         """Process and display completed images"""
#         if "images" not in completed_data or not completed_data["images"]:
#             self.log("No images found in completed data")
#             self.root.after(0, self.end_generation)
#             return
        
#         images = completed_data["images"]
#         self.log(f"Found {len(images)} generated images")
        
#         # Store image URLs
#         self.generated_image_urls = [img["url"] for img in images if img.get("url")]
        
#         # Display images
#         self.root.after(0, lambda: self.display_images(self.generated_image_urls))
    
#     def display_images(self, image_urls):
#         """Display images in the UI"""
#         try:
#             # Create frame for image grid
#             image_grid = ttk.Frame(self.images_display)
#             image_grid.pack(fill=tk.BOTH, expand=True)
            
#             # Calculate grid dimensions
#             num_images = len(image_urls)
#             cols = min(2, num_images)  # Max 2 columns
#             rows = (num_images + cols - 1) // cols
            
#             # Download and display each image
#             for i, url in enumerate(image_urls):
#                 # Calculate grid position
#                 row = i // cols
#                 col = i % cols
                
#                 # Create frame for this image
#                 img_frame = ttk.Frame(image_grid, padding=5)
#                 img_frame.grid(row=row, column=col, padx=5, pady=5, sticky=tk.NSEW)
                
#                 # Add placeholder while loading
#                 placeholder_label = ttk.Label(img_frame, text="Loading image...", font=("Arial", 12))
#                 placeholder_label.pack(padx=5, pady=5)
                
#                 # Start a thread to download the image
#                 thread = threading.Thread(
#                     target=self.load_image_thread,
#                     args=(url, img_frame, placeholder_label, i)
#                 )
#                 thread.daemon = True
#                 thread.start()
            
#             # Configure grid weights
#             for i in range(rows):
#                 image_grid.rowconfigure(i, weight=1)
#             for i in range(cols):
#                 image_grid.columnconfigure(i, weight=1)
                
#         except Exception as e:
#             self.log(f"Error displaying images: {str(e)}")
        
#         finally:
#             # End generation process
#             self.end_generation()
    
#     def load_image_thread(self, url, frame, placeholder_label, index):
#         """Thread function to load an image from URL"""
#         try:
#             # Download the image
#             response = requests.get(url)
#             if response.status_code != 200:
#                 self.root.after(0, lambda: self.log(f"Error downloading image {index+1}: Status code {response.status_code}"))
#                 return
            
#             # Create PIL Image from bytes
#             img = Image.open(BytesIO(response.content))
            
#             # Resize for display (maintaining aspect ratio)
#             target_size = 300  # Target size for display
#             ratio = min(target_size / img.width, target_size / img.height)
#             new_size = (int(img.width * ratio), int(img.height * ratio))
#             img_resized = img.resize(new_size, Image.Resampling.LANCZOS)
            
#             # Convert to PhotoImage
#             photo = ImageTk.PhotoImage(img_resized)
#             self.image_references.append(photo)  # Keep a reference
            
#             # Remove placeholder and display image
#             self.root.after(0, lambda: placeholder_label.pack_forget())
            
#             # Create and display the image label
#             image_label = ttk.Label(frame, image=photo)
#             self.root.after(0, lambda: image_label.pack(padx=5, pady=5))
            
#             # Add URL label and save button
#             url_text = f"Image {index+1}"
#             url_label = ttk.Label(frame, text=url_text, cursor="hand2")
#             url_label.bind("<Button-1>", lambda e, u=url: webbrowser.open(u))
#             self.root.after(0, lambda: url_label.pack(padx=5, pady=(0, 5)))
            
#             # Add save button
#             save_button = ttk.Button(
#                 frame, 
#                 text="Save Image", 
#                 command=lambda u=url, i=index: self.save_image(u, i)
#             )
#             self.root.after(0, lambda: save_button.pack(padx=5, pady=5))
            
#         except Exception as e:
#             self.root.after(0, lambda: self.log(f"Error loading image {index+1}: {str(e)}"))
    
#     def save_image(self, url, index):
#         """Save an image to disk"""
#         try:
#             # Download the image
#             response = requests.get(url)
#             if response.status_code != 200:
#                 self.log(f"Error downloading image for saving: Status code {response.status_code}")
#                 return
            
#             # Create a unique filename
#             output_filename = f"starryai_{int(time.time())}_{index+1}.png"
            
#             # Save the image
#             with open(output_filename, "wb") as f:
#                 f.write(response.content)
                
#             self.log(f"Image saved to {output_filename}")
            
#         except Exception as e:
#             self.log(f"Error saving image: {str(e)}")
    
#     def end_generation(self):
#         """End the generation process, reset UI"""
#         self.generate_button.configure(state="normal")
#         self.progress.stop()
#         self.status_var.set("Ready")

# def main():
#     root = tk.Tk()
#     app = StarryAIGenerator(root)
#     root.mainloop()

# if __name__ == "__main__":
#     main()














# import tkinter as tk
# from tkinter import ttk, scrolledtext, filedialog
# import requests
# import json
# import time
# import os
# import threading
# from PIL import Image, ImageTk
# from io import BytesIO
# import webbrowser

# class StarryAIGenerator:
#     def __init__(self, root):
#         self.root = root
#         self.root.title("StarryAI Image Generator")
#         self.root.geometry("1000x800")
#         self.root.configure(bg="#f0f0f0")
        
#         # Your API key - you might want to store this more securely in a real application
#         self.api_key = "EQeCGEkNmLFDpolGL4nxdu49f7Q_NQ"
        
#         # Create UI elements
#         self.create_widgets()
        
#         # Store image references to prevent garbage collection
#         self.image_references = []
        
#         # For storing generated image URLs
#         self.generated_image_urls = []
        
#     def create_widgets(self):
#         # Main frame
#         main_frame = ttk.Frame(self.root, padding="10")
#         main_frame.pack(fill=tk.BOTH, expand=True)
        
#         # Title label
#         title_label = ttk.Label(
#             main_frame, 
#             text="StarryAI Image Generator", 
#             font=("Arial", 20, "bold")
#         )
#         title_label.pack(pady=(0, 20))
        
#         # Input frame
#         input_frame = ttk.Frame(main_frame)
#         input_frame.pack(fill=tk.X, pady=10)
        
#         # Prompt label and entry
#         prompt_label = ttk.Label(input_frame, text="Enter your prompt:", font=("Arial", 12))
#         prompt_label.pack(side=tk.LEFT, padx=(0, 10))
        
#         self.prompt_entry = ttk.Entry(input_frame, width=50, font=("Arial", 12))
#         self.prompt_entry.pack(side=tk.LEFT, fill=tk.X, expand=True, padx=(0, 10))
        
#         # Generate button
#         self.generate_button = ttk.Button(
#             input_frame, 
#             text="Generate Images", 
#             command=self.start_generation
#         )
#         self.generate_button.pack(side=tk.RIGHT)
        
#         # Options frame
#         options_frame = ttk.LabelFrame(main_frame, text="Options", padding="10")
#         options_frame.pack(fill=tk.X, pady=10)
        
#         # Model selection
#         model_label = ttk.Label(options_frame, text="Model:")
#         model_label.grid(row=0, column=0, sticky=tk.W, padx=5, pady=5)
        
#         self.model_var = tk.StringVar(value="lyra")
#         model_combo = ttk.Combobox(
#             options_frame, 
#             textvariable=self.model_var, 
#             values=["lyra"],
#             state="readonly",
#             width=15
#         )
#         model_combo.grid(row=0, column=1, sticky=tk.W, padx=5, pady=5)
        
#         # Aspect ratio
#         aspect_label = ttk.Label(options_frame, text="Aspect Ratio:")
#         aspect_label.grid(row=0, column=2, sticky=tk.W, padx=5, pady=5)
        
#         self.aspect_var = tk.StringVar(value="square")
#         aspect_combo = ttk.Combobox(
#             options_frame, 
#             textvariable=self.aspect_var, 
#             values=["square", "portrait", "landscape"],
#             state="readonly",
#             width=15
#         )
#         aspect_combo.grid(row=0, column=3, sticky=tk.W, padx=5, pady=5)
        
#         # High resolution
#         self.high_res_var = tk.BooleanVar(value=False)
#         high_res_check = ttk.Checkbutton(
#             options_frame, 
#             text="High Resolution", 
#             variable=self.high_res_var
#         )
#         high_res_check.grid(row=0, column=4, sticky=tk.W, padx=5, pady=5)
        
#         # Steps
#         steps_label = ttk.Label(options_frame, text="Steps:")
#         steps_label.grid(row=1, column=0, sticky=tk.W, padx=5, pady=5)
        
#         self.steps_var = tk.IntVar(value=20)
#         steps_spin = ttk.Spinbox(
#             options_frame, 
#             from_=5, 
#             to=50, 
#             textvariable=self.steps_var,
#             width=5
#         )
#         steps_spin.grid(row=1, column=1, sticky=tk.W, padx=5, pady=5)
        
#         # Number of images
#         images_label = ttk.Label(options_frame, text="Images:")
#         images_label.grid(row=1, column=2, sticky=tk.W, padx=5, pady=5)
        
#         self.images_var = tk.IntVar(value=4)
#         images_spin = ttk.Spinbox(
#             options_frame, 
#             from_=1, 
#             to=4, 
#             textvariable=self.images_var,
#             width=5
#         )
#         images_spin.grid(row=1, column=3, sticky=tk.W, padx=5, pady=5)
        
#         # Progress frame
#         progress_frame = ttk.LabelFrame(main_frame, text="Progress", padding="10")
#         progress_frame.pack(fill=tk.X, pady=10)
        
#         # Status label
#         self.status_var = tk.StringVar(value="Ready")
#         status_label = ttk.Label(progress_frame, textvariable=self.status_var)
#         status_label.pack(side=tk.LEFT, padx=(0, 10))
        
#         # Progress bar
#         self.progress = ttk.Progressbar(progress_frame, mode="indeterminate", length=300)
#         self.progress.pack(side=tk.LEFT, fill=tk.X, expand=True)
        
#         # Log frame
#         log_frame = ttk.LabelFrame(main_frame, text="Log", padding="10")
#         log_frame.pack(fill=tk.BOTH, expand=True, pady=10)
        
#         # Log text area
#         self.log_text = scrolledtext.ScrolledText(log_frame, height=5, wrap=tk.WORD)
#         self.log_text.pack(fill=tk.BOTH, expand=True)
        
#         # Images frame with scrolling capability
#         images_outer_frame = ttk.LabelFrame(main_frame, text="Generated Images", padding="10")
#         images_outer_frame.pack(fill=tk.BOTH, expand=True, pady=10)
        
#         # Add a canvas with scrollbar for image display
#         self.images_canvas = tk.Canvas(images_outer_frame)
#         scrollbar = ttk.Scrollbar(images_outer_frame, orient="vertical", command=self.images_canvas.yview)
#         self.images_frame = ttk.Frame(self.images_canvas)
        
#         # Configure the canvas
#         self.images_canvas.configure(yscrollcommand=scrollbar.set)
#         scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
#         self.images_canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
        
#         # Create window in canvas for the images frame
#         self.canvas_frame = self.images_canvas.create_window((0, 0), window=self.images_frame, anchor="nw")
        
#         # Configure canvas scrolling
#         self.images_frame.bind("<Configure>", self.on_frame_configure)
#         self.images_canvas.bind("<Configure>", self.on_canvas_configure)
        
#         # Bind mousewheel for scrolling
#         self.images_canvas.bind_all("<MouseWheel>", self.on_mousewheel)
    
#     def on_frame_configure(self, event):
#         """Reset the scroll region to encompass the inner frame"""
#         self.images_canvas.configure(scrollregion=self.images_canvas.bbox("all"))
    
#     def on_canvas_configure(self, event):
#         """When canvas is resized, update the window size"""
#         canvas_width = event.width
#         self.images_canvas.itemconfig(self.canvas_frame, width=canvas_width)
    
#     def on_mousewheel(self, event):
#         """Handle mousewheel scrolling"""
#         self.images_canvas.yview_scroll(int(-1*(event.delta/120)), "units")
    
#     def log(self, message):
#         """Add message to log text area"""
#         self.log_text.configure(state="normal")
#         self.log_text.insert(tk.END, message + "\n")
#         self.log_text.see(tk.END)
#         self.log_text.configure(state="disabled")
#         self.root.update_idletasks()
    
#     def start_generation(self):
#         """Start image generation in a separate thread"""
#         prompt = self.prompt_entry.get().strip()
#         if not prompt:
#             self.log("Please enter a prompt")
#             return
        
#         # Disable the generate button and start progress bar
#         self.generate_button.configure(state="disabled")
#         self.progress.start()
#         self.status_var.set("Generating...")
        
#         # Clear previous images
#         for widget in self.images_frame.winfo_children():
#             widget.destroy()
#         self.image_references.clear()
#         self.generated_image_urls.clear()
        
#         # Start generation in a new thread
#         thread = threading.Thread(target=self.generate_images, args=(prompt,))
#         thread.daemon = True
#         thread.start()
    
#     def generate_images(self, prompt):
#         """Generate images in a background thread"""
#         try:
#             self.log(f"Starting image generation with prompt: '{prompt}'")
            
#             # Get parameters from UI
#             model = self.model_var.get()
#             aspect_ratio = self.aspect_var.get()
#             high_res = self.high_res_var.get()
#             steps = self.steps_var.get()
#             num_images = self.images_var.get()
            
#             # API call parameters
#             url = "https://api.starryai.com/creations/"
#             payload = {
#                 "prompt": prompt,
#                 "model": model,
#                 "aspectRatio": aspect_ratio,
#                 "highResolution": high_res,
#                 "images": num_images,
#                 "steps": steps,
#                 "initialImageMode": "color"
#             }
#             headers = {
#                 "accept": "application/json",
#                 "content-type": "application/json",
#                 "X-API-Key": self.api_key
#             }
            
#             # Make API request
#             self.log("Sending request to StarryAI API...")
#             response = requests.post(url, json=payload, headers=headers)
            
#             self.log(f"Response status code: {response.status_code}")
            
#             if response.status_code != 200:
#                 self.log(f"Error: API returned status code {response.status_code}")
#                 self.log(f"Response content: {response.text}")
#                 self.root.after(0, self.end_generation)
#                 return
            
#             # Parse response
#             try:
#                 creation_data = response.json()
#                 self.log(f"Creation started with ID: {creation_data.get('id')}")
                
#                 # Wait for creation to complete
#                 self.wait_for_creation(creation_data.get('id'))
                
#             except json.JSONDecodeError:
#                 self.log("Error: Could not parse JSON response")
#                 self.root.after(0, self.end_generation)
        
#         except Exception as e:
#             self.log(f"Error during generation: {str(e)}")
#             self.root.after(0, self.end_generation)
    
#     def wait_for_creation(self, creation_id, max_wait_time=300, check_interval=5):
#         """Wait for an image creation to complete"""
#         start_time = time.time()
        
#         while time.time() - start_time < max_wait_time:
#             self.log(f"Checking status of creation {creation_id}...")
            
#             # Check status
#             url = f"https://api.starryai.com/creations/{creation_id}"
#             headers = {
#                 "accept": "application/json",
#                 "X-API-Key": self.api_key
#             }
            
#             response = requests.get(url, headers=headers)
            
#             if response.status_code != 200:
#                 self.log(f"Error checking status: API returned status code {response.status_code}")
#                 self.log(f"Response: {response.text}")
#                 self.root.after(0, self.end_generation)
#                 return
            
#             try:
#                 status_data = response.json()
#                 status = status_data.get("status")
#                 self.log(f"Current status: {status}")
                
#                 if status == "completed":
#                     # Process completed images
#                     self.process_completed_images(status_data)
#                     return
#                 elif status == "failed":
#                     self.log("Creation failed")
#                     self.root.after(0, self.end_generation)
#                     return
                
#                 self.log(f"Waiting {check_interval} seconds before checking again...")
#                 time.sleep(check_interval)
                
#             except json.JSONDecodeError:
#                 self.log("Error: Could not parse JSON response")
#                 self.root.after(0, self.end_generation)
#                 return
        
#         self.log(f"Timed out after {max_wait_time} seconds")
#         self.root.after(0, self.end_generation)
    
#     def process_completed_images(self, completed_data):
#         """Process and display completed images"""
#         if "images" not in completed_data or not completed_data["images"]:
#             self.log("No images found in completed data")
#             self.root.after(0, self.end_generation)
#             return
        
#         images = completed_data["images"]
#         self.log(f"Found {len(images)} generated images")
        
#         # Store image URLs
#         self.generated_image_urls = [img["url"] for img in images if img.get("url")]
        
#         # Display images
#         self.root.after(0, lambda: self.display_images(self.generated_image_urls))
    
#     def display_images(self, image_urls):
#         """Display images in the UI"""
#         try:
#             # Calculate grid dimensions
#             num_images = len(image_urls)
#             cols = 2  # Fixed at 2 columns
#             rows = (num_images + cols - 1) // cols
            
#             # Configure grid
#             for i in range(cols):
#                 self.images_frame.columnconfigure(i, weight=1, minsize=400)
            
#             # Download and display each image
#             for i, url in enumerate(image_urls):
#                 # Calculate grid position
#                 row = i // cols
#                 col = i % cols
                
#                 # Create frame for this image
#                 img_frame = ttk.Frame(self.images_frame, padding=5, borderwidth=2, relief="ridge")
#                 img_frame.grid(row=row, column=col, padx=10, pady=10, sticky="nsew")
                
#                 # Make image frame expand to fill its cell
#                 img_frame.columnconfigure(0, weight=1)
#                 img_frame.rowconfigure(0, weight=1)
                
#                 # Add placeholder while loading
#                 placeholder_label = ttk.Label(img_frame, text="Loading image...", font=("Arial", 12))
#                 placeholder_label.grid(row=0, column=0, padx=5, pady=5)
                
#                 # Start a thread to download the image
#                 thread = threading.Thread(
#                     target=self.load_image_thread,
#                     args=(url, img_frame, placeholder_label, i)
#                 )
#                 thread.daemon = True
#                 thread.start()
            
#             # Update canvas scroll region
#             self.images_frame.update_idletasks()
#             self.images_canvas.configure(scrollregion=self.images_canvas.bbox("all"))
            
#         except Exception as e:
#             self.log(f"Error displaying images: {str(e)}")
        
#         finally:
#             # End generation process
#             self.end_generation()
    
#     def load_image_thread(self, url, frame, placeholder_label, index):
#         """Thread function to load an image from URL"""
#         try:
#             # Download the image
#             response = requests.get(url)
#             if response.status_code != 200:
#                 self.root.after(0, lambda: self.log(f"Error downloading image {index+1}: Status code {response.status_code}"))
#                 return
            
#             # Create PIL Image from bytes
#             img = Image.open(BytesIO(response.content))
            
#             # Calculate display size with fixed width but maintaining aspect ratio
#             display_width = 350  # Fixed width for display
#             display_height = int(img.height * (display_width / img.width))
            
#             # Resize for display (maintaining aspect ratio)
#             img_resized = img.resize((display_width, display_height), Image.Resampling.LANCZOS)
            
#             # Convert to PhotoImage
#             photo = ImageTk.PhotoImage(img_resized)
#             self.image_references.append(photo)  # Keep a reference
            
#             # Remove placeholder
#             self.root.after(0, lambda: placeholder_label.grid_forget())
            
#             # Create and display the image label with proper grid configuration
#             def display_image():
#                 image_label = ttk.Label(frame, image=photo)
#                 image_label.grid(row=0, column=0, padx=5, pady=5, sticky="nsew")
                
#                 # Add URL label
#                 url_text = f"Image {index+1}"
#                 url_label = ttk.Label(frame, text=url_text, cursor="hand2")
#                 url_label.bind("<Button-1>", lambda e, u=url: webbrowser.open(u))
#                 url_label.grid(row=1, column=0, padx=5, pady=(0, 5), sticky="w")
                
#                 # Add button frame for organizing buttons
#                 button_frame = ttk.Frame(frame)
#                 button_frame.grid(row=2, column=0, padx=5, pady=5, sticky="ew")
                
#                 # Add save button
#                 save_button = ttk.Button(
#                     button_frame, 
#                     text="Save Image", 
#                     command=lambda u=url, i=index: self.save_image(u, i)
#                 )
#                 save_button.pack(side=tk.LEFT, padx=5)
                
#                 # Update canvas scroll region to ensure all content is visible
#                 self.images_frame.update_idletasks()
#                 self.images_canvas.configure(scrollregion=self.images_canvas.bbox("all"))
            
#             self.root.after(0, display_image)
            
#         except Exception as e:
#             self.root.after(0, lambda: self.log(f"Error loading image {index+1}: {str(e)}"))
    
#     def save_image(self, url, index):
#         """Save an image to disk"""
#         try:
#             # Ask user for save location
#             file_types = [('PNG Image', '*.png'), ('JPEG Image', '*.jpg')]
#             filename = filedialog.asksaveasfilename(
#                 defaultextension=".png",
#                 filetypes=file_types,
#                 title="Save Image As",
#                 initialfile=f"starryai_{int(time.time())}_{index+1}"
#             )
            
#             if not filename:  # User cancelled
#                 return
                
#             # Download the image
#             response = requests.get(url)
#             if response.status_code != 200:
#                 self.log(f"Error downloading image for saving: Status code {response.status_code}")
#                 return
            
#             # Save the image
#             with open(filename, "wb") as f:
#                 f.write(response.content)
                
#             self.log(f"Image saved to {filename}")
            
#         except Exception as e:
#             self.log(f"Error saving image: {str(e)}")
    
#     def end_generation(self):
#         """End the generation process, reset UI"""
#         self.generate_button.configure(state="normal")
#         self.progress.stop()
#         self.status_var.set("Ready")

# def main():
#     root = tk.Tk()
#     app = StarryAIGenerator(root)
#     root.mainloop()

# if __name__ == "__main__":
#     main()


















# import streamlit as st
# import google.generativeai as genai
# import requests
# import time
# import json
# from PIL import Image
# from io import BytesIO
# import os

# class CombinedAIApp:
#     def __init__(self):
#         # Set page config
#         st.set_page_config(
#             page_title="Generation Hub",
#             page_icon="🤖",
#             layout="wide",
#             initial_sidebar_state="expanded"
#         )
        
#         # Add title and description
#         st.title("Generation Hub")
#         st.markdown("Generate text with prompt")
        
#         # API keys hardcoded (not recommended for production)
#         self.gemini_api_key = "AIzaSyCzHa6m60yJ6-gd81SABHUrNpK6m3Nl4uY"
#         self.starry_api_key = "EQeCGEkNmLFDpolGL4nxdu49f7Q_NQ"
        
#         # Initialize session state for chat history
#         if "gemini_chat_history" not in st.session_state:
#             st.session_state.gemini_chat_history = []
#             st.session_state.gemini_initialized = False
#             st.session_state.generated_image_urls = []
        
#         # Create tabs
#         tab1, tab2 = st.tabs(["Text Generation", "Image Generation"])
        
#         # Build UI for each tab
#         with tab1:
#             self.build_text_generation_tab()
        
#         with tab2:
#             self.build_image_generation_tab()
    
#     def initialize_gemini(self):
#         """Initialize Gemini API and chat"""
#         if not st.session_state.gemini_initialized and self.gemini_api_key:
#             try:
#                 genai.configure(api_key=self.gemini_api_key)
#                 st.session_state.gemini_model = genai.GenerativeModel('gemini-2.0-flash')
#                 st.session_state.gemini_chat = st.session_state.gemini_model.start_chat(history=[])
#                 st.session_state.gemini_initialized = True
                
#                 # Add welcome message if no history
#                 if not st.session_state.gemini_chat_history:
#                     st.session_state.gemini_chat_history.append({
#                         "role": "assistant",
#                         "content": "Hello! How can I assist you today?"
#                     })
                
#                 return True
#             except Exception as e:
#                 st.error(f"Error initializing Gemini API: {str(e)}")
#                 return False
#         return st.session_state.gemini_initialized
    
#     def build_text_generation_tab(self):
#         """Build the text generation tab UI"""
#         st.header("Text Generation")
        
#         # Chat container to display messages
#         chat_container = st.container()
        
#         # Display chat history
#         with chat_container:
#             for message in st.session_state.gemini_chat_history:
#                 if message["role"] == "user":
#                     st.chat_message("user").write(message["content"])
#                 else:
#                     st.chat_message("assistant").write(message["content"])
        
#         # Input for user message
#         user_message = st.chat_input("Type your message here...")
        
#         # Process user input
#         if user_message:
#             # Add user message to chat history
#             st.session_state.gemini_chat_history.append({
#                 "role": "user",
#                 "content": user_message
#             })
            
#             # Display user message
#             with chat_container:
#                 st.chat_message("user").write(user_message)
            
#             # Initialize Gemini if needed
#             if self.initialize_gemini():
#                 with st.spinner("Thinking..."):
#                     try:
#                         # Get response from Gemini
#                         response = st.session_state.gemini_chat.send_message(user_message)
#                         response_text = response.text
                        
#                         # Add AI response to chat history
#                         st.session_state.gemini_chat_history.append({
#                             "role": "assistant",
#                             "content": response_text
#                         })
                        
#                         # Display AI response
#                         with chat_container:
#                             st.chat_message("assistant").write(response_text)
                    
#                     except Exception as e:
#                         error_message = f"An error occurred: {str(e)}"
#                         st.error(error_message)
                        
#                         # Add error to chat history
#                         st.session_state.gemini_chat_history.append({
#                             "role": "assistant",
#                             "content": error_message
#                         })
        
#         # Clear chat button
#         if st.button("Clear Chat History", key="clear_chat"):
#             st.session_state.gemini_chat_history = []
#             if st.session_state.gemini_initialized:
#                 st.session_state.gemini_chat = st.session_state.gemini_model.start_chat(history=[])
#             st.session_state.gemini_chat_history.append({
#                 "role": "assistant",
#                 "content": "Chat history cleared. How can I assist you?"
#             })
#             st.rerun()
    
#     def build_image_generation_tab(self):
#         """Build the image generation tab UI"""
#         st.header("Image Generation")
        
#         # Create two columns for input and options
#         col1, col2 = st.columns([3, 2])
        
#         # Prompt input
#         with col1:
#             prompt = st.text_area("Enter your prompt:", height=100)
            
#             if st.button("Generate Images", key="generate_image"):
#                 if not prompt.strip():
#                     st.error("Please enter a prompt")
#                 elif not self.starry_api_key:
#                     st.error("Please enter your StarryAI API key")
#                 else:
#                     # Clear previous images
#                     st.session_state.generated_image_urls = []
                    
#                     # Start generation process
#                     with st.spinner("Generating images..."):
#                         self.generate_images(prompt)
        
#         # Options
#         with col2:
#             st.subheader("Options")
#             model = st.selectbox("Model", ["lyra"], index=0)
#             aspect_ratio = st.selectbox("Aspect Ratio", ["square", "portrait", "landscape"], index=0)
#             high_res = st.checkbox("High Resolution", value=False)
#             steps = st.slider("Steps", min_value=5, max_value=50, value=20)
#             num_images = st.slider("Number of Images", min_value=1, max_value=1, value=1)
            
#             # Store options in session state
#             st.session_state.image_options = {
#                 "model": model,
#                 "aspect_ratio": aspect_ratio,
#                 "high_res": high_res,
#                 "steps": steps,
#                 "num_images": num_images
#             }
        
#         # Display area for generated images
#         if st.session_state.generated_image_urls:
#             st.subheader("Generated Images")
            
#             # Create a grid for images (2 columns)
#             cols = st.columns(2)
            
#             for i, url in enumerate(st.session_state.generated_image_urls):
#                 col_index = i % 2
                
#                 with cols[col_index]:
#                     try:
#                         # Display image
#                         response = requests.get(url)
#                         if response.status_code == 200:
#                             img = Image.open(BytesIO(response.content))
#                             st.image(img, caption=f"Image {i+1}", use_container_width=True)
                            
#                             # Add download button
#                             img_bytes = BytesIO()
#                             img.save(img_bytes, format="PNG")
#                             st.download_button(
#                                 label="Download Image",
#                                 data=img_bytes.getvalue(),
#                                 file_name=f"ai_generated_{int(time.time())}_{i+1}.png",
#                                 mime="image/png"
#                             )
#                         else:
#                             st.error(f"Could not load image {i+1}")
                    
#                     except Exception as e:
#                         st.error(f"Error displaying image {i+1}: {str(e)}")
        
#         # Log area
#         if "image_log" in st.session_state and st.session_state.image_log:
#             st.subheader("Generation Log")
#             st.text_area("Log", value=st.session_state.image_log, height=200, disabled=True)
    
#     def generate_images(self, prompt):
#         """Generate images using StarryAI API"""
#         try:
#             # Initialize log in session state if not exists
#             if "image_log" not in st.session_state:
#                 st.session_state.image_log = ""
            
#             # Log function
#             def log(message):
#                 st.session_state.image_log = f"{st.session_state.image_log}\n{message}"
            
#             log(f"Starting image generation with prompt: '{prompt}'")
            
#             # Get parameters from session state
#             options = st.session_state.image_options
#             model = options["model"]
#             aspect_ratio = options["aspect_ratio"]
#             high_res = options["high_res"]
#             steps = options["steps"]
#             num_images = options["num_images"]
            
#             # API call parameters
#             url = "https://api.starryai.com/creations/"
#             payload = {
#                 "prompt": prompt,
#                 "model": model,
#                 "aspectRatio": aspect_ratio,
#                 "highResolution": high_res,
#                 "images": num_images,
#                 "steps": steps,
#                 "initialImageMode": "color"
#             }
#             headers = {
#                 "accept": "application/json",
#                 "content-type": "application/json",
#                 "X-API-Key": self.starry_api_key
#             }
            
#             # Make API request
#             log("Sending request to StarryAI API...")
#             response = requests.post(url, json=payload, headers=headers)
            
#             log(f"Response status code: {response.status_code}")
            
#             if response.status_code != 200:
#                 log(f"Error: API returned status code {response.status_code}")
#                 log(f"Response content: {response.text}")
#                 st.error(f"API Error: {response.text}")
#                 return
            
#             # Parse response
#             try:
#                 creation_data = response.json()
#                 creation_id = creation_data.get('id')
#                 log(f"Creation started with ID: {creation_id}")
                
#                 # Wait for creation to complete
#                 self.wait_for_creation(creation_id, log)
                
#             except json.JSONDecodeError:
#                 log("Error: Could not parse JSON response")
#                 st.error("Could not parse API response")
        
#         except Exception as e:
#             if "image_log" in st.session_state:
#                 st.session_state.image_log += f"\nError during generation: {str(e)}"
#             st.error(f"Error during image generation: {str(e)}")
    
#     def wait_for_creation(self, creation_id, log_func, max_wait_time=300, check_interval=5):
#         """Wait for an image creation to complete"""
#         start_time = time.time()
#         progress_bar = st.progress(0)
        
#         while time.time() - start_time < max_wait_time:
#             # Update progress bar (approximate)
#             elapsed = time.time() - start_time
#             progress = min(elapsed / max_wait_time, 0.95)
#             progress_bar.progress(progress)
            
#             log_func(f"Checking status of creation {creation_id}...")
            
#             # Check status
#             url = f"https://api.starryai.com/creations/{creation_id}"
#             headers = {
#                 "accept": "application/json",
#                 "X-API-Key": self.starry_api_key
#             }
            
#             response = requests.get(url, headers=headers)
            
#             if response.status_code != 200:
#                 log_func(f"Error checking status: API returned status code {response.status_code}")
#                 log_func(f"Response: {response.text}")
#                 st.error(f"Error checking status: {response.text}")
#                 return
            
#             try:
#                 status_data = response.json()
#                 status = status_data.get("status")
#                 log_func(f"Current status: {status}")
                
#                 if status == "completed":
#                     # Process completed images
#                     progress_bar.progress(1.0)
#                     self.process_completed_images(status_data, log_func)
#                     return
#                 elif status == "failed":
#                     log_func("Creation failed")
#                     st.error("Image creation failed")
#                     return
                
#                 log_func(f"Waiting {check_interval} seconds before checking again...")
#                 time.sleep(check_interval)
                
#             except json.JSONDecodeError:
#                 log_func("Error: Could not parse JSON response")
#                 st.error("Could not parse API response")
#                 return
        
#         log_func(f"Timed out after {max_wait_time} seconds")
#         st.error(f"Image generation timed out after {max_wait_time} seconds")
    
#     def process_completed_images(self, completed_data, log_func):
#         """Process completed images"""
#         if "images" not in completed_data or not completed_data["images"]:
#             log_func("No images found in completed data")
#             st.error("No images were generated")
#             return
        
#         images = completed_data["images"]
#         log_func(f"Found {len(images)} generated images")
        
#         # Store image URLs in session state
#         st.session_state.generated_image_urls = [img["url"] for img in images if img.get("url")]
#         log_func(f"Image generation complete! {len(st.session_state.generated_image_urls)} images are ready.")
        
#         # Success message
#         st.success(f"Successfully generated {len(st.session_state.generated_image_urls)} images!")


# if __name__ == "__main__":
#     app = CombinedAIApp()













import streamlit as st
import google.generativeai as genai
import requests
import time
import json
from PIL import Image
from io import BytesIO
import os

class CombinedAIApp:
    def __init__(self):
        # Set page config
        st.set_page_config(
            page_title="Generation Hub",
            page_icon="🤖",
            layout="wide",
            initial_sidebar_state="expanded"
        )
        
        # Add title and description
        st.title("Generation Hub")
        st.markdown("Generate text with prompt")
        
        # API keys hardcoded (not recommended for production)
        self.gemini_api_key = "AIzaSyCzHa6m60yJ6-gd81SABHUrNpK6m3Nl4uY"
        self.starry_api_key = "NW1VgLEailri7dwSb1QzceJAJcqyEA"
        
        # Initialize session state for chat history
        if "gemini_chat_history" not in st.session_state:
            st.session_state.gemini_chat_history = []
            st.session_state.gemini_initialized = False
            st.session_state.generated_image_urls = []
        
        # Create tabs
        tab1, tab2 = st.tabs(["Text Generation", "Image Generation"])
        
        # Build UI for each tab
        with tab1:
            self.build_text_generation_tab()
        
        with tab2:
            self.build_image_generation_tab()
    
    def initialize_gemini(self):
        """Initialize Gemini API and chat"""
        if not st.session_state.gemini_initialized and self.gemini_api_key:
            try:
                genai.configure(api_key=self.gemini_api_key)
                st.session_state.gemini_model = genai.GenerativeModel('gemini-2.0-flash')
                st.session_state.gemini_chat = st.session_state.gemini_model.start_chat(history=[])
                st.session_state.gemini_initialized = True
                
                # Add welcome message if no history
                if not st.session_state.gemini_chat_history:
                    st.session_state.gemini_chat_history.append({
                        "role": "assistant",
                        "content": "Hello! How can I assist you today?"
                    })
                
                return True
            except Exception as e:
                st.error(f"Error initializing Gemini API: {str(e)}")
                return False
        return st.session_state.gemini_initialized
    
    def build_text_generation_tab(self):
        """Build the text generation tab UI"""
        st.header("Text Generation")
        
        # Chat container to display messages
        chat_container = st.container()
        
        # Display chat history
        with chat_container:
            for message in st.session_state.gemini_chat_history:
                if message["role"] == "user":
                    st.chat_message("user").write(message["content"])
                else:
                    st.chat_message("assistant").write(message["content"])
        
        # Input for user message
        user_message = st.chat_input("Type your message here...")
        
        # Process user input
        if user_message:
            # Add user message to chat history
            st.session_state.gemini_chat_history.append({
                "role": "user",
                "content": user_message
            })
            
            # Display user message
            with chat_container:
                st.chat_message("user").write(user_message)
            
            # Initialize Gemini if needed
            if self.initialize_gemini():
                with st.spinner("Thinking..."):
                    try:
                        # Get response from Gemini
                        response = st.session_state.gemini_chat.send_message(user_message)
                        response_text = response.text
                        
                        # Add AI response to chat history
                        st.session_state.gemini_chat_history.append({
                            "role": "assistant",
                            "content": response_text
                        })
                        
                        # Display AI response
                        with chat_container:
                            st.chat_message("assistant").write(response_text)
                    
                    except Exception as e:
                        error_message = f"An error occurred: {str(e)}"
                        st.error(error_message)
                        
                        # Add error to chat history
                        st.session_state.gemini_chat_history.append({
                            "role": "assistant",
                            "content": error_message
                        })
        
        # Clear chat button
        if st.button("Clear Chat History", key="clear_chat"):
            st.session_state.gemini_chat_history = []
            if st.session_state.gemini_initialized:
                st.session_state.gemini_chat = st.session_state.gemini_model.start_chat(history=[])
            st.session_state.gemini_chat_history.append({
                "role": "assistant",
                "content": "Chat history cleared. How can I assist you?"
            })
            st.rerun()
    
    def build_image_generation_tab(self):
        """Build the image generation tab UI"""
        st.header("Image Generation")
        
        # Create two columns for input and options
        col1, col2 = st.columns([3, 2])
        
        # Prompt input
        with col1:
            prompt = st.text_area("Enter your prompt:", height=100, 
                                  placeholder="Describe the image you want to generate in detail...")
            
            # Advanced prompt features
            st.subheader("Advanced Prompt Features")
            
            with st.expander("Prompt Enhancement"):
                enhance_prompt = st.checkbox("Enhance my prompt automatically", value=True)
                enhancement_style = st.selectbox(
                    "Enhancement style", 
                    ["Detailed", "Artistic", "Photorealistic", "Minimalist"],
                    index=0
                )
                
                negative_prompt = st.text_area(
                    "Negative prompt (what to exclude):", 
                    height=100, 
                    placeholder="Elements you want to avoid in the image..."
                )
            
            if st.button("Generate Images", key="generate_image"):
                if not prompt.strip():
                    st.error("Please enter a prompt")
                elif not self.starry_api_key:
                    st.error("Please enter your StarryAI API key")
                else:
                    # Process prompt if enhancement is enabled
                    if enhance_prompt:
                        processed_prompt = self.enhance_prompt(prompt, enhancement_style)
                        st.info(f"Enhanced prompt: {processed_prompt}")
                    else:
                        processed_prompt = prompt
                    
                    # Clear previous images
                    st.session_state.generated_image_urls = []
                    
                    # Start generation process
                    with st.spinner("Generating images..."):
                        self.generate_images(processed_prompt, negative_prompt)
        
        # Options
        with col2:
            st.subheader("Image Options")
            
            # Main options
            model = st.selectbox("Model", ["lyra", "orion"], index=0, 
                                help="Lyra is best for realistic images; Orion for creative/artistic ones")
            
            # Force number of images to always be 1
            # This line is hidden from the UI but ensures we always send 1 as the value
            num_images = 1
            
            aspect_ratio = st.selectbox(
                "Aspect Ratio", 
                ["square", "portrait", "landscape"], 
                index=0,
                help="Square (1:1), Portrait (2:3), Landscape (3:2)"
            )
            
            # Advanced image settings
            with st.expander("Advanced Settings"):
                high_res = st.checkbox("High Resolution", value=True)
                steps = st.slider("Steps", min_value=10, max_value=50, value=30, 
                                 help="More steps = more detail but takes longer")
                
                guidance_scale = st.slider("Guidance Scale", min_value=1.0, max_value=20.0, value=7.5, step=0.5,
                                         help="How closely to follow the prompt (higher = more faithful)")
                
                seed = st.number_input("Seed (leave at -1 for random)", 
                                      min_value=-1, max_value=999999, value=-1,
                                      help="Use same seed to get consistent results")
            
            # Store options in session state
            st.session_state.image_options = {
                "model": model,
                "aspect_ratio": aspect_ratio,
                "high_res": high_res,
                "steps": steps,
                "num_images": num_images,  # Always set to 1
                "guidance_scale": guidance_scale,
                "seed": seed
            }
            
            # Display usage information
            with st.expander("Usage Information"):
                st.info("""
                **Tips for best results:**
                - Be specific in your prompts
                - Include details about style, lighting, composition
                - Use the negative prompt to exclude unwanted elements
                - Higher steps (30-50) produce more detailed images
                """)
        
        # Display area for generated images
        if st.session_state.generated_image_urls:
            st.subheader("Generated Image")
            
            for i, url in enumerate(st.session_state.generated_image_urls):
                try:
                    # Display image (centered)
                    response = requests.get(url)
                    if response.status_code == 200:
                        img = Image.open(BytesIO(response.content))
                        st.image(img, caption=f"Generated Image", use_container_width=True)
                        
                        # Download button
                        img_bytes = BytesIO()
                        img.save(img_bytes, format="PNG")
                        st.download_button(
                            label="Download Image",
                            data=img_bytes.getvalue(),
                            file_name=f"ai_generated_{int(time.time())}.png",
                            mime="image/png"
                        )
                    else:
                        st.error(f"Could not load image")
                
                except Exception as e:
                    st.error(f"Error displaying image: {str(e)}")
        
        # Image history section
        if "image_history" not in st.session_state:
            st.session_state.image_history = []
        
        if st.session_state.image_history:
            with st.expander("Image Generation History"):
                for i, entry in enumerate(reversed(st.session_state.image_history[-5:])):  # Show last 5
                    st.markdown(f"**Prompt:** {entry['prompt']}")
                    st.image(entry['url'], width=200)
                    st.markdown("---")
        
        # Log area (collapsible)
        if "image_log" in st.session_state and st.session_state.image_log:
            with st.expander("Generation Log", expanded=False):
                st.text_area("Log", value=st.session_state.image_log, height=200, disabled=True)
    
    def enhance_prompt(self, prompt, style):
        """Enhance the user's prompt based on the selected style"""
        style_additions = {
            "Detailed": "highly detailed, intricate, 8k resolution, crisp focus",
            "Artistic": "artistic style, vibrant colors, expressive, painterly technique",
            "Photorealistic": "photorealistic, professional photography, dramatic lighting, high resolution",
            "Minimalist": "minimalist style, clean lines, simple composition, elegant"
        }
        
        # Add style enhancements to the prompt
        enhanced_prompt = f"{prompt}, {style_additions.get(style, '')}"
        return enhanced_prompt
    
    def generate_images(self, prompt, negative_prompt=""):
        """Generate images using StarryAI API"""
        try:
            # Store the prompts for possible regeneration
            st.session_state.last_prompt = prompt
            st.session_state.last_negative_prompt = negative_prompt
            
            # Initialize log in session state if not exists
            if "image_log" not in st.session_state:
                st.session_state.image_log = ""
            
            # Log function
            def log(message):
                st.session_state.image_log = f"{st.session_state.image_log}\n{message}"
            
            log(f"Starting image generation with prompt: '{prompt}'")
            if negative_prompt:
                log(f"Negative prompt: '{negative_prompt}'")
            
            # Get parameters from session state
            options = st.session_state.image_options
            model = options["model"]
            aspect_ratio = options["aspect_ratio"]
            high_res = options["high_res"]
            steps = options["steps"]
            # Force number of images to always be 1, regardless of what's in options
            num_images = 1
            guidance_scale = options.get("guidance_scale", 7.5)
            seed = options.get("seed", -1)
            
            # API call parameters
            url = "https://api.starryai.com/creations/"
            payload = {
                "prompt": prompt,
                "model": model,
                "aspectRatio": aspect_ratio,
                "highResolution": high_res,
                "images": num_images,  # Always set to 1
                "steps": steps,
                "initialImageMode": "color",
                "negativePrompt": negative_prompt if negative_prompt else "",
                "guidanceScale": guidance_scale
            }
            
            # Add seed if provided with a value other than -1
            if seed != -1:
                payload["seed"] = seed
                
            headers = {
                "accept": "application/json",
                "content-type": "application/json",
                "X-API-Key": self.starry_api_key
            }
            
            # Make API request
            log("Sending request to StarryAI API...")
            log(f"API payload: {json.dumps(payload, indent=2)}")
            response = requests.post(url, json=payload, headers=headers)
            
            log(f"Response status code: {response.status_code}")
            
            if response.status_code != 200:
                log(f"Error: API returned status code {response.status_code}")
                log(f"Response content: {response.text}")
                st.error(f"API Error: {response.text}")
                return
            
            # Parse response
            try:
                creation_data = response.json()
                creation_id = creation_data.get('id')
                log(f"Creation started with ID: {creation_id}")
                
                # Wait for creation to complete
                self.wait_for_creation(creation_id, log)
                
            except json.JSONDecodeError:
                log("Error: Could not parse JSON response")
                st.error("Could not parse API response")
        
        except Exception as e:
            if "image_log" in st.session_state:
                st.session_state.image_log += f"\nError during generation: {str(e)}"
            st.error(f"Error during image generation: {str(e)}")
    
    def wait_for_creation(self, creation_id, log_func, max_wait_time=300, check_interval=5):
        """Wait for an image creation to complete"""
        start_time = time.time()
        progress_bar = st.progress(0)
        status_text = st.empty()
        
        while time.time() - start_time < max_wait_time:
            # Update progress bar (approximate)
            elapsed = time.time() - start_time
            progress = min(elapsed / max_wait_time, 0.95)
            progress_bar.progress(progress)
            
            # Update status message
            current_step = int(progress * 100)
            status_message = f"Processing image... {current_step}%"
            if current_step < 20:
                status_message += " (Analyzing prompt)"
            elif current_step < 50:
                status_message += " (Generating initial composition)"
            elif current_step < 80:
                status_message += " (Adding details)"
            else:
                status_message += " (Finalizing image)"
            
            status_text.text(status_message)
            
            log_func(f"Checking status of creation {creation_id}...")
            
            # Check status
            url = f"https://api.starryai.com/creations/{creation_id}"
            headers = {
                "accept": "application/json",
                "X-API-Key": self.starry_api_key
            }
            
            response = requests.get(url, headers=headers)
            
            if response.status_code != 200:
                log_func(f"Error checking status: API returned status code {response.status_code}")
                log_func(f"Response: {response.text}")
                st.error(f"Error checking status: {response.text}")
                return
            
            try:
                status_data = response.json()
                status = status_data.get("status")
                log_func(f"Current status: {status}")
                
                if status == "completed":
                    # Process completed images
                    progress_bar.progress(1.0)
                    status_text.text("Image generation complete!")
                    self.process_completed_images(status_data, log_func)
                    return
                elif status == "failed":
                    log_func("Creation failed")
                    status_text.text("Image generation failed")
                    st.error("Image creation failed")
                    return
                
                log_func(f"Waiting {check_interval} seconds before checking again...")
                time.sleep(check_interval)
                
            except json.JSONDecodeError:
                log_func("Error: Could not parse JSON response")
                st.error("Could not parse API response")
                return
        
        log_func(f"Timed out after {max_wait_time} seconds")
        st.error(f"Image generation timed out after {max_wait_time} seconds")
    
    def process_completed_images(self, completed_data, log_func):
        """Process completed images"""
        if "images" not in completed_data or not completed_data["images"]:
            log_func("No images found in completed data")
            st.error("No images were generated")
            return
        
        images = completed_data["images"]
        log_func(f"Found {len(images)} generated images")
        
        # Store image URLs in session state
        st.session_state.generated_image_urls = [img["url"] for img in images if img.get("url")]
        log_func(f"Image generation complete! {len(st.session_state.generated_image_urls)} images are ready.")
        
        # Add to image history
        if len(st.session_state.generated_image_urls) > 0:
            for url in st.session_state.generated_image_urls:
                if "image_history" not in st.session_state:
                    st.session_state.image_history = []
                    
                st.session_state.image_history.append({
                    "prompt": st.session_state.last_prompt,
                    "url": url,
                    "timestamp": time.time()
                })
        
        # Success message
        st.success(f"Successfully generated {len(st.session_state.generated_image_urls)} image!")


if __name__ == "__main__":
    app = CombinedAIApp()












# import gradio as gr
# import google.generativeai as genai
# import requests
# import time
# import json
# from PIL import Image
# from io import BytesIO
# import os

# class CombinedAIGradioApp:
#     def __init__(self):
#         # API keys hardcoded (not recommended for production)
#         self.gemini_api_key = "AIzaSyCzHa6m60yJ6-gd81SABHUrNpK6m3Nl4uY"
#         self.starry_api_key = "NW1VgLEailri7dwSb1QzceJAJcqyEA"
        
#         # Initialize Gemini
#         self.initialize_gemini()
        
#         # Create Gradio interface
#         self.create_interface()
    
#     def initialize_gemini(self):
#         """Initialize Gemini API and chat"""
#         try:
#             if self.gemini_api_key:
#                 genai.configure(api_key=self.gemini_api_key)
#                 self.gemini_model = genai.GenerativeModel('gemini-2.0-flash')
#                 self.gemini_chat = self.gemini_model.start_chat(history=[])
#                 return True
#         except Exception as e:
#             print(f"Error initializing Gemini API: {str(e)}")
#             return False
    
#     def create_interface(self):
#         """Create Gradio interface with tabs for text and image generation"""
#         # Text Generation Tab Components
#         with gr.Blocks() as text_gen:
#             gr.Markdown("# Text Generation")
            
#             # Fixed: Removed 'bubble' parameter and added 'type' parameter
#             chatbot = gr.Chatbot(
#                 label="Chat History",
#                 height=500,
#                 type="messages",  # Added this parameter
#                 avatar_images=(None, "https://img.icons8.com/color/96/000000/bot.png")
#             )
            
#             with gr.Row():
#                 msg = gr.Textbox(
#                     label="Type your message",
#                     placeholder="Ask me anything...",
#                     scale=9
#                 )
#                 clear_btn = gr.Button("Clear Chat", scale=1)
            
#             # Set up event handlers for text generation
#             msg.submit(self.text_generation, inputs=[msg, chatbot], outputs=[msg, chatbot])
#             clear_btn.click(self.clear_chat, outputs=[chatbot])
        
#         # Image Generation Tab Components
#         with gr.Blocks() as image_gen:
#             gr.Markdown("# Image Generation")
            
#             with gr.Row():
#                 with gr.Column(scale=3):
#                     prompt = gr.Textbox(
#                         label="Enter your prompt",
#                         placeholder="Describe the image you want to generate in detail...",
#                         lines=5
#                     )
                    
#                     with gr.Accordion("Advanced Prompt Features", open=False):
#                         enhance_prompt = gr.Checkbox(label="Enhance my prompt automatically", value=True)
#                         enhancement_style = gr.Dropdown(
#                             label="Enhancement style",
#                             choices=["Detailed", "Artistic", "Photorealistic", "Minimalist"],
#                             value="Detailed"
#                         )
#                         negative_prompt = gr.Textbox(
#                             label="Negative prompt (what to exclude)",
#                             placeholder="Elements you want to avoid in the image...",
#                             lines=3
#                         )
                    
#                     gen_btn = gr.Button("Generate Image", variant="primary")
                
#                 with gr.Column(scale=2):
#                     gr.Markdown("### Image Options")
                    
#                     model = gr.Dropdown(
#                         label="Model",
#                         choices=["lyra", "orion"],
#                         value="lyra",
#                         info="Lyra is best for realistic images; Orion for creative/artistic ones"
#                     )
                    
#                     aspect_ratio = gr.Dropdown(
#                         label="Aspect Ratio",
#                         choices=["square", "portrait", "landscape"],
#                         value="square",
#                         info="Square (1:1), Portrait (2:3), Landscape (3:2)"
#                     )
                    
#                     with gr.Accordion("Advanced Settings", open=False):
#                         high_res = gr.Checkbox(label="High Resolution", value=True)
#                         steps = gr.Slider(
#                             label="Steps",
#                             minimum=10,
#                             maximum=50,
#                             value=30,
#                             step=1,
#                             info="More steps = more detail but takes longer"
#                         )
#                         guidance_scale = gr.Slider(
#                             label="Guidance Scale",
#                             minimum=1.0,
#                             maximum=20.0,
#                             value=7.5,
#                             step=0.5,
#                             info="How closely to follow the prompt (higher = more faithful)"
#                         )
#                         seed = gr.Number(
#                             label="Seed (leave at -1 for random)",
#                             value=-1,
#                             minimum=-1,
#                             info="Use same seed to get consistent results"
#                         )
            
#             with gr.Row():
#                 with gr.Column():
#                     output_image = gr.Image(label="Generated Image", type="pil")
#                     status = gr.Markdown("Status: Ready")
#                     log_output = gr.Textbox(label="Generation Log", lines=5, visible=False)
            
#             # Set up event handler for image generation
#             gen_btn.click(
#                 self.image_generation,
#                 inputs=[
#                     prompt, enhance_prompt, enhancement_style, negative_prompt,
#                     model, aspect_ratio, high_res, steps, guidance_scale, seed
#                 ],
#                 outputs=[output_image, status, log_output]
#             )
        
#         # Create the tabbed interface
#         demo = gr.TabbedInterface(
#             [text_gen, image_gen],
#             ["Text Generation", "Image Generation"],
#             title="Generation Hub",
#             css="""
#             .gr-header-text {
#                 text-align: center;
#                 font-size: 24px;
#                 font-weight: bold;
#             }
#             """
#         )
        
#         # Fixed: Added queue parameter to fix async context manager issue
#         demo.queue()  # Initialize the queue before launching
        
#         # Launch the app
#         demo.launch()
    
#     def text_generation(self, message, history):
#         """Handle text generation requests"""
#         # Validate input
#         if not message or message.strip() == "":
#             return message, history
        
#         # Update message format to match 'messages' type
#         try:
#             # Generate response with Gemini
#             response = self.gemini_chat.send_message(message)
#             response_text = response.text
            
#             # For 'messages' type chatbot, return in the format required
#             history_msgs = history + [{"role": "user", "content": message}, 
#                                      {"role": "assistant", "content": response_text}]
            
#             return "", history_msgs
#         except Exception as e:
#             error_message = f"An error occurred: {str(e)}"
#             history_msgs = history + [{"role": "user", "content": message}, 
#                                      {"role": "assistant", "content": error_message}]
#             return "", history_msgs
    
#     def clear_chat(self):
#         """Clear chat history"""
#         # Reset Gemini chat
#         if hasattr(self, 'gemini_model'):
#             self.gemini_chat = self.gemini_model.start_chat(history=[])
        
#         # Return empty chat history
#         return []
    
#     def enhance_prompt(self, prompt, style):
#         """Enhance the user's prompt based on the selected style"""
#         style_additions = {
#             "Detailed": "highly detailed, intricate, 8k resolution, crisp focus",
#             "Artistic": "artistic style, vibrant colors, expressive, painterly technique",
#             "Photorealistic": "photorealistic, professional photography, dramatic lighting, high resolution",
#             "Minimalist": "minimalist style, clean lines, simple composition, elegant"
#         }
        
#         # Add style enhancements to the prompt
#         enhanced_prompt = f"{prompt}, {style_additions.get(style, '')}"
#         return enhanced_prompt
    
#     def image_generation(self, prompt, enhance_prompt, enhancement_style, negative_prompt, 
#                          model, aspect_ratio, high_res, steps, guidance_scale, seed):
#         """Generate images using StarryAI API"""
#         # Initialize log messages
#         log_messages = []
        
#         def log(message):
#             log_messages.append(message)
#             return "\n".join(log_messages)
        
#         # Check inputs
#         if not prompt or prompt.strip() == "":
#             return None, "Status: Error - Please enter a prompt", log("Error: Empty prompt")
        
#         if not self.starry_api_key:
#             return None, "Status: Error - API key not configured", log("Error: Missing API key")
        
#         try:
#             # Process prompt if enhancement is enabled
#             if enhance_prompt:
#                 processed_prompt = self.enhance_prompt(prompt, enhancement_style)
#                 log(f"Enhanced prompt: {processed_prompt}")
#             else:
#                 processed_prompt = prompt
            
#             log(f"Starting image generation with prompt: '{processed_prompt}'")
#             if negative_prompt:
#                 log(f"Negative prompt: '{negative_prompt}'")
            
#             # API call parameters
#             url = "https://api.starryai.com/creations/"
#             payload = {
#                 "prompt": processed_prompt,
#                 "model": model,
#                 "aspectRatio": aspect_ratio,
#                 "highResolution": high_res,
#                 "images": 1,  # Always generate 1 image
#                 "steps": steps,
#                 "initialImageMode": "color",
#                 "negativePrompt": negative_prompt if negative_prompt else "",
#                 "guidanceScale": guidance_scale
#             }
            
#             # Add seed if provided with a value other than -1
#             if seed != -1:
#                 payload["seed"] = seed
                
#             headers = {
#                 "accept": "application/json",
#                 "content-type": "application/json",
#                 "X-API-Key": self.starry_api_key
#             }
            
#             # Make API request
#             log("Sending request to StarryAI API...")
#             status_message = "Status: Sending request to API..."
#             yield None, status_message, log("")
            
#             response = requests.post(url, json=payload, headers=headers)
#             log(f"Response status code: {response.status_code}")
            
#             if response.status_code != 200:
#                 log(f"Error: API returned status code {response.status_code}")
#                 log(f"Response content: {response.text}")
#                 return None, f"Status: Error - API returned {response.status_code}", "\n".join(log_messages)
            
#             # Parse response
#             creation_data = response.json()
#             creation_id = creation_data.get('id')
#             log(f"Creation started with ID: {creation_id}")
            
#             # Wait for creation to complete
#             status_message = "Status: Processing image..."
#             yield None, status_message, "\n".join(log_messages)
            
#             # Polling for image completion
#             max_wait_time = 300  # 5 minutes
#             check_interval = 5    # 5 seconds
#             start_time = time.time()
            
#             while time.time() - start_time < max_wait_time:
#                 # Update status message
#                 elapsed = time.time() - start_time
#                 current_step = int((elapsed / max_wait_time) * 100)
                
#                 status_text = f"Status: Processing image... {min(current_step, 99)}%"
#                 if current_step < 20:
#                     status_text += " (Analyzing prompt)"
#                 elif current_step < 50:
#                     status_text += " (Generating initial composition)"
#                 elif current_step < 80:
#                     status_text += " (Adding details)"
#                 else:
#                     status_text += " (Finalizing image)"
                
#                 yield None, status_text, "\n".join(log_messages)
                
#                 # Check status
#                 log(f"Checking status of creation {creation_id}...")
#                 status_url = f"https://api.starryai.com/creations/{creation_id}"
                
#                 status_response = requests.get(status_url, headers=headers)
                
#                 if status_response.status_code != 200:
#                     log(f"Error checking status: API returned status code {status_response.status_code}")
#                     return None, "Status: Error checking image status", "\n".join(log_messages)
                
#                 status_data = status_response.json()
#                 status = status_data.get("status")
#                 log(f"Current status: {status}")
                
#                 if status == "completed":
#                     # Process completed images
#                     log("Image generation complete!")
                    
#                     if "images" not in status_data or not status_data["images"]:
#                         log("No images found in completed data")
#                         return None, "Status: Error - No images were generated", "\n".join(log_messages)
                    
#                     images = status_data["images"]
#                     image_url = images[0].get("url") if images else None
                    
#                     if not image_url:
#                         log("No image URL found")
#                         return None, "Status: Error - No image URL found", "\n".join(log_messages)
                    
#                     # Get the image
#                     log(f"Downloading image from {image_url}")
#                     img_response = requests.get(image_url)
#                     if img_response.status_code != 200:
#                         log("Failed to download image")
#                         return None, "Status: Error downloading the generated image", "\n".join(log_messages)
                    
#                     # Create PIL image
#                     image = Image.open(BytesIO(img_response.content))
                    
#                     return image, "Status: Image generation successful!", "\n".join(log_messages)
                    
#                 elif status == "failed":
#                     log("Creation failed")
#                     return None, "Status: Image generation failed", "\n".join(log_messages)
                
#                 # Wait before checking again
#                 time.sleep(check_interval)
            
#             # Timeout
#             log(f"Timed out after {max_wait_time} seconds")
#             return None, f"Status: Timed out after {max_wait_time} seconds", "\n".join(log_messages)
            
#         except Exception as e:
#             error_message = f"Error during image generation: {str(e)}"
#             log(error_message)
#             return None, f"Status: {error_message}", "\n".join(log_messages)

# if __name__ == "__main__":
#     app = CombinedAIGradioApp()