Spaces:
Sleeping
Sleeping
Fix Gradio 6.0 compatibility and improve image handling
Browse files
app.py
CHANGED
|
@@ -60,17 +60,17 @@ Response:"""
|
|
| 60 |
return True, ""
|
| 61 |
|
| 62 |
|
| 63 |
-
def search_anatomy_image(query: str) -> tuple[
|
| 64 |
"""
|
| 65 |
Search for anatomy images using SERPAPI Google Images.
|
| 66 |
-
Returns (
|
| 67 |
"""
|
| 68 |
try:
|
| 69 |
params = {
|
| 70 |
"engine": "google_images",
|
| 71 |
"q": f"{query} anatomy diagram",
|
| 72 |
"api_key": SERPAPI_KEY,
|
| 73 |
-
"num":
|
| 74 |
"safe": "active"
|
| 75 |
}
|
| 76 |
|
|
@@ -80,14 +80,23 @@ def search_anatomy_image(query: str) -> tuple[str, str]:
|
|
| 80 |
data = response.json()
|
| 81 |
|
| 82 |
if "images_results" in data and len(data["images_results"]) > 0:
|
| 83 |
-
# Get
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
else:
|
| 87 |
-
return
|
| 88 |
|
| 89 |
except Exception as e:
|
| 90 |
-
return
|
| 91 |
|
| 92 |
|
| 93 |
def download_image(image_url: str) -> Image.Image:
|
|
@@ -95,7 +104,14 @@ def download_image(image_url: str) -> Image.Image:
|
|
| 95 |
Download and return PIL Image from URL.
|
| 96 |
"""
|
| 97 |
try:
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
response.raise_for_status()
|
| 100 |
img = Image.open(BytesIO(response.content))
|
| 101 |
return img
|
|
@@ -155,19 +171,28 @@ def process_anatomy_query(query: str) -> tuple:
|
|
| 155 |
if not is_valid:
|
| 156 |
return None, "", validation_msg
|
| 157 |
|
| 158 |
-
# Search for
|
| 159 |
-
|
| 160 |
|
| 161 |
# Generate educational information
|
| 162 |
info = generate_anatomy_info(query)
|
| 163 |
|
| 164 |
-
#
|
| 165 |
image = None
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
|
| 172 |
# Prepare result
|
| 173 |
error_message = ""
|
|
@@ -178,7 +203,7 @@ def process_anatomy_query(query: str) -> tuple:
|
|
| 178 |
|
| 179 |
|
| 180 |
# Create Gradio interface
|
| 181 |
-
with gr.Blocks(
|
| 182 |
gr.Markdown(
|
| 183 |
"""
|
| 184 |
# 🩺 AnatomyBot - Your MBBS Anatomy Tutor
|
|
@@ -241,4 +266,4 @@ if __name__ == "__main__":
|
|
| 241 |
if not HYPERBOLIC_API_KEY or HYPERBOLIC_API_KEY == "your_hyperbolic_api_key_here":
|
| 242 |
print("⚠️ WARNING: HYPERBOLIC_API_KEY not configured in .env file")
|
| 243 |
|
| 244 |
-
demo.launch(share=False, server_name="0.0.0.0", server_port=
|
|
|
|
| 60 |
return True, ""
|
| 61 |
|
| 62 |
|
| 63 |
+
def search_anatomy_image(query: str) -> tuple[list, str]:
|
| 64 |
"""
|
| 65 |
Search for anatomy images using SERPAPI Google Images.
|
| 66 |
+
Returns (list_of_image_urls, error_message)
|
| 67 |
"""
|
| 68 |
try:
|
| 69 |
params = {
|
| 70 |
"engine": "google_images",
|
| 71 |
"q": f"{query} anatomy diagram",
|
| 72 |
"api_key": SERPAPI_KEY,
|
| 73 |
+
"num": 10, # Get more results for fallback
|
| 74 |
"safe": "active"
|
| 75 |
}
|
| 76 |
|
|
|
|
| 80 |
data = response.json()
|
| 81 |
|
| 82 |
if "images_results" in data and len(data["images_results"]) > 0:
|
| 83 |
+
# Get multiple image URLs, filter out SVG files
|
| 84 |
+
image_urls = []
|
| 85 |
+
for img in data["images_results"]:
|
| 86 |
+
url = img.get("original", "")
|
| 87 |
+
# Skip SVG files and other problematic formats
|
| 88 |
+
if url and not url.lower().endswith('.svg'):
|
| 89 |
+
image_urls.append(url)
|
| 90 |
+
|
| 91 |
+
if image_urls:
|
| 92 |
+
return image_urls, ""
|
| 93 |
+
else:
|
| 94 |
+
return [], "No supported image formats found (SVG files excluded)."
|
| 95 |
else:
|
| 96 |
+
return [], "No images found for this anatomy topic."
|
| 97 |
|
| 98 |
except Exception as e:
|
| 99 |
+
return [], f"Error searching for images: {str(e)}"
|
| 100 |
|
| 101 |
|
| 102 |
def download_image(image_url: str) -> Image.Image:
|
|
|
|
| 104 |
Download and return PIL Image from URL.
|
| 105 |
"""
|
| 106 |
try:
|
| 107 |
+
# Add headers to mimic a browser request and avoid 403 errors
|
| 108 |
+
headers = {
|
| 109 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
|
| 110 |
+
'Accept': 'image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8',
|
| 111 |
+
'Accept-Language': 'en-US,en;q=0.9',
|
| 112 |
+
'Referer': 'https://www.google.com/'
|
| 113 |
+
}
|
| 114 |
+
response = requests.get(image_url, headers=headers, timeout=10)
|
| 115 |
response.raise_for_status()
|
| 116 |
img = Image.open(BytesIO(response.content))
|
| 117 |
return img
|
|
|
|
| 171 |
if not is_valid:
|
| 172 |
return None, "", validation_msg
|
| 173 |
|
| 174 |
+
# Search for images
|
| 175 |
+
image_urls, img_error = search_anatomy_image(query)
|
| 176 |
|
| 177 |
# Generate educational information
|
| 178 |
info = generate_anatomy_info(query)
|
| 179 |
|
| 180 |
+
# Try to download images from the list until one succeeds
|
| 181 |
image = None
|
| 182 |
+
download_error = ""
|
| 183 |
+
|
| 184 |
+
if image_urls:
|
| 185 |
+
for url in image_urls[:5]: # Try up to 5 images
|
| 186 |
+
try:
|
| 187 |
+
image = download_image(url)
|
| 188 |
+
download_error = "" # Success!
|
| 189 |
+
break # Stop trying once we get a valid image
|
| 190 |
+
except Exception as e:
|
| 191 |
+
download_error = str(e)
|
| 192 |
+
continue # Try next image
|
| 193 |
+
|
| 194 |
+
if not image and download_error:
|
| 195 |
+
img_error = f"Could not download images. Last error: {download_error}"
|
| 196 |
|
| 197 |
# Prepare result
|
| 198 |
error_message = ""
|
|
|
|
| 203 |
|
| 204 |
|
| 205 |
# Create Gradio interface
|
| 206 |
+
with gr.Blocks(title="AnatomyBot - MBBS Anatomy Tutor") as demo:
|
| 207 |
gr.Markdown(
|
| 208 |
"""
|
| 209 |
# 🩺 AnatomyBot - Your MBBS Anatomy Tutor
|
|
|
|
| 266 |
if not HYPERBOLIC_API_KEY or HYPERBOLIC_API_KEY == "your_hyperbolic_api_key_here":
|
| 267 |
print("⚠️ WARNING: HYPERBOLIC_API_KEY not configured in .env file")
|
| 268 |
|
| 269 |
+
demo.launch(share=False, server_name="0.0.0.0", server_port=7861)
|