transformers js errors
Browse files710 unmodified lines7117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417421 unmodified line7447457467477487492060 unmodified lines28102811281228132814281514 unmodified lines283028312832283328342835144 unmodified lines298029812982298329842985298629872988298929902991299229932994299529962997299879 unmodified lines307830793080308130823083308430853086710 unmodified lines'style.css': ''}# Patterns to match the three code blockshtml_pattern = r'```html\s*\n([\s\S]+?)\n```'js_pattern = r'```javascript\s*\n([\s\S]+?)\n```'css_pattern = r'```css\s*\n([\s\S]+?)\n```'# Extract HTML contenthtml_match = re.search(html_pattern, text, re.IGNORECASE)if html_match:files['index.html'] = html_match.group(1).strip()# Extract JavaScript contentjs_match = re.search(js_pattern, text, re.IGNORECASE)if js_match:files['index.js'] = js_match.group(1).strip()# Extract CSS contentcss_match = re.search(css_pattern, text, re.IGNORECASE)if css_match:files['style.css'] = css_match.group(1).strip()# Fallback: support === index.html === format if any file is missingif not (files['index.html'] and files['index.js'] and files['style.css']):# Use regex to extract sectionshtml_fallback = re.search(r'===\s*index\.html\s*===\n([\s\S]+?)(?=\n===|$)', text, re.IGNORECASE)js_fallback = re.search(r'===\s*index\.js\s*===\n([\s\S]+?)(?=\n===|$)', text, re.IGNORECASE)css_fallback = re.search(r'===\s*style\.css\s*===\n([\s\S]+?)(?=\n===|$)', text, re.IGNORECASE)if html_fallback:files['index.html'] = html_fallback.group(1).strip()if js_fallback:1 unmodified lineif css_fallback:files['style.css'] = css_fallback.group(1).strip()return filesdef format_transformers_js_output(files):2060 unmodified linesdef hide_deploy_components(*args):return [gr.Textbox(visible=False), gr.Dropdown(visible=False), gr.Button(visible=False)]# Load project button eventload_project_btn.click(14 unmodified lines# Update preview when code or language changescode_output.change(preview_logic, inputs=[code_output, language_dropdown], outputs=sandbox)language_dropdown.change(preview_logic, inputs=[code_output, language_dropdown], outputs=sandbox)clear_btn.click(clear_history, outputs=[history, history_output, file_input, website_url_input])clear_btn.click(hide_deploy_components, None, [space_name_input, sdk_dropdown, deploy_btn])# Reset space name and button text when clearing144 unmodified lineserror_prefix = "Error duplicating Streamlit space" if not is_update else "Error updating Streamlit space"return gr.update(value=f"{error_prefix}: {e}", visible=True)# Transformers.js logicelif sdk_name == "Transformers.js" and not is_update:try:# Use duplicate_space to create a transformers.js template spacefrom huggingface_hub import duplicate_space# Duplicate the transformers.js template spaceduplicated_repo = duplicate_space(from_id="static-templates/transformers.js",to_id=space_name.strip(),token=token.token,exist_ok=True)print("Duplicated repo result:", duplicated_repo, type(duplicated_repo))# Parse the transformers.js output to get the three filesfiles = parse_transformers_js_output(code)79 unmodified linesexcept Exception as e:# Handle potential RepoUrl object errorserror_msg = str(e)if "'url'" in error_msg or "RepoUrl" in error_msg:return gr.update(value=f"Error duplicating Transformers.js space: RepoUrl handling error. Please try again. Details: {error_msg}", visible=True)return gr.update(value=f"Error duplicating Transformers.js space: {error_msg}", visible=True)# Svelte logicelif sdk_name == "Svelte" and not is_update:try:710 unmodified lines7117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617621 unmodified line7647657667677687697707717727737747757767777787797807817827837847857867877882060 unmodified lines284928502851285228532854285528562857285828592860286114 unmodified lines28762877287828792880288128822883144 unmodified lines30283029303030313032303330343035303630373038303930403041304230433044304530463047304879 unmodified lines3128312931303131313231333134313531363137313831393140314131423143314431453146314731483149710 unmodified lines'style.css': ''}# Multiple patterns to match the three code blocks with different variationshtml_patterns = [r'```html\s*\n([\s\S]+?)\n```',r'```htm\s*\n([\s\S]+?)\n```',r'```\s*(?:index\.html|html)\s*\n([\s\S]+?)\n```']js_patterns = [r'```javascript\s*\n([\s\S]+?)\n```',r'```js\s*\n([\s\S]+?)\n```',r'```\s*(?:index\.js|javascript)\s*\n([\s\S]+?)\n```']css_patterns = [r'```css\s*\n([\s\S]+?)\n```',r'```\s*(?:style\.css|css)\s*\n([\s\S]+?)\n```']# Extract HTML contentfor pattern in html_patterns:html_match = re.search(pattern, text, re.IGNORECASE)if html_match:files['index.html'] = html_match.group(1).strip()break# Extract JavaScript contentfor pattern in js_patterns:js_match = re.search(pattern, text, re.IGNORECASE)if js_match:files['index.js'] = js_match.group(1).strip()break# Extract CSS contentfor pattern in css_patterns:css_match = re.search(pattern, text, re.IGNORECASE)if css_match:files['style.css'] = css_match.group(1).strip()break# Fallback: support === index.html === format if any file is missingif not (files['index.html'] and files['index.js'] and files['style.css']):# Use regex to extract sectionshtml_fallback = re.search(r'===\s*index\.html\s*===\s*\n([\s\S]+?)(?=\n===|$)', text, re.IGNORECASE)js_fallback = re.search(r'===\s*index\.js\s*===\s*\n([\s\S]+?)(?=\n===|$)', text, re.IGNORECASE)css_fallback = re.search(r'===\s*style\.css\s*===\s*\n([\s\S]+?)(?=\n===|$)', text, re.IGNORECASE)if html_fallback:files['index.html'] = html_fallback.group(1).strip()if js_fallback:1 unmodified lineif css_fallback:files['style.css'] = css_fallback.group(1).strip()# Additional fallback: extract from numbered sections or file headersif not (files['index.html'] and files['index.js'] and files['style.css']):# Try patterns like "1. index.html:" or "**index.html**"patterns = [(r'(?:^\d+\.\s*|^##\s*|^\*\*\s*)index\.html(?:\s*:|\*\*:?)\s*\n([\s\S]+?)(?=\n(?:\d+\.|##|\*\*|===)|$)', 'index.html'),(r'(?:^\d+\.\s*|^##\s*|^\*\*\s*)index\.js(?:\s*:|\*\*:?)\s*\n([\s\S]+?)(?=\n(?:\d+\.|##|\*\*|===)|$)', 'index.js'),(r'(?:^\d+\.\s*|^##\s*|^\*\*\s*)style\.css(?:\s*:|\*\*:?)\s*\n([\s\S]+?)(?=\n(?:\d+\.|##|\*\*|===)|$)', 'style.css')]for pattern, file_key in patterns:if not files[file_key]:match = re.search(pattern, text, re.IGNORECASE | re.MULTILINE)if match:# Clean up the content by removing any code block markerscontent = match.group(1).strip()content = re.sub(r'^```\w*\s*\n', '', content)content = re.sub(r'\n```\s*$', '', content)files[file_key] = content.strip()return filesdef format_transformers_js_output(files):2060 unmodified linesdef hide_deploy_components(*args):return [gr.Textbox(visible=False), gr.Dropdown(visible=False), gr.Button(visible=False)]def update_deploy_button_text(space_name):"""Update deploy button text based on whether it's a new space or update"""if "/" in space_name.strip():return gr.update(value="๐ Update Space")else:return gr.update(value="๐ Deploy App")# Load project button eventload_project_btn.click(14 unmodified lines# Update preview when code or language changescode_output.change(preview_logic, inputs=[code_output, language_dropdown], outputs=sandbox)language_dropdown.change(preview_logic, inputs=[code_output, language_dropdown], outputs=sandbox)# Update deploy button text when space name changesspace_name_input.change(update_deploy_button_text, inputs=[space_name_input], outputs=[deploy_btn])clear_btn.click(clear_history, outputs=[history, history_output, file_input, website_url_input])clear_btn.click(hide_deploy_components, None, [space_name_input, sdk_dropdown, deploy_btn])# Reset space name and button text when clearing144 unmodified lineserror_prefix = "Error duplicating Streamlit space" if not is_update else "Error updating Streamlit space"return gr.update(value=f"{error_prefix}: {e}", visible=True)# Transformers.js logicelif sdk_name == "Transformers.js":try:# Only duplicate template space for new spaces, not updatesif not is_update:# Use duplicate_space to create a transformers.js template spacefrom huggingface_hub import duplicate_space# Duplicate the transformers.js template spaceduplicated_repo = duplicate_space(from_id="static-templates/transformers.js",to_id=space_name.strip(),token=token.token,exist_ok=True)print("Duplicated repo result:", duplicated_repo, type(duplicated_repo))# Parse the transformers.js output to get the three filesfiles = parse_transformers_js_output(code)79 unmodified linesexcept Exception as e:# Handle potential RepoUrl object errorserror_msg = str(e)if "'url'" in error_msg or "RepoUrl" in error_msg and not is_update:# Extract the URL from RepoUrl object if possibletry:if 'duplicated_repo' in locals() and hasattr(duplicated_repo, 'url'):repo_url = duplicated_repo.urlelif 'duplicated_repo' in locals() and hasattr(duplicated_repo, '_url'):repo_url = duplicated_repo._urlelse:repo_url = f"https://huggingface.co/spaces/{repo_id}"return gr.update(value=f"Error: Could not properly handle space creation response. Space may have been created successfully. Check: {repo_url}", visible=True)except: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)# General error handling for both creation and updatesaction_verb = "updating" if is_update else "duplicating"return gr.update(value=f"Error {action_verb} Transformers.js space: {error_msg}", visible=True)# Svelte logicelif sdk_name == "Svelte" and not is_update:try:
app.py+94-31