Update app.py
Browse files
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 |
-
|
|
|
|
| 8 |
params = {
|
| 9 |
-
"format":
|
| 10 |
-
"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 |
-
|
| 19 |
-
|
| 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 |
-
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
gr.Number(label="Product Type ID (optional)", value=None),
|
| 38 |
-
]
|
| 39 |
-
|
| 40 |
-
outputs = gr.JSON(label="API Response")
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
# Launch the app
|
| 51 |
-
|
|
|
|
|
|
|
|
|
| 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()
|