AyoubChLin commited on
Commit
8873319
·
verified ·
1 Parent(s): bb39193

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +98 -16
app.py CHANGED
@@ -1,23 +1,105 @@
1
- from gradio_client import Client
 
 
 
 
 
 
 
 
 
 
2
 
3
- client = Client("https://ayoubchlin-ayoubchlin-bart-mnli-cnn-news.hf.space/")
4
 
5
- result = client.predict(
6
- "Prior to the introduction of scopes, Dropbox API apps would select only their level of content access (described below). Business API apps would select from one of four permission types to determine the API calls they have access to:", # str representing input in 'Input' Textbox component
7
- 'politics,news,sports,tech,science', # str representing input in 'Possible class names (comma-separated)' Textbox component
8
- False, # bool representing input in 'Allow multiple true classes' Checkbox component
9
- api_name="/predict"
10
- )
11
 
12
- import json
 
 
 
 
 
13
 
 
 
 
14
 
15
 
16
- # Open the file in read mode
17
- with open(result, 'r') as file:
18
- # Read the contents of the file
19
- json_data = json.load(file)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
- # Do further processing with the JSON data
22
- # For example, print the content of the JSON file
23
- print(json_data["label"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from keras.models import load_model
3
+ import numpy as np
4
+ import tensorflow as tf
5
+ import nltk
6
+ import re
7
+ from nltk.corpus import stopwords
8
+ from nltk.tokenize import TweetTokenizer
9
+ from nltk.tokenize import word_tokenize
10
+ from tensorflow.keras.preprocessing.text import Tokenizer
11
+ from tensorflow.keras.preprocessing.sequence import pad_sequences
12
 
13
+ import subprocess
14
 
15
+ # Command to execute
16
+ command = "git clone https://huggingface.co/lydiadida/lstmhatespeachdetection"
 
 
 
 
17
 
18
+ # Execute the command
19
+ try:
20
+ subprocess.run(command, shell=True, check=True)
21
+ print("Git clone command executed successfully.")
22
+ except subprocess.CalledProcessError as e:
23
+ print(f"Error executing git clone command: {e}")
24
 
25
+ # Load the LSTM model
26
+ model_path = "model.h5" # Set your model path here
27
+ lstm_model = load_lstm_model(model_path)
28
 
29
 
30
+ def clean_text(text):
31
+ # Remove stopwords
32
+ stop_words = set(stopwords.words('english'))
33
+ words = nltk.word_tokenize(text)
34
+ filtered_words = [word for word in words if word not in stop_words]
35
+
36
+ # Remove Twitter usernames
37
+ text = re.sub(r'@\w+', '', ' '.join(filtered_words))
38
+
39
+ # Remove URLs
40
+ text = re.sub(r'http\S+', '', text)
41
+
42
+ # Tokenize using TweetTokenizer
43
+ tokenizer = TweetTokenizer(preserve_case=True)
44
+ text = tokenizer.tokenize(text)
45
+
46
+ # Remove hashtag symbols
47
+ text = [word.replace('#', '') for word in text]
48
+
49
+ # Remove short words
50
+ text = ' '.join([word.lower() for word in text if len(word) > 2])
51
+
52
+ # Remove digits
53
+ text = re.sub(r'\d+', '', text)
54
+
55
+ # Remove non-alphanumeric characters
56
+ text = re.sub(r'[^a-zA-Z\s]', '', text)
57
+
58
+ return text
59
 
60
+
61
+ def preprocess_text(text):
62
+ # Clean the text
63
+ cleaned_text = clean_text(text)
64
+
65
+ # Tokenize and pad sequences
66
+ token = Tokenizer()
67
+ token.fit_on_texts([cleaned_text])
68
+ text_sequences = token.texts_to_sequences([cleaned_text])
69
+ padded_sequences = pad_sequences(text_sequences, maxlen=100)
70
+
71
+ return padded_sequences
72
+
73
+ # Function to load the saved LSTM model
74
+ @st.cache(allow_output_mutation=True)
75
+ def load_lstm_model(model_path):
76
+ return load_model(model_path)
77
+
78
+ # Function to predict hate speech
79
+ def predict_hate_speech(text):
80
+ # Preprocess the text
81
+ padded_sequences = preprocess_text(text)
82
+ prediction = lstm_model.predict(padded_sequences)
83
+ return prediction
84
+
85
+ # Main function to run the Streamlit app
86
+ def main():
87
+ # Set up Streamlit UI
88
+ st.title("Hate Speech Detection")
89
+ st.write("Enter text below to detect hate speech:")
90
+ input_text = st.text_area("Input Text", "")
91
+
92
+ if st.button("Detect Hate Speech"):
93
+ if input_text:
94
+ # Predict hate speech
95
+ prediction = predict_hate_speech(input_text, lstm_model)
96
+ if prediction > 0.5:
97
+ st.error("Hate Speech Detected")
98
+ else:
99
+ st.success("No Hate Speech Detected")
100
+ else:
101
+ st.warning("Please enter some text")
102
+
103
+ # Run the app
104
+ if __name__ == "__main__":
105
+ main()