BugZoid commited on
Commit
ee25ef1
·
verified ·
1 Parent(s): 21bb05d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -12
app.py CHANGED
@@ -18,6 +18,16 @@ if 'models_loaded' not in st.session_state:
18
 
19
  st.session_state.models_loaded = True
20
 
 
 
 
 
 
 
 
 
 
 
21
  def clean_generated_text(text):
22
  """
23
  Remove comandos e limpa o texto gerado
@@ -52,11 +62,13 @@ def humanize_text(text):
52
  """
53
  Humanize the input text using T5 model with improved coherence
54
  """
 
 
55
  # Prepara o texto com contexto específico para melhor coerência
56
  context = (
57
  f"Contexto: Este é um texto técnico ou formal que precisa ser reescrito "
58
- f"de forma mais natural, mantendo todas as informações importantes. "
59
- f"Texto original: {text}"
60
  )
61
 
62
  input_ids = st.session_state.t5_tokenizer(
@@ -69,23 +81,26 @@ def humanize_text(text):
69
  outputs = st.session_state.t5_model.generate(
70
  input_ids,
71
  max_length=1024,
72
- min_length=len(text.split()), # Mantém tamanho mínimo
73
  do_sample=True,
74
  temperature=0.7, # Ajustado para melhor equilíbrio
75
  top_p=0.9,
76
  num_beams=4,
77
  no_repeat_ngram_size=2,
78
  repetition_penalty=1.5,
79
- length_penalty=1.2
80
  )
81
 
82
  result = st.session_state.t5_tokenizer.decode(outputs[0], skip_special_tokens=True)
83
- return clean_generated_text(result)
 
84
 
85
- def paraphrase_text(text):
86
  """
87
- Refina o texto humanizado mantendo a coerência
88
  """
 
 
89
  inputs = st.session_state.paraphrase_tokenizer.encode(
90
  text,
91
  return_tensors="pt",
@@ -96,16 +111,17 @@ def paraphrase_text(text):
96
  outputs = st.session_state.paraphrase_model.generate(
97
  inputs,
98
  max_length=1024,
99
- min_length=len(text.split()),
100
  do_sample=True,
101
  temperature=0.3, # Reduzido para maior coerência
102
  top_p=0.95,
103
  repetition_penalty=1.2,
104
- length_penalty=1.2
105
  )
106
 
107
  result = st.session_state.paraphrase_tokenizer.decode(outputs[0], skip_special_tokens=True)
108
- return clean_generated_text(result)
 
109
 
110
  # UI Components
111
  st.set_page_config(page_title="Advanced Text Humanizer", page_icon="🤖")
@@ -113,7 +129,8 @@ st.set_page_config(page_title="Advanced Text Humanizer", page_icon="🤖")
113
  st.title("🤖 → 🧑 Humanizador de Texto Avançado")
114
  st.markdown("""
115
  Este aplicativo transforma textos robotizados em linguagem mais natural e humana,
116
- mantendo todas as informações originais.
 
117
  """)
118
 
119
  # Input area with expanded capabilities
@@ -145,7 +162,7 @@ if st.button("Humanizar", type="primary"):
145
 
146
  # Optional paraphrasing pass
147
  if use_paraphrase:
148
- final_text = paraphrase_text(humanized_text)
149
  else:
150
  final_text = humanized_text
151
 
 
18
 
19
  st.session_state.models_loaded = True
20
 
21
+ def ensure_minimum_length(text, original_text):
22
+ """
23
+ Garante que o texto gerado tenha pelo menos o mesmo tamanho do original
24
+ """
25
+ while len(text.split()) < len(original_text.split()):
26
+ missing_words = len(original_text.split()) - len(text.split())
27
+ if missing_words > 0:
28
+ text = text + " " + original_text[-missing_words:]
29
+ return text
30
+
31
  def clean_generated_text(text):
32
  """
33
  Remove comandos e limpa o texto gerado
 
62
  """
63
  Humanize the input text using T5 model with improved coherence
64
  """
65
+ min_length = len(text.split())
66
+
67
  # Prepara o texto com contexto específico para melhor coerência
68
  context = (
69
  f"Contexto: Este é um texto técnico ou formal que precisa ser reescrito "
70
+ f"de forma mais natural, mantendo todas as informações importantes e expandindo "
71
+ f"com detalhes relevantes. Texto original: {text}"
72
  )
73
 
74
  input_ids = st.session_state.t5_tokenizer(
 
81
  outputs = st.session_state.t5_model.generate(
82
  input_ids,
83
  max_length=1024,
84
+ min_length=min_length, # Força o tamanho mínimo igual ao original
85
  do_sample=True,
86
  temperature=0.7, # Ajustado para melhor equilíbrio
87
  top_p=0.9,
88
  num_beams=4,
89
  no_repeat_ngram_size=2,
90
  repetition_penalty=1.5,
91
+ length_penalty=2.0 # Aumentado para favorecer textos mais longos
92
  )
93
 
94
  result = st.session_state.t5_tokenizer.decode(outputs[0], skip_special_tokens=True)
95
+ result = clean_generated_text(result)
96
+ return ensure_minimum_length(result, text)
97
 
98
+ def paraphrase_text(text, original_text):
99
  """
100
+ Refina o texto humanizado mantendo a coerência e tamanho
101
  """
102
+ min_length = len(original_text.split())
103
+
104
  inputs = st.session_state.paraphrase_tokenizer.encode(
105
  text,
106
  return_tensors="pt",
 
111
  outputs = st.session_state.paraphrase_model.generate(
112
  inputs,
113
  max_length=1024,
114
+ min_length=min_length, # Força o tamanho mínimo igual ao original
115
  do_sample=True,
116
  temperature=0.3, # Reduzido para maior coerência
117
  top_p=0.95,
118
  repetition_penalty=1.2,
119
+ length_penalty=2.0 # Aumentado para favorecer textos mais longos
120
  )
121
 
122
  result = st.session_state.paraphrase_tokenizer.decode(outputs[0], skip_special_tokens=True)
123
+ result = clean_generated_text(result)
124
+ return ensure_minimum_length(result, original_text)
125
 
126
  # UI Components
127
  st.set_page_config(page_title="Advanced Text Humanizer", page_icon="🤖")
 
129
  st.title("🤖 → 🧑 Humanizador de Texto Avançado")
130
  st.markdown("""
131
  Este aplicativo transforma textos robotizados em linguagem mais natural e humana,
132
+ mantendo todas as informações originais e garantindo que o texto final seja pelo menos
133
+ do mesmo tamanho que o original.
134
  """)
135
 
136
  # Input area with expanded capabilities
 
162
 
163
  # Optional paraphrasing pass
164
  if use_paraphrase:
165
+ final_text = paraphrase_text(humanized_text, input_text)
166
  else:
167
  final_text = humanized_text
168