# I made the following improvements to the code: # 1. Updated the input and output components to use Gradio's new Input and Output classes # 2. Added a progress bar to show progress while generating text # 3. Added an error message to be displayed if the client fails to generate text # 4. Added a default value to be returned if the model is unable to generate text # 5. Moved the clear and copy methods to be class methods # 6. Simplified the launch_interface method to make the code more readable # 7. Added comments to the code to explain each method/function import gradio as gr import pyperclip from text_generation import InferenceAPIClient class TextGenerator: """ TextGenerator class generates text based on user input using OpenAI's text-generation model. """ def __init__(self, model: str): self.client = InferenceAPIClient(model=model) self.input_text = gr.inputs.Textbox(placeholder="Enter your text here...", label="Enter input text here") self.output_text = gr.outputs.Textbox(placeholder="Bot's response", label="Bot's response:") self.progress_bar = gr.outputs.ProgressBar(label="Progress") self.generate_button = gr.inputs.Button(label="Generate") self.clear_button = gr.inputs.Button(label="Clear") self.copy_button = gr.outputs.Button(label="Copy to Clipboard") self.interface = gr.Interface(self.generate_output_text, inputs=[self.input_text, self.generate_button, self.clear_button], outputs=[self.output_text, self.copy_button, self.progress_bar], title="Text Generation Bot", theme="default", layout="vertical", description="This bot generates text based on the input you provide. Please enter your text below.") def generate_output_text(self, input_text: str) -> str: """ This method generates and returns text based on the provided input. Args: input_text : str : The user's input. Returns: str : The generated text based on the user's input. """ try: # Show the progress bar self.progress_bar.update(0) # Generate the text generated_text = self.client.generate_text(input_text) except Exception as e: # If an exception occurs, display an error message error_message = f"Sorry, an error occurred. {str(e)}. Please try again." self.output_text.update(error_message) return # If no text is generated, display a default message if not generated_text: default_text = "Sorry, unable to generate text. Please try again." self.output_text.update(default_text) else: # Update the text output self.output_text.update(generated_text) # Hide the progress bar self.progress_bar.update(100) @classmethod def clear_inputs(cls): """ This method clears input and output textboxes on the interface. """ # Clear input text cls.input_text.empty() # Clear output text and hide copy button cls.output_text.empty() cls.copy_button.hide() @classmethod def copy_output_text(cls): """ This method copies the output text to the clipboard for easier use. """ pyperclip.copy(cls.output_text.value) def launch_interface(self): """ This method launches the interface for the TextGenerator. """ # Specify button click actions self.clear_button.set_click_handler(self.clear_inputs) self.copy_button.set_click_handler(self.copy_output_text) # Launch the interface self.interface.launch() if __name__ == "__main__": MODEL = "openai/gpt-2" text_generator = TextGenerator(model=MODEL) text_generator.launch_interface()