Lwasinam commited on
Commit
587c4c4
1 Parent(s): b6c33b1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -0
app.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import DistilBertTokenizer, DistilBertForSequenceClassification
3
+
4
+ def load_model(model_path, device):
5
+ model = DistilBertForSequenceClassification.from_pretrained(model_path)
6
+ model.to(device)
7
+ model.eval()
8
+ return model
9
+
10
+ def run_inference(model, tokenizer, label_decoder, device, user_input):
11
+ model.eval() # Set the model to evaluation mode
12
+
13
+ # user_input = input("Enter a text for prediction: ")
14
+
15
+ # Tokenize user input
16
+ input_ids = tokenizer.encode(user_input, return_tensors="pt").to(device)
17
+
18
+ with torch.no_grad():
19
+ outputs = model(input_ids)
20
+ predicted_label = torch.argmax(outputs.logits, dim=1).tolist()
21
+
22
+ # Extracting the text and predicted outcome
23
+ input_text = tokenizer.decode(input_ids[0], skip_special_tokens=True)
24
+ predicted_outcome = label_decoder[predicted_label[0]]
25
+
26
+ # Display the results
27
+ print(f"Text: {input_text}")
28
+ print(f"Predicted Outcome: {predicted_outcome}")
29
+ print()
30
+ return predicted_outcome # Add a new line for better readability
31
+
32
+ # Example usage
33
+ model_path = "/home/lwasinam/AI_Projects/hate_speech_detection/model6" # Replace with the actual path to your model
34
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
35
+ tokenizer = DistilBertTokenizer.from_pretrained("distilbert-base-uncased") # Replace with your desired tokenizer
36
+
37
+ # Load model
38
+ model = load_model(model_path, device)
39
+
40
+
41
+ label_decoder = {0: "Not Hate", 1: "Hate",}
42
+
43
+
44
+ # Assuming you have label_decoder defined
45
+
46
+
47
+ import streamlit as st
48
+
49
+ st.title("Hate Speech Detection")
50
+
51
+ user_input = st.text_input("Enter your text:")
52
+ if user_input:
53
+ result = run_inference(model, tokenizer, label_decoder, device, user_input)
54
+ st.write("Inference Result:", result)