remove website redesign
Browse files1092 unmodified lines10931094109510961097109810991100110111021103110411051106110711081109792 unmodified lines19021903190419051906190719081909191019113535 unmodified lines54475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578533 unmodified lines5819582058215822582358245825182 unmodified lines60086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160321645 unmodified lines76787679768076817682768376847685768676877688768937 unmodified lines772777287729773077317732773377347735773677377738536 unmodified lines827582768277827882798280828134 unmodified lines831683178318831983208321832236 unmodified lines83598360836183628363836483651092 unmodified lines# ConfigurationHTML_SYSTEM_PROMPT = """ONLY USE HTML, CSS AND JAVASCRIPT. If you want to use ICON make sure to import the library first. Try to create the best UI possible by using only HTML, CSS and JAVASCRIPT. MAKE IT RESPONSIVE USING MODERN CSS. Use as much as you can modern CSS for the styling, if you can't do something with modern CSS, then use custom CSS. Also, try to elaborate as much as you can, to create something unique. ALWAYS GIVE THE RESPONSE INTO A SINGLE HTML FILEFor website redesign tasks:- Use the provided original HTML code as the starting point for redesign- Preserve all original content, structure, and functionality- Keep the same semantic HTML structure but enhance the styling- Reuse all original images and their URLs from the HTML code- Create a modern, responsive design with improved typography and spacing- Use modern CSS frameworks and design patterns- Ensure accessibility and mobile responsiveness- Maintain the same navigation and user flow- Enhance the visual design while keeping the original layout structureIf an image is provided, analyze it and use the visual information to better understand the user's requirements.Always respond with code that can be executed or rendered directly.792 unmodified lines"title": "Extract Text from Image","description": "Upload an image containing text and I'll extract and process the text content"},{"title": "Website Redesign","description": "Enter a website URL to extract its content and redesign it with a modern, responsive layout"},{"title": "Modify HTML","description": "After generating HTML, ask me to modify it with specific changes using search/replace format"3535 unmodified lines# Return the first demo description as fallbackreturn DEMO_LIST[0]['description']def extract_website_content(url: str) -> str:"""Extract HTML code and content from a website URL"""try:# Validate URLparsed_url = urlparse(url)if not parsed_url.scheme:url = "https://" + urlparsed_url = urlparse(url)if not parsed_url.netloc:return "Error: Invalid URL provided"# Set comprehensive headers to mimic a real browser requestheaders = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36','Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8','Accept-Language': 'en-US,en;q=0.9','Accept-Encoding': 'gzip, deflate, br','DNT': '1','Connection': 'keep-alive','Upgrade-Insecure-Requests': '1','Sec-Fetch-Dest': 'document','Sec-Fetch-Mode': 'navigate','Sec-Fetch-Site': 'none','Sec-Fetch-User': '?1','Cache-Control': 'max-age=0'}# Create a session to maintain cookies and handle redirectssession = requests.Session()session.headers.update(headers)# Make the request with retry logicmax_retries = 3for attempt in range(max_retries):try:response = session.get(url, timeout=15, allow_redirects=True)response.raise_for_status()breakexcept requests.exceptions.HTTPError as e:if e.response.status_code == 403 and attempt < max_retries - 1:# Try with different User-Agent on 403session.headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'continueelse:raise# Get the raw HTML content with proper encodingtry:# Try to get the content with automatic encoding detectionresponse.encoding = response.apparent_encodingraw_html = response.textexcept:# Fallback to UTF-8 if encoding detection failsraw_html = response.content.decode('utf-8', errors='ignore')# Debug: Check if we got valid HTMLif not raw_html.strip().startswith('<!DOCTYPE') and not raw_html.strip().startswith('<html'):print(f"Warning: Response doesn't look like HTML. First 200 chars: {raw_html[:200]}")print(f"Response headers: {dict(response.headers)}")print(f"Response encoding: {response.encoding}")print(f"Apparent encoding: {response.apparent_encoding}")# Try alternative approachestry:raw_html = response.content.decode('latin-1', errors='ignore')print("Tried latin-1 decoding")except:try:raw_html = response.content.decode('utf-8', errors='ignore')print("Tried UTF-8 decoding")except:raw_html = response.content.decode('cp1252', errors='ignore')print("Tried cp1252 decoding")# Parse HTML content for analysissoup = BeautifulSoup(raw_html, 'html.parser')# Check if this is a JavaScript-heavy sitescript_tags = soup.find_all('script')if len(script_tags) > 10:print(f"Warning: This site has {len(script_tags)} script tags - it may be a JavaScript-heavy site")print("The content might be loaded dynamically and not available in the initial HTML")# Extract titletitle = soup.find('title')title_text = title.get_text().strip() if title else "No title found"# Extract meta descriptionmeta_desc = soup.find('meta', attrs={'name': 'description'})description = meta_desc.get('content', '') if meta_desc else ""# Extract main content areas for analysiscontent_sections = []main_selectors = ['main', 'article', '.content', '.main-content', '.post-content','#content', '#main', '.entry-content', '.post-body']for selector in main_selectors:elements = soup.select(selector)for element in elements:text = element.get_text().strip()if len(text) > 100: # Only include substantial contentcontent_sections.append(text)# Extract navigation links for analysisnav_links = []nav_elements = soup.find_all(['nav', 'header'])for nav in nav_elements:links = nav.find_all('a')for link in links:link_text = link.get_text().strip()link_href = link.get('href', '')if link_text and link_href:nav_links.append(f"{link_text}: {link_href}")# Extract and fix image URLs in the HTMLimg_elements = soup.find_all('img')for img in img_elements:src = img.get('src', '')if src:# Handle different URL formatsif src.startswith('//'):# Protocol-relative URLabsolute_src = 'https:' + srcimg['src'] = absolute_srcelif src.startswith('/'):# Root-relative URLabsolute_src = urljoin(url, src)img['src'] = absolute_srcelif not src.startswith(('http://', 'https://')):# Relative URLabsolute_src = urljoin(url, src)img['src'] = absolute_src# If it's already absolute, keep it as is# Also check for data-src (lazy loading) and other common attributesdata_src = img.get('data-src', '')if data_src and not src:# Use data-src if src is emptyif data_src.startswith('//'):absolute_data_src = 'https:' + data_srcimg['src'] = absolute_data_srcelif data_src.startswith('/'):absolute_data_src = urljoin(url, data_src)img['src'] = absolute_data_srcelif not data_src.startswith(('http://', 'https://')):absolute_data_src = urljoin(url, data_src)img['src'] = absolute_data_srcelse:img['src'] = data_src# Also fix background image URLs in style attributeselements_with_style = soup.find_all(attrs={'style': True})for element in elements_with_style:style_attr = element.get('style', '')# Find and replace relative URLs in background-imageimport rebg_pattern = r'background-image:\s*url\(["\']?([^"\']+)["\']?\)'matches = re.findall(bg_pattern, style_attr, re.IGNORECASE)for match in matches:if match:if match.startswith('//'):absolute_bg = 'https:' + matchstyle_attr = style_attr.replace(match, absolute_bg)elif match.startswith('/'):absolute_bg = urljoin(url, match)style_attr = style_attr.replace(match, absolute_bg)elif not match.startswith(('http://', 'https://')):absolute_bg = urljoin(url, match)style_attr = style_attr.replace(match, absolute_bg)element['style'] = style_attr# Fix background images in <style> tagsstyle_elements = soup.find_all('style')for style in style_elements:if style.string:style_content = style.string# Find and replace relative URLs in background-imagebg_pattern = r'background-image:\s*url\(["\']?([^"\']+)["\']?\)'matches = re.findall(bg_pattern, style_content, re.IGNORECASE)for match in matches:if match:if match.startswith('//'):absolute_bg = 'https:' + matchstyle_content = style_content.replace(match, absolute_bg)elif match.startswith('/'):absolute_bg = urljoin(url, match)style_content = style_content.replace(match, absolute_bg)elif not match.startswith(('http://', 'https://')):absolute_bg = urljoin(url, match)style_content = style_content.replace(match, absolute_bg)style.string = style_content# Extract images for analysis (after fixing URLs)images = []img_elements = soup.find_all('img')for img in img_elements:src = img.get('src', '')alt = img.get('alt', '')if src:images.append({'src': src, 'alt': alt})# Debug: Print some image URLs to see what we're gettingprint(f"Found {len(images)} images:")for i, img in enumerate(images[:5]): # Show first 5 imagesprint(f" {i+1}. {img['alt'] or 'No alt'} - {img['src']}")# Test a few image URLs to see if they're accessibledef test_image_url(img_url):try:test_response = requests.head(img_url, timeout=5, allow_redirects=True)return test_response.status_code == 200except:return False# Test first few imagesworking_images = []for img in images[:10]: # Test first 10 imagesif test_image_url(img['src']):working_images.append(img)else:print(f" ❌ Broken image: {img['src']}")print(f"Working images: {len(working_images)} out of {len(images)}")# Get the modified HTML with absolute URLsmodified_html = str(soup)# Clean and format the HTML for better readability# Remove unnecessary whitespace and commentsimport recleaned_html = re.sub(r'<!--.*?-->', '', modified_html, flags=re.DOTALL) # Remove HTML commentscleaned_html = re.sub(r'\s+', ' ', cleaned_html) # Normalize whitespacecleaned_html = re.sub(r'>\s+<', '><', cleaned_html) # Remove whitespace between tags# Limit HTML size to avoid token limits (keep first 15000 chars)if len(cleaned_html) > 15000:cleaned_html = cleaned_html[:15000] + "\n<!-- ... HTML truncated for length ... -->"# Check if we got any meaningful contentif not title_text or title_text == "No title found":title_text = url.split('/')[-1] or url.split('/')[-2] or "Website"# If we couldn't extract any meaningful content, provide a fallbackif len(cleaned_html.strip()) < 100:website_content = f"""WEBSITE REDESIGN - EXTRACTION FAILED====================================URL: {url}Title: {title_text}ERROR: Could not extract meaningful HTML content from this website. This could be due to:1. The website uses heavy JavaScript to load content dynamically2. The website has anti-bot protection3. The website requires authentication4. The website is using advanced compression or encodingFALLBACK APPROACH:Please create a modern, responsive website design for a {title_text.lower()} website. Since I couldn't extract the original content, you can:1. Create a typical layout for this type of website2. Use placeholder content that would be appropriate3. Include modern design elements and responsive features4. Use a clean, professional design with good typography5. Make it mobile-friendly and accessibleThe website appears to be: {title_text}"""return website_content.strip()# Compile the extracted content with the actual HTML codewebsite_content = f"""WEBSITE REDESIGN - ORIGINAL HTML CODE=====================================URL: {url}Title: {title_text}Description: {description}PAGE ANALYSIS:- This appears to be a {title_text.lower()} website- Contains {len(content_sections)} main content sections- Has {len(nav_links)} navigation links- Includes {len(images)} imagesIMAGES FOUND (use these exact URLs in your redesign):{chr(10).join([f"• {img['alt'] or 'Image'} - {img['src']}" for img in working_images[:20]]) if working_images else "No working images found"}ALL IMAGES (including potentially broken ones):{chr(10).join([f"• {img['alt'] or 'Image'} - {img['src']}" for img in images[:20]]) if images else "No images found"}ORIGINAL HTML CODE (use this as the base for redesign):```html{cleaned_html}```REDESIGN INSTRUCTIONS:Please redesign this website with a modern, responsive layout while:1. Preserving all the original content and structure2. Maintaining the same navigation and functionality3. Using the original images and their URLs (listed above)4. Creating a modern, clean design with improved typography and spacing5. Making it fully responsive for mobile devices6. Using modern CSS frameworks and best practices7. Keeping the same semantic structure but with enhanced stylingIMPORTANT: All image URLs in the HTML code above have been converted to absolute URLs and are ready to use. Make sure to preserve these exact image URLs in your redesigned version.The HTML code above contains the complete original website structure with all images properly linked. Use it as your starting point and create a modernized version."""return website_content.strip()except requests.exceptions.HTTPError as e:if e.response.status_code == 403:return f"Error: Website blocked access (403 Forbidden). This website may have anti-bot protection. Try a different website or provide a description of what you want to build instead."elif e.response.status_code == 404:return f"Error: Website not found (404). Please check the URL and try again."elif e.response.status_code >= 500:return f"Error: Website server error ({e.response.status_code}). Please try again later."else:return f"Error accessing website: HTTP {e.response.status_code} - {str(e)}"except requests.exceptions.Timeout:return "Error: Request timed out. The website may be slow or unavailable."except requests.exceptions.ConnectionError:return "Error: Could not connect to the website. Please check your internet connection and the URL."except requests.exceptions.RequestException as e:return f"Error accessing website: {str(e)}"except Exception as e:return f"Error extracting website content: {str(e)}"stop_generation = False33 unmodified lines}def generation_code(query: str | None, vlm_image: Optional[gr.Image], website_url: str | None, _setting: Dict[str, str], _history: Optional[History], _current_model: Dict, language: str = "html", provider: str = "auto", profile: gr.OAuthProfile | None = None, token: gr.OAuthToken | None = None):# Check authentication firstis_authenticated, auth_message = check_authentication(profile, token)if not is_authenticated:182 unmodified linesmessages = history_to_messages(_history, system_prompt)# Extract website content and append to query if website URL is presentwebsite_text = ""if website_url and website_url.strip():website_text = extract_website_content(website_url.strip())if website_text and not website_text.startswith("Error"):website_text = website_text[:8000] # Limit to 8000 chars for prompt sizequery = f"{query}\n\n[Website content to redesign below]\n{website_text}"elif website_text.startswith("Error"):# Provide helpful guidance when website extraction failsfallback_guidance = """Since I couldn't extract the website content, please provide additional details about what you'd like to build:1. What type of website is this? (e.g., e-commerce, blog, portfolio, dashboard)2. What are the main features you want?3. What's the target audience?4. Any specific design preferences? (colors, style, layout)This will help me create a better design for you."""query = f"{query}\n\n[Error extracting website: {website_text}]{fallback_guidance}"# Use the original query without search enhancementenhanced_query = query1645 unmodified lines"### Command Reference\n""- **Language**: 'use streamlit' | 'use gradio' | 'use html'\n""- **Model**: 'model <name>' (exact match to items in the Model dropdown)\n""- **Website redesign**: include a URL in your message (e.g., 'https://example.com')\n""- **Files**: attach documents or images directly for reference\n""- **Multiple directives**: separate with commas. The first segment is the main build prompt.\n\n""Examples:\n""- anycoder coffee shop website\n""- redesign https://example.com, use streamlit\n""- dashboard ui with minimalist design"))37 unmodified lineslabel="Code Language",visible=True)website_url_input = gr.Textbox(label="website for redesign",placeholder="https://example.com",lines=1,visible=True)image_input = gr.Image(label="UI design image",visible=False536 unmodified linesshow_progress="hidden",).then(generation_code,inputs=[input, image_input, website_url_input, setting, history, current_model, language_dropdown, provider_state],outputs=[code_output, history, history_output]).then(end_generation_ui,34 unmodified linesshow_progress="hidden",).then(generation_code,inputs=[input, image_input, website_url_input, setting, history, current_model, language_dropdown, provider_state],outputs=[code_output, history, history_output]).then(end_generation_ui,36 unmodified lines# 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, website_url_input])clear_btn.click(hide_deploy_components, None, [space_name_input, deploy_btn])# Reset space name and button text when clearingclear_btn.click(1092 unmodified lines109310941095109610971098792 unmodified lines1891189218931894189518963535 unmodified lines54325433543454355436543733 unmodified lines5471547254735474547554765477182 unmodified lines5660566156625663566456651645 unmodified lines731173127313731473157316731773187319732037 unmodified lines735873597360736173627363536 unmodified lines790079017902790379047905790634 unmodified lines794179427943794479457946794736 unmodified lines79847985798679877988798979901092 unmodified lines# ConfigurationHTML_SYSTEM_PROMPT = """ONLY USE HTML, CSS AND JAVASCRIPT. If you want to use ICON make sure to import the library first. Try to create the best UI possible by using only HTML, CSS and JAVASCRIPT. MAKE IT RESPONSIVE USING MODERN CSS. Use as much as you can modern CSS for the styling, if you can't do something with modern CSS, then use custom CSS. Also, try to elaborate as much as you can, to create something unique. ALWAYS GIVE THE RESPONSE INTO A SINGLE HTML FILEIf an image is provided, analyze it and use the visual information to better understand the user's requirements.Always respond with code that can be executed or rendered directly.792 unmodified lines"title": "Extract Text from Image","description": "Upload an image containing text and I'll extract and process the text content"},{"title": "Modify HTML","description": "After generating HTML, ask me to modify it with specific changes using search/replace format"3535 unmodified lines# Return the first demo description as fallbackreturn DEMO_LIST[0]['description']stop_generation = False33 unmodified lines}def generation_code(query: str | None, vlm_image: Optional[gr.Image], _setting: Dict[str, str], _history: Optional[History], _current_model: Dict, language: str = "html", provider: str = "auto", profile: gr.OAuthProfile | None = None, token: gr.OAuthToken | None = None):# Check authentication firstis_authenticated, auth_message = check_authentication(profile, token)if not is_authenticated:182 unmodified linesmessages = history_to_messages(_history, system_prompt)# Use the original query without search enhancementenhanced_query = query1645 unmodified lines"### Command Reference\n""- **Language**: 'use streamlit' | 'use gradio' | 'use html'\n""- **Model**: 'model <name>' (exact match to items in the Model dropdown)\n""- **Files**: attach documents or images directly for reference\n""- **Multiple directives**: separate with commas. The first segment is the main build prompt.\n\n""Examples:\n""- anycoder coffee shop website\n""- dashboard ui with minimalist design"))37 unmodified lineslabel="Code Language",visible=True)image_input = gr.Image(label="UI design image",visible=False536 unmodified linesshow_progress="hidden",).then(generation_code,inputs=[input, image_input, setting, history, current_model, language_dropdown, provider_state],outputs=[code_output, history, history_output]).then(end_generation_ui,34 unmodified linesshow_progress="hidden",).then(generation_code,inputs=[input, image_input, setting, history, current_model, language_dropdown, provider_state],outputs=[code_output, history, history_output]).then(end_generation_ui,36 unmodified lines# 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])clear_btn.click(hide_deploy_components, None, [space_name_input, deploy_btn])# Reset space name and button text when clearingclear_btn.click(
app.py+4-379