1+ # Youtube: Magno Efren
2+
3+ from tkinter import Tk , Frame , Button ,PhotoImage ,Label ,Text
4+ from tkinter .scrolledtext import ScrolledText
5+ from tkinter import filedialog , messagebox
6+ from customtkinter import CTk , CTkButton
7+ import openai #pip install openai
8+
9+ openai .api_key = "OPENAI_API_KEY"
10+
11+ def generate_text (prompt ):
12+ try :
13+ response = openai .Completion .create (
14+ model = "text-davinci-003" ,
15+ prompt = prompt ,
16+ temperature = 0.7 ,
17+ max_tokens = 100 ,
18+ top_p = 1 ,
19+ frequency_penalty = 0 ,
20+ presence_penalty = 0 ,
21+ )
22+ return response .choices [0 ].text
23+ except Exception as e :
24+ return 'No se conecto a la IA'
25+
26+ def save_file ():
27+ text = text_out .get ("1.0" , 'end' )
28+ file_path = filedialog .asksaveasfilename (defaultextension = ".txt" )
29+ with open (file_path , "w" ) as file :
30+ file .write (text )
31+
32+
33+ def show_data ():
34+ question = text_in .get ('1.0' , 'end' )
35+ if len (question ) != 0 :
36+ text_in .delete ('1.0' , 'end' )
37+ question_show = '\n ' + 'YO: ' + '\n ' + question
38+ text_out .insert ('end' , question_show )
39+ res = generate_text (question )
40+ res = 'IA: ' + res #'\n'
41+ text_out .insert ('end' , res )
42+ else :
43+ messagebox .showerror ('Error' , 'No se logro comunicar' )
44+
45+ window = CTk ()
46+ window .geometry ('600x400+400+100' )
47+ window .title ('Aplicación ChatGTP' )
48+ window .config (bg = 'black' )
49+ window .minsize (500 , 300 )
50+ window .iconphoto (False , PhotoImage (file = 'assets/icon.png' ))
51+
52+ img_save = PhotoImage (file = 'assets/save.png' )
53+ img_send = PhotoImage (file = 'assets/send.png' )
54+
55+
56+ frame_text = Frame (window , bg = 'white' , width = 400 , height = 400 )
57+ frame_text .grid (column = 0 , row = 0 , sticky = 'nsew' , pady = 5 , padx = 5 )
58+ frame_control = Frame (window , bg = 'black' , width = 200 , height = 400 )
59+ frame_control .grid (column = 1 , row = 0 , sticky = 'nsew' , pady = 5 , padx = 5 )
60+
61+ window .columnconfigure (0 , weight = 6 )
62+ window .columnconfigure (1 , weight = 1 )
63+ window .rowconfigure (0 , weight = 1 )
64+
65+ frame_text .grid_propagate (0 )
66+ frame_control .grid_propagate (0 )
67+
68+ frame_text .columnconfigure (0 , weight = 1 )
69+ frame_text .rowconfigure (0 , weight = 1 )
70+
71+ frame_control .columnconfigure (0 , weight = 1 )
72+ frame_control .rowconfigure ([0 ,1 ,2 ], weight = 1 )
73+
74+ text_out = ScrolledText (frame_text , font = ('Arial' , 12 ), insertbackground = 'blue' ,
75+ bg = 'black' , fg = 'white' )
76+ text_out .grid (column = 0 , row = 0 , sticky = 'nsew' )
77+
78+ text_in = Text (frame_control , font = ('Arial' , 12 ), insertbackground = 'blue' ,
79+ bg = 'black' , fg = 'white' , height = 12 )
80+ text_in .grid (column = 0 , row = 0 )
81+
82+ button_send = CTkButton (frame_control ,image = img_send ,compound = 'left' ,text = ' ENVIAR' ,
83+ text_font = ('Arial' , 11 , 'bold' ) , fg_color = 'blue' , command = show_data )
84+ button_send .grid (column = 0 , row = 1 , sticky = 'nsew' ,pady = 15 , padx = 15 )
85+
86+ button_save = CTkButton (frame_control ,image = img_save ,compound = 'left' ,text = 'GUARDAR' ,
87+ text_font = ('Arial' , 11 , 'bold' ) , fg_color = 'blue' , command = save_file )
88+ button_save .grid (column = 0 , row = 2 , sticky = 'nsew' ,pady = 15 , padx = 15 )
89+
90+
91+ window .mainloop ()
0 commit comments