File size: 3,384 Bytes
e59eba8
e9ddc31
3eaa3b6
ddd3e5e
e59eba8
3eaa3b6
e59eba8
e9ddc31
 
 
 
 
 
 
 
 
 
3eaa3b6
e9ddc31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ddd3e5e
e9ddc31
 
73abdf6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import gradio as gr
from services.analyse_image import ErrorResponse, analyze_image
from dotenv import load_dotenv
from utils.utils import image_to_base64_data_uri

load_dotenv()

async def upload_and_analyse(image_path):
    if image_path is None:
        return (
            gr.update(visible=False),
            gr.update(visible=False),
            gr.update(visible=False),
            "❗ No image provided."
        )

    try:
        data_url = image_to_base64_data_uri(image_path)
        response = await analyze_image(data_url)

        if isinstance(response, ErrorResponse):
            return (
                gr.update(visible=False),
                gr.update(visible=False),
                gr.update(visible=False),
                f"❌ API Error: {response.status_code} - {response.error}"
            )

        obj_type = response.object_type
        data = response.data

        if obj_type == "car":
            return (
                gr.update(value=[
                    ["Brand", data.brand],
                    ["Color", data.color],
                    ["Type", data.type],
                    ["Other Details", getattr(data, "other_details", "")]
                ], visible=True),
                gr.update(visible=False),
                gr.update(visible=False),
                "βœ… Detected object: Car"
            )
        elif obj_type == "animal":
            return (
                gr.update(visible=False),
                gr.update(value=[
                    ["Name", data.name],
                    ["Color", data.color],
                    ["Other Details", getattr(data, "other_details", "")]
                ], visible=True),
                gr.update(visible=False),
                "βœ… Detected object: Animal"
            )
        elif obj_type == "flower":
            return (
                gr.update(visible=False),
                gr.update(visible=False),
                gr.update(value=[
                    ["Name", data.name],
                    ["Color", data.color]
                ], visible=True),
                "βœ… Detected object: Flower"
            )
        else:
            return (
                gr.update(visible=False),
                gr.update(visible=False),
                gr.update(visible=False),
                f"⚠️ Unknown object type: {obj_type}"
            )

    except Exception as e:
        return (
            gr.update(visible=False),
            gr.update(visible=False),
            gr.update(visible=False),
            f"❌ Exception: {str(e)}"
        )
        
with gr.Blocks() as demo:
    gr.Markdown("## 🧠 Upload an Image to Analyze (Car, Animal, Flower)")

    image_input = gr.Image(type="filepath", label="Upload Image")
    btn = gr.Button("Analyze")

    car_block = gr.Dataframe(headers=["Field", "Value"], label="πŸš— Car Info", visible=False)
    animal_block = gr.Dataframe(headers=["Field", "Value"], label="🐾 Animal Info", visible=False)
    flower_block = gr.Dataframe(headers=["Field", "Value"], label="🌸 Flower Info", visible=False)

    status = gr.Textbox(label="Status", interactive=False)

    btn.click(
        fn=upload_and_analyse,
        inputs=image_input,
        outputs=[car_block, animal_block, flower_block, status],
        show_progress=True  # πŸ› οΈ Important when using async fn
    )

demo.launch(share=True)