Spaces:
Running
Running
update streamlit
Browse files
app.py
CHANGED
|
@@ -316,7 +316,7 @@ def remove_code_block(text):
|
|
| 316 |
if match:
|
| 317 |
extracted = match.group(1).strip()
|
| 318 |
# Remove a leading language marker line (e.g., 'python') if present
|
| 319 |
-
if extracted.split('\n', 1)[0].strip().lower() in ['python', 'html', 'css', 'javascript', 'json', 'c', 'cpp', 'markdown', 'latex', 'jinja2', 'typescript', 'yaml', 'dockerfile', 'shell', 'r', 'sql', 'sql-mssql', 'sql-mysql', 'sql-mariadb', 'sql-sqlite', 'sql-cassandra', 'sql-
|
| 320 |
return extracted.split('\n', 1)[1] if '\n' in extracted else ''
|
| 321 |
return extracted
|
| 322 |
# If no code block is found, check if the entire text is HTML
|
|
@@ -327,7 +327,7 @@ def remove_code_block(text):
|
|
| 327 |
return text.strip()[9:-3].strip()
|
| 328 |
# Remove a leading language marker line if present (fallback)
|
| 329 |
lines = text.strip().split('\n', 1)
|
| 330 |
-
if lines[0].strip().lower() in ['python', 'html', 'css', 'javascript', 'json', 'c', 'cpp', 'markdown', 'latex', 'jinja2', 'typescript', 'yaml', 'dockerfile', 'shell', 'r', 'sql', 'sql-mssql', 'sql-mysql', 'sql-mariadb', 'sql-sqlite', 'sql-cassandra', 'sql-
|
| 331 |
return lines[1] if len(lines) > 1 else ''
|
| 332 |
return text.strip()
|
| 333 |
|
|
@@ -1331,6 +1331,7 @@ with gr.Blocks(
|
|
| 1331 |
profile: gr.OAuthProfile | None = None,
|
| 1332 |
token: gr.OAuthToken | None = None
|
| 1333 |
):
|
|
|
|
| 1334 |
if not code or not code.strip():
|
| 1335 |
return gr.update(value="No code to deploy.", visible=True)
|
| 1336 |
if profile is None or token is None:
|
|
@@ -1355,15 +1356,101 @@ with gr.Blocks(
|
|
| 1355 |
)
|
| 1356 |
except Exception as e:
|
| 1357 |
return gr.update(value=f"Error creating Space: {e}", visible=True)
|
| 1358 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1359 |
if sdk == "static":
|
| 1360 |
file_name = "index.html"
|
| 1361 |
else:
|
| 1362 |
file_name = "app.py"
|
|
|
|
| 1363 |
with tempfile.NamedTemporaryFile("w", suffix=f".{file_name.split('.')[-1]}", delete=False) as f:
|
| 1364 |
f.write(code)
|
| 1365 |
temp_path = f.name
|
| 1366 |
-
# Upload the file
|
| 1367 |
try:
|
| 1368 |
api.upload_file(
|
| 1369 |
path_or_fileobj=temp_path,
|
|
|
|
| 316 |
if match:
|
| 317 |
extracted = match.group(1).strip()
|
| 318 |
# Remove a leading language marker line (e.g., 'python') if present
|
| 319 |
+
if extracted.split('\n', 1)[0].strip().lower() in ['python', 'html', 'css', 'javascript', 'json', 'c', 'cpp', 'markdown', 'latex', 'jinja2', 'typescript', 'yaml', 'dockerfile', 'shell', 'r', 'sql', 'sql-mssql', 'sql-mysql', 'sql-mariadb', 'sql-sqlite', 'sql-cassandra', 'sql-plSQL', 'sql-hive', 'sql-pgsql', 'sql-gql', 'sql-gpsql', 'sql-sparksql', 'sql-esper']:
|
| 320 |
return extracted.split('\n', 1)[1] if '\n' in extracted else ''
|
| 321 |
return extracted
|
| 322 |
# If no code block is found, check if the entire text is HTML
|
|
|
|
| 327 |
return text.strip()[9:-3].strip()
|
| 328 |
# Remove a leading language marker line if present (fallback)
|
| 329 |
lines = text.strip().split('\n', 1)
|
| 330 |
+
if lines[0].strip().lower() in ['python', 'html', 'css', 'javascript', 'json', 'c', 'cpp', 'markdown', 'latex', 'jinja2', 'typescript', 'yaml', 'dockerfile', 'shell', 'r', 'sql', 'sql-mssql', 'sql-mysql', 'sql-mariadb', 'sql-sqlite', 'sql-cassandra', 'sql-plSQL', 'sql-hive', 'sql-pgsql', 'sql-gql', 'sql-gpsql', 'sql-sparksql', 'sql-esper']:
|
| 331 |
return lines[1] if len(lines) > 1 else ''
|
| 332 |
return text.strip()
|
| 333 |
|
|
|
|
| 1331 |
profile: gr.OAuthProfile | None = None,
|
| 1332 |
token: gr.OAuthToken | None = None
|
| 1333 |
):
|
| 1334 |
+
import shutil
|
| 1335 |
if not code or not code.strip():
|
| 1336 |
return gr.update(value="No code to deploy.", visible=True)
|
| 1337 |
if profile is None or token is None:
|
|
|
|
| 1356 |
)
|
| 1357 |
except Exception as e:
|
| 1358 |
return gr.update(value=f"Error creating Space: {e}", visible=True)
|
| 1359 |
+
# Streamlit/docker logic
|
| 1360 |
+
if sdk == "docker":
|
| 1361 |
+
import tempfile, os
|
| 1362 |
+
temp_dir = tempfile.mkdtemp()
|
| 1363 |
+
try:
|
| 1364 |
+
# 1. Write requirements.txt
|
| 1365 |
+
reqs = "altair\npandas\nstreamlit\n"
|
| 1366 |
+
req_path = os.path.join(temp_dir, "requirements.txt")
|
| 1367 |
+
with open(req_path, "w") as f:
|
| 1368 |
+
f.write(reqs)
|
| 1369 |
+
# 2. Write src/streamlit_app.py
|
| 1370 |
+
src_dir = os.path.join(temp_dir, "src")
|
| 1371 |
+
os.makedirs(src_dir, exist_ok=True)
|
| 1372 |
+
app_path = os.path.join(src_dir, "streamlit_app.py")
|
| 1373 |
+
with open(app_path, "w") as f:
|
| 1374 |
+
f.write(code)
|
| 1375 |
+
# 3. Write Dockerfile
|
| 1376 |
+
dockerfile_content = '''FROM python:3.9-slim
|
| 1377 |
+
WORKDIR /app
|
| 1378 |
+
RUN apt-get update && apt-get install -y \\
|
| 1379 |
+
build-essential \\
|
| 1380 |
+
curl \\
|
| 1381 |
+
software-properties-common \\
|
| 1382 |
+
git \\
|
| 1383 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 1384 |
+
COPY requirements.txt ./
|
| 1385 |
+
COPY src/ ./src/
|
| 1386 |
+
RUN pip3 install -r requirements.txt
|
| 1387 |
+
EXPOSE 8501
|
| 1388 |
+
HEALTHCHECK CMD curl --fail http://localhost:8501/_stcore/health
|
| 1389 |
+
ENTRYPOINT ["streamlit", "run", "src/streamlit_app.py", "--server.port=8501", "--server.address=0.0.0.0"]
|
| 1390 |
+
'''
|
| 1391 |
+
dockerfile_path = os.path.join(temp_dir, "Dockerfile")
|
| 1392 |
+
with open(dockerfile_path, "w") as f:
|
| 1393 |
+
f.write(dockerfile_content)
|
| 1394 |
+
# 4. Write README.md in the required format
|
| 1395 |
+
readme_content = f'''---
|
| 1396 |
+
title: {space_name.strip()}
|
| 1397 |
+
emoji: π
|
| 1398 |
+
colorFrom: red
|
| 1399 |
+
colorTo: red
|
| 1400 |
+
sdk: docker
|
| 1401 |
+
app_port: 8501
|
| 1402 |
+
tags:
|
| 1403 |
+
- streamlit
|
| 1404 |
+
pinned: false
|
| 1405 |
+
short_description: Streamlit template space
|
| 1406 |
+
---
|
| 1407 |
+
# Welcome to Streamlit!
|
| 1408 |
+
Edit `/src/streamlit_app.py` to customize this app to your heart's desire. :heart:
|
| 1409 |
+
If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community\nforums](https://discuss.streamlit.io).
|
| 1410 |
+
'''
|
| 1411 |
+
readme_path = os.path.join(temp_dir, "README.md")
|
| 1412 |
+
with open(readme_path, "w") as f:
|
| 1413 |
+
f.write(readme_content)
|
| 1414 |
+
# Upload all four files
|
| 1415 |
+
api.upload_file(
|
| 1416 |
+
path_or_fileobj=req_path,
|
| 1417 |
+
path_in_repo="requirements.txt",
|
| 1418 |
+
repo_id=repo_id,
|
| 1419 |
+
repo_type="space"
|
| 1420 |
+
)
|
| 1421 |
+
api.upload_file(
|
| 1422 |
+
path_or_fileobj=app_path,
|
| 1423 |
+
path_in_repo="src/streamlit_app.py",
|
| 1424 |
+
repo_id=repo_id,
|
| 1425 |
+
repo_type="space"
|
| 1426 |
+
)
|
| 1427 |
+
api.upload_file(
|
| 1428 |
+
path_or_fileobj=dockerfile_path,
|
| 1429 |
+
path_in_repo="Dockerfile",
|
| 1430 |
+
repo_id=repo_id,
|
| 1431 |
+
repo_type="space"
|
| 1432 |
+
)
|
| 1433 |
+
api.upload_file(
|
| 1434 |
+
path_or_fileobj=readme_path,
|
| 1435 |
+
path_in_repo="README.md",
|
| 1436 |
+
repo_id=repo_id,
|
| 1437 |
+
repo_type="space"
|
| 1438 |
+
)
|
| 1439 |
+
space_url = f"https://huggingface.co/spaces/{repo_id}"
|
| 1440 |
+
return gr.update(value=f"β
Deployed! [Open your Space here]({space_url})", visible=True)
|
| 1441 |
+
except Exception as e:
|
| 1442 |
+
return gr.update(value=f"Error uploading Streamlit files: {e}", visible=True)
|
| 1443 |
+
finally:
|
| 1444 |
+
shutil.rmtree(temp_dir, ignore_errors=True)
|
| 1445 |
+
# Other SDKs (existing logic)
|
| 1446 |
if sdk == "static":
|
| 1447 |
file_name = "index.html"
|
| 1448 |
else:
|
| 1449 |
file_name = "app.py"
|
| 1450 |
+
import tempfile
|
| 1451 |
with tempfile.NamedTemporaryFile("w", suffix=f".{file_name.split('.')[-1]}", delete=False) as f:
|
| 1452 |
f.write(code)
|
| 1453 |
temp_path = f.name
|
|
|
|
| 1454 |
try:
|
| 1455 |
api.upload_file(
|
| 1456 |
path_or_fileobj=temp_path,
|