Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -2039,6 +2039,70 @@ SITEMAP_XML = """<?xml version="1.0" encoding="UTF-8"?>
|
|
| 2039 |
|
| 2040 |
app = FastAPI()
|
| 2041 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2042 |
@app.get("/sitemap.xml")
|
| 2043 |
def sitemap():
|
| 2044 |
return Response(
|
|
|
|
| 2039 |
|
| 2040 |
app = FastAPI()
|
| 2041 |
|
| 2042 |
+
# ============================================================
|
| 2043 |
+
# SEO METADATA FIX - Remove Duplicate OG Tags
|
| 2044 |
+
# ============================================================
|
| 2045 |
+
|
| 2046 |
+
from fastapi import Response
|
| 2047 |
+
from fastapi.middleware.base import BaseHTTPMiddleware
|
| 2048 |
+
import re
|
| 2049 |
+
|
| 2050 |
+
class SEOMetadataMiddleware(BaseHTTPMiddleware):
|
| 2051 |
+
"""Intercept HTML responses and fix duplicate OG tags."""
|
| 2052 |
+
|
| 2053 |
+
async def dispatch(self, request, call_next):
|
| 2054 |
+
response = await call_next(request)
|
| 2055 |
+
|
| 2056 |
+
# Only process HTML responses
|
| 2057 |
+
if response.headers.get("content-type", "").startswith("text/html"):
|
| 2058 |
+
body = b""
|
| 2059 |
+
async for chunk in response.body_iterator:
|
| 2060 |
+
body += chunk
|
| 2061 |
+
|
| 2062 |
+
try:
|
| 2063 |
+
html = body.decode("utf-8")
|
| 2064 |
+
|
| 2065 |
+
# Remove the default Gradio OG image (first occurrence in <head>)
|
| 2066 |
+
default_og_pattern = r'<meta\s+property="og:image"\s+content="https://raw\.githubusercontent\.com/gradio-app/gradio/main/js/_website/src/lib/assets/img/header-image\.jpg"\s*/>'
|
| 2067 |
+
html = re.sub(default_og_pattern, '', html)
|
| 2068 |
+
|
| 2069 |
+
# Also remove the default Twitter image if needed
|
| 2070 |
+
default_twitter_pattern = r'<meta\s+name="twitter:image"\s+content="https://raw\.githubusercontent\.com/gradio-app/gradio/main/js/_website/src/lib/assets/img/header-image\.jpg"\s*/>'
|
| 2071 |
+
html = re.sub(default_twitter_pattern, '', html)
|
| 2072 |
+
|
| 2073 |
+
# Ensure the correct OG image is present (if missing, inject it)
|
| 2074 |
+
correct_og = '<meta property="og:image" content="https://huggingface.co/spaces/GoogleCalc/HowMuchFeed/resolve/main/social-preview.jpg">'
|
| 2075 |
+
if correct_og not in html:
|
| 2076 |
+
# Inject it right after the opening <head> tag
|
| 2077 |
+
html = html.replace('<head>', f'<head>\n {correct_og}')
|
| 2078 |
+
|
| 2079 |
+
# Inject SOCIAL_META if missing (optional but safe)
|
| 2080 |
+
# Check if your SOCIAL_META is already injected by Gradio
|
| 2081 |
+
social_meta_check = 'How Much Feed? — Broiler Feed Calculator'
|
| 2082 |
+
if social_meta_check not in html:
|
| 2083 |
+
# Inject the full SOCIAL_META from your app
|
| 2084 |
+
html = html.replace('<head>', f'<head>\n{SOCIAL_META}')
|
| 2085 |
+
|
| 2086 |
+
# Ensure we still have a valid OG image
|
| 2087 |
+
if not re.search(r'<meta\s+property="og:image"\s+content="', html):
|
| 2088 |
+
html = html.replace('</head>', f' {correct_og}\n</head>')
|
| 2089 |
+
|
| 2090 |
+
return Response(
|
| 2091 |
+
content=html.encode("utf-8"),
|
| 2092 |
+
status_code=response.status_code,
|
| 2093 |
+
headers=dict(response.headers),
|
| 2094 |
+
media_type=response.media_type,
|
| 2095 |
+
)
|
| 2096 |
+
except Exception as e:
|
| 2097 |
+
print(f"⚠️ SEO middleware error: {e}")
|
| 2098 |
+
# If anything fails, return original response
|
| 2099 |
+
return response
|
| 2100 |
+
|
| 2101 |
+
return response
|
| 2102 |
+
|
| 2103 |
+
# Add the middleware to your FastAPI app
|
| 2104 |
+
app.add_middleware(SEOMetadataMiddleware)
|
| 2105 |
+
|
| 2106 |
@app.get("/sitemap.xml")
|
| 2107 |
def sitemap():
|
| 2108 |
return Response(
|