sagar007 commited on
Commit
bdc217e
·
verified ·
1 Parent(s): c78be87

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -60
app.py CHANGED
@@ -153,69 +153,48 @@ def generate_text(prompt, max_length=100, temperature=0.7, top_k=50):
153
 
154
  # Gradio interface
155
  def gradio_generate(prompt, max_length, temperature, top_k):
156
- return generate_text(prompt, max_length, temperature, top_k)
 
 
 
 
157
 
158
  # Custom CSS for the animation effect
159
- custom_css = """
160
  <style>
161
- .output-box {
162
- border: 1px solid #e0e0e0;
163
- border-radius: 8px;
164
- padding: 20px;
165
- font-family: Arial, sans-serif;
166
- line-height: 1.6;
167
- height: 300px;
168
- overflow-y: auto;
169
- background-color: #f9f9f9;
170
- }
171
- .blinking-cursor {
172
- display: inline-block;
173
- width: 10px;
174
- height: 20px;
175
- background-color: #333;
176
- animation: blink 0.7s infinite;
177
- }
178
- @keyframes blink {
179
- 0% { opacity: 0; }
180
- 50% { opacity: 1; }
181
- 100% { opacity: 0; }
182
- }
183
  </style>
184
  """
185
 
186
- # JavaScript for the typing animation
187
- js_code = """
188
- function typeText(text, element) {
189
- let index = 0;
190
- element.innerHTML = '';
191
- function type() {
192
- if (index < text.length) {
193
- element.innerHTML += text[index];
194
- index++;
195
- setTimeout(type, 50); // Adjust typing speed here
196
- } else {
197
- element.innerHTML += '<span class="blinking-cursor"></span>';
198
- }
199
- }
200
- type();
201
- }
202
- """
203
-
204
- iface = gr.Interface(
205
- fn=gradio_generate,
206
- inputs=[
207
- gr.Textbox(label="Prompt", placeholder="Enter your prompt here..."),
208
- gr.Slider(minimum=20, maximum=500, value=100, step=1, label="Max Length"),
209
- gr.Slider(minimum=0.1, maximum=1.0, value=0.7, step=0.1, label="Temperature"),
210
- gr.Slider(minimum=1, maximum=100, value=50, step=1, label="Top-k")
211
- ],
212
- outputs=gr.HTML(label="Generated Text"),
213
- title="Animated GPT Text Generator",
214
- description="Enter a prompt and adjust parameters to generate text using a fine-tuned GPT model.",
215
- css=custom_css,
216
- js=js_code,
217
- live=True
218
- )
219
-
220
- # Launch the app
221
- iface.launch()
 
153
 
154
  # Gradio interface
155
  def gradio_generate(prompt, max_length, temperature, top_k):
156
+ output = ""
157
+ for token in generate_text(prompt, max_length, temperature, top_k):
158
+ output += token
159
+ time.sleep(0.05) # Simulate typing effect
160
+ yield output
161
 
162
  # Custom CSS for the animation effect
163
+ css = """
164
  <style>
165
+ body { background-color: #1e1e1e; color: #ffffff; font-family: Arial, sans-serif; }
166
+ .container { max-width: 800px; margin: 0 auto; padding: 20px; }
167
+ .header { text-align: center; margin-bottom: 30px; }
168
+ .chat-box { background-color: #2a2a2a; border-radius: 10px; padding: 20px; margin-bottom: 20px; }
169
+ .user-input { background-color: #3a3a3a; border: none; color: #ffffff; padding: 10px; border-radius: 5px; width: 100%; }
170
+ .generate-btn { background-color: #5465ff; color: white; border: none; padding: 10px 20px; border-radius: 5px; cursor: pointer; }
171
+ .output-box { background-color: #2a2a2a; border-radius: 10px; padding: 20px; margin-top: 20px; min-height: 100px; }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
172
  </style>
173
  """
174
 
175
+ # 6. Gradio App Definition
176
+ with gr.Blocks(css=css) as demo:
177
+ gr.HTML("<div class='header'><h1>🌟 GPT-2 Text Generator</h1></div>")
178
+
179
+ with gr.Row():
180
+ with gr.Column(scale=3):
181
+ prompt = gr.Textbox(placeholder="Enter your prompt here...", label="Prompt", elem_classes="user-input")
182
+ with gr.Column(scale=1):
183
+ generate_btn = gr.Button("Generate", elem_classes="generate-btn")
184
+
185
+ with gr.Row():
186
+ max_length = gr.Slider(minimum=20, maximum=500, value=100, step=1, label="Max Length")
187
+ temperature = gr.Slider(minimum=0.1, maximum=1.0, value=0.7, step=0.1, label="Temperature")
188
+ top_k = gr.Slider(minimum=1, maximum=100, value=50, step=1, label="Top-k")
189
+
190
+ output = gr.Markdown(elem_classes="output-box")
191
+
192
+ generate_btn.click(
193
+ gradio_generate,
194
+ inputs=[prompt, max_length, temperature, top_k],
195
+ outputs=output
196
+ )
197
+
198
+ # 7. Launch the app
199
+ if __name__ == "__main__":
200
+ demo.launch()