akhaliq HF Staff commited on
Commit
5d2566d
·
1 Parent(s): 4b7d538

update streamlit

Browse files
Files changed (1) hide show
  1. app.py +43 -91
app.py CHANGED
@@ -1346,102 +1346,54 @@ with gr.Blocks(
1346
  }
1347
  sdk = sdk_map.get(sdk_name, "gradio")
1348
  api = HfApi(token=token.token)
1349
- # Create the Space if it doesn't exist
1350
- try:
1351
- api.create_repo(
1352
- repo_id=repo_id, # e.g. username/space_name
1353
- repo_type="space",
1354
- space_sdk=sdk, # Use selected SDK
1355
- exist_ok=True # Don't error if it already exists
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"
 
1346
  }
1347
  sdk = sdk_map.get(sdk_name, "gradio")
1348
  api = HfApi(token=token.token)
1349
+ # Create the Space if it doesn't exist (only for non-Streamlit spaces)
1350
+ if sdk != "docker":
1351
+ try:
1352
+ api.create_repo(
1353
+ repo_id=repo_id, # e.g. username/space_name
1354
+ repo_type="space",
1355
+ space_sdk=sdk, # Use selected SDK
1356
+ exist_ok=True # Don't error if it already exists
1357
+ )
1358
+ except Exception as e:
1359
+ return gr.update(value=f"Error creating Space: {e}", visible=True)
1360
  # Streamlit/docker logic
1361
  if sdk == "docker":
 
 
1362
  try:
1363
+ # Use duplicate_space to create a Streamlit template space
1364
+ from huggingface_hub import duplicate_space
1365
+
1366
+ # Duplicate the streamlit template space
1367
+ duplicated_repo = duplicate_space(
1368
+ from_id="streamlit/streamlit-template-space",
1369
+ to_id=space_name.strip(),
1370
+ token=token.token,
1371
+ exist_ok=True
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1372
  )
1373
+
1374
+ # Upload the user's code to the duplicated space
1375
+ import tempfile
1376
+ with tempfile.NamedTemporaryFile("w", suffix=".py", delete=False) as f:
1377
+ f.write(code)
1378
+ temp_path = f.name
1379
+
1380
+ try:
1381
+ api.upload_file(
1382
+ path_or_fileobj=temp_path,
1383
+ path_in_repo="src/streamlit_app.py",
1384
+ repo_id=repo_id,
1385
+ repo_type="space"
1386
+ )
1387
+ space_url = f"https://huggingface.co/spaces/{repo_id}"
1388
+ return gr.update(value=f"✅ Deployed! [Open your Space here]({space_url})", visible=True)
1389
+ except Exception as e:
1390
+ return gr.update(value=f"Error uploading Streamlit app: {e}", visible=True)
1391
+ finally:
1392
+ import os
1393
+ os.unlink(temp_path)
1394
+
1395
  except Exception as e:
1396
+ return gr.update(value=f"Error duplicating Streamlit space: {e}", visible=True)
 
 
1397
  # Other SDKs (existing logic)
1398
  if sdk == "static":
1399
  file_name = "index.html"