alexandre-huynh commited on
Commit
fba408d
1 Parent(s): cbc0346

Formatted generate_text output

Browse files
Files changed (1) hide show
  1. app.py +24 -0
app.py CHANGED
@@ -26,6 +26,7 @@ from tensorflow import keras
26
  import keras_nlp
27
  #from google.colab import drive
28
  import time
 
29
 
30
  os.environ["KERAS_BACKEND"] = "tensorflow" # or "tensorflow" or "torch"
31
 
@@ -46,8 +47,31 @@ gpt2_lm.load_weights(checkpoint_path)
46
 
47
  """# Chargement et configuration de gradio"""
48
 
 
49
  def generate_text(prompt):
50
  return gpt2_lm.generate(prompt, max_length=100)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
  # CSS styles
53
  css = """
 
26
  import keras_nlp
27
  #from google.colab import drive
28
  import time
29
+ import re
30
 
31
  os.environ["KERAS_BACKEND"] = "tensorflow" # or "tensorflow" or "torch"
32
 
 
47
 
48
  """# Chargement et configuration de gradio"""
49
 
50
+ """
51
  def generate_text(prompt):
52
  return gpt2_lm.generate(prompt, max_length=100)
53
+ """
54
+
55
+ # Expérimental
56
+ def format_text(text, to_remove):
57
+ # Function to format generated text
58
+ # - removes the prompt from the answer
59
+ # - removes unnecessary space chars before punctuation
60
+ # - capitalize the first letter of each sentence
61
+ text = text.replace(to_remove, '')
62
+ text = re.sub(r' +,', ',', text) # comma
63
+ text = re.sub(r' +\.', '.', text) # full stop
64
+ text = '. '.join(sentence.strip().capitalize() for sentence in text.split('.'))
65
+
66
+ return text
67
+
68
+ # Expérimental
69
+ def generate_text(prompt):
70
+ output = gpt2_lm.generate(prompt, max_length=150)
71
+ formatted_output = format_text(output, prompt)
72
+ print(f"DEBUG - GPT-2 Output : {output}")
73
+ print(f"DEBUG - Formatted GPT-2 Output : {formatted_output}")
74
+ return formatted_output
75
 
76
  # CSS styles
77
  css = """