akhaliq HF Staff commited on
Commit
eb426b6
·
1 Parent(s): 5644d41

update for streamlit deploy

Browse files
Files changed (1) hide show
  1. app.py +65 -10
app.py CHANGED
@@ -1358,19 +1358,74 @@ with gr.Blocks(
1358
  # Save code to a temporary file
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,
1370
- path_in_repo=file_name,
1371
- repo_id=repo_id,
1372
- repo_type="space"
1373
- )
 
1374
  space_url = f"https://huggingface.co/spaces/{repo_id}"
1375
  return gr.update(value=f"✅ Deployed! [Open your Space here]({space_url})", visible=True)
1376
  except Exception as e:
 
1358
  # Save code to a temporary file
1359
  if sdk == "static":
1360
  file_name = "index.html"
1361
+ with tempfile.NamedTemporaryFile("w", suffix=".html", delete=False) as f:
1362
+ f.write(code)
1363
+ temp_path = f.name
1364
+ files_to_upload = [(temp_path, file_name)]
1365
+ elif sdk == "docker": # Streamlit (Python)
1366
+ import shutil
1367
+ import os
1368
+ import tempfile
1369
+ # Create a temp dir for the structure
1370
+ temp_dir = tempfile.mkdtemp()
1371
+ # 1. Write requirements.txt
1372
+ reqs = "altair\npandas\nstreamlit\n"
1373
+ req_path = os.path.join(temp_dir, "requirements.txt")
1374
+ with open(req_path, "w") as f:
1375
+ f.write(reqs)
1376
+ # 2. Write Dockerfile
1377
+ dockerfile_content = '''FROM python:3.9-slim
1378
+
1379
+ WORKDIR /app
1380
+
1381
+ RUN apt-get update && apt-get install -y \
1382
+ build-essential \
1383
+ curl \
1384
+ software-properties-common \
1385
+ git \
1386
+ && rm -rf /var/lib/apt/lists/*
1387
+
1388
+ COPY requirements.txt ./
1389
+ COPY src/ ./src/
1390
+
1391
+ RUN pip3 install -r requirements.txt
1392
+
1393
+ EXPOSE 8501
1394
+
1395
+ HEALTHCHECK CMD curl --fail http://localhost:8501/_stcore/health
1396
+
1397
+ ENTRYPOINT ["streamlit", "run", "src/streamlit_app.py", "--server.port=8501", "--server.address=0.0.0.0"]
1398
+ '''
1399
+ dockerfile_path = os.path.join(temp_dir, "Dockerfile")
1400
+ with open(dockerfile_path, "w") as f:
1401
+ f.write(dockerfile_content)
1402
+ # 3. Write src/streamlit_app.py
1403
+ src_dir = os.path.join(temp_dir, "src")
1404
+ os.makedirs(src_dir, exist_ok=True)
1405
+ app_py_path = os.path.join(src_dir, "streamlit_app.py")
1406
+ with open(app_py_path, "w") as f:
1407
+ f.write(code)
1408
+ # Prepare files to upload
1409
+ files_to_upload = [
1410
+ (req_path, "requirements.txt"),
1411
+ (dockerfile_path, "Dockerfile"),
1412
+ (app_py_path, "src/streamlit_app.py"),
1413
+ ]
1414
  else:
1415
  file_name = "app.py"
1416
+ with tempfile.NamedTemporaryFile("w", suffix=".py", delete=False) as f:
1417
+ f.write(code)
1418
+ temp_path = f.name
1419
+ files_to_upload = [(temp_path, file_name)]
1420
+ # Upload all files
1421
  try:
1422
+ for local_path, repo_path in files_to_upload:
1423
+ api.upload_file(
1424
+ path_or_fileobj=local_path,
1425
+ path_in_repo=repo_path,
1426
+ repo_id=repo_id,
1427
+ repo_type="space"
1428
+ )
1429
  space_url = f"https://huggingface.co/spaces/{repo_id}"
1430
  return gr.update(value=f"✅ Deployed! [Open your Space here]({space_url})", visible=True)
1431
  except Exception as e: