Jezia commited on
Commit
b30b5d8
·
1 Parent(s): a134b00

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -10
app.py CHANGED
@@ -3,10 +3,11 @@ from gradio import mix
3
  import tensorflow as tf
4
  import numpy as np
5
  import torch
6
- from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
 
 
7
  from huggingface_hub import from_pretrained_keras
8
 
9
- tokenizer = AutoTokenizer.from_pretrained("bert-base-cased")
10
  model = from_pretrained_keras("keras-io/text-generation-miniature-gpt")
11
 
12
  def tokenize_data(text):
@@ -21,14 +22,14 @@ def tokenize_data(text):
21
  return inputs
22
 
23
  def generate_answers(text):
24
- inputs = tokenize_data(text)
25
- x = tf.dtypes.cast(inputs['input_ids'], tf.int64)
26
- predictions, _ = model.predict(x)
27
- #predictions, _ = model.predict(inputs['input_ids'])
28
- results = np.argmax(predictions, axis=1)
29
- answer = tokenizer.decode(results[0].flatten(), skip_special_tokens=True)
30
- return answer
31
-
32
  examples = [["Once upon a time, "], ["In a galaxy far far away"]]
33
  title = "Text Generation with Miniature GPT"
34
  description = "Gradio Demo for a miniature with GPT. To use it, simply add your text, or click one of the examples to load them. Read more at the links below."
 
3
  import tensorflow as tf
4
  import numpy as np
5
  import torch
6
+ from keras.preprocessing.sequence import pad_sequences
7
+ import pickle
8
+
9
  from huggingface_hub import from_pretrained_keras
10
 
 
11
  model = from_pretrained_keras("keras-io/text-generation-miniature-gpt")
12
 
13
  def tokenize_data(text):
 
22
  return inputs
23
 
24
  def generate_answers(text):
25
+ sequence_test = tokenizer.texts_to_sequences([text])
26
+ padded_test = pad_sequences(sequence_test, maxlen= 80, padding='post')
27
+ predictions,_ = model.predict(padded_test)
28
+ results = np.argmax(predictions, axis=1)[0]
29
+ answer = tokenizer.sequences_to_texts([results] )
30
+ answertoString = ' '.join([str(elem) for elem in answer])
31
+ return answertoString
32
+
33
  examples = [["Once upon a time, "], ["In a galaxy far far away"]]
34
  title = "Text Generation with Miniature GPT"
35
  description = "Gradio Demo for a miniature with GPT. To use it, simply add your text, or click one of the examples to load them. Read more at the links below."