sailajavuriti commited on
Commit
d217612
·
verified ·
1 Parent(s): 7186f0f

Create model.py

Browse files
Files changed (1) hide show
  1. model.py +76 -0
model.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ import os
8
+
9
+ # Step 1: Set your Hugging Face access token
10
+ hf_token = "" # Replace with your actual token
11
+
12
+ # Initialize the Hugging Face API client
13
+ api = HfApi()
14
+
15
+ # Step 2: Create a sample dataset
16
+ data = {
17
+ "Name": ["John Doe", "Jane Smith", "Mike Johnson"],
18
+ "Email": ["johndoe@example.com", "janesmith@example.com", "mikej@example.com"],
19
+ "Case Problem": ["Login Issues", "Payment Failure", "UI Bug"],
20
+ "Feedback": ["Negative", "Positive", "Neutral"],
21
+ "Details": [
22
+ "Unable to login after password reset.",
23
+ "Payment went through after retrying.",
24
+ "The interface is a bit confusing at times."
25
+ ]
26
+ }
27
+
28
+ # Create a DataFrame from the sample data
29
+ df = pd.DataFrame(data)
30
+
31
+ # Convert the DataFrame into a Hugging Face Dataset
32
+ dataset = Dataset.from_pandas(df)
33
+
34
+ # Step 3: Upload the dataset to Hugging Face
35
+ repo_id = "SailajaS/case-feedback-dataset" # Replace with your actual Hugging Face repo name
36
+ try:
37
+ # Create a repository on Hugging Face to store the dataset
38
+ api.create_repo(repo_id=repo_id, repo_type="dataset", token=hf_token)
39
+ print(f"Successfully created repository: {repo_id}")
40
+ except Exception as e:
41
+ print(f"Error creating repository: {e}")
42
+
43
+ # Initialize the repository and push the dataset to Hugging Face
44
+ repo = Repository(local_dir="./dataset_repo", clone_from=repo_id)
45
+ dataset.to_csv("./dataset_repo/dataset.csv")
46
+ repo.push_to_hub()
47
+
48
+ # Step 4: Load pre-trained DistilBERT model and tokenizer
49
+ model_name = "distilbert-base-uncased"
50
+ model = DistilBertForSequenceClassification.from_pretrained(model_name)
51
+ tokenizer = DistilBertTokenizer.from_pretrained(model_name)
52
+
53
+ # Step 5: Define a function to predict feedback based on case details
54
+ def predict_case_feedback(details):
55
+ inputs = tokenizer(details, return_tensors="pt", padding=True, truncation=True, max_length=512)
56
+ with torch.no_grad():
57
+ outputs = model(**inputs)
58
+ logits = outputs.logits
59
+ predicted_class = logits.argmax(dim=-1).item()
60
+
61
+ feedback_labels = ["Negative", "Positive", "Neutral"]
62
+ feedback = feedback_labels[predicted_class]
63
+
64
+ return f"Predicted Feedback: {feedback}"
65
+
66
+ # Step 6: Create Gradio interface for the prediction function
67
+ interface = gr.Interface(
68
+ fn=predict_case_feedback,
69
+ inputs="text", # Input: Text
70
+ outputs="text", # Output: Text
71
+ title="Case Feedback Prediction",
72
+ description="Enter the case details to predict feedback (Positive, Negative, Neutral)"
73
+ )
74
+
75
+ # Step 7: Launch the Gradio app
76
+ interface.launch()