Spaces:
Running
Running
update
Browse files
app.py
CHANGED
@@ -1104,12 +1104,19 @@ 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 |
return (
|
1110 |
-
'import gradio as gr\n
|
|
|
1111 |
'def show_html():\n'
|
1112 |
-
f'
|
|
|
|
|
1113 |
'demo = gr.Interface(fn=show_html, inputs=None, outputs=gr.HTML())\n\n'
|
1114 |
'if __name__ == "__main__":\n'
|
1115 |
' demo.launch()\n'
|
@@ -1351,6 +1358,8 @@ with gr.Blocks(
|
|
1351 |
):
|
1352 |
if not code or not code.strip():
|
1353 |
return gr.update(value="No code to deploy.", visible=True)
|
|
|
|
|
1354 |
if profile is None or token is None:
|
1355 |
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)
|
1356 |
username = profile.username
|
@@ -1362,9 +1371,12 @@ with gr.Blocks(
|
|
1362 |
"Static (HTML)": "static"
|
1363 |
}
|
1364 |
sdk = sdk_map.get(sdk_name, "gradio")
|
|
|
|
|
1365 |
api = HfApi(token=token.token)
|
1366 |
# Create the Space if it doesn't exist
|
1367 |
try:
|
|
|
1368 |
api.create_repo(
|
1369 |
repo_id=repo_id, # e.g. username/space_name
|
1370 |
repo_type="space",
|
@@ -1372,11 +1384,26 @@ with gr.Blocks(
|
|
1372 |
exist_ok=True # Don't error if it already exists
|
1373 |
)
|
1374 |
except Exception as e:
|
|
|
1375 |
return gr.update(value=f"Error creating Space: {e}", visible=True)
|
1376 |
# Save code to a temporary file
|
1377 |
if sdk == "static":
|
1378 |
file_name = "index.html"
|
1379 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1380 |
f.write(code)
|
1381 |
temp_path = f.name
|
1382 |
files_to_upload = [(temp_path, file_name)]
|
@@ -1441,7 +1468,7 @@ short_description: Streamlit template space
|
|
1441 |
|
1442 |
Edit `/src/streamlit_app.py` to customize this app to your heart's desire. :heart:
|
1443 |
|
1444 |
-
If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
|
1445 |
'''
|
1446 |
readme_path = os.path.join(temp_dir, "README.md")
|
1447 |
with open(readme_path, "w", encoding="utf-8") as f:
|
@@ -1462,13 +1489,14 @@ If you have any questions, checkout our [documentation](https://docs.streamlit.i
|
|
1462 |
else:
|
1463 |
# Code is HTML, wrap it in a Gradio app structure
|
1464 |
app_py = wrap_html_in_gradio_app(code)
|
1465 |
-
with tempfile.NamedTemporaryFile("w", suffix=".py", delete=False) as f:
|
1466 |
f.write(app_py)
|
1467 |
temp_path = f.name
|
1468 |
files_to_upload = [(temp_path, file_name)]
|
1469 |
# Upload all files
|
1470 |
try:
|
1471 |
for local_path, repo_path in files_to_upload:
|
|
|
1472 |
api.upload_file(
|
1473 |
path_or_fileobj=local_path,
|
1474 |
path_in_repo=repo_path,
|
@@ -1478,7 +1506,17 @@ If you have any questions, checkout our [documentation](https://docs.streamlit.i
|
|
1478 |
space_url = f"https://huggingface.co/spaces/{repo_id}"
|
1479 |
return gr.update(value=f"✅ Deployed! [Open your Space here]({space_url})", visible=True)
|
1480 |
except Exception as e:
|
|
|
1481 |
return gr.update(value=f"Error uploading file: {e}", visible=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1482 |
|
1483 |
# Connect the deploy button to the new function
|
1484 |
deploy_btn.click(
|
|
|
1104 |
# Deploy to Spaces logic
|
1105 |
|
1106 |
def wrap_html_in_gradio_app(html_code):
|
1107 |
+
# Use a more robust approach to handle HTML with quotes
|
1108 |
+
import base64
|
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' html_b64 = "{html_b64}"\n'
|
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'
|
|
|
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 |
"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 |
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 |
+
# Ensure the code is properly formatted for static HTML deployment
|
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)]
|
|
|
1468 |
|
1469 |
Edit `/src/streamlit_app.py` to customize this app to your heart's desire. :heart:
|
1470 |
|
1471 |
+
If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community forums](https://discuss.streamlit.io).
|
1472 |
'''
|
1473 |
readme_path = os.path.join(temp_dir, "README.md")
|
1474 |
with open(readme_path, "w", encoding="utf-8") as f:
|
|
|
1489 |
else:
|
1490 |
# Code is HTML, wrap it in a Gradio app structure
|
1491 |
app_py = wrap_html_in_gradio_app(code)
|
1492 |
+
with tempfile.NamedTemporaryFile("w", suffix=".py", delete=False, encoding='utf-8') as f:
|
1493 |
f.write(app_py)
|
1494 |
temp_path = f.name
|
1495 |
files_to_upload = [(temp_path, file_name)]
|
1496 |
# Upload all files
|
1497 |
try:
|
1498 |
for local_path, repo_path in files_to_upload:
|
1499 |
+
print(f"Uploading {local_path} to {repo_path} in {repo_id}")
|
1500 |
api.upload_file(
|
1501 |
path_or_fileobj=local_path,
|
1502 |
path_in_repo=repo_path,
|
|
|
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 |
+
for local_path, _ in files_to_upload:
|
1515 |
+
try:
|
1516 |
+
if os.path.exists(local_path):
|
1517 |
+
os.unlink(local_path)
|
1518 |
+
except Exception as cleanup_error:
|
1519 |
+
print(f"Warning: Could not clean up {local_path}: {cleanup_error}")
|
1520 |
|
1521 |
# Connect the deploy button to the new function
|
1522 |
deploy_btn.click(
|