File size: 2,242 Bytes
d16b422
 
c96c440
 
 
d16b422
c96c440
 
 
 
 
 
d16b422
c96c440
 
 
d16b422
 
c96c440
d16b422
c96c440
 
d16b422
 
c96c440
 
 
 
 
 
 
 
 
d16b422
 
 
 
c96c440
 
 
 
d16b422
6989673
 
 
 
 
 
 
 
 
 
c96c440
6989673
d16b422
 
 
c96c440
 
 
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
import gradio as gr
import csv
import gemini
import base64
import httpx

# Load the protected-birds.txt file into a dictionary
protected_birds_dict = {}
with open('protected-birds.txt', 'r') as txtfile:
    for line in txtfile:
        bird, confidence = line.strip().split(',')
        protected_birds_dict[bird.lower()] = float(confidence)

# Initialize the gemini client
def init_client(api_key):
    return gemini.Client(api_key)

# Function to check if the feather is from a protected bird
def check_feather(image_url, api_key):
    # Load the image
    image_media_type = "image/jpeg"
    image_data = base64.b64encode(httpx.get(image_url).content).decode("utf-8")
    
    # Make a prediction
    client = init_client(api_key)
    response = client.predict(
        model_id="text-bison-001",
        inputs={
            "image": image_data,
            "question": "Describe this image."
        }
    )
    model_predictions = response["candidates"][0]["output"]
    
    # Check if the feather is from a protected bird
    protected = False
    protected_birds_list = []
    for bird, confidence in protected_birds_dict.items():
        if bird in model_predictions.lower():
            protected = True
            protected_birds_list.append([bird, confidence, model_predictions])
    
    if protected:
        # Display the results
        gr.Interface(fn=lambda: protected_birds_list, inputs="text", outputs="text").launch()
        user_verification = gr.inputs.Checkbox(label="Verify the results")
        if user_verification:
            # Output the result in a table format
            gr.Interface(fn=lambda: protected_birds_list, inputs=user_verification, outputs="html").launch()
            # Prompt user to make a report
            gr.Interface(fn=lambda: "If confident, report to: https://www.fws.gov/contact-us", inputs=user_verification, outputs="text").launch()
    else:
        gr.Interface(fn=lambda: "No protected bird detected", inputs=image_url, outputs="text").launch()

    return protected, protected_birds_list

# Gradio interface
image_url = gr.inputs.Textbox()
api_key = gr.inputs.Textbox(label="Google API Key")
gr.Interface(fn=check_feather, inputs=[image_url, api_key], outputs="text").launch()