sonyps1928
commited on
Commit
Β·
5b97012
1
Parent(s):
cef31a4
update app2
Browse files
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
|
115 |
-
with gr.Blocks(
|
116 |
-
|
117 |
-
gr.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
|
130 |
else:
|
131 |
-
gr.
|
|
|
|
|
|
|
|
|
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.
|
153 |
|
154 |
with gr.Accordion("βοΈ Generation Parameters", open=False):
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
step=1,
|
184 |
-
label="π’ Top-k"
|
185 |
-
)
|
186 |
|
187 |
-
generate_btn = gr.Button("π Generate Text", variant="primary"
|
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.
|
200 |
|
201 |
# Examples
|
202 |
-
gr.
|
203 |
-
|
204 |
-
[
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
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
|
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 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
)
|