Update app.py
Browse files
app.py
CHANGED
|
@@ -57,12 +57,12 @@ for col in required_columns:
|
|
| 57 |
# β
Convert "Case Problem" column to lowercase and remove spaces for consistency
|
| 58 |
df["Case Problem"] = df["Case Problem"].astype(str).str.strip().str.lower()
|
| 59 |
|
| 60 |
-
# β
Train and save LabelEncoder
|
| 61 |
encoder = LabelEncoder()
|
| 62 |
df["Case Problem Encoded"] = encoder.fit_transform(df["Case Problem"])
|
| 63 |
df["Feedback Encoded"] = encoder.fit_transform(df["Feedback"])
|
| 64 |
|
| 65 |
-
# β
Save encoder
|
| 66 |
joblib.dump(encoder, "case_problem_encoder.pkl")
|
| 67 |
|
| 68 |
# β
Train Model
|
|
@@ -72,7 +72,7 @@ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_
|
|
| 72 |
model = RandomForestClassifier(n_estimators=100, random_state=42)
|
| 73 |
model.fit(X_train, y_train)
|
| 74 |
|
| 75 |
-
# β
Save
|
| 76 |
joblib.dump(model, "feedback_model.pkl")
|
| 77 |
print("β
Model trained successfully!")
|
| 78 |
|
|
@@ -108,7 +108,7 @@ async def predict_feedback(data: PredictionInput):
|
|
| 108 |
except Exception as e:
|
| 109 |
return {"error": str(e)}
|
| 110 |
|
| 111 |
-
# β
Gradio UI with
|
| 112 |
def gradio_interface(case_problem):
|
| 113 |
if model is None:
|
| 114 |
return "Model not trained yet."
|
|
@@ -128,14 +128,15 @@ def gradio_interface(case_problem):
|
|
| 128 |
except Exception as e:
|
| 129 |
return f"Error: {str(e)}"
|
| 130 |
|
| 131 |
-
# β
Start
|
| 132 |
def start_app():
|
| 133 |
""" Start API and Gradio Interface """
|
| 134 |
gr_interface = gr.Interface(
|
| 135 |
fn=gradio_interface,
|
| 136 |
-
inputs="
|
| 137 |
-
outputs="
|
| 138 |
-
live=
|
|
|
|
| 139 |
)
|
| 140 |
gr_interface.launch(share=True, debug=True) # β
Debugging enabled
|
| 141 |
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
|
|
| 57 |
# β
Convert "Case Problem" column to lowercase and remove spaces for consistency
|
| 58 |
df["Case Problem"] = df["Case Problem"].astype(str).str.strip().str.lower()
|
| 59 |
|
| 60 |
+
# β
Train and save LabelEncoder
|
| 61 |
encoder = LabelEncoder()
|
| 62 |
df["Case Problem Encoded"] = encoder.fit_transform(df["Case Problem"])
|
| 63 |
df["Feedback Encoded"] = encoder.fit_transform(df["Feedback"])
|
| 64 |
|
| 65 |
+
# β
Save encoder
|
| 66 |
joblib.dump(encoder, "case_problem_encoder.pkl")
|
| 67 |
|
| 68 |
# β
Train Model
|
|
|
|
| 72 |
model = RandomForestClassifier(n_estimators=100, random_state=42)
|
| 73 |
model.fit(X_train, y_train)
|
| 74 |
|
| 75 |
+
# β
Save model
|
| 76 |
joblib.dump(model, "feedback_model.pkl")
|
| 77 |
print("β
Model trained successfully!")
|
| 78 |
|
|
|
|
| 108 |
except Exception as e:
|
| 109 |
return {"error": str(e)}
|
| 110 |
|
| 111 |
+
# β
Gradio UI with Submit button
|
| 112 |
def gradio_interface(case_problem):
|
| 113 |
if model is None:
|
| 114 |
return "Model not trained yet."
|
|
|
|
| 128 |
except Exception as e:
|
| 129 |
return f"Error: {str(e)}"
|
| 130 |
|
| 131 |
+
# β
Start API & Gradio with Submit button
|
| 132 |
def start_app():
|
| 133 |
""" Start API and Gradio Interface """
|
| 134 |
gr_interface = gr.Interface(
|
| 135 |
fn=gradio_interface,
|
| 136 |
+
inputs=gr.Textbox(label="Enter Case Problem"),
|
| 137 |
+
outputs=gr.Textbox(label="Predicted Feedback"),
|
| 138 |
+
live=False, # β
Submit button enabled
|
| 139 |
+
allow_flagging="never",
|
| 140 |
)
|
| 141 |
gr_interface.launch(share=True, debug=True) # β
Debugging enabled
|
| 142 |
uvicorn.run(app, host="0.0.0.0", port=8000)
|