Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
import tensorflow as tf
|
4 |
+
import pandas as pd
|
5 |
+
from tensorflow.keras.layers import TextVectorization
|
6 |
+
|
7 |
+
# Load your data
|
8 |
+
new_data = pd.read_csv('data_train.csv') # Make sure to adjust the path if necessary
|
9 |
+
|
10 |
+
x = new_data['comment_text']
|
11 |
+
y = new_data[new_data.columns[2:]].values
|
12 |
+
|
13 |
+
max_features = 100000
|
14 |
+
vectorizer = TextVectorization(max_tokens=max_features,
|
15 |
+
output_sequence_length=1500,
|
16 |
+
output_mode='int')
|
17 |
+
vectorizer.get_vocabulary()
|
18 |
+
vectorizer.adapt(x.values)
|
19 |
+
|
20 |
+
model = tf.keras.models.load_model('finalprojecttoxic.h5')
|
21 |
+
|
22 |
+
translator_hindi = pipeline("translation", model="Helsinki-NLP/opus-mt-hi-en", tokenizer="Helsinki-NLP/opus-mt-hi-en")
|
23 |
+
|
24 |
+
def translate_hindi(from_text):
|
25 |
+
result2 = translator_hindi(from_text)
|
26 |
+
return result2[0]['translation_text']
|
27 |
+
|
28 |
+
def score_comment(comment):
|
29 |
+
vectorized_comment = vectorizer([comment])
|
30 |
+
results = model.predict(vectorized_comment)
|
31 |
+
|
32 |
+
text = ''
|
33 |
+
for idx, col in enumerate(new_data.columns[2:]):
|
34 |
+
text += '{}: {}\n'.format(col, results[0][idx] > 0.5)
|
35 |
+
|
36 |
+
return text
|
37 |
+
|
38 |
+
def combined_models(input):
|
39 |
+
translated_text = translate_hindi(input)
|
40 |
+
toxic_score = score_comment(input)
|
41 |
+
|
42 |
+
return translated_text, toxic_score
|
43 |
+
|
44 |
+
interface = gr.Interface(fn=combined_models, inputs="text", outputs=["text", "text"], title="Toxic Comment Analyzer")
|
45 |
+
interface.launch(share=True)
|