Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from pathlib import Path | |
| # ----------------------------- | |
| # Image paths | |
| # ----------------------------- | |
| BASE_DIR = Path(__file__).parent | |
| OUTPUT_IMAGES = [ | |
| BASE_DIR / "out1.png", | |
| BASE_DIR / "out2.png", | |
| BASE_DIR / "out3.png", | |
| BASE_DIR / "out4.png", | |
| BASE_DIR / "out5.png", | |
| ] | |
| # ----------------------------- | |
| # Analyze function | |
| # ----------------------------- | |
| def analyze_image(input_image): | |
| """ | |
| The user uploads an input image. | |
| When Analyze is clicked, the app returns the five preloaded output images. | |
| """ | |
| if input_image is None: | |
| raise gr.Error("Please upload an input image before clicking Analyze.") | |
| results = [] | |
| for img_path in OUTPUT_IMAGES: | |
| if img_path.exists(): | |
| results.append(str(img_path)) | |
| else: | |
| raise gr.Error(f"Missing image file: {img_path.name}") | |
| return results | |
| # ----------------------------- | |
| # Custom CSS | |
| # ----------------------------- | |
| custom_css = """ | |
| #analyze-btn { | |
| background: black !important; | |
| color: white !important; | |
| border: 1px solid black !important; | |
| } | |
| #analyze-btn:hover { | |
| background: #222222 !important; | |
| color: white !important; | |
| } | |
| """ | |
| # ----------------------------- | |
| # Gradio Interface | |
| # ----------------------------- | |
| with gr.Blocks(title="Cell Image Analysis", css=custom_css) as demo: | |
| gr.Markdown( | |
| """ | |
| # Cell Image Analysis Demo | |
| """ | |
| ) | |
| with gr.Row(): | |
| with gr.Column(): | |
| input_image = gr.Image( | |
| label="Upload Input Image", | |
| type="pil", | |
| height=400 | |
| ) | |
| analyze_button = gr.Button( | |
| "Analyze", | |
| variant="primary", | |
| elem_id="analyze-btn" | |
| ) | |
| with gr.Column(): | |
| output_gallery = gr.Gallery( | |
| label="Analysis Output Images", | |
| columns=2, | |
| rows=3, | |
| height="auto", | |
| object_fit="contain" | |
| ) | |
| analyze_button.click( | |
| fn=analyze_image, | |
| inputs=input_image, | |
| outputs=output_gallery | |
| ) | |
| # ----------------------------- | |
| # Launch app | |
| # ----------------------------- | |
| if __name__ == "__main__": | |
| demo.launch() |