akhaliq HF Staff commited on
Commit
46dc0f5
·
1 Parent(s): 634b663

requirements txt update

Browse files
Files changed (1) hide show
  1. app.py +90 -46
app.py CHANGED
@@ -7604,34 +7604,56 @@ with gr.Blocks(
7604
  exist_ok=True
7605
  )
7606
 
7607
- # Generate and upload requirements.txt for Streamlit apps
7608
  import_statements = extract_import_statements(code)
7609
  requirements_content = generate_requirements_txt_with_llm(import_statements)
7610
 
7611
  import tempfile
7612
 
7613
- # Upload requirements.txt first
7614
- try:
7615
- with tempfile.NamedTemporaryFile("w", suffix=".txt", delete=False) as f:
7616
- f.write(requirements_content)
7617
- requirements_temp_path = f.name
7618
-
7619
- api.upload_file(
7620
- path_or_fileobj=requirements_temp_path,
7621
- path_in_repo="requirements.txt",
7622
- repo_id=repo_id,
7623
- repo_type="space"
7624
- )
7625
- except Exception as e:
7626
- error_msg = str(e)
7627
- if "403 Forbidden" in error_msg and "write token" in error_msg:
7628
- return gr.update(value=f"Error uploading requirements.txt: Permission denied. Please ensure you have write access to {repo_id} and your token has the correct permissions.", visible=True)
7629
- else:
7630
- return gr.update(value=f"Error uploading requirements.txt: {e}", visible=True)
7631
- finally:
7632
- import os
7633
- if 'requirements_temp_path' in locals():
7634
- os.unlink(requirements_temp_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7635
 
7636
  # Add anycoder tag to existing README
7637
  add_anycoder_tag_to_readme(api, repo_id)
@@ -7980,34 +8002,56 @@ with gr.Blocks(
7980
  import os
7981
  os.unlink(temp_path)
7982
  else:
7983
- # Generate and upload requirements.txt for Gradio apps
7984
  import_statements = extract_import_statements(code)
7985
  requirements_content = generate_requirements_txt_with_llm(import_statements)
7986
 
7987
  import tempfile
7988
 
7989
- # Upload requirements.txt first
7990
- try:
7991
- with tempfile.NamedTemporaryFile("w", suffix=".txt", delete=False) as f:
7992
- f.write(requirements_content)
7993
- requirements_temp_path = f.name
7994
-
7995
- api.upload_file(
7996
- path_or_fileobj=requirements_temp_path,
7997
- path_in_repo="requirements.txt",
7998
- repo_id=repo_id,
7999
- repo_type="space"
8000
- )
8001
- except Exception as e:
8002
- error_msg = str(e)
8003
- if "403 Forbidden" in error_msg and "write token" in error_msg:
8004
- return gr.update(value=f"Error uploading requirements.txt: Permission denied. Please ensure you have write access to {repo_id} and your token has the correct permissions.", visible=True)
8005
- else:
8006
- return gr.update(value=f"Error uploading requirements.txt: {e}", visible=True)
8007
- finally:
8008
- import os
8009
- if 'requirements_temp_path' in locals():
8010
- os.unlink(requirements_temp_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8011
 
8012
  # Add anycoder tag to existing README
8013
  add_anycoder_tag_to_readme(api, repo_id)
 
7604
  exist_ok=True
7605
  )
7606
 
7607
+ # Generate requirements.txt for Streamlit apps and upload only if needed
7608
  import_statements = extract_import_statements(code)
7609
  requirements_content = generate_requirements_txt_with_llm(import_statements)
7610
 
7611
  import tempfile
7612
 
7613
+ # Check if we need to upload requirements.txt
7614
+ should_upload_requirements = True
7615
+ if is_update:
7616
+ try:
7617
+ # Try to get existing requirements.txt content
7618
+ existing_requirements = api.hf_hub_download(
7619
+ repo_id=repo_id,
7620
+ filename="requirements.txt",
7621
+ repo_type="space"
7622
+ )
7623
+ with open(existing_requirements, 'r') as f:
7624
+ existing_content = f.read().strip()
7625
+
7626
+ # Compare with new content
7627
+ if existing_content == requirements_content.strip():
7628
+ should_upload_requirements = False
7629
+
7630
+ except Exception:
7631
+ # File doesn't exist or can't be accessed, so we should upload
7632
+ should_upload_requirements = True
7633
+
7634
+ # Upload requirements.txt only if needed
7635
+ if should_upload_requirements:
7636
+ try:
7637
+ with tempfile.NamedTemporaryFile("w", suffix=".txt", delete=False) as f:
7638
+ f.write(requirements_content)
7639
+ requirements_temp_path = f.name
7640
+
7641
+ api.upload_file(
7642
+ path_or_fileobj=requirements_temp_path,
7643
+ path_in_repo="requirements.txt",
7644
+ repo_id=repo_id,
7645
+ repo_type="space"
7646
+ )
7647
+ except Exception as e:
7648
+ error_msg = str(e)
7649
+ if "403 Forbidden" in error_msg and "write token" in error_msg:
7650
+ return gr.update(value=f"Error uploading requirements.txt: Permission denied. Please ensure you have write access to {repo_id} and your token has the correct permissions.", visible=True)
7651
+ else:
7652
+ return gr.update(value=f"Error uploading requirements.txt: {e}", visible=True)
7653
+ finally:
7654
+ import os
7655
+ if 'requirements_temp_path' in locals():
7656
+ os.unlink(requirements_temp_path)
7657
 
7658
  # Add anycoder tag to existing README
7659
  add_anycoder_tag_to_readme(api, repo_id)
 
8002
  import os
8003
  os.unlink(temp_path)
8004
  else:
8005
+ # Generate requirements.txt for Gradio apps and upload only if needed
8006
  import_statements = extract_import_statements(code)
8007
  requirements_content = generate_requirements_txt_with_llm(import_statements)
8008
 
8009
  import tempfile
8010
 
8011
+ # Check if we need to upload requirements.txt
8012
+ should_upload_requirements = True
8013
+ if is_update:
8014
+ try:
8015
+ # Try to get existing requirements.txt content
8016
+ existing_requirements = api.hf_hub_download(
8017
+ repo_id=repo_id,
8018
+ filename="requirements.txt",
8019
+ repo_type="space"
8020
+ )
8021
+ with open(existing_requirements, 'r') as f:
8022
+ existing_content = f.read().strip()
8023
+
8024
+ # Compare with new content
8025
+ if existing_content == requirements_content.strip():
8026
+ should_upload_requirements = False
8027
+
8028
+ except Exception:
8029
+ # File doesn't exist or can't be accessed, so we should upload
8030
+ should_upload_requirements = True
8031
+
8032
+ # Upload requirements.txt only if needed
8033
+ if should_upload_requirements:
8034
+ try:
8035
+ with tempfile.NamedTemporaryFile("w", suffix=".txt", delete=False) as f:
8036
+ f.write(requirements_content)
8037
+ requirements_temp_path = f.name
8038
+
8039
+ api.upload_file(
8040
+ path_or_fileobj=requirements_temp_path,
8041
+ path_in_repo="requirements.txt",
8042
+ repo_id=repo_id,
8043
+ repo_type="space"
8044
+ )
8045
+ except Exception as e:
8046
+ error_msg = str(e)
8047
+ if "403 Forbidden" in error_msg and "write token" in error_msg:
8048
+ return gr.update(value=f"Error uploading requirements.txt: Permission denied. Please ensure you have write access to {repo_id} and your token has the correct permissions.", visible=True)
8049
+ else:
8050
+ return gr.update(value=f"Error uploading requirements.txt: {e}", visible=True)
8051
+ finally:
8052
+ import os
8053
+ if 'requirements_temp_path' in locals():
8054
+ os.unlink(requirements_temp_path)
8055
 
8056
  # Add anycoder tag to existing README
8057
  add_anycoder_tag_to_readme(api, repo_id)