Spaces:
Sleeping
Sleeping
Upload gradio_app_py.py
Browse files- gradio_app_py.py +54 -0
gradio_app_py.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""gradio_app.py
|
3 |
+
|
4 |
+
Automatically generated by Colaboratory.
|
5 |
+
|
6 |
+
Original file is located at
|
7 |
+
https://colab.research.google.com/drive/1OQvi3I_q3WfavYBpjovCYfv2SPYt__pF
|
8 |
+
"""
|
9 |
+
|
10 |
+
import json
|
11 |
+
import gradio as gr
|
12 |
+
import tensorflow as tf
|
13 |
+
from tensorflow.keras.models import load_model
|
14 |
+
from tensorflow.keras.preprocessing.text import tokenizer_from_json
|
15 |
+
import tensorflow_addons as tfa
|
16 |
+
|
17 |
+
# Load the pre-trained model and tokenizer
|
18 |
+
model = tf.keras.models.load_model('baseline.h5')
|
19 |
+
|
20 |
+
# Assuming you have already loaded the tokenizer configuration from the JSON file.
|
21 |
+
# Replace 'path' with the actual path to the directory where 'tokenizer.json' is saved.
|
22 |
+
with open('tokenizer.json', 'r', encoding='utf-8') as f:
|
23 |
+
tokenizer_config = json.load(f)
|
24 |
+
|
25 |
+
tokenizer = tf.keras.preprocessing.text.tokenizer_from_json(tokenizer_config)
|
26 |
+
|
27 |
+
# Define the labels for classification
|
28 |
+
labels = ['toxic', 'severe_toxic', 'obscene', 'threat', 'insult', 'identity_hate']
|
29 |
+
|
30 |
+
def classify_comment(comment):
|
31 |
+
# Tokenize the comment and convert it into sequences
|
32 |
+
comment_sequence = tokenizer.texts_to_sequences([comment])
|
33 |
+
comment_sequence = tf.keras.preprocessing.sequence.pad_sequences(comment_sequence, maxlen=200)
|
34 |
+
|
35 |
+
# Make predictions
|
36 |
+
predictions = model.predict(comment_sequence)[0]
|
37 |
+
results = dict(zip(labels, predictions))
|
38 |
+
|
39 |
+
return results
|
40 |
+
|
41 |
+
# Create the Gradio interface
|
42 |
+
comment_input = gr.inputs.Textbox(label="Enter your comment here")
|
43 |
+
output_text = gr.outputs.Textbox(label="Classification Results")
|
44 |
+
|
45 |
+
iface = gr.Interface(
|
46 |
+
fn=classify_comment,
|
47 |
+
inputs=comment_input,
|
48 |
+
outputs=output_text,
|
49 |
+
live=True # Set to True for live updates without needing to restart the server
|
50 |
+
)
|
51 |
+
|
52 |
+
# Launch the Gradio app
|
53 |
+
iface.launch()
|
54 |
+
|