Spaces:
Running
Running
update
Browse files
app.py
CHANGED
|
@@ -1104,19 +1104,12 @@ This will help me create a better design for you."""
|
|
| 1104 |
# Deploy to Spaces logic
|
| 1105 |
|
| 1106 |
def wrap_html_in_gradio_app(html_code):
|
| 1107 |
-
#
|
| 1108 |
-
|
| 1109 |
-
# Encode HTML as base64 to avoid quote issues
|
| 1110 |
-
html_bytes = html_code.encode('utf-8')
|
| 1111 |
-
html_b64 = base64.b64encode(html_bytes).decode('utf-8')
|
| 1112 |
-
|
| 1113 |
return (
|
| 1114 |
-
'import gradio as gr\n'
|
| 1115 |
-
'import base64\n\n'
|
| 1116 |
'def show_html():\n'
|
| 1117 |
-
f'
|
| 1118 |
-
' html_code = base64.b64decode(html_b64).decode("utf-8")\n'
|
| 1119 |
-
' return html_code\n\n'
|
| 1120 |
'demo = gr.Interface(fn=show_html, inputs=None, outputs=gr.HTML())\n\n'
|
| 1121 |
'if __name__ == "__main__":\n'
|
| 1122 |
' demo.launch()\n'
|
|
@@ -1224,7 +1217,7 @@ with gr.Blocks(
|
|
| 1224 |
label="app name (e.g. my-cool-app)",
|
| 1225 |
placeholder="Enter your app name",
|
| 1226 |
lines=1,
|
| 1227 |
-
visible=
|
| 1228 |
)
|
| 1229 |
sdk_choices = [
|
| 1230 |
("Gradio (Python)", "gradio"),
|
|
@@ -1235,9 +1228,9 @@ with gr.Blocks(
|
|
| 1235 |
choices=[x[0] for x in sdk_choices],
|
| 1236 |
value="Static (HTML)",
|
| 1237 |
label="App SDK",
|
| 1238 |
-
visible=
|
| 1239 |
)
|
| 1240 |
-
deploy_btn = gr.Button("π Deploy App", variant="primary", visible=
|
| 1241 |
deploy_status = gr.Markdown(visible=False, label="Deploy status")
|
| 1242 |
# --- End move ---
|
| 1243 |
search_toggle = gr.Checkbox(
|
|
@@ -1326,26 +1319,17 @@ with gr.Blocks(
|
|
| 1326 |
else:
|
| 1327 |
return "<div style='padding:1em;color:#888;text-align:center;'>Preview is only available for HTML. Please download your code using the download button above.</div>"
|
| 1328 |
|
| 1329 |
-
def show_deploy_components(*args):
|
| 1330 |
-
return [gr.Textbox(visible=True), gr.Dropdown(visible=True), gr.Button(visible=True), gr.Markdown(visible=True)]
|
| 1331 |
|
| 1332 |
-
def hide_deploy_components(*args):
|
| 1333 |
-
return [gr.Textbox(visible=False), gr.Dropdown(visible=False), gr.Button(visible=False), gr.Markdown(visible=False)]
|
| 1334 |
|
| 1335 |
btn.click(
|
| 1336 |
generation_code,
|
| 1337 |
inputs=[input, image_input, file_input, website_url_input, setting, history, current_model, search_toggle, language_dropdown, provider_state],
|
| 1338 |
outputs=[code_output, history, sandbox, history_output]
|
| 1339 |
-
).then(
|
| 1340 |
-
show_deploy_components,
|
| 1341 |
-
None,
|
| 1342 |
-
[space_name_input, sdk_dropdown, deploy_btn, deploy_status]
|
| 1343 |
)
|
| 1344 |
# Update preview when code or language changes
|
| 1345 |
code_output.change(preview_logic, inputs=[code_output, language_dropdown], outputs=sandbox)
|
| 1346 |
language_dropdown.change(preview_logic, inputs=[code_output, language_dropdown], outputs=sandbox)
|
| 1347 |
clear_btn.click(clear_history, outputs=[history, history_output, file_input, website_url_input])
|
| 1348 |
-
clear_btn.click(hide_deploy_components, None, [space_name_input, sdk_dropdown, deploy_btn, deploy_status])
|
| 1349 |
|
| 1350 |
# Deploy to Spaces logic
|
| 1351 |
|
|
@@ -1358,8 +1342,6 @@ with gr.Blocks(
|
|
| 1358 |
):
|
| 1359 |
if not code or not code.strip():
|
| 1360 |
return gr.update(value="No code to deploy.", visible=True)
|
| 1361 |
-
if not space_name or not space_name.strip():
|
| 1362 |
-
return gr.update(value="Please enter a valid app name.", visible=True)
|
| 1363 |
if profile is None or token is None:
|
| 1364 |
return gr.update(value="Please log in with your Hugging Face account to deploy to your own Space. Otherwise, use the default deploy (opens in new tab).", visible=True)
|
| 1365 |
username = profile.username
|
|
@@ -1371,12 +1353,9 @@ with gr.Blocks(
|
|
| 1371 |
"Static (HTML)": "static"
|
| 1372 |
}
|
| 1373 |
sdk = sdk_map.get(sdk_name, "gradio")
|
| 1374 |
-
if not sdk:
|
| 1375 |
-
return gr.update(value="Invalid SDK selection. Please choose a valid SDK.", visible=True)
|
| 1376 |
api = HfApi(token=token.token)
|
| 1377 |
# Create the Space if it doesn't exist
|
| 1378 |
try:
|
| 1379 |
-
print(f"Creating Space {repo_id} with SDK {sdk}")
|
| 1380 |
api.create_repo(
|
| 1381 |
repo_id=repo_id, # e.g. username/space_name
|
| 1382 |
repo_type="space",
|
|
@@ -1384,29 +1363,11 @@ with gr.Blocks(
|
|
| 1384 |
exist_ok=True # Don't error if it already exists
|
| 1385 |
)
|
| 1386 |
except Exception as e:
|
| 1387 |
-
print(f"Error creating Space: {e}")
|
| 1388 |
return gr.update(value=f"Error creating Space: {e}", visible=True)
|
| 1389 |
# Save code to a temporary file
|
| 1390 |
if sdk == "static":
|
| 1391 |
file_name = "index.html"
|
| 1392 |
-
|
| 1393 |
-
if not code.strip().startswith('<!DOCTYPE html>') and not code.strip().startswith('<html'):
|
| 1394 |
-
# Wrap in basic HTML structure if not already present
|
| 1395 |
-
code = f"""<!DOCTYPE html>
|
| 1396 |
-
<html lang="en">
|
| 1397 |
-
<head>
|
| 1398 |
-
<meta charset="UTF-8">
|
| 1399 |
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 1400 |
-
<title>Generated App</title>
|
| 1401 |
-
</head>
|
| 1402 |
-
<body>
|
| 1403 |
-
{code}
|
| 1404 |
-
</body>
|
| 1405 |
-
</html>"""
|
| 1406 |
-
with tempfile.NamedTemporaryFile("w", suffix=".html", delete=False, encoding='utf-8') as f:
|
| 1407 |
-
f.write(code)
|
| 1408 |
-
temp_path = f.name
|
| 1409 |
-
files_to_upload = [(temp_path, file_name)]
|
| 1410 |
elif sdk == "docker": # Streamlit (Python)
|
| 1411 |
import shutil
|
| 1412 |
import os
|
|
@@ -1485,43 +1446,56 @@ If you have any questions, checkout our [documentation](https://docs.streamlit.i
|
|
| 1485 |
# Check if the code is already Python/Gradio code or HTML
|
| 1486 |
if code.strip().startswith('import gradio') or code.strip().startswith('import gr') or 'gradio' in code.lower():
|
| 1487 |
# Code is already Python/Gradio, use it directly
|
| 1488 |
-
|
| 1489 |
else:
|
| 1490 |
# Code is HTML, wrap it in a Gradio app structure
|
| 1491 |
-
|
| 1492 |
with tempfile.NamedTemporaryFile("w", suffix=".py", delete=False, encoding='utf-8') as f:
|
| 1493 |
-
f.write(
|
| 1494 |
temp_path = f.name
|
| 1495 |
files_to_upload = [(temp_path, file_name)]
|
| 1496 |
-
# Upload
|
| 1497 |
try:
|
| 1498 |
-
|
| 1499 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1500 |
api.upload_file(
|
| 1501 |
-
path_or_fileobj=
|
| 1502 |
-
path_in_repo=
|
| 1503 |
repo_id=repo_id,
|
| 1504 |
repo_type="space"
|
| 1505 |
)
|
| 1506 |
space_url = f"https://huggingface.co/spaces/{repo_id}"
|
| 1507 |
return gr.update(value=f"β
Deployed! [Open your Space here]({space_url})", visible=True)
|
| 1508 |
except Exception as e:
|
| 1509 |
-
print(f"Deployment error: {e}")
|
| 1510 |
return gr.update(value=f"Error uploading file: {e}", visible=True)
|
| 1511 |
finally:
|
| 1512 |
# Clean up temporary files
|
| 1513 |
import os
|
| 1514 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1515 |
try:
|
| 1516 |
-
if os.path.exists(
|
| 1517 |
-
os.unlink(
|
| 1518 |
except Exception as cleanup_error:
|
| 1519 |
-
print(f"Warning: Could not clean up {
|
| 1520 |
|
| 1521 |
# Connect the deploy button to the new function
|
| 1522 |
deploy_btn.click(
|
| 1523 |
deploy_to_user_space,
|
| 1524 |
-
inputs=[code_output, space_name_input, sdk_dropdown],
|
| 1525 |
outputs=deploy_status
|
| 1526 |
)
|
| 1527 |
# Keep the old deploy method as fallback (if not logged in, user can still use the old method)
|
|
|
|
| 1104 |
# Deploy to Spaces logic
|
| 1105 |
|
| 1106 |
def wrap_html_in_gradio_app(html_code):
|
| 1107 |
+
# Escape triple quotes for safe embedding
|
| 1108 |
+
safe_html = html_code.replace('"""', r'\"\"\"')
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1109 |
return (
|
| 1110 |
+
'import gradio as gr\n\n'
|
|
|
|
| 1111 |
'def show_html():\n'
|
| 1112 |
+
f' return """{safe_html}"""\n\n'
|
|
|
|
|
|
|
| 1113 |
'demo = gr.Interface(fn=show_html, inputs=None, outputs=gr.HTML())\n\n'
|
| 1114 |
'if __name__ == "__main__":\n'
|
| 1115 |
' demo.launch()\n'
|
|
|
|
| 1217 |
label="app name (e.g. my-cool-app)",
|
| 1218 |
placeholder="Enter your app name",
|
| 1219 |
lines=1,
|
| 1220 |
+
visible=True
|
| 1221 |
)
|
| 1222 |
sdk_choices = [
|
| 1223 |
("Gradio (Python)", "gradio"),
|
|
|
|
| 1228 |
choices=[x[0] for x in sdk_choices],
|
| 1229 |
value="Static (HTML)",
|
| 1230 |
label="App SDK",
|
| 1231 |
+
visible=True
|
| 1232 |
)
|
| 1233 |
+
deploy_btn = gr.Button("π Deploy App", variant="primary", visible=True)
|
| 1234 |
deploy_status = gr.Markdown(visible=False, label="Deploy status")
|
| 1235 |
# --- End move ---
|
| 1236 |
search_toggle = gr.Checkbox(
|
|
|
|
| 1319 |
else:
|
| 1320 |
return "<div style='padding:1em;color:#888;text-align:center;'>Preview is only available for HTML. Please download your code using the download button above.</div>"
|
| 1321 |
|
|
|
|
|
|
|
| 1322 |
|
|
|
|
|
|
|
| 1323 |
|
| 1324 |
btn.click(
|
| 1325 |
generation_code,
|
| 1326 |
inputs=[input, image_input, file_input, website_url_input, setting, history, current_model, search_toggle, language_dropdown, provider_state],
|
| 1327 |
outputs=[code_output, history, sandbox, history_output]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1328 |
)
|
| 1329 |
# Update preview when code or language changes
|
| 1330 |
code_output.change(preview_logic, inputs=[code_output, language_dropdown], outputs=sandbox)
|
| 1331 |
language_dropdown.change(preview_logic, inputs=[code_output, language_dropdown], outputs=sandbox)
|
| 1332 |
clear_btn.click(clear_history, outputs=[history, history_output, file_input, website_url_input])
|
|
|
|
| 1333 |
|
| 1334 |
# Deploy to Spaces logic
|
| 1335 |
|
|
|
|
| 1342 |
):
|
| 1343 |
if not code or not code.strip():
|
| 1344 |
return gr.update(value="No code to deploy.", visible=True)
|
|
|
|
|
|
|
| 1345 |
if profile is None or token is None:
|
| 1346 |
return gr.update(value="Please log in with your Hugging Face account to deploy to your own Space. Otherwise, use the default deploy (opens in new tab).", visible=True)
|
| 1347 |
username = profile.username
|
|
|
|
| 1353 |
"Static (HTML)": "static"
|
| 1354 |
}
|
| 1355 |
sdk = sdk_map.get(sdk_name, "gradio")
|
|
|
|
|
|
|
| 1356 |
api = HfApi(token=token.token)
|
| 1357 |
# Create the Space if it doesn't exist
|
| 1358 |
try:
|
|
|
|
| 1359 |
api.create_repo(
|
| 1360 |
repo_id=repo_id, # e.g. username/space_name
|
| 1361 |
repo_type="space",
|
|
|
|
| 1363 |
exist_ok=True # Don't error if it already exists
|
| 1364 |
)
|
| 1365 |
except Exception as e:
|
|
|
|
| 1366 |
return gr.update(value=f"Error creating Space: {e}", visible=True)
|
| 1367 |
# Save code to a temporary file
|
| 1368 |
if sdk == "static":
|
| 1369 |
file_name = "index.html"
|
| 1370 |
+
app_code = code
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1371 |
elif sdk == "docker": # Streamlit (Python)
|
| 1372 |
import shutil
|
| 1373 |
import os
|
|
|
|
| 1446 |
# Check if the code is already Python/Gradio code or HTML
|
| 1447 |
if code.strip().startswith('import gradio') or code.strip().startswith('import gr') or 'gradio' in code.lower():
|
| 1448 |
# Code is already Python/Gradio, use it directly
|
| 1449 |
+
app_code = code
|
| 1450 |
else:
|
| 1451 |
# Code is HTML, wrap it in a Gradio app structure
|
| 1452 |
+
app_code = wrap_html_in_gradio_app(code)
|
| 1453 |
with tempfile.NamedTemporaryFile("w", suffix=".py", delete=False, encoding='utf-8') as f:
|
| 1454 |
+
f.write(app_code)
|
| 1455 |
temp_path = f.name
|
| 1456 |
files_to_upload = [(temp_path, file_name)]
|
| 1457 |
+
# Upload the file(s)
|
| 1458 |
try:
|
| 1459 |
+
if sdk == "docker": # Streamlit - upload multiple files
|
| 1460 |
+
for local_path, repo_path in files_to_upload:
|
| 1461 |
+
api.upload_file(
|
| 1462 |
+
path_or_fileobj=local_path,
|
| 1463 |
+
path_in_repo=repo_path,
|
| 1464 |
+
repo_id=repo_id,
|
| 1465 |
+
repo_type="space"
|
| 1466 |
+
)
|
| 1467 |
+
else: # Gradio or Static - upload single file
|
| 1468 |
api.upload_file(
|
| 1469 |
+
path_or_fileobj=temp_path,
|
| 1470 |
+
path_in_repo=file_name,
|
| 1471 |
repo_id=repo_id,
|
| 1472 |
repo_type="space"
|
| 1473 |
)
|
| 1474 |
space_url = f"https://huggingface.co/spaces/{repo_id}"
|
| 1475 |
return gr.update(value=f"β
Deployed! [Open your Space here]({space_url})", visible=True)
|
| 1476 |
except Exception as e:
|
|
|
|
| 1477 |
return gr.update(value=f"Error uploading file: {e}", visible=True)
|
| 1478 |
finally:
|
| 1479 |
# Clean up temporary files
|
| 1480 |
import os
|
| 1481 |
+
if sdk == "docker": # Streamlit - clean up multiple files
|
| 1482 |
+
for local_path, _ in files_to_upload:
|
| 1483 |
+
try:
|
| 1484 |
+
if os.path.exists(local_path):
|
| 1485 |
+
os.unlink(local_path)
|
| 1486 |
+
except Exception as cleanup_error:
|
| 1487 |
+
print(f"Warning: Could not clean up {local_path}: {cleanup_error}")
|
| 1488 |
+
else: # Gradio or Static - clean up single file
|
| 1489 |
try:
|
| 1490 |
+
if os.path.exists(temp_path):
|
| 1491 |
+
os.unlink(temp_path)
|
| 1492 |
except Exception as cleanup_error:
|
| 1493 |
+
print(f"Warning: Could not clean up {temp_path}: {cleanup_error}")
|
| 1494 |
|
| 1495 |
# Connect the deploy button to the new function
|
| 1496 |
deploy_btn.click(
|
| 1497 |
deploy_to_user_space,
|
| 1498 |
+
inputs=[code_output, space_name_input, sdk_dropdown, login_button, login_button],
|
| 1499 |
outputs=deploy_status
|
| 1500 |
)
|
| 1501 |
# Keep the old deploy method as fallback (if not logged in, user can still use the old method)
|