AIcoder35235 commited on
Commit
a707ddc
·
verified ·
1 Parent(s): ee16559

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -11
app.py CHANGED
@@ -3,6 +3,7 @@ from pydantic import BaseModel
3
  from fastapi.middleware.cors import CORSMiddleware
4
  from transformers import pipeline
5
  from PIL import Image
 
6
  import base64
7
  import io
8
  import requests
@@ -24,13 +25,49 @@ pipe = pipeline("image-classification", model="dima806/chest_xray_pneumonia_dete
24
  print("Agent Ready!")
25
 
26
  # --- REQUEST SCHEMA ---
27
- # Pydantic allows either field to be optional, so the user can send one or the other
28
  class PredictRequest(BaseModel):
29
  image: str | None = None
30
  image_url: str | None = None
31
 
32
- # --- ENDPOINTS ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
 
34
  @app.get("/health")
35
  def health_check():
36
  return {"status": "ok"}
@@ -40,16 +77,9 @@ def predict(req: PredictRequest):
40
  try:
41
  img = None
42
 
43
- # 1. Handle URL Input
44
  if req.image_url:
45
- # The fake ID card so Wikipedia lets our AI download the image
46
- headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"}
47
- response = requests.get(req.image_url, stream=True, headers=headers)
48
-
49
- if response.status_code != 200:
50
- raise Exception(f"Could not download image. Server returned: {response.status_code}")
51
-
52
- img = Image.open(response.raw).convert("RGB")
53
 
54
  # 2. Handle Base64 Input
55
  elif req.image:
 
3
  from fastapi.middleware.cors import CORSMiddleware
4
  from transformers import pipeline
5
  from PIL import Image
6
+ from bs4 import BeautifulSoup
7
  import base64
8
  import io
9
  import requests
 
25
  print("Agent Ready!")
26
 
27
  # --- REQUEST SCHEMA ---
 
28
  class PredictRequest(BaseModel):
29
  image: str | None = None
30
  image_url: str | None = None
31
 
32
+ # --- SMART FETCHER HELPER ---
33
+ def get_image_from_any_url(url: str):
34
+ """Smart fetcher that handles both raw images and HTML webpages."""
35
+ headers = {
36
+ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36",
37
+ "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
38
+ "Referer": "https://google.com"
39
+ }
40
+
41
+ # 1. Fetch whatever is at the URL
42
+ response = requests.get(url, headers=headers, timeout=10)
43
+ if response.status_code != 200:
44
+ raise Exception(f"Site blocked us (Error {response.status_code})")
45
+
46
+ content_type = response.headers.get('Content-Type', '').lower()
47
+
48
+ # 2. If it's already an image, just return it
49
+ if content_type.startswith('image/'):
50
+ return Image.open(io.BytesIO(response.content)).convert("RGB")
51
+
52
+ # 3. If it's a webpage, hunt for the main Open Graph image
53
+ elif content_type.startswith('text/html'):
54
+ print("Webpage detected! Scraping for the main image...")
55
+ soup = BeautifulSoup(response.text, 'html.parser')
56
+ og_image = soup.find('meta', property='og:image')
57
+
58
+ if og_image and og_image.get('content'):
59
+ actual_image_url = og_image['content']
60
+ print(f"Found hidden image at: {actual_image_url}")
61
+
62
+ img_response = requests.get(actual_image_url, headers=headers, timeout=10)
63
+ return Image.open(io.BytesIO(img_response.content)).convert("RGB")
64
+ else:
65
+ raise Exception("Could not find a main image on this webpage.")
66
+
67
+ else:
68
+ raise Exception(f"Unsupported link type: {content_type}")
69
 
70
+ # --- ENDPOINTS ---
71
  @app.get("/health")
72
  def health_check():
73
  return {"status": "ok"}
 
77
  try:
78
  img = None
79
 
80
+ # 1. Handle URL Input (Using the new Smart Fetcher)
81
  if req.image_url:
82
+ img = get_image_from_any_url(req.image_url)
 
 
 
 
 
 
 
83
 
84
  # 2. Handle Base64 Input
85
  elif req.image: