Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,7 @@ import gradio as gr
|
|
| 2 |
import pandas as pd
|
| 3 |
import joblib
|
| 4 |
import numpy as np
|
| 5 |
-
from typing import Dict, Any
|
| 6 |
|
| 7 |
# 1. Load the Model and Artifacts
|
| 8 |
# Model and artifact paths - adjust as necessary
|
|
@@ -87,7 +87,7 @@ def preprocess_data(data: Dict[str, Any]) -> pd.DataFrame:
|
|
| 87 |
return pd.DataFrame()
|
| 88 |
|
| 89 |
# 3. Prediction Function
|
| 90 |
-
def predict_vendor_score(*args: Any) ->
|
| 91 |
"""
|
| 92 |
Predicts the vendor performance score based on the input data from Vendor_Log__c.
|
| 93 |
Returns a dictionary containing the Subcontractor_Performance_Score__c fields.
|
|
@@ -97,11 +97,16 @@ def predict_vendor_score(*args: Any) -> Dict[str, Any]:
|
|
| 97 |
the Vendor_Log__c fields.
|
| 98 |
|
| 99 |
Returns:
|
| 100 |
-
|
| 101 |
-
and a final score percentage.
|
| 102 |
"""
|
| 103 |
if model is None:
|
| 104 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
# 1. Prepare input data as a dictionary, mapping to Vendor_Log__c fields
|
| 107 |
input_data = {
|
|
@@ -117,10 +122,13 @@ def predict_vendor_score(*args: Any) -> Dict[str, Any]:
|
|
| 117 |
# 2. Preprocess the data
|
| 118 |
processed_df = preprocess_data(input_data)
|
| 119 |
if processed_df.empty:
|
| 120 |
-
return
|
| 121 |
-
'
|
| 122 |
-
'
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
# 3. Make Prediction
|
| 126 |
try:
|
|
@@ -130,34 +138,36 @@ def predict_vendor_score(*args: Any) -> Dict[str, Any]:
|
|
| 130 |
# IMPORTANT: Adjust this mapping based on your model's output.
|
| 131 |
# This is a *crucial* step where you tell Gradio how to interpret
|
| 132 |
# the numbers coming from your model.
|
| 133 |
-
output_data =
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
}
|
| 141 |
return output_data
|
| 142 |
|
| 143 |
except Exception as e:
|
| 144 |
print(f"Error during prediction: {e}")
|
| 145 |
-
return
|
| 146 |
-
|
| 147 |
-
'
|
| 148 |
-
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
# 4. Gradio Interface
|
| 151 |
iface = gr.Interface(
|
| 152 |
fn=predict_vendor_score,
|
| 153 |
inputs=[
|
| 154 |
-
gr.Textbox(label="Quality Report (Long Text)", type="text"),
|
| 155 |
gr.Number(label="Delay Days (Number)"),
|
| 156 |
gr.Textbox(label="Incident Log (Long Text)", type="text"),
|
| 157 |
gr.Textbox(label="Vendor ID (Text)"), # Send ID
|
| 158 |
gr.Textbox(label="Work Details (Long Text)", type="text"),
|
| 159 |
-
gr.DateTime(label="Work Completion Date"),
|
| 160 |
-
gr.DateTime(label="Actual Completion Date"), #
|
| 161 |
],
|
| 162 |
outputs=[ # Changed to a list
|
| 163 |
gr.HighlightedText(label="Quality Score (%)"),
|
|
@@ -174,3 +184,4 @@ iface = gr.Interface(
|
|
| 174 |
# 5. Launch the Interface
|
| 175 |
if __name__ == "__main__":
|
| 176 |
iface.launch()
|
|
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import joblib
|
| 4 |
import numpy as np
|
| 5 |
+
from typing import Dict, Any, List
|
| 6 |
|
| 7 |
# 1. Load the Model and Artifacts
|
| 8 |
# Model and artifact paths - adjust as necessary
|
|
|
|
| 87 |
return pd.DataFrame()
|
| 88 |
|
| 89 |
# 3. Prediction Function
|
| 90 |
+
def predict_vendor_score(*args: Any) -> List[Any]: # Change return type
|
| 91 |
"""
|
| 92 |
Predicts the vendor performance score based on the input data from Vendor_Log__c.
|
| 93 |
Returns a dictionary containing the Subcontractor_Performance_Score__c fields.
|
|
|
|
| 97 |
the Vendor_Log__c fields.
|
| 98 |
|
| 99 |
Returns:
|
| 100 |
+
List[Any]: A list of output values.
|
|
|
|
| 101 |
"""
|
| 102 |
if model is None:
|
| 103 |
+
return [
|
| 104 |
+
'Model not loaded. Check server logs.', # Quality Score
|
| 105 |
+
'Model not loaded. Check server logs.', # Timeliness Score
|
| 106 |
+
'Model not loaded. Check server logs.', # Safety Score
|
| 107 |
+
'Model not loaded. Check server logs.', # Communication Score
|
| 108 |
+
'Model not loaded. Check server logs.', # Final Score
|
| 109 |
+
]
|
| 110 |
|
| 111 |
# 1. Prepare input data as a dictionary, mapping to Vendor_Log__c fields
|
| 112 |
input_data = {
|
|
|
|
| 122 |
# 2. Preprocess the data
|
| 123 |
processed_df = preprocess_data(input_data)
|
| 124 |
if processed_df.empty:
|
| 125 |
+
return [
|
| 126 |
+
'Error in input data. Check logs.', # Quality Score
|
| 127 |
+
'Error in input data. Check logs.', # Timeliness Score
|
| 128 |
+
'Error in input data. Check logs.', # Safety Score
|
| 129 |
+
'Error in input data. Check logs.', # Communication Score
|
| 130 |
+
'Error in input data. Check logs.' # Final Score
|
| 131 |
+
]
|
| 132 |
|
| 133 |
# 3. Make Prediction
|
| 134 |
try:
|
|
|
|
| 138 |
# IMPORTANT: Adjust this mapping based on your model's output.
|
| 139 |
# This is a *crucial* step where you tell Gradio how to interpret
|
| 140 |
# the numbers coming from your model.
|
| 141 |
+
output_data = [
|
| 142 |
+
predictions[0] * 100, # Quality Score (%)
|
| 143 |
+
predictions[1] * 100, # Timeliness Score (%)
|
| 144 |
+
predictions[2] * 100, # Safety Score (%)
|
| 145 |
+
predictions[3] * 100, # Communication Score (%)
|
| 146 |
+
(predictions[0] + predictions[1] + predictions[2] + predictions[3]) / 4 * 100, # Final Score (%)
|
| 147 |
+
]
|
|
|
|
| 148 |
return output_data
|
| 149 |
|
| 150 |
except Exception as e:
|
| 151 |
print(f"Error during prediction: {e}")
|
| 152 |
+
return [
|
| 153 |
+
f'Error during prediction: {e}', # Quality Score
|
| 154 |
+
f'Error during prediction: {e}', # Timeliness Score
|
| 155 |
+
f'Error during prediction: {e}', # Safety Score
|
| 156 |
+
f'Error during prediction: {e}', # Communication Score
|
| 157 |
+
f'Error during prediction: {e}' # Final Score
|
| 158 |
+
]
|
| 159 |
|
| 160 |
# 4. Gradio Interface
|
| 161 |
iface = gr.Interface(
|
| 162 |
fn=predict_vendor_score,
|
| 163 |
inputs=[
|
| 164 |
+
gr.Textbox(label="Quality Report (Long Text)", type="text"),
|
| 165 |
gr.Number(label="Delay Days (Number)"),
|
| 166 |
gr.Textbox(label="Incident Log (Long Text)", type="text"),
|
| 167 |
gr.Textbox(label="Vendor ID (Text)"), # Send ID
|
| 168 |
gr.Textbox(label="Work Details (Long Text)", type="text"),
|
| 169 |
+
gr.DateTime(label="Work Completion Date", type="datetime"), # Corrected to "datetime"
|
| 170 |
+
gr.DateTime(label="Actual Completion Date", type="datetime"), # Corrected to "datetime"
|
| 171 |
],
|
| 172 |
outputs=[ # Changed to a list
|
| 173 |
gr.HighlightedText(label="Quality Score (%)"),
|
|
|
|
| 184 |
# 5. Launch the Interface
|
| 185 |
if __name__ == "__main__":
|
| 186 |
iface.launch()
|
| 187 |
+
|