Update app.py
Browse files
app.py
CHANGED
|
@@ -1,102 +1,20 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
# Function to download dataset
|
| 22 |
-
def download_dataset():
|
| 23 |
-
if not os.path.exists(DATASET_PATH):
|
| 24 |
-
print("π₯ Downloading dataset from Hugging Face...")
|
| 25 |
-
response = requests.get(DATASET_URL)
|
| 26 |
-
|
| 27 |
-
if response.status_code == 200:
|
| 28 |
-
with open(DATASET_PATH, "wb") as file:
|
| 29 |
-
file.write(response.content)
|
| 30 |
-
print("β
Dataset downloaded successfully!")
|
| 31 |
-
else:
|
| 32 |
-
print(f"β Failed to download dataset: {response.status_code}")
|
| 33 |
-
raise Exception("Dataset download failed.")
|
| 34 |
-
|
| 35 |
-
# β
Download dataset at startup
|
| 36 |
-
download_dataset()
|
| 37 |
-
|
| 38 |
-
# β
Load dataset
|
| 39 |
-
df = pd.read_csv(DATASET_PATH)
|
| 40 |
-
|
| 41 |
-
# β
Check if necessary columns exist
|
| 42 |
-
required_columns = ["Case Problem", "Feedback"]
|
| 43 |
-
for col in required_columns:
|
| 44 |
-
if col not in df.columns:
|
| 45 |
-
raise Exception(f"β Column '{col}' is missing from the dataset!")
|
| 46 |
-
|
| 47 |
-
# β
Encode categorical variables
|
| 48 |
-
encoder = LabelEncoder()
|
| 49 |
-
df["Case Problem"] = encoder.fit_transform(df["Case Problem"])
|
| 50 |
-
df["Feedback"] = encoder.fit_transform(df["Feedback"])
|
| 51 |
-
|
| 52 |
-
# β
Train Model
|
| 53 |
-
X = df[["Case Problem"]]
|
| 54 |
-
y = df["Feedback"]
|
| 55 |
-
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 56 |
-
model = RandomForestClassifier(n_estimators=100, random_state=42)
|
| 57 |
-
model.fit(X_train, y_train)
|
| 58 |
-
|
| 59 |
-
# β
Save model
|
| 60 |
-
joblib.dump(model, "feedback_model.pkl")
|
| 61 |
-
print("β
Model trained successfully!")
|
| 62 |
-
|
| 63 |
-
# β
API Input Model
|
| 64 |
-
class PredictionInput(BaseModel):
|
| 65 |
-
case_problem: str
|
| 66 |
-
|
| 67 |
-
@app.post("/predict/")
|
| 68 |
-
def predict_feedback(data: PredictionInput):
|
| 69 |
-
""" Predicts feedback based on Case Problem """
|
| 70 |
-
if model is None:
|
| 71 |
-
return {"error": "Model is not trained yet."}
|
| 72 |
-
|
| 73 |
-
try:
|
| 74 |
-
case_problem_encoded = encoder.transform([data.case_problem])
|
| 75 |
-
prediction = model.predict([[case_problem_encoded[0]]])
|
| 76 |
-
feedback_predicted = encoder.inverse_transform(prediction)[0]
|
| 77 |
-
return {"Predicted Feedback": feedback_predicted}
|
| 78 |
-
except Exception as e:
|
| 79 |
-
return {"error": str(e)}
|
| 80 |
-
|
| 81 |
-
# β
Gradio UI
|
| 82 |
-
def gradio_interface(case_problem):
|
| 83 |
-
if model is None:
|
| 84 |
-
return "Model not trained yet."
|
| 85 |
-
|
| 86 |
-
try:
|
| 87 |
-
case_problem_encoded = encoder.transform([case_problem])
|
| 88 |
-
prediction = model.predict([[case_problem_encoded[0]]])
|
| 89 |
-
feedback_predicted = encoder.inverse_transform(prediction)[0]
|
| 90 |
-
return f"Predicted Feedback: {feedback_predicted}"
|
| 91 |
-
except:
|
| 92 |
-
return "Invalid case problem. Please enter a valid category."
|
| 93 |
-
|
| 94 |
-
# β
Start both API & Gradio
|
| 95 |
-
def start_app():
|
| 96 |
-
""" Start API and Gradio Interface """
|
| 97 |
-
gr_interface = gr.Interface(fn=gradio_interface, inputs="text", outputs="text")
|
| 98 |
-
gr_interface.launch()
|
| 99 |
-
uvicorn.run(app, host="0.0.0.0", port=8000)
|
| 100 |
-
|
| 101 |
-
if __name__ == "__main__":
|
| 102 |
-
start_app()
|
|
|
|
| 1 |
+
runtime error
|
| 2 |
+
Exit code: 1. Reason: π₯ Downloading dataset from Hugging Face...
|
| 3 |
+
β
Dataset downloaded successfully!
|
| 4 |
+
Traceback (most recent call last):
|
| 5 |
+
File "/home/user/app/app.py", line 39, in <module>
|
| 6 |
+
df = pd.read_csv(DATASET_PATH)
|
| 7 |
+
File "/usr/local/lib/python3.10/site-packages/pandas/io/parsers/readers.py", line 1026, in read_csv
|
| 8 |
+
return _read(filepath_or_buffer, kwds)
|
| 9 |
+
File "/usr/local/lib/python3.10/site-packages/pandas/io/parsers/readers.py", line 626, in _read
|
| 10 |
+
return parser.read(nrows)
|
| 11 |
+
File "/usr/local/lib/python3.10/site-packages/pandas/io/parsers/readers.py", line 1923, in read
|
| 12 |
+
) = self._engine.read( # type: ignore[attr-defined]
|
| 13 |
+
File "/usr/local/lib/python3.10/site-packages/pandas/io/parsers/c_parser_wrapper.py", line 234, in read
|
| 14 |
+
chunks = self._reader.read_low_memory(nrows)
|
| 15 |
+
File "parsers.pyx", line 838, in pandas._libs.parsers.TextReader.read_low_memory
|
| 16 |
+
File "parsers.pyx", line 905, in pandas._libs.parsers.TextReader._read_rows
|
| 17 |
+
File "parsers.pyx", line 874, in pandas._libs.parsers.TextReader._tokenize_rows
|
| 18 |
+
File "parsers.pyx", line 891, in pandas._libs.parsers.TextReader._check_tokenize_status
|
| 19 |
+
File "parsers.pyx", line 2061, in pandas._libs.parsers.raise_parser_error
|
| 20 |
+
pandas.errors.ParserError: Error tokenizing data. C error: Expected 1 fields in line 5, saw 3
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|