MMOON commited on
Commit
9f15b58
·
verified ·
1 Parent(s): c0ebbe1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -35
app.py CHANGED
@@ -4,48 +4,51 @@ import requests
4
  # API endpoint
5
  BASE_URL = "https://api.datalake.sante.service.ec.europa.eu/sante/pesticides/pesticide_residues_products"
6
 
7
- def fetch_data(format, language, product_id, product_parent_id, product_code, product_type_id):
 
8
  params = {
9
- "format": format,
10
- "language": language,
11
- "product_id": product_id,
12
- "product_parent_id": product_parent_id,
13
- "product_code": product_code,
14
- "product_type_id": product_type_id,
15
  "api-version": "v2.0"
16
  }
17
-
18
- # Remove None values from params
19
- params = {k: v for k, v in params.items() if v is not None}
20
-
21
- # Make the API request
22
- headers = {"Content-Type": "application/json"}
23
  response = requests.get(BASE_URL, params=params, headers=headers)
24
-
25
  if response.status_code == 200:
26
- return response.json()
27
  else:
28
- return f"Error: {response.status_code} - {response.text}"
29
 
30
- # Gradio interface
31
- inputs = [
32
- gr.Dropdown(["json", "xml"], label="Format", value="json"),
33
- gr.Dropdown(["en", "fr", "de"], label="Language", value="en"),
34
- gr.Number(label="Product ID (optional)", value=None),
35
- gr.Number(label="Product Parent ID (optional)", value=None),
36
- gr.Textbox(label="Product Code (optional)", value=""),
37
- gr.Number(label="Product Type ID (optional)", value=None),
38
- ]
39
-
40
- outputs = gr.JSON(label="API Response")
41
 
42
- interface = gr.Interface(
43
- fn=fetch_data,
44
- inputs=inputs,
45
- outputs=outputs,
46
- title="Pesticide Residues Products API Explorer",
47
- description="Fetch data from the pesticide residues products API."
48
- )
 
 
 
 
 
 
 
 
 
 
 
49
 
50
  # Launch the app
51
- interface.launch()
 
 
 
4
  # API endpoint
5
  BASE_URL = "https://api.datalake.sante.service.ec.europa.eu/sante/pesticides/pesticide_residues_products"
6
 
7
+ # Fetch the list of products from the API
8
+ def fetch_products():
9
  params = {
10
+ "format": "json",
11
+ "language": "en",
 
 
 
 
12
  "api-version": "v2.0"
13
  }
14
+ headers = {
15
+ "Content-Type": "application/json",
16
+ "Cache-Control": "no-cache"
17
+ }
 
 
18
  response = requests.get(BASE_URL, params=params, headers=headers)
 
19
  if response.status_code == 200:
20
+ return response.json()["value"]
21
  else:
22
+ raise Exception(f"Failed to fetch products: {response.status_code} - {response.text}")
23
 
24
+ # Fetch details for a specific product
25
+ def fetch_product_details(product_name):
26
+ products = fetch_products()
27
+ for product in products:
28
+ if product["product_name"] == product_name:
29
+ return product
30
+ return f"Product '{product_name}' not found."
 
 
 
 
31
 
32
+ # Gradio interface
33
+ def create_interface():
34
+ # Fetch the list of products
35
+ products = fetch_products()
36
+ product_names = [product["product_name"] for product in products]
37
+
38
+ # Define the Gradio interface
39
+ inputs = gr.Dropdown(product_names, label="Select a Product")
40
+ outputs = gr.JSON(label="Product Details")
41
+
42
+ interface = gr.Interface(
43
+ fn=fetch_product_details,
44
+ inputs=inputs,
45
+ outputs=outputs,
46
+ title="Pesticide Residues Products Explorer",
47
+ description="Select a product to view its details."
48
+ )
49
+ return interface
50
 
51
  # Launch the app
52
+ if __name__ == "__main__":
53
+ interface = create_interface()
54
+ interface.launch()