sonyps1928 commited on
Commit
5b97012
Β·
1 Parent(s): cef31a4

update app2

Browse files
Files changed (1) hide show
  1. app.py +109 -71
app.py CHANGED
@@ -111,10 +111,25 @@ def generate_text(prompt, max_length=100, temperature=0.7, top_p=0.9, top_k=50,
111
  print(error_msg)
112
  return error_msg
113
 
114
- # Create Gradio interface with conditional elements
115
- with gr.Blocks(title="πŸ” Secure GPT-2 Generator") as demo:
116
- gr.Markdown("# πŸ” Secure GPT-2 Text Generator")
117
- gr.Markdown("**Security Features**: API Authentication β€’ Rate Limiting β€’ Admin Protection")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
 
119
  # Security status display
120
  security_status = []
@@ -126,17 +141,25 @@ with gr.Blocks(title="πŸ” Secure GPT-2 Generator") as demo:
126
  security_status.append("πŸ‘€ Admin Protection Active")
127
 
128
  if security_status:
129
- gr.Markdown(f"**Active Security**: {' β€’ '.join(security_status)}")
 
 
 
 
 
130
  else:
131
- gr.Markdown("⚠️ **No security features enabled** - running in public mode")
 
 
 
 
132
 
133
  with gr.Row():
134
- with gr.Column():
135
  prompt_input = gr.Textbox(
136
  label="✏️ Text Prompt",
137
  placeholder="Enter your prompt here... (max 1000 chars)",
138
- lines=3,
139
- max_lines=5
140
  )
141
 
142
  # Show API key input only if API_KEY is configured
@@ -144,72 +167,69 @@ with gr.Blocks(title="πŸ” Secure GPT-2 Generator") as demo:
144
  api_key_input = gr.Textbox(
145
  label="πŸ”‘ API Key (Required)",
146
  type="password",
147
- placeholder="Enter your API key...",
148
- info="API authentication is enabled for this Space"
149
  )
 
150
  else:
151
  api_key_input = gr.Textbox(value="", visible=False)
152
- gr.Markdown("πŸ”“ **Public Access**: No API key required")
153
 
154
  with gr.Accordion("βš™οΈ Generation Parameters", open=False):
155
- with gr.Row():
156
- max_length = gr.Slider(
157
- minimum=10,
158
- maximum=200,
159
- value=100,
160
- step=10,
161
- label="πŸ“ Max Length"
162
- )
163
- temperature = gr.Slider(
164
- minimum=0.1,
165
- maximum=2.0,
166
- value=0.7,
167
- step=0.1,
168
- label="🌑️ Temperature"
169
- )
170
-
171
- with gr.Row():
172
- top_p = gr.Slider(
173
- minimum=0.1,
174
- maximum=1.0,
175
- value=0.9,
176
- step=0.1,
177
- label="🎯 Top-p"
178
- )
179
- top_k = gr.Slider(
180
- minimum=1,
181
- maximum=100,
182
- value=50,
183
- step=1,
184
- label="πŸ”’ Top-k"
185
- )
186
 
187
- generate_btn = gr.Button("πŸš€ Generate Text", variant="primary", size="lg")
188
 
189
- with gr.Column():
190
  output_text = gr.Textbox(
191
  label="πŸ“„ Generated Text",
192
  lines=12,
193
- placeholder="Generated text will appear here...",
194
- show_copy_button=True
195
  )
196
 
197
  # Rate limit info
198
  if API_KEY:
199
- gr.Markdown("**Rate Limits**: 100 requests per hour per API key")
200
 
201
  # Examples
202
- gr.Examples(
203
- examples=[
204
- ["Once upon a time in a distant galaxy,"],
205
- ["The future of artificial intelligence is"],
206
- ["In the heart of the ancient forest,"],
207
- ["The detective walked into the room and noticed"],
208
- ["Write a short story about a robot who dreams of"],
209
- ],
210
- inputs=prompt_input,
211
- label="πŸ’‘ Example Prompts"
212
- )
 
213
 
214
  # Connect the generation function
215
  generate_btn.click(
@@ -218,16 +238,34 @@ with gr.Blocks(title="πŸ” Secure GPT-2 Generator") as demo:
218
  outputs=output_text
219
  )
220
 
221
- # Launch with authentication
222
- auth_tuple = None
223
- if ADMIN_PASSWORD:
224
- auth_tuple = ("admin", ADMIN_PASSWORD)
225
- print("πŸ” Admin authentication enabled")
226
-
227
  if __name__ == "__main__":
228
- demo.launch(
229
- auth=auth_tuple,
230
- show_api=True, # Enable API documentation
231
- show_error=True
232
- )
233
- print("πŸš€ Secure GPT-2 Generator is running!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
  print(error_msg)
112
  return error_msg
113
 
114
+ # Create Gradio interface - FIXED VERSION with proper CSS and configuration
115
+ with gr.Blocks(
116
+ title="πŸ” Secure GPT-2 Generator",
117
+ theme=gr.themes.Soft(), # Use a built-in theme to avoid i18n issues
118
+ css="""
119
+ .gradio-container {
120
+ max-width: 1200px !important;
121
+ }
122
+ .security-status {
123
+ background: linear-gradient(45deg, #f0f8ff, #e6f3ff);
124
+ padding: 15px;
125
+ border-radius: 8px;
126
+ margin: 10px 0;
127
+ }
128
+ """
129
+ ) as demo:
130
+
131
+ # Header
132
+ gr.HTML("<h1>πŸ” Secure GPT-2 Text Generator</h1>")
133
 
134
  # Security status display
135
  security_status = []
 
141
  security_status.append("πŸ‘€ Admin Protection Active")
142
 
143
  if security_status:
144
+ gr.HTML(f"""
145
+ <div class="security-status">
146
+ <strong>Active Security Features:</strong><br>
147
+ {' β€’ '.join(security_status)}
148
+ </div>
149
+ """)
150
  else:
151
+ gr.HTML("""
152
+ <div class="security-status">
153
+ ⚠️ <strong>No security features enabled</strong> - running in public mode
154
+ </div>
155
+ """)
156
 
157
  with gr.Row():
158
+ with gr.Column(scale=1):
159
  prompt_input = gr.Textbox(
160
  label="✏️ Text Prompt",
161
  placeholder="Enter your prompt here... (max 1000 chars)",
162
+ lines=3
 
163
  )
164
 
165
  # Show API key input only if API_KEY is configured
 
167
  api_key_input = gr.Textbox(
168
  label="πŸ”‘ API Key (Required)",
169
  type="password",
170
+ placeholder="Enter your API key..."
 
171
  )
172
+ gr.HTML("<p><em>API authentication is enabled for this Space</em></p>")
173
  else:
174
  api_key_input = gr.Textbox(value="", visible=False)
175
+ gr.HTML("<p>πŸ”“ <strong>Public Access:</strong> No API key required</p>")
176
 
177
  with gr.Accordion("βš™οΈ Generation Parameters", open=False):
178
+ max_length = gr.Slider(
179
+ minimum=10,
180
+ maximum=200,
181
+ value=100,
182
+ step=10,
183
+ label="πŸ“ Max Length"
184
+ )
185
+ temperature = gr.Slider(
186
+ minimum=0.1,
187
+ maximum=2.0,
188
+ value=0.7,
189
+ step=0.1,
190
+ label="🌑️ Temperature"
191
+ )
192
+ top_p = gr.Slider(
193
+ minimum=0.1,
194
+ maximum=1.0,
195
+ value=0.9,
196
+ step=0.1,
197
+ label="🎯 Top-p"
198
+ )
199
+ top_k = gr.Slider(
200
+ minimum=1,
201
+ maximum=100,
202
+ value=50,
203
+ step=1,
204
+ label="πŸ”’ Top-k"
205
+ )
 
 
 
206
 
207
+ generate_btn = gr.Button("πŸš€ Generate Text", variant="primary")
208
 
209
+ with gr.Column(scale=1):
210
  output_text = gr.Textbox(
211
  label="πŸ“„ Generated Text",
212
  lines=12,
213
+ placeholder="Generated text will appear here..."
 
214
  )
215
 
216
  # Rate limit info
217
  if API_KEY:
218
+ gr.HTML("<p><strong>Rate Limits:</strong> 100 requests per hour per API key</p>")
219
 
220
  # Examples
221
+ with gr.Row():
222
+ gr.Examples(
223
+ examples=[
224
+ ["Once upon a time in a distant galaxy,"],
225
+ ["The future of artificial intelligence is"],
226
+ ["In the heart of the ancient forest,"],
227
+ ["The detective walked into the room and noticed"],
228
+ ["Write a short story about a robot who dreams of"],
229
+ ],
230
+ inputs=prompt_input,
231
+ label="πŸ’‘ Example Prompts"
232
+ )
233
 
234
  # Connect the generation function
235
  generate_btn.click(
 
238
  outputs=output_text
239
  )
240
 
241
+ # Launch configuration - FIXED VERSION
 
 
 
 
 
242
  if __name__ == "__main__":
243
+ # Determine authentication
244
+ auth_tuple = None
245
+ if ADMIN_PASSWORD:
246
+ auth_tuple = ("admin", ADMIN_PASSWORD)
247
+ print("πŸ” Admin authentication enabled")
248
+
249
+ # Launch with proper configuration for HF Spaces
250
+ try:
251
+ demo.launch(
252
+ auth=auth_tuple,
253
+ server_name="0.0.0.0", # Allow external connections
254
+ server_port=7860, # Standard HF Spaces port
255
+ show_api=True, # Enable API documentation
256
+ show_error=True, # Show detailed errors
257
+ share=False, # Don't create ngrok tunnel (HF handles this)
258
+ inbrowser=False, # Don't try to open browser
259
+ prevent_thread_lock=False,
260
+ ssr_mode=False # Disable SSR to prevent i18n issues
261
+ )
262
+ print("πŸš€ Secure GPT-2 Generator is running!")
263
+ except Exception as e:
264
+ print(f"❌ Launch error: {e}")
265
+ # Fallback launch configuration
266
+ print("πŸ”„ Trying fallback configuration...")
267
+ demo.launch(
268
+ auth=auth_tuple,
269
+ show_api=True,
270
+ ssr_mode=False
271
+ )