ClayTreeClay commited on
Commit
8ee0874
·
1 Parent(s): 8fa9571

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -0
app.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Python script, Unicode text, UTF-8 text executable, with CRLF line terminators
2
+ import requests
3
+ import json
4
+ from pprint import pprint
5
+ import gradio as gr
6
+
7
+ API_KEY = "2b109sYMpWyL50uehM9kUesDMO" # Your API_KEY here
8
+ PROJECT = "all"; # try specific floras: "weurope", "canada"
9
+ api_endpoint = f"https://my-api.plantnet.org/v2/identify/{PROJECT}?api-key={API_KEY}&lang=zh"
10
+
11
+ def identify_plant(image_paths, organs):
12
+ files = []
13
+ for image_path in image_paths:
14
+ image_data = open(image_path, 'rb')
15
+ files.append(('images', (image_path, image_data)))
16
+
17
+ data = {'organs': organs}
18
+ req = requests.Request('POST', url=api_endpoint, files=files, data=data)
19
+ prepared = req.prepare()
20
+
21
+ s = requests.Session()
22
+ response = s.send(prepared)
23
+ json_result = json.loads(response.text)
24
+
25
+ # Close the opened files
26
+ for _, (_, image_data) in files:
27
+ image_data.close()
28
+
29
+ return response.status_code, json_result
30
+
31
+
32
+ def gradio_interface(files, organs):
33
+ image_paths = [file.name for file in files]
34
+ print(image_paths)
35
+ status_code, json_result = identify_plant(image_paths, organs)
36
+
37
+ return json_result.get("bestMatch",None), json_result
38
+ with gr.Blocks() as demo:
39
+ file_output = gr.File()
40
+ upload_button = gr.UploadButton("Click to Upload a File", file_types=["image", "video"], file_count="multiple")
41
+ organs_input = gr.CheckboxGroup(choices=["flower", "leaf", "fruit", "bark", "habit"])
42
+ best_match_text = gr.Textbox()
43
+ json_text = gr.JSON()
44
+ upload_button.upload(gradio_interface, inputs=[upload_button,organs_input], outputs = [best_match_text,json_text])
45
+
46
+ #gr.Interface(fn=gradio_interface, inputs=[image_input, organs_input], outputs=outputs).launch()
47
+ demo.launch(share=True)
48
+
49
+