Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from fastapi import FastAPI
|
| 2 |
import pandas as pd
|
| 3 |
import uvicorn
|
| 4 |
import joblib
|
|
@@ -8,22 +8,30 @@ from sklearn.preprocessing import LabelEncoder
|
|
| 8 |
from pydantic import BaseModel
|
| 9 |
import gradio as gr
|
| 10 |
import os
|
|
|
|
| 11 |
|
| 12 |
app = FastAPI()
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# Function to load dataset
|
| 19 |
def load_data(file_path):
|
| 20 |
-
|
| 21 |
-
df = pd.read_csv(file_path)
|
| 22 |
-
elif file_path.endswith(".xlsx"):
|
| 23 |
-
df = pd.read_excel(file_path)
|
| 24 |
-
else:
|
| 25 |
-
raise ValueError("Unsupported file format. Please upload a CSV or Excel file.")
|
| 26 |
-
|
| 27 |
return df
|
| 28 |
|
| 29 |
# Placeholder for dataset and model
|
|
@@ -31,35 +39,26 @@ df = None
|
|
| 31 |
model = None
|
| 32 |
encoder = LabelEncoder()
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
""" Upload and process the dataset """
|
| 37 |
-
global df, model, encoder
|
| 38 |
-
|
| 39 |
-
file_path = os.path.join(UPLOAD_DIR, file.filename)
|
| 40 |
-
|
| 41 |
-
# Save the uploaded file
|
| 42 |
-
with open(file_path, "wb") as buffer:
|
| 43 |
-
buffer.write(await file.read())
|
| 44 |
-
|
| 45 |
-
# Load the dataset
|
| 46 |
-
df = load_data(file_path)
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
df["Feedback"] = encoder.fit_transform(df["Feedback"])
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 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 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
|
|
|
|
|
|
|
| 63 |
|
| 64 |
# API Input Model
|
| 65 |
class PredictionInput(BaseModel):
|
|
@@ -69,7 +68,7 @@ class PredictionInput(BaseModel):
|
|
| 69 |
def predict_feedback(data: PredictionInput):
|
| 70 |
""" Predicts feedback based on Case Problem """
|
| 71 |
if model is None:
|
| 72 |
-
return {"error": "Model is not trained yet.
|
| 73 |
|
| 74 |
case_problem_encoded = encoder.transform([data.case_problem])
|
| 75 |
prediction = model.predict([[case_problem_encoded[0]]])
|
|
@@ -79,7 +78,7 @@ def predict_feedback(data: PredictionInput):
|
|
| 79 |
# Gradio UI
|
| 80 |
def gradio_interface(case_problem):
|
| 81 |
if model is None:
|
| 82 |
-
return "Model not trained yet.
|
| 83 |
|
| 84 |
case_problem_encoded = encoder.transform([case_problem])
|
| 85 |
prediction = model.predict([[case_problem_encoded[0]]])
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
import pandas as pd
|
| 3 |
import uvicorn
|
| 4 |
import joblib
|
|
|
|
| 8 |
from pydantic import BaseModel
|
| 9 |
import gradio as gr
|
| 10 |
import os
|
| 11 |
+
import requests
|
| 12 |
|
| 13 |
app = FastAPI()
|
| 14 |
|
| 15 |
+
# Hugging Face Dataset URL
|
| 16 |
+
DATASET_URL = "https://huggingface.co/datasets/SailajaS/CDART/resolve/main/train.csv"
|
| 17 |
+
|
| 18 |
+
# File path for saving dataset
|
| 19 |
+
DATASET_PATH = "dataset.csv"
|
| 20 |
+
|
| 21 |
+
# Function to download dataset
|
| 22 |
+
def download_dataset():
|
| 23 |
+
if not os.path.exists(DATASET_PATH):
|
| 24 |
+
response = requests.get(DATASET_URL)
|
| 25 |
+
if response.status_code == 200:
|
| 26 |
+
with open(DATASET_PATH, "wb") as file:
|
| 27 |
+
file.write(response.content)
|
| 28 |
+
print("✅ Dataset downloaded successfully!")
|
| 29 |
+
else:
|
| 30 |
+
raise Exception(f"❌ Failed to download dataset: {response.status_code}")
|
| 31 |
|
| 32 |
# Function to load dataset
|
| 33 |
def load_data(file_path):
|
| 34 |
+
df = pd.read_csv(file_path) # Load CSV directly
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
return df
|
| 36 |
|
| 37 |
# Placeholder for dataset and model
|
|
|
|
| 39 |
model = None
|
| 40 |
encoder = LabelEncoder()
|
| 41 |
|
| 42 |
+
# Download dataset at startup
|
| 43 |
+
download_dataset()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
# Load dataset
|
| 46 |
+
df = load_data(DATASET_PATH)
|
|
|
|
| 47 |
|
| 48 |
+
# Encode categorical variables
|
| 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):
|
|
|
|
| 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 |
case_problem_encoded = encoder.transform([data.case_problem])
|
| 74 |
prediction = model.predict([[case_problem_encoded[0]]])
|
|
|
|
| 78 |
# Gradio UI
|
| 79 |
def gradio_interface(case_problem):
|
| 80 |
if model is None:
|
| 81 |
+
return "Model not trained yet."
|
| 82 |
|
| 83 |
case_problem_encoded = encoder.transform([case_problem])
|
| 84 |
prediction = model.predict([[case_problem_encoded[0]]])
|