Spaces:
Runtime error
Runtime error
| import logging | |
| import urllib.request | |
| from pathlib import Path | |
| # Set up logging | |
| logging.basicConfig( | |
| level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s" | |
| ) | |
| logger = logging.getLogger(__name__) | |
| # Sample X-ray image URLs (from public sources) | |
| SAMPLE_IMAGES = [ | |
| # Normal chest X-ray | |
| { | |
| "url": "https://prod-images-static.radiopaedia.org/images/53448173/322830a37f0fa0852773ca2db3e8d8_big_gallery.jpeg", | |
| "filename": "normal_chest_xray.jpg", | |
| "description": "Normal chest X-ray", | |
| }, | |
| # X-ray with pneumonia | |
| { | |
| "url": "https://prod-images-static.radiopaedia.org/images/52465460/e4d8791bd7502ab72af8d9e5c322db_big_gallery.jpg", | |
| "filename": "pneumonia_xray.jpg", | |
| "description": "X-ray with pneumonia", | |
| }, | |
| # X-ray with cardiomegaly | |
| { | |
| "url": "https://prod-images-static.radiopaedia.org/images/556520/cf17c05750adb04b2a6e23afb47c7d_big_gallery.jpg", | |
| "filename": "cardiomegaly_xray.jpg", | |
| "description": "X-ray with cardiomegaly", | |
| }, | |
| # X-ray with lung nodule | |
| { | |
| "url": "https://prod-images-static.radiopaedia.org/images/19972291/41eed1a2cdad06d26c3f415a6ed65a_big_gallery.jpeg", | |
| "filename": "nodule_xray.jpg", | |
| "description": "X-ray with lung nodule", | |
| }, | |
| ] | |
| def download_sample_images(output_dir="data/sample"): | |
| """ | |
| Download sample X-ray images for testing. | |
| Args: | |
| output_dir (str): Directory to save images | |
| Returns: | |
| list: Paths to downloaded images | |
| """ | |
| # Get the directory of the script | |
| script_dir = Path(__file__).resolve().parent.parent | |
| # Create output directory if it doesn't exist | |
| output_path = script_dir / output_dir | |
| output_path.mkdir(parents=True, exist_ok=True) | |
| downloaded_paths = [] | |
| for image in SAMPLE_IMAGES: | |
| try: | |
| filename = image["filename"] | |
| url = image["url"] | |
| output_file = output_path / filename | |
| # Skip if file already exists | |
| if output_file.exists(): | |
| logger.info(f"File already exists: {output_file}") | |
| downloaded_paths.append(str(output_file)) | |
| continue | |
| # Download the image | |
| logger.info(f"Downloading {url} to {output_file}") | |
| # Set a user agent to avoid blocking | |
| opener = urllib.request.build_opener() | |
| opener.addheaders = [("User-Agent", "Mozilla/5.0")] | |
| urllib.request.install_opener(opener) | |
| # Download the file | |
| urllib.request.urlretrieve(url, output_file) | |
| logger.info(f"Successfully downloaded {filename}") | |
| downloaded_paths.append(str(output_file)) | |
| except Exception as e: | |
| logger.error(f"Error downloading {image['url']}: {e}") | |
| logger.info( | |
| f"Downloaded {len(downloaded_paths)} out of {len(SAMPLE_IMAGES)} images" | |
| ) | |
| return downloaded_paths | |
| def create_sample_info_file(output_dir="data/sample"): | |
| """ | |
| Create a text file with information about the sample images. | |
| Args: | |
| output_dir (str): Directory with sample images | |
| """ | |
| # Get the directory of the script | |
| script_dir = Path(__file__).resolve().parent.parent | |
| # Output path | |
| output_path = script_dir / output_dir | |
| info_file = output_path / "sample_info.txt" | |
| with open(info_file, "w") as f: | |
| f.write("# Sample X-ray Images\n\n") | |
| for image in SAMPLE_IMAGES: | |
| f.write(f"## {image['filename']}\n") | |
| f.write(f"Description: {image['description']}\n") | |
| f.write(f"Source: {image['url']}\n\n") | |
| f.write( | |
| "\nThese images are used for testing and demonstration purposes only.\n" | |
| ) | |
| f.write( | |
| "Please note that these images are from public medical education sources.\n" | |
| ) | |
| f.write("Do not use for clinical decision making.\n") | |
| logger.info(f"Created sample info file: {info_file}") | |
| if __name__ == "__main__": | |
| # Download sample images | |
| downloaded_paths = download_sample_images() | |
| # Create info file | |
| create_sample_info_file() | |
| print(f"Downloaded {len(downloaded_paths)} sample images.") | |
| print("Run the application with: python app.py") | |