File size: 3,392 Bytes
a0562b3
 
6bde7ff
 
 
a0562b3
 
 
 
 
6bde7ff
 
a0562b3
 
6bde7ff
 
 
 
d682b63
a0562b3
 
 
 
 
 
6bde7ff
d682b63
 
 
 
126f7c1
 
d682b63
 
 
 
 
 
 
 
1785474
 
d682b63
 
 
 
 
 
 
a0562b3
 
d682b63
 
a0562b3
6bde7ff
a0562b3
d682b63
 
a0562b3
d682b63
a0562b3
 
 
 
d682b63
a0562b3
 
 
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
# https://www.gradio.app/guides/sharing-your-app#mounting-within-another-fast-api-app
import gradio as gr
import json
import logging
from PredictService import PredictService

log_format = "[%(asctime)s] [%(levelname)s] [%(name)s] %(message)s"
logging.basicConfig(level=logging.INFO, format=log_format)
logger = logging.getLogger()

svc = PredictService()


def api_classification(url):
    url_to_use = url
    if url_to_use == "exemple":
        url_to_use = "https://images.pexels.com/photos/326900/pexels-photo-326900.jpeg?cs=srgb&dl=pexels-pixabay-326900.jpg&fm=jpg"
    predictions = svc.predict(url_to_use)
    return json.dumps(predictions), url_to_use


with gr.Blocks() as app:
    with gr.Tab("BioCLIP API"):
        with gr.Row():
            with gr.Column():
                # https://www.gradio.app/guides/key-component-concepts
                gr.HTML(value="""
    This <a href="https://huggingface.co/spaces/3oly/grBird">Gradio BioCLIP API</a> is a <a href="https://github.com/Imageomics/bioclip">BioCLIP</a> based prediction.<br/><br/>
    <small>origin is a <a href="https://huggingface.co/spaces/imageomics/bioclip-demo">BioCLIP DEMO</a> 
    <a href="https://huggingface.co/spaces/imageomics/bioclip-demo/discussions/7">discussion</a>
    following <a href="https://github.com/boly38/pyBird">pyBird</a> MVP.<br/>
    This endpoint is used by <a href="https://bsky.app/profile/botensky.bsky.social">@botEnSky</a> BlueSky bot.</small>
    <br/>
    <h2>How to use Gradio UI ?</h2>
    <ul>
    <li>You must input a <b>public url of an image</b> and submit by clicking on "predict".</li>
    <li>You will get TreeOfLife predictions as result.</li>
    </ul>
    <h2>How to use Gradio API (Node.js, Python, Bash Curl) ?</h2>
    <ul>
    <li>You must follow the demo footer link called "Use via API". There is some example provided 
    (+ <a href="https://github.com/boly38/bioclipBootstrap?tab=readme-ov-file#content-for-3olygrbird">github bootstrap</a>).</li>
    </ul>
    <h2>Credits</h2>
    <ul>
    <li>This API endpoint is (only) an augmented fork of the <a href="https://github.com/Imageomics/bioclip">BioCLIP</a> : a wrapper that provide another form of API call.</li>
    <li>Research context, model, and contact are available at <a href="https://github.com/Imageomics/bioclip">Imageomics/bioclip</a>.</li>
    </ul>    
                """, show_label=False)
                api_input = gr.Textbox(
                    lines=1,
                    label="Input a public image url",
                    show_label=False,
                    info="Add image url here.",
                    value="https://natureconservancy-h.assetsadobe.com/is/image/content/dam/tnc/nature/en/photos/d/o/Downy-woodpecker-Matt-Williams.jpg?crop=0%2C39%2C3097%2C2322&wid=820&hei=615&scl=3.776829268292683"
                )
                api_classification_btn = gr.Button("predict", variant="primary")
                api_classification_output_gallery = gr.Image(label="Input image used")
            with gr.Column():
                api_classification_output_json = gr.JSON(label="This is classification result")  # https://www.gradio.app/docs/gradio/json

    api_classification_btn.click(
        fn=api_classification,
        inputs=[api_input],
        outputs=[api_classification_output_json, api_classification_output_gallery],
    )
app.queue(max_size=20)
app.launch()