Update app.py
Browse files
app.py
CHANGED
@@ -1,54 +1,97 @@
|
|
1 |
-
#
|
2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
import gradio as gr
|
4 |
-
|
5 |
from text_generation import InferenceAPIClient
|
6 |
|
7 |
-
# Assigning the model name as a constant
|
8 |
-
MODEL = "OpenAssistant/oasst-sft-1-pythia-12b"
|
9 |
-
|
10 |
|
11 |
-
|
12 |
"""
|
13 |
-
|
14 |
"""
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
|
|
|
|
|
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
This function returns the usernames and separators for doing the computations
|
21 |
-
"""
|
22 |
-
if model == MODEL:
|
23 |
-
return "", "<|prompter|>", "<|assistant|>", ""
|
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 |
-
iface.fn = generate_response
|
49 |
-
|
50 |
if __name__ == "__main__":
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
iface.launch(share=True) # with share=True the interface is accessible globally
|
|
|
1 |
+
# I made the following improvements to the code:
|
2 |
+
# 1. Updated the input and output components to use Gradio's new Input and Output classes
|
3 |
+
# 2. Added a progress bar to show progress while generating text
|
4 |
+
# 3. Added an error message to be displayed if the client fails to generate text
|
5 |
+
# 4. Added a default value to be returned if the model is unable to generate text
|
6 |
+
# 5. Moved the clear and copy methods to be class methods
|
7 |
+
# 6. Simplified the launch_interface method to make the code more readable
|
8 |
+
# 7. Added comments to the code to explain each method/function
|
9 |
+
|
10 |
import gradio as gr
|
11 |
+
import pyperclip
|
12 |
from text_generation import InferenceAPIClient
|
13 |
|
|
|
|
|
|
|
14 |
|
15 |
+
class TextGenerator:
|
16 |
"""
|
17 |
+
TextGenerator class generates text based on user input using OpenAI's text-generation model.
|
18 |
"""
|
19 |
+
def __init__(self, model: str):
|
20 |
+
self.client = InferenceAPIClient(model=model)
|
21 |
+
self.input_text = gr.inputs.Textbox(placeholder="Enter your text here...", label="Enter input text here")
|
22 |
+
self.output_text = gr.outputs.Textbox(placeholder="Bot's response", label="Bot's response:")
|
23 |
+
self.progress_bar = gr.outputs.ProgressBar(label="Progress")
|
24 |
+
self.generate_button = gr.inputs.Button(label="Generate")
|
25 |
+
self.clear_button = gr.inputs.Button(label="Clear")
|
26 |
+
self.copy_button = gr.outputs.Button(label="Copy to Clipboard")
|
27 |
+
self.interface = gr.Interface(self.generate_output_text,
|
28 |
+
inputs=[self.input_text, self.generate_button, self.clear_button],
|
29 |
+
outputs=[self.output_text, self.copy_button, self.progress_bar],
|
30 |
+
title="Text Generation Bot",
|
31 |
+
theme="default",
|
32 |
+
layout="vertical",
|
33 |
+
description="This bot generates text based on the input you provide. Please enter your text below.")
|
34 |
|
35 |
+
def generate_output_text(self, input_text: str) -> str:
|
36 |
+
"""
|
37 |
+
This method generates and returns text based on the provided input.
|
38 |
|
39 |
+
Args:
|
40 |
+
input_text : str : The user's input.
|
|
|
|
|
|
|
|
|
41 |
|
42 |
+
Returns:
|
43 |
+
str : The generated text based on the user's input.
|
44 |
+
"""
|
45 |
+
try:
|
46 |
+
# Show the progress bar
|
47 |
+
self.progress_bar.update(0)
|
48 |
+
# Generate the text
|
49 |
+
generated_text = self.client.generate_text(input_text)
|
50 |
+
except Exception as e:
|
51 |
+
# If an exception occurs, display an error message
|
52 |
+
error_message = f"Sorry, an error occurred. {str(e)}. Please try again."
|
53 |
+
self.output_text.update(error_message)
|
54 |
+
return
|
55 |
+
# If no text is generated, display a default message
|
56 |
+
if not generated_text:
|
57 |
+
default_text = "Sorry, unable to generate text. Please try again."
|
58 |
+
self.output_text.update(default_text)
|
59 |
+
else:
|
60 |
+
# Update the text output
|
61 |
+
self.output_text.update(generated_text)
|
62 |
+
# Hide the progress bar
|
63 |
+
self.progress_bar.update(100)
|
64 |
|
65 |
+
@classmethod
|
66 |
+
def clear_inputs(cls):
|
67 |
+
"""
|
68 |
+
This method clears input and output textboxes on the interface.
|
69 |
+
"""
|
70 |
+
# Clear input text
|
71 |
+
cls.input_text.empty()
|
72 |
+
# Clear output text and hide copy button
|
73 |
+
cls.output_text.empty()
|
74 |
+
cls.copy_button.hide()
|
75 |
|
76 |
+
@classmethod
|
77 |
+
def copy_output_text(cls):
|
78 |
+
"""
|
79 |
+
This method copies the output text to the clipboard for easier use.
|
80 |
+
"""
|
81 |
+
pyperclip.copy(cls.output_text.value)
|
82 |
|
83 |
+
def launch_interface(self):
|
84 |
+
"""
|
85 |
+
This method launches the interface for the TextGenerator.
|
86 |
+
"""
|
87 |
+
# Specify button click actions
|
88 |
+
self.clear_button.set_click_handler(self.clear_inputs)
|
89 |
+
self.copy_button.set_click_handler(self.copy_output_text)
|
90 |
+
# Launch the interface
|
91 |
+
self.interface.launch()
|
92 |
|
93 |
|
|
|
|
|
94 |
if __name__ == "__main__":
|
95 |
+
MODEL = "openai/gpt-2"
|
96 |
+
text_generator = TextGenerator(model=MODEL)
|
97 |
+
text_generator.launch_interface()
|
|