CoolFace
Apppublic

OOFMAN29803/SoloconLM_50M

sourceHugging Faceunknownupdated 2y agoView on Hugging Face
0likes
app.py92 linesDownload Raw Back to root
1import tensorflow as tf2from tensorflow.keras.layers import Embedding, MultiHeadAttention, Dense, Input, Dropout, LayerNormalization3from tensorflow.keras.models import Model, load_model4import numpy as np5import gradio as gr6 7# Define the Positional Encoding layer8class PositionalEncoding(tf.keras.layers.Layer):9    def __init__(self, position, d_model):10        super(PositionalEncoding, self).__init__()11        self.pos_encoding = self.positional_encoding(position, d_model)12 13    def get_angles(self, pos, i, d_model):14        angles = 1 / tf.pow(10000, (2 * (i // 2)) / tf.cast(d_model, tf.float32))15        return pos * angles16 17    def positional_encoding(self, position, d_model):18        angle_rads = self.get_angles(19            tf.range(position, dtype=tf.float32)[:, tf.newaxis],20            tf.range(d_model, dtype=tf.float32)[tf.newaxis, :],21            d_model22        )23        sines = tf.math.sin(angle_rads[:, 0::2])24        cosines = tf.math.cos(angle_rads[:, 1::2])25        pos_encoding = tf.concat([sines, cosines], axis=-1)26        pos_encoding = pos_encoding[tf.newaxis, ...]27        return tf.cast(pos_encoding, tf.float32)28 29    def call(self, inputs):30        return inputs + self.pos_encoding[:, :tf.shape(inputs)[1], :]31 32# Define the Transformer Encoder Layer33class TransformerEncoderLayer(tf.keras.layers.Layer):34    def __init__(self, d_model, num_heads, dff, rate=0.1):35        super().__init__()36        self.mha = MultiHeadAttention(key_dim=d_model, num_heads=num_heads)37        self.ffn = tf.keras.Sequential([38            Dense(dff, activation='relu'),39            Dense(d_model)40        ])41        self.layernorm1 = LayerNormalization(epsilon=1e-6)42        self.layernorm2 = LayerNormalization(epsilon=1e-6)43        self.layernorm3 = LayerNormalization(epsilon=1e-6)44        self.dropout1 = Dropout(rate)45        self.dropout2 = Dropout(rate)46 47    def call(self, x, training, mask=None):48        attn_output = self.mha(x, x, x, attention_mask=mask)49        attn_output = self.dropout1(attn_output, training=training)50        out1 = self.layernorm1(x + attn_output)51        ffn_output = self.ffn(out1)52        ffn_output = self.dropout2(ffn_output, training=training)53        out2 = self.layernorm3(ffn_output)54        return self.layernorm2(out1 + out2)55 56# Define tokenizer function for questions and answers57def qa_tokenizer(text, vocab_size=2000):58    # Dummy implementation, replace with actual tokenization logic59    return [ord(c) % vocab_size for c in text]60 61def preprocess_input(text, max_length=500, vocab_size=2000):62    tokenized_text = qa_tokenizer(text, vocab_size)63    padded_text = tf.keras.preprocessing.sequence.pad_sequences([tokenized_text], maxlen=max_length, padding='post')64    return padded_text65 66def postprocess_output(prediction, vocab_size=2000):67    # Convert the prediction to a sequence of token IDs68    predicted_sequence = np.argmax(prediction, axis=-1)[0]69    # Convert the token IDs to a numeric format70    numeric_sequence = ' '.join(str(id) for id in predicted_sequence)71    return numeric_sequence72 73# Load the trained model with custom objects74custom_objects = {75    'PositionalEncoding': PositionalEncoding,76    'TransformerEncoderLayer': TransformerEncoderLayer77}78model = load_model('Solocon DemoTest.h5', custom_objects=custom_objects)79 80# Function to make predictions81def predict(text):82    input_data = preprocess_input(text)83    predictions = model.predict(input_data)84    output = postprocess_output(predictions)85    return output86 87# Example usage88iface = gr.Interface(fn=predict, inputs="text", outputs="text")89 90# Launch the interface91iface.launch()92