import gradio as gr import requests # Define the function to call the model hosted on Hugging Face def get_fertilizer_recommendation(crop_name, target_yield, field_size, ph_water, organic_carbon, total_nitrogen, phosphorus, potassium, soil_moisture): # Replace this with the URL of your Hugging Face model url = "https://api-inference.huggingface.co/models/your-model-name" headers = {"Authorization": "Bearer YOUR_HUGGING_FACE_API_KEY"} # Define the input payload payload = { 'Crop Name': [crop_name], 'Target Yield': [target_yield], 'Field Size': [field_size], 'pH (water)': [ph_water], 'Organic Carbon': [organic_carbon], 'Total Nitrogen': [total_nitrogen], 'Phosphorus (M3)': [phosphorus], 'Potassium (exch.)': [potassium], 'Soil moisture': [soil_moisture] } # Make the request to the model response = requests.post(url, headers=headers, json=payload) result = response.json() # Extract the relevant output fields output = f""" Predicted Fertilizer Requirements: Nitrogen (N) Need: {result['Nitrogen (N) Need']} Phosphorus (P2O5) Need: {result['Phosphorus (P2O5) Need']} Potassium (K2O) Need: {result['Potassium (K2O) Need']} Organic Matter Need: {result['Organic Matter Need']} Lime Need: {result['Lime Need']} Lime Application - Requirement: {result['Lime Application - Requirement']} Organic Matter Application - Requirement: {result['Organic Matter Application - Requirement']} 1st Application - Requirement (1): {result['1st Application - Requirement (1)']} 1st Application - Requirement (2): {result['1st Application - Requirement (2)']} Lime Application - Instruction: {result['Lime Application - Instruction']} Lime Application: {result['Lime Application']} Organic Matter Application - Instruction: {result['Organic Matter Application - Instruction']} Organic Matter Application: {result['Organic Matter Application']} 1st Application: {result['1st Application']} 1st Application - Type fertilizer (1): {result['1st Application - Type fertilizer (1)']} 1st Application - Type fertilizer (2): {result['1st Application - Type fertilizer (2)']} """ return output # Create the Gradio interface interface = gr.Interface( fn=get_fertilizer_recommendation, inputs=[ gr.inputs.Textbox(label="Crop Name"), gr.inputs.Number(label="Target Yield"), gr.inputs.Number(label="Field Size"), gr.inputs.Number(label="pH (water)"), gr.inputs.Number(label="Organic Carbon"), gr.inputs.Number(label="Total Nitrogen"), gr.inputs.Number(label="Phosphorus (M3)"), gr.inputs.Number(label="Potassium (exch.)"), gr.inputs.Number(label="Soil moisture"), ], outputs="text", title="Fertilizer Application Advisor", description="Input the details of your field and get fertilizer recommendations." ) # Launch the interface interface.launch()