Update MODEL.PY
Browse files
MODEL.PY
CHANGED
|
@@ -1,7 +1,11 @@
|
|
| 1 |
import pandas as pd
|
| 2 |
from datasets import Dataset
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
data = {
|
| 6 |
"Name": ["John Doe", "Jane Smith", "Mike Johnson"],
|
| 7 |
"Email": ["johndoe@example.com", "janesmith@example.com", "mikej@example.com"],
|
|
@@ -14,11 +18,56 @@ data = {
|
|
| 14 |
]
|
| 15 |
}
|
| 16 |
|
| 17 |
-
# Create a
|
| 18 |
df = pd.DataFrame(data)
|
| 19 |
|
| 20 |
# Convert the DataFrame to a Hugging Face Dataset
|
| 21 |
dataset = Dataset.from_pandas(df)
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import pandas as pd
|
| 2 |
from datasets import Dataset
|
| 3 |
+
from transformers import DistilBertForSequenceClassification, DistilBertTokenizer
|
| 4 |
+
import torch
|
| 5 |
+
import gradio as gr
|
| 6 |
+
from huggingface_hub import HfApi, Repository
|
| 7 |
|
| 8 |
+
# Step 1: Create a sample dataset
|
| 9 |
data = {
|
| 10 |
"Name": ["John Doe", "Jane Smith", "Mike Johnson"],
|
| 11 |
"Email": ["johndoe@example.com", "janesmith@example.com", "mikej@example.com"],
|
|
|
|
| 18 |
]
|
| 19 |
}
|
| 20 |
|
| 21 |
+
# Create a DataFrame
|
| 22 |
df = pd.DataFrame(data)
|
| 23 |
|
| 24 |
# Convert the DataFrame to a Hugging Face Dataset
|
| 25 |
dataset = Dataset.from_pandas(df)
|
| 26 |
|
| 27 |
+
# Step 2: Upload the dataset to Hugging Face
|
| 28 |
+
# Authenticate using your Hugging Face credentials
|
| 29 |
+
api = HfApi()
|
| 30 |
+
repo_id = "SailajaS/case-feedback-dataset" # Replace with your actual repo name
|
| 31 |
+
|
| 32 |
+
# Create a repo to store the dataset
|
| 33 |
+
api.create_repo(repo_id=repo_id, repo_type="dataset")
|
| 34 |
+
|
| 35 |
+
# Initialize the repository
|
| 36 |
+
repo = Repository(local_dir="./dataset_repo", clone_from=repo_id)
|
| 37 |
+
|
| 38 |
+
# Save the dataset to a CSV file
|
| 39 |
+
dataset.to_csv("./dataset_repo/dataset.csv")
|
| 40 |
+
|
| 41 |
+
# Push the dataset to Hugging Face
|
| 42 |
+
repo.push_to_hub()
|
| 43 |
+
|
| 44 |
+
# Step 3: Load pre-trained DistilBERT model and tokenizer
|
| 45 |
+
model_name = "distilbert-base-uncased"
|
| 46 |
+
model = DistilBertForSequenceClassification.from_pretrained(model_name)
|
| 47 |
+
tokenizer = DistilBertTokenizer.from_pretrained(model_name)
|
| 48 |
+
|
| 49 |
+
# Step 4: Define the prediction function
|
| 50 |
+
def predict_case_feedback(details):
|
| 51 |
+
inputs = tokenizer(details, return_tensors="pt", padding=True, truncation=True, max_length=512)
|
| 52 |
+
with torch.no_grad():
|
| 53 |
+
outputs = model(**inputs)
|
| 54 |
+
logits = outputs.logits
|
| 55 |
+
predicted_class = logits.argmax(dim=-1).item()
|
| 56 |
+
|
| 57 |
+
# Map predicted class to label
|
| 58 |
+
feedback_labels = ["Negative", "Positive", "Neutral"]
|
| 59 |
+
feedback = feedback_labels[predicted_class]
|
| 60 |
+
|
| 61 |
+
return f"Predicted Feedback: {feedback}"
|
| 62 |
+
|
| 63 |
+
# Step 5: Create Gradio interface for the prediction function
|
| 64 |
+
interface = gr.Interface(
|
| 65 |
+
fn=predict_case_feedback, # Prediction function
|
| 66 |
+
inputs="text", # Input: Text
|
| 67 |
+
outputs="text", # Output: Text
|
| 68 |
+
title="Case Feedback Prediction",
|
| 69 |
+
description="Enter the case details to predict feedback (Positive, Negative, Neutral)"
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
# Launch Gradio app
|
| 73 |
+
interface.launch()
|