Spaces:
Sleeping
Sleeping
RahulChhatbar
commited on
Commit
•
26958c7
1
Parent(s):
5202c0e
test.py changes
Browse files
app.py
CHANGED
@@ -3,9 +3,11 @@ from openai import OpenAI
|
|
3 |
from transformers import pipeline
|
4 |
import os
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
|
|
|
|
9 |
|
10 |
def local_generate_completion(prompt, max_tokens, temperature, repetition_penalty, top_p):
|
11 |
prompt = prompt.strip()
|
@@ -60,143 +62,146 @@ def clear_fields():
|
|
60 |
def update_prompt(selected_example):
|
61 |
return selected_example, ""
|
62 |
|
63 |
-
js = """
|
64 |
-
function createGradioAnimation() {
|
65 |
-
var container = document.createElement('div');
|
66 |
-
container.id = 'gradio-animation';
|
67 |
-
container.style.textAlign = 'center';
|
68 |
-
container.style.marginBottom = '20px';
|
69 |
-
|
70 |
-
// Create a container for the entire line
|
71 |
-
var lineContainer = document.createElement('div');
|
72 |
-
lineContainer.style.fontSize = '2.5em';
|
73 |
-
lineContainer.style.fontWeight = 'bold';
|
74 |
-
|
75 |
-
// "Welcome to" normal text
|
76 |
-
var welcomeText = document.createElement('span');
|
77 |
-
welcomeText.innerText = 'Welcome to, ';
|
78 |
-
welcomeText.style.fontWeight = 'normal'; // Keep "Welcome to" normal
|
79 |
-
welcomeText.style.fontSize = '0.65em'; // Reduce font size
|
80 |
-
lineContainer.appendChild(welcomeText);
|
81 |
-
|
82 |
-
// Fancy "TextGenPro" text
|
83 |
-
var fancyText = document.createElement('span');
|
84 |
-
fancyText.style.color = '#eab440';
|
85 |
-
fancyText.style.display = 'inline-block';
|
86 |
-
fancyText.style.textShadow = '0 1px #0267c1, -1px 0 #0093f5, -1px 2px #0267c1, -2px 1px #0093f5, -2px 3px #0267c1, -3px 2px #0093f5, -3px 4px #0267c1, -4px 3px #0093f5, -4px 5px #0267c1, -5px 4px #0093f5, -5px 6px #0267c1, -6px 5px #0093f5, -6px 7px #0267c1, 2px 2px 2px rgba(206, 89, 55, 0)';
|
87 |
-
|
88 |
-
var text = 'TextGenPro';
|
89 |
-
for (var i = 0; i < text.length; i++) {
|
90 |
-
(function(i) {
|
91 |
-
setTimeout(function() {
|
92 |
-
var letter = document.createElement('span');
|
93 |
-
letter.style.opacity = '0';
|
94 |
-
letter.style.transition = 'opacity 0.5s, transform 0.5s'; // Animation for opacity and transform
|
95 |
-
letter.innerText = text[i];
|
96 |
-
|
97 |
-
// Set initial scale and rotation
|
98 |
-
letter.style.transform = 'scale(0.5) rotate(-10deg)';
|
99 |
-
|
100 |
-
fancyText.appendChild(letter);
|
101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
setTimeout(function() {
|
103 |
-
letter
|
104 |
-
letter.style.
|
105 |
-
|
106 |
-
|
107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
prompt_input
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
repetition_penalty_slider_local = gr.Slider(minimum=1, maximum=5, value=1.5, step=0.1, label="Repetition Penalty")
|
141 |
-
max_tokens_slider_local = gr.Slider(minimum=1, maximum=4000, value=250, step=1, label="Max Tokens")
|
142 |
-
top_p_slider_local = gr.Slider(minimum=0, maximum=1, value=1, step=0.01, label="Top-p (nucleus sampling)")
|
143 |
-
|
144 |
-
with gr.Row():
|
145 |
-
api_generate_button = gr.Button("API Model Text Generation")
|
146 |
-
local_generate_button = gr.Button("Local Model Text Generation")
|
147 |
-
append_button = gr.Button("Append Completion to Prompt")
|
148 |
-
clear_button = gr.Button("Clear All Fields")
|
149 |
-
|
150 |
-
with gr.Row():
|
151 |
-
stop_button = gr.Button("Stop Generation", elem_id="stop-button")
|
152 |
-
|
153 |
-
example_prompts = [
|
154 |
-
"Select Example Prompt",
|
155 |
-
"As the clock struck midnight, she discovered a hidden message that said",
|
156 |
-
"She had always dreamed of traveling to Paris, but her first stop was",
|
157 |
-
"The new cafe in town serves a special latte that tastes like",
|
158 |
-
"He promised to finish the project by Friday, but something unexpected happened",
|
159 |
-
"After months of searching, the treasure map led them to a hidden cave with",
|
160 |
-
"As she opened the old book, a dusty letter fell out, saying"
|
161 |
-
]
|
162 |
-
|
163 |
-
examples_dropdown = gr.Dropdown(
|
164 |
-
label="Example Prompts",
|
165 |
-
choices=example_prompts[1:],
|
166 |
-
value=example_prompts[0],
|
167 |
-
allow_custom_value=True
|
168 |
-
)
|
169 |
-
|
170 |
-
examples_dropdown.change(
|
171 |
-
update_prompt,
|
172 |
-
inputs=[examples_dropdown],
|
173 |
-
outputs=[prompt_input, output_text]
|
174 |
-
)
|
175 |
-
|
176 |
-
api_generation_event = api_generate_button.click(
|
177 |
-
lambda prompt, temperature, repetition_penalty, max_tokens, stop_phrase, top_p: api_generate_completion(prompt, temperature, repetition_penalty, max_tokens, stop_phrase, top_p, api_key),
|
178 |
-
inputs=[prompt_input, temperature_slider_api, repetition_penalty_slider_api, max_tokens_slider_api, stop_phrase_input_api, top_p_slider_api],
|
179 |
-
outputs=output_text
|
180 |
-
)
|
181 |
-
|
182 |
-
local_generation_event = local_generate_button.click(
|
183 |
-
local_generate_completion,
|
184 |
-
inputs=[prompt_input, max_tokens_slider_local, temperature_slider_local, repetition_penalty_slider_local, top_p_slider_local],
|
185 |
-
outputs=output_text
|
186 |
-
)
|
187 |
-
|
188 |
-
append_button.click(
|
189 |
-
append_completion,
|
190 |
-
inputs=[prompt_input, output_text],
|
191 |
-
outputs=[prompt_input, output_text]
|
192 |
-
)
|
193 |
-
|
194 |
-
clear_button.click(
|
195 |
-
clear_fields,
|
196 |
-
outputs=[prompt_input, output_text]
|
197 |
-
)
|
198 |
-
|
199 |
-
stop_button.click(None, None, None, cancels=[api_generation_event, local_generation_event])
|
200 |
|
201 |
-
if __name__ == "__main__":
|
202 |
iface.launch(share=False)
|
|
|
3 |
from transformers import pipeline
|
4 |
import os
|
5 |
|
6 |
+
def check_api_key():
|
7 |
+
api_key = os.environ.get('HYPERBOLIC_API_KEY')
|
8 |
+
if api_key is None:
|
9 |
+
raise ValueError("Please set the HYPERBOLIC_API_KEY environment variable.")
|
10 |
+
return api_key
|
11 |
|
12 |
def local_generate_completion(prompt, max_tokens, temperature, repetition_penalty, top_p):
|
13 |
prompt = prompt.strip()
|
|
|
62 |
def update_prompt(selected_example):
|
63 |
return selected_example, ""
|
64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
+
if __name__ == "__main__":
|
67 |
+
api_key = check_api_key()
|
68 |
+
|
69 |
+
js = """
|
70 |
+
function createGradioAnimation() {
|
71 |
+
var container = document.createElement('div');
|
72 |
+
container.id = 'gradio-animation';
|
73 |
+
container.style.textAlign = 'center';
|
74 |
+
container.style.marginBottom = '20px';
|
75 |
+
|
76 |
+
// Create a container for the entire line
|
77 |
+
var lineContainer = document.createElement('div');
|
78 |
+
lineContainer.style.fontSize = '2.5em';
|
79 |
+
lineContainer.style.fontWeight = 'bold';
|
80 |
+
|
81 |
+
// "Welcome to" normal text
|
82 |
+
var welcomeText = document.createElement('span');
|
83 |
+
welcomeText.innerText = 'Welcome to, ';
|
84 |
+
welcomeText.style.fontWeight = 'normal'; // Keep "Welcome to" normal
|
85 |
+
welcomeText.style.fontSize = '0.65em'; // Reduce font size
|
86 |
+
lineContainer.appendChild(welcomeText);
|
87 |
+
|
88 |
+
// Fancy "TextGenPro" text
|
89 |
+
var fancyText = document.createElement('span');
|
90 |
+
fancyText.style.color = '#eab440';
|
91 |
+
fancyText.style.display = 'inline-block';
|
92 |
+
fancyText.style.textShadow = '0 1px #0267c1, -1px 0 #0093f5, -1px 2px #0267c1, -2px 1px #0093f5, -2px 3px #0267c1, -3px 2px #0093f5, -3px 4px #0267c1, -4px 3px #0093f5, -4px 5px #0267c1, -5px 4px #0093f5, -5px 6px #0267c1, -6px 5px #0093f5, -6px 7px #0267c1, 2px 2px 2px rgba(206, 89, 55, 0)';
|
93 |
+
|
94 |
+
var text = 'TextGenPro';
|
95 |
+
for (var i = 0; i < text.length; i++) {
|
96 |
+
(function(i) {
|
97 |
setTimeout(function() {
|
98 |
+
var letter = document.createElement('span');
|
99 |
+
letter.style.opacity = '0';
|
100 |
+
letter.style.transition = 'opacity 0.5s, transform 0.5s'; // Animation for opacity and transform
|
101 |
+
letter.innerText = text[i];
|
102 |
+
|
103 |
+
// Set initial scale and rotation
|
104 |
+
letter.style.transform = 'scale(0.5) rotate(-10deg)';
|
105 |
+
|
106 |
+
fancyText.appendChild(letter);
|
107 |
+
|
108 |
+
setTimeout(function() {
|
109 |
+
letter.style.opacity = '1';
|
110 |
+
letter.style.transform = 'scale(1) rotate(0deg)'; // Final scaling and rotation
|
111 |
+
}, 50);
|
112 |
+
}, i * 150); // Slight delay for each letter
|
113 |
+
})(i);
|
114 |
+
}
|
115 |
+
|
116 |
+
lineContainer.appendChild(fancyText);
|
117 |
+
|
118 |
+
container.appendChild(lineContainer);
|
119 |
+
|
120 |
+
var gradioContainer = document.querySelector('.gradio-container');
|
121 |
+
gradioContainer.insertBefore(container, gradioContainer.firstChild);
|
122 |
+
|
123 |
+
return 'Fancy single-line animation created';
|
124 |
}
|
125 |
+
"""
|
126 |
+
|
127 |
+
with gr.Blocks(theme=gr.themes.Soft(), css="#stop-button {background-color: red; color: white;}", js=js) as iface:
|
128 |
+
with gr.Row():
|
129 |
+
with gr.Column():
|
130 |
+
prompt_input = gr.Textbox(label="Prompt", value="Today is a beautiful day for", lines=10)
|
131 |
+
with gr.Column():
|
132 |
+
output_text = gr.Textbox(label="Generated Completion", lines=10)
|
133 |
+
|
134 |
+
with gr.Row():
|
135 |
+
with gr.Column():
|
136 |
+
with gr.Accordion("API Model Parameters", open=False):
|
137 |
+
temperature_slider_api = gr.Slider(minimum=0, maximum=1, value=0.7, step=0.1, label="Temperature")
|
138 |
+
repetition_penalty_slider_api = gr.Slider(minimum=1, maximum=5, value=1.5, step=0.1, label="Repetition Penalty")
|
139 |
+
max_tokens_slider_api = gr.Slider(minimum=1, maximum=4000, value=250, step=1, label="Max Tokens")
|
140 |
+
top_p_slider_api = gr.Slider(minimum=0, maximum=1, value=1, step=0.01, label="Top-p (nucleus sampling)")
|
141 |
+
stop_phrase_input_api = gr.Textbox(label="Stop Phrase", placeholder="Enter stop phrase (optional)")
|
142 |
+
|
143 |
+
with gr.Column():
|
144 |
+
with gr.Accordion("Local Model Parameters", open=False):
|
145 |
+
temperature_slider_local = gr.Slider(minimum=0, maximum=1, value=0.7, step=0.1, label="Temperature")
|
146 |
+
repetition_penalty_slider_local = gr.Slider(minimum=1, maximum=5, value=1.5, step=0.1, label="Repetition Penalty")
|
147 |
+
max_tokens_slider_local = gr.Slider(minimum=1, maximum=4000, value=250, step=1, label="Max Tokens")
|
148 |
+
top_p_slider_local = gr.Slider(minimum=0, maximum=1, value=1, step=0.01, label="Top-p (nucleus sampling)")
|
149 |
+
|
150 |
+
with gr.Row():
|
151 |
+
api_generate_button = gr.Button("API Model Text Generation")
|
152 |
+
local_generate_button = gr.Button("Local Model Text Generation")
|
153 |
+
append_button = gr.Button("Append Completion to Prompt")
|
154 |
+
clear_button = gr.Button("Clear All Fields")
|
155 |
+
|
156 |
+
with gr.Row():
|
157 |
+
stop_button = gr.Button("Stop Generation", elem_id="stop-button")
|
158 |
+
|
159 |
+
example_prompts = [
|
160 |
+
"Select Example Prompt",
|
161 |
+
"As the clock struck midnight, she discovered a hidden message that said",
|
162 |
+
"She had always dreamed of traveling to Paris, but her first stop was",
|
163 |
+
"The new cafe in town serves a special latte that tastes like",
|
164 |
+
"He promised to finish the project by Friday, but something unexpected happened",
|
165 |
+
"After months of searching, the treasure map led them to a hidden cave with",
|
166 |
+
"As she opened the old book, a dusty letter fell out, saying"
|
167 |
+
]
|
168 |
+
|
169 |
+
examples_dropdown = gr.Dropdown(
|
170 |
+
label="Example Prompts",
|
171 |
+
choices=example_prompts[1:],
|
172 |
+
value=example_prompts[0],
|
173 |
+
allow_custom_value=True
|
174 |
+
)
|
175 |
|
176 |
+
examples_dropdown.change(
|
177 |
+
update_prompt,
|
178 |
+
inputs=[examples_dropdown],
|
179 |
+
outputs=[prompt_input, output_text]
|
180 |
+
)
|
181 |
+
|
182 |
+
api_generation_event = api_generate_button.click(
|
183 |
+
lambda prompt, temperature, repetition_penalty, max_tokens, stop_phrase, top_p: api_generate_completion(prompt, temperature, repetition_penalty, max_tokens, stop_phrase, top_p, api_key),
|
184 |
+
inputs=[prompt_input, temperature_slider_api, repetition_penalty_slider_api, max_tokens_slider_api, stop_phrase_input_api, top_p_slider_api],
|
185 |
+
outputs=output_text
|
186 |
+
)
|
187 |
+
|
188 |
+
local_generation_event = local_generate_button.click(
|
189 |
+
local_generate_completion,
|
190 |
+
inputs=[prompt_input, max_tokens_slider_local, temperature_slider_local, repetition_penalty_slider_local, top_p_slider_local],
|
191 |
+
outputs=output_text
|
192 |
+
)
|
193 |
+
|
194 |
+
append_button.click(
|
195 |
+
append_completion,
|
196 |
+
inputs=[prompt_input, output_text],
|
197 |
+
outputs=[prompt_input, output_text]
|
198 |
+
)
|
199 |
+
|
200 |
+
clear_button.click(
|
201 |
+
clear_fields,
|
202 |
+
outputs=[prompt_input, output_text]
|
203 |
+
)
|
204 |
+
|
205 |
+
stop_button.click(None, None, None, cancels=[api_generation_event, local_generation_event])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
206 |
|
|
|
207 |
iface.launch(share=False)
|
test.py
CHANGED
@@ -2,6 +2,11 @@ import pytest
|
|
2 |
from app import local_generate_completion, append_completion, clear_fields, update_prompt
|
3 |
|
4 |
|
|
|
|
|
|
|
|
|
|
|
5 |
@pytest.mark.timeout(10)
|
6 |
def test_append_completion():
|
7 |
prompt = " Test prompt "
|
|
|
2 |
from app import local_generate_completion, append_completion, clear_fields, update_prompt
|
3 |
|
4 |
|
5 |
+
@pytest.fixture(autouse=True)
|
6 |
+
def set_api_key(monkeypatch):
|
7 |
+
monkeypatch.setenv("HYPERBOLIC_API_KEY", "test_api_key")
|
8 |
+
|
9 |
+
|
10 |
@pytest.mark.timeout(10)
|
11 |
def test_append_completion():
|
12 |
prompt = " Test prompt "
|