akhaliq HF Staff commited on
Commit
de2e4ad
·
1 Parent(s): e8ac778

transformers js errors

Browse files
Files changed (1) hide show
  1. app.py +94 -31
app.py CHANGED
@@ -711,32 +711,52 @@ def parse_transformers_js_output(text):
711
  'style.css': ''
712
  }
713
 
714
- # Patterns to match the three code blocks
715
- html_pattern = r'```html\s*\n([\s\S]+?)\n```'
716
- js_pattern = r'```javascript\s*\n([\s\S]+?)\n```'
717
- css_pattern = r'```css\s*\n([\s\S]+?)\n```'
 
 
 
 
 
 
 
 
 
 
 
 
 
718
 
719
  # Extract HTML content
720
- html_match = re.search(html_pattern, text, re.IGNORECASE)
721
- if html_match:
722
- files['index.html'] = html_match.group(1).strip()
 
 
723
 
724
  # Extract JavaScript content
725
- js_match = re.search(js_pattern, text, re.IGNORECASE)
726
- if js_match:
727
- files['index.js'] = js_match.group(1).strip()
 
 
728
 
729
  # Extract CSS content
730
- css_match = re.search(css_pattern, text, re.IGNORECASE)
731
- if css_match:
732
- files['style.css'] = css_match.group(1).strip()
 
 
733
 
734
  # Fallback: support === index.html === format if any file is missing
735
  if not (files['index.html'] and files['index.js'] and files['style.css']):
736
  # Use regex to extract sections
737
- html_fallback = re.search(r'===\s*index\.html\s*===\n([\s\S]+?)(?=\n===|$)', text, re.IGNORECASE)
738
- js_fallback = re.search(r'===\s*index\.js\s*===\n([\s\S]+?)(?=\n===|$)', text, re.IGNORECASE)
739
- css_fallback = re.search(r'===\s*style\.css\s*===\n([\s\S]+?)(?=\n===|$)', text, re.IGNORECASE)
 
740
  if html_fallback:
741
  files['index.html'] = html_fallback.group(1).strip()
742
  if js_fallback:
@@ -744,6 +764,25 @@ def parse_transformers_js_output(text):
744
  if css_fallback:
745
  files['style.css'] = css_fallback.group(1).strip()
746
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
747
  return files
748
 
749
  def format_transformers_js_output(files):
@@ -2810,6 +2849,13 @@ with gr.Blocks(
2810
 
2811
  def hide_deploy_components(*args):
2812
  return [gr.Textbox(visible=False), gr.Dropdown(visible=False), gr.Button(visible=False)]
 
 
 
 
 
 
 
2813
 
2814
  # Load project button event
2815
  load_project_btn.click(
@@ -2830,6 +2876,8 @@ with gr.Blocks(
2830
  # Update preview when code or language changes
2831
  code_output.change(preview_logic, inputs=[code_output, language_dropdown], outputs=sandbox)
2832
  language_dropdown.change(preview_logic, inputs=[code_output, language_dropdown], outputs=sandbox)
 
 
2833
  clear_btn.click(clear_history, outputs=[history, history_output, file_input, website_url_input])
2834
  clear_btn.click(hide_deploy_components, None, [space_name_input, sdk_dropdown, deploy_btn])
2835
  # Reset space name and button text when clearing
@@ -2980,19 +3028,21 @@ with gr.Blocks(
2980
  error_prefix = "Error duplicating Streamlit space" if not is_update else "Error updating Streamlit space"
2981
  return gr.update(value=f"{error_prefix}: {e}", visible=True)
2982
  # Transformers.js logic
2983
- elif sdk_name == "Transformers.js" and not is_update:
2984
  try:
2985
- # Use duplicate_space to create a transformers.js template space
2986
- from huggingface_hub import duplicate_space
2987
-
2988
- # Duplicate the transformers.js template space
2989
- duplicated_repo = duplicate_space(
2990
- from_id="static-templates/transformers.js",
2991
- to_id=space_name.strip(),
2992
- token=token.token,
2993
- exist_ok=True
2994
- )
2995
- print("Duplicated repo result:", duplicated_repo, type(duplicated_repo))
 
 
2996
  # Parse the transformers.js output to get the three files
2997
  files = parse_transformers_js_output(code)
2998
 
@@ -3078,9 +3128,22 @@ with gr.Blocks(
3078
  except Exception as e:
3079
  # Handle potential RepoUrl object errors
3080
  error_msg = str(e)
3081
- if "'url'" in error_msg or "RepoUrl" in error_msg:
3082
- return gr.update(value=f"Error duplicating Transformers.js space: RepoUrl handling error. Please try again. Details: {error_msg}", visible=True)
3083
- return gr.update(value=f"Error duplicating Transformers.js space: {error_msg}", visible=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
3084
  # Svelte logic
3085
  elif sdk_name == "Svelte" and not is_update:
3086
  try:
 
711
  'style.css': ''
712
  }
713
 
714
+ # Multiple patterns to match the three code blocks with different variations
715
+ html_patterns = [
716
+ r'```html\s*\n([\s\S]+?)\n```',
717
+ r'```htm\s*\n([\s\S]+?)\n```',
718
+ r'```\s*(?:index\.html|html)\s*\n([\s\S]+?)\n```'
719
+ ]
720
+
721
+ js_patterns = [
722
+ r'```javascript\s*\n([\s\S]+?)\n```',
723
+ r'```js\s*\n([\s\S]+?)\n```',
724
+ r'```\s*(?:index\.js|javascript)\s*\n([\s\S]+?)\n```'
725
+ ]
726
+
727
+ css_patterns = [
728
+ r'```css\s*\n([\s\S]+?)\n```',
729
+ r'```\s*(?:style\.css|css)\s*\n([\s\S]+?)\n```'
730
+ ]
731
 
732
  # Extract HTML content
733
+ for pattern in html_patterns:
734
+ html_match = re.search(pattern, text, re.IGNORECASE)
735
+ if html_match:
736
+ files['index.html'] = html_match.group(1).strip()
737
+ break
738
 
739
  # Extract JavaScript content
740
+ for pattern in js_patterns:
741
+ js_match = re.search(pattern, text, re.IGNORECASE)
742
+ if js_match:
743
+ files['index.js'] = js_match.group(1).strip()
744
+ break
745
 
746
  # Extract CSS content
747
+ for pattern in css_patterns:
748
+ css_match = re.search(pattern, text, re.IGNORECASE)
749
+ if css_match:
750
+ files['style.css'] = css_match.group(1).strip()
751
+ break
752
 
753
  # Fallback: support === index.html === format if any file is missing
754
  if not (files['index.html'] and files['index.js'] and files['style.css']):
755
  # Use regex to extract sections
756
+ html_fallback = re.search(r'===\s*index\.html\s*===\s*\n([\s\S]+?)(?=\n===|$)', text, re.IGNORECASE)
757
+ js_fallback = re.search(r'===\s*index\.js\s*===\s*\n([\s\S]+?)(?=\n===|$)', text, re.IGNORECASE)
758
+ css_fallback = re.search(r'===\s*style\.css\s*===\s*\n([\s\S]+?)(?=\n===|$)', text, re.IGNORECASE)
759
+
760
  if html_fallback:
761
  files['index.html'] = html_fallback.group(1).strip()
762
  if js_fallback:
 
764
  if css_fallback:
765
  files['style.css'] = css_fallback.group(1).strip()
766
 
767
+ # Additional fallback: extract from numbered sections or file headers
768
+ if not (files['index.html'] and files['index.js'] and files['style.css']):
769
+ # Try patterns like "1. index.html:" or "**index.html**"
770
+ patterns = [
771
+ (r'(?:^\d+\.\s*|^##\s*|^\*\*\s*)index\.html(?:\s*:|\*\*:?)\s*\n([\s\S]+?)(?=\n(?:\d+\.|##|\*\*|===)|$)', 'index.html'),
772
+ (r'(?:^\d+\.\s*|^##\s*|^\*\*\s*)index\.js(?:\s*:|\*\*:?)\s*\n([\s\S]+?)(?=\n(?:\d+\.|##|\*\*|===)|$)', 'index.js'),
773
+ (r'(?:^\d+\.\s*|^##\s*|^\*\*\s*)style\.css(?:\s*:|\*\*:?)\s*\n([\s\S]+?)(?=\n(?:\d+\.|##|\*\*|===)|$)', 'style.css')
774
+ ]
775
+
776
+ for pattern, file_key in patterns:
777
+ if not files[file_key]:
778
+ match = re.search(pattern, text, re.IGNORECASE | re.MULTILINE)
779
+ if match:
780
+ # Clean up the content by removing any code block markers
781
+ content = match.group(1).strip()
782
+ content = re.sub(r'^```\w*\s*\n', '', content)
783
+ content = re.sub(r'\n```\s*$', '', content)
784
+ files[file_key] = content.strip()
785
+
786
  return files
787
 
788
  def format_transformers_js_output(files):
 
2849
 
2850
  def hide_deploy_components(*args):
2851
  return [gr.Textbox(visible=False), gr.Dropdown(visible=False), gr.Button(visible=False)]
2852
+
2853
+ def update_deploy_button_text(space_name):
2854
+ """Update deploy button text based on whether it's a new space or update"""
2855
+ if "/" in space_name.strip():
2856
+ return gr.update(value="🔄 Update Space")
2857
+ else:
2858
+ return gr.update(value="🚀 Deploy App")
2859
 
2860
  # Load project button event
2861
  load_project_btn.click(
 
2876
  # Update preview when code or language changes
2877
  code_output.change(preview_logic, inputs=[code_output, language_dropdown], outputs=sandbox)
2878
  language_dropdown.change(preview_logic, inputs=[code_output, language_dropdown], outputs=sandbox)
2879
+ # Update deploy button text when space name changes
2880
+ space_name_input.change(update_deploy_button_text, inputs=[space_name_input], outputs=[deploy_btn])
2881
  clear_btn.click(clear_history, outputs=[history, history_output, file_input, website_url_input])
2882
  clear_btn.click(hide_deploy_components, None, [space_name_input, sdk_dropdown, deploy_btn])
2883
  # Reset space name and button text when clearing
 
3028
  error_prefix = "Error duplicating Streamlit space" if not is_update else "Error updating Streamlit space"
3029
  return gr.update(value=f"{error_prefix}: {e}", visible=True)
3030
  # Transformers.js logic
3031
+ elif sdk_name == "Transformers.js":
3032
  try:
3033
+ # Only duplicate template space for new spaces, not updates
3034
+ if not is_update:
3035
+ # Use duplicate_space to create a transformers.js template space
3036
+ from huggingface_hub import duplicate_space
3037
+
3038
+ # Duplicate the transformers.js template space
3039
+ duplicated_repo = duplicate_space(
3040
+ from_id="static-templates/transformers.js",
3041
+ to_id=space_name.strip(),
3042
+ token=token.token,
3043
+ exist_ok=True
3044
+ )
3045
+ print("Duplicated repo result:", duplicated_repo, type(duplicated_repo))
3046
  # Parse the transformers.js output to get the three files
3047
  files = parse_transformers_js_output(code)
3048
 
 
3128
  except Exception as e:
3129
  # Handle potential RepoUrl object errors
3130
  error_msg = str(e)
3131
+ if "'url'" in error_msg or "RepoUrl" in error_msg and not is_update:
3132
+ # Extract the URL from RepoUrl object if possible
3133
+ try:
3134
+ if 'duplicated_repo' in locals() and hasattr(duplicated_repo, 'url'):
3135
+ repo_url = duplicated_repo.url
3136
+ elif 'duplicated_repo' in locals() and hasattr(duplicated_repo, '_url'):
3137
+ repo_url = duplicated_repo._url
3138
+ else:
3139
+ repo_url = f"https://huggingface.co/spaces/{repo_id}"
3140
+ return gr.update(value=f"Error: Could not properly handle space creation response. Space may have been created successfully. Check: {repo_url}", visible=True)
3141
+ except:
3142
+ return gr.update(value=f"Error duplicating Transformers.js space: RepoUrl handling error. Please try again manually at https://huggingface.co/new-space", visible=True)
3143
+
3144
+ # General error handling for both creation and updates
3145
+ action_verb = "updating" if is_update else "duplicating"
3146
+ return gr.update(value=f"Error {action_verb} Transformers.js space: {error_msg}", visible=True)
3147
  # Svelte logic
3148
  elif sdk_name == "Svelte" and not is_update:
3149
  try: