import os import pandas as pd import numpy as np from sklearn.model_selection import train_test_split from sklearn.ensemble import RandomForestRegressor from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score import gradio as gr import io def train_and_predict(data, Qwater, Qgas, BHP, WHP, WHT, Tsep, Psep, Choke_in): # Load data from the uploaded CSV data = pd.read_csv(io.BytesIO(data)) # Define input features and target variable input_features = ['Qwater', 'Qgas', 'BHP', 'WHP', 'WHT', 'Tsep', 'Psep', 'Choke_in'] target_variable = 'Qoil' # Split the dataset into training and testing sets X = data[input_features] y = data[target_variable] X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # Train the random forest regression model rf = RandomForestRegressor(n_estimators=100, random_state=42) rf.fit(X_train, y_train) # Fine-tune the model rf_tuned = RandomForestRegressor(n_estimators=200, max_depth=10, random_state=42) rf_tuned.fit(X_train, y_train) # Make prediction new_input = [[Qwater, Qgas, BHP, WHP, WHT, Tsep, Psep, Choke_in]] predicted_qoil = rf_tuned.predict(new_input) return f"The number of barrels produced per day given these inputs is {predicted_qoil[0]}" iface = gr.Interface( fn=train_and_predict, inputs=[gr.inputs.File(type="bytes"), "number", "number", "number", "number", "number", "number", "number", "number"], outputs="text", theme = gr.themes.Monochrome( primary_hue="blue", secondary_hue="blue", neutral_hue="blue", ), title="Oil Flow Production & Optimization Application", description="""This application is the interface of a machine learning model (utilizing the Random Forest algorithm) that allows Oil and Gas executives with no coding knowledge or experience to enter inputs related to oil production and predict the number of barrels that will be produced per day given those inputs. The executive can engage in scenario planning to optimize oil flow by tweaking/changing various input values to see what would result in greater oil flow. The executive can also ask questions about the model or get explanations of the inputs and/or results via the attached GPT 3.5 chatbot. STEPS: Upload the oil production dataset. Enter the planned or anticipated input values (Qwater, Qgas, BHP, WHP, WHT, Tsep, Psep, Choke_in) and then click Submit. The model will initiate and complete its training and validation in the background using the dataset provided and then return the output value (Qoil) to the user.' Input Features: Qwater: Water Flow Rate Qgas: Gas Flow Rate BHP: Bottom Hole Pressure WHP: Wellhead Pressure WHT: Wellhead Temperature Tsep: Separator Temperature Psep: Separator Pressure Choke_in: Choke Valve Opening Target Variable: Qoil: Oil Flow Rate (measured in barrels per day) Ask questions about the meaning of these values using the application's AI chatbot here: https://aitechproducts.com/oilflowbot.html) Go back to: Oil & Gas Products""") iface.launch(auth=(os.environ['USERNAME1'],os.environ['PASSWORD1']))