TUTORIAL: How to Create a Python GUI that will use the power of CHATgpt

 CHATgpt SEARCH BOX V1.0 ... seeking comments, features, better code...


'''This script (I named it py_chat_to_me.py, but you can call it whatever you want!) creates a GUI with an input field for the topic and another input field for the file name. It also creates a button that when pressed, calls the generate_text function to generate the text and save it to the specified file. This file must be saved where the api_key.py folder lives. The api_key.py folder contains the your private openai.com access key.

The associated libraries are openai, re, api_key (see script at the bottom), and tkinter'''


import openai
import re
from api_key import API_KEY
import tkinter as tk
from tkinter import messagebox

openai.api_key = API_KEY

# Set the model to use
model_engine = "text-davinci-003"
def generate_text():
    
    # Get the topic from the user input
    topic = topic_entry.get()
    prompt = topic
    
    # Generate text using the GPT-3 model
    completions = openai.Completion.create(
        engine=model_engine,
        prompt=prompt,
        max_tokens=1024,
        n=1,
        stop=None,
        temperature=0.5,
    )
    
    # Print the generated text
    generated_text = completions.choices[0].text
    
    # Get the file name from the user input
    textname = file_entry.get()
    
    # Save the text in a file
    with open(textname, "w") as file:
        file.write(generated_text.strip())
    messagebox.showinfo("Success", "The Text Has Been Generated Successfully!")

# Create the main window
root = tk.Tk()
root.title("AI Text Generator")

# Create the topic label and entry
topic_label = tk.Label(root, text="What topic you want to write about: ")
topic_label.pack()
topic_entry = tk.Entry(root)
topic_entry.pack()

# Create the file name label and entry
file_label = tk.Label(root, text="What will be the name of your file? ")
file_label.pack()
file_entry = tk.Entry(root)
file_entry.pack()

# Create the generate button
generate_button = tk.Button(root, text="Generate Text", command=generate_text)
generate_button.pack()

# Start the main loop
root.mainloop()


# here's the code for api_key,py. Only one line. You need to register, for free, with OPENAI.COM and 
# get your own secret key, which starts with sk... Save it to same folder as the  py_chat_to_me.py file. 

API_KEY = "YOUR_SECRET_KEY_FROM_OPENAI.COM"

Please don't forget to leave your comments... Enjoy it!






Comments

Popular posts from this blog

Understanding Variables in Python 3.0 for Beginners

First Steps: Your Initial Guide to Prompting ChatGPT

Evolución de los puntos de vista de los partidos políticos estadounidenses sobre Oriente Medio y las relaciones con Israel (1976 - 2023)