import gradio as gr import requests # Using a general-purpose model hosted on Hugging Face HF_MODEL_URL = "https://api-inference.huggingface.co/models/distilbert-base-uncased" HF_API_KEY = "YOUR_HUGGINGFACE_API_KEY" # Make sure your API key is valid # Function to get a response from the Hugging Face model API def get_concrete_mix_design(strength, slump, w_c_ratio, coarse_aggregate, fine_aggregate, mix_code): payload = { "inputs": { "text": f"Strength: {strength} MPa, Slump: {slump} mm, Water-Cement Ratio: {w_c_ratio}, " f"Coarse Aggregate: {coarse_aggregate} kg/m³, Fine Aggregate: {fine_aggregate} kg/m³, " f"Mix Code: {mix_code}" } } headers = {"Authorization": f"Bearer {HF_API_KEY}"} try: response = requests.post(HF_MODEL_URL, headers=headers, json=payload) response.raise_for_status() # Raise an HTTPError for bad responses (4xx, 5xx) if response.status_code == 200: result = response.json() generated_text = result.get('generated_text', 'No response from model.') return generated_text else: return f"Error: Received status code {response.status_code}. Response: {response.text}" except requests.exceptions.RequestException as e: return f"Request failed: {e}" # Function to handle the user interface logic def concrete_mix_ui(strength, slump, w_c_ratio, coarse_aggregate, fine_aggregate, mix_code): # Get the mix design based on the user's input and selected mix code mix_design = get_concrete_mix_design(strength, slump, w_c_ratio, coarse_aggregate, fine_aggregate, mix_code) return mix_design # User Interface: Define the input components inputs = [ gr.Slider(minimum=20, maximum=60, step=1, label="Compressive Strength (MPa)", value=30), gr.Slider(minimum=0, maximum=250, step=5, label="Slump (mm)", value=100), gr.Slider(minimum=0.3, maximum=0.7, step=0.01, label="Water-Cement Ratio", value=0.5), gr.Slider(minimum=100, maximum=1000, step=10, label="Coarse Aggregate (kg/m³)", value=600), gr.Slider(minimum=100, maximum=1000, step=10, label="Fine Aggregate (kg/m³)", value=500), gr.Dropdown(choices=["IS 456", "ACI", "BS", "Other"], label="Select Mix Design Code", value="IS 456") ] # Output component to display the result output = gr.Textbox(label="Concrete Mix Design Output", interactive=False) # Build the Gradio interface interface = gr.Interface( fn=concrete_mix_ui, inputs=inputs, outputs=output, title="Concrete Mix Design Calculator", description="Enter your concrete mix parameters and select the appropriate mix design code to get the best mix design.", live=True, theme="compact", ) # Launch the app with public link interface.launch(share=True)