Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -2043,8 +2043,8 @@ app = FastAPI()
|
|
| 2043 |
# SEO METADATA FIX - Remove Duplicate OG Tags
|
| 2044 |
# ============================================================
|
| 2045 |
|
| 2046 |
-
from
|
| 2047 |
-
from
|
| 2048 |
import re
|
| 2049 |
|
| 2050 |
class SEOMetadataMiddleware(BaseHTTPMiddleware):
|
|
@@ -2054,38 +2054,30 @@ class SEOMetadataMiddleware(BaseHTTPMiddleware):
|
|
| 2054 |
response = await call_next(request)
|
| 2055 |
|
| 2056 |
# Only process HTML responses
|
| 2057 |
-
|
| 2058 |
-
|
| 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
|
| 2066 |
-
|
| 2067 |
-
html = re.sub(
|
| 2068 |
|
| 2069 |
-
#
|
| 2070 |
-
|
| 2071 |
-
html = re.sub(
|
| 2072 |
|
| 2073 |
-
#
|
| 2074 |
-
correct_og = '
|
| 2075 |
if correct_og not in html:
|
| 2076 |
-
# Inject it
|
| 2077 |
-
|
| 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"),
|
|
@@ -2095,14 +2087,13 @@ class SEOMetadataMiddleware(BaseHTTPMiddleware):
|
|
| 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
|
| 2104 |
app.add_middleware(SEOMetadataMiddleware)
|
| 2105 |
-
|
| 2106 |
@app.get("/sitemap.xml")
|
| 2107 |
def sitemap():
|
| 2108 |
return Response(
|
|
|
|
| 2043 |
# SEO METADATA FIX - Remove Duplicate OG Tags
|
| 2044 |
# ============================================================
|
| 2045 |
|
| 2046 |
+
from starlette.middleware.base import BaseHTTPMiddleware
|
| 2047 |
+
from starlette.responses import Response
|
| 2048 |
import re
|
| 2049 |
|
| 2050 |
class SEOMetadataMiddleware(BaseHTTPMiddleware):
|
|
|
|
| 2054 |
response = await call_next(request)
|
| 2055 |
|
| 2056 |
# Only process HTML responses
|
| 2057 |
+
content_type = response.headers.get("content-type", "")
|
| 2058 |
+
if "text/html" in content_type:
|
|
|
|
|
|
|
|
|
|
| 2059 |
try:
|
| 2060 |
+
# Read the response body
|
| 2061 |
+
body = b""
|
| 2062 |
+
async for chunk in response.body_iterator:
|
| 2063 |
+
body += chunk
|
| 2064 |
+
|
| 2065 |
html = body.decode("utf-8")
|
| 2066 |
|
| 2067 |
+
# Remove the default Gradio OG image
|
| 2068 |
+
default_og = 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*/>'
|
| 2069 |
+
html = re.sub(default_og, '', html)
|
| 2070 |
|
| 2071 |
+
# Remove default Twitter image
|
| 2072 |
+
default_twitter = 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*/>'
|
| 2073 |
+
html = re.sub(default_twitter, '', html)
|
| 2074 |
|
| 2075 |
+
# Check if correct OG image exists
|
| 2076 |
+
correct_og = 'content="https://huggingface.co/spaces/GoogleCalc/HowMuchFeed/resolve/main/social-preview.jpg"'
|
| 2077 |
if correct_og not in html:
|
| 2078 |
+
# Inject it
|
| 2079 |
+
og_tag = '<meta property="og:image" content="https://huggingface.co/spaces/GoogleCalc/HowMuchFeed/resolve/main/social-preview.jpg">'
|
| 2080 |
+
html = html.replace('<head>', f'<head>\n {og_tag}')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2081 |
|
| 2082 |
return Response(
|
| 2083 |
content=html.encode("utf-8"),
|
|
|
|
| 2087 |
)
|
| 2088 |
except Exception as e:
|
| 2089 |
print(f"⚠️ SEO middleware error: {e}")
|
|
|
|
| 2090 |
return response
|
| 2091 |
|
| 2092 |
return response
|
| 2093 |
|
| 2094 |
+
# Add the middleware AFTER the app is created
|
| 2095 |
app.add_middleware(SEOMetadataMiddleware)
|
| 2096 |
+
|
| 2097 |
@app.get("/sitemap.xml")
|
| 2098 |
def sitemap():
|
| 2099 |
return Response(
|