Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,35 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import tensorflow as tf
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import pickle
|
| 5 |
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
| 6 |
import re
|
|
|
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
with open('textgen_tokenizer.pkl', 'rb') as handle:
|
| 18 |
-
textgen_tokenizer = pickle.load(handle)
|
| 19 |
|
| 20 |
-
|
|
|
|
|
|
|
| 21 |
|
| 22 |
# Text cleaning function
|
| 23 |
def clean_text(text):
|
|
@@ -28,6 +40,38 @@ def clean_text(text):
|
|
| 28 |
text = ' '.join(text.split())
|
| 29 |
return text
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
# Classification function
|
| 32 |
def classify_text(text, model, tokenizer):
|
| 33 |
cleaned_text = clean_text(text)
|
|
@@ -74,10 +118,21 @@ def generate_text(prompt, model, tokenizer, max_length=50, temperature=0.7):
|
|
| 74 |
|
| 75 |
return input_text
|
| 76 |
|
| 77 |
-
#
|
| 78 |
-
|
|
|
|
|
|
|
| 79 |
|
| 80 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
def classify_interface(text):
|
| 82 |
subject, confidence = classify_text(text, classifier_model, classifier_tokenizer)
|
| 83 |
return f"Subject: {subject} (Confidence: {confidence:.2f}%)"
|
|
@@ -85,7 +140,7 @@ def classify_interface(text):
|
|
| 85 |
def generate_interface(prompt, length=50, temp=0.7):
|
| 86 |
return generate_text(prompt, textgen_model, textgen_tokenizer, max_length=int(length), temperature=float(temp))
|
| 87 |
|
| 88 |
-
# Create
|
| 89 |
with gr.Blocks(title="Science Text Analyzer") as demo:
|
| 90 |
gr.Markdown("# Science Text Analyzer")
|
| 91 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import tensorflow as tf
|
| 3 |
+
import keras
|
| 4 |
import numpy as np
|
| 5 |
import pickle
|
| 6 |
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
| 7 |
import re
|
| 8 |
+
import os
|
| 9 |
|
| 10 |
+
# Define and register the custom Perplexity metric
|
| 11 |
+
@keras.saving.register_keras_serializable(package="Custom")
|
| 12 |
+
class Perplexity(keras.metrics.Metric):
|
| 13 |
+
def __init__(self, name='perplexity', dtype=None, **kwargs):
|
| 14 |
+
super().__init__(name=name, dtype=dtype, **kwargs)
|
| 15 |
+
self.cross_entropy = keras.metrics.Mean(name='cross_entropy')
|
| 16 |
+
|
| 17 |
+
def update_state(self, y_true, y_pred, sample_weight=None):
|
| 18 |
+
# Calculate cross-entropy
|
| 19 |
+
cross_entropy_values = tf.keras.losses.sparse_categorical_crossentropy(y_true, y_pred)
|
| 20 |
+
# Update the internal mean metric
|
| 21 |
+
self.cross_entropy.update_state(cross_entropy_values, sample_weight)
|
| 22 |
+
|
| 23 |
+
def result(self):
|
| 24 |
+
# Perplexity is the exponential of the cross-entropy
|
| 25 |
+
return tf.exp(self.cross_entropy.result())
|
| 26 |
|
| 27 |
+
def reset_state(self):
|
| 28 |
+
self.cross_entropy.reset_state()
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
def get_config(self):
|
| 31 |
+
config = super().get_config()
|
| 32 |
+
return config
|
| 33 |
|
| 34 |
# Text cleaning function
|
| 35 |
def clean_text(text):
|
|
|
|
| 40 |
text = ' '.join(text.split())
|
| 41 |
return text
|
| 42 |
|
| 43 |
+
# Load models and tokenizers
|
| 44 |
+
def load_models():
|
| 45 |
+
print("Loading models and tokenizers...")
|
| 46 |
+
|
| 47 |
+
# Load models with custom objects for Perplexity
|
| 48 |
+
custom_objects = {'Perplexity': Perplexity}
|
| 49 |
+
|
| 50 |
+
try:
|
| 51 |
+
with keras.saving.custom_object_scope(custom_objects):
|
| 52 |
+
classifier_model = keras.models.load_model('classifier_model.keras')
|
| 53 |
+
textgen_model = keras.models.load_model('textgen_model.keras')
|
| 54 |
+
|
| 55 |
+
print("Models loaded successfully with custom objects")
|
| 56 |
+
except Exception as e:
|
| 57 |
+
print(f"Error loading models with custom objects: {e}")
|
| 58 |
+
raise
|
| 59 |
+
|
| 60 |
+
# Load tokenizers
|
| 61 |
+
try:
|
| 62 |
+
with open('classifier_tokenizer.pkl', 'rb') as handle:
|
| 63 |
+
classifier_tokenizer = pickle.load(handle)
|
| 64 |
+
|
| 65 |
+
with open('textgen_tokenizer.pkl', 'rb') as handle:
|
| 66 |
+
textgen_tokenizer = pickle.load(handle)
|
| 67 |
+
|
| 68 |
+
print("Tokenizers loaded successfully")
|
| 69 |
+
except Exception as e:
|
| 70 |
+
print(f"Error loading tokenizers: {e}")
|
| 71 |
+
raise
|
| 72 |
+
|
| 73 |
+
return classifier_model, classifier_tokenizer, textgen_model, textgen_tokenizer
|
| 74 |
+
|
| 75 |
# Classification function
|
| 76 |
def classify_text(text, model, tokenizer):
|
| 77 |
cleaned_text = clean_text(text)
|
|
|
|
| 118 |
|
| 119 |
return input_text
|
| 120 |
|
| 121 |
+
# Print environment info for debugging
|
| 122 |
+
print(f"TensorFlow version: {tf.__version__}")
|
| 123 |
+
print(f"Keras version: {tf.keras.__version__}")
|
| 124 |
+
print(f"Current directory contents: {os.listdir('.')}")
|
| 125 |
|
| 126 |
+
# Load models with error handling
|
| 127 |
+
try:
|
| 128 |
+
print("Starting model loading process...")
|
| 129 |
+
classifier_model, classifier_tokenizer, textgen_model, textgen_tokenizer = load_models()
|
| 130 |
+
print("Models and tokenizers loaded successfully")
|
| 131 |
+
except Exception as e:
|
| 132 |
+
print(f"Error in model loading process: {e}")
|
| 133 |
+
raise
|
| 134 |
+
|
| 135 |
+
# Create Gradio interface functions
|
| 136 |
def classify_interface(text):
|
| 137 |
subject, confidence = classify_text(text, classifier_model, classifier_tokenizer)
|
| 138 |
return f"Subject: {subject} (Confidence: {confidence:.2f}%)"
|
|
|
|
| 140 |
def generate_interface(prompt, length=50, temp=0.7):
|
| 141 |
return generate_text(prompt, textgen_model, textgen_tokenizer, max_length=int(length), temperature=float(temp))
|
| 142 |
|
| 143 |
+
# Create Gradio interface
|
| 144 |
with gr.Blocks(title="Science Text Analyzer") as demo:
|
| 145 |
gr.Markdown("# Science Text Analyzer")
|
| 146 |
|