Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,26 +1,123 @@
|
|
1 |
import streamlit as st
|
2 |
-
from transformers import
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
-
#
|
5 |
-
|
6 |
-
model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
-
#
|
15 |
-
|
16 |
-
if input_text:
|
17 |
-
# Pedir ao robô para humanizar o texto
|
18 |
-
input_ids = tokenizer(f"humanize: {input_text}", return_tensors="pt").input_ids
|
19 |
-
outputs = model.generate(input_ids, max_length=512)
|
20 |
-
humanized_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
else:
|
26 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from transformers import (
|
3 |
+
AutoTokenizer,
|
4 |
+
AutoModelForSeq2SeqGeneration,
|
5 |
+
T5ForConditionalGeneration,
|
6 |
+
T5Tokenizer
|
7 |
+
)
|
8 |
|
9 |
+
# Initialize session state for models if not already done
|
10 |
+
if 'models_loaded' not in st.session_state:
|
11 |
+
# Load the main T5 model and tokenizer (using t5-base for better quality)
|
12 |
+
st.session_state.t5_tokenizer = T5Tokenizer.from_pretrained("t5-base")
|
13 |
+
st.session_state.t5_model = T5ForConditionalGeneration.from_pretrained("t5-base")
|
14 |
+
|
15 |
+
# Load the paraphrasing model and tokenizer
|
16 |
+
st.session_state.paraphrase_tokenizer = AutoTokenizer.from_pretrained("facebook/bart-large-cnn")
|
17 |
+
st.session_state.paraphrase_model = AutoModelForSeq2SeqGeneration.from_pretrained("facebook/bart-large-cnn")
|
18 |
+
|
19 |
+
st.session_state.models_loaded = True
|
20 |
|
21 |
+
def paraphrase_text(text):
|
22 |
+
"""
|
23 |
+
Apply paraphrasing to the input text using BART model
|
24 |
+
"""
|
25 |
+
inputs = st.session_state.paraphrase_tokenizer.encode(
|
26 |
+
text,
|
27 |
+
return_tensors="pt",
|
28 |
+
max_length=512,
|
29 |
+
truncation=True
|
30 |
+
)
|
31 |
+
|
32 |
+
outputs = st.session_state.paraphrase_model.generate(
|
33 |
+
inputs,
|
34 |
+
max_length=512,
|
35 |
+
do_sample=True,
|
36 |
+
temperature=0.7,
|
37 |
+
top_p=0.9
|
38 |
+
)
|
39 |
+
|
40 |
+
return st.session_state.paraphrase_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
41 |
|
42 |
+
def humanize_text(text):
|
43 |
+
"""
|
44 |
+
Humanize the input text using T5 model
|
45 |
+
"""
|
46 |
+
input_ids = st.session_state.t5_tokenizer(
|
47 |
+
f"humanize: {text}",
|
48 |
+
return_tensors="pt",
|
49 |
+
max_length=512,
|
50 |
+
truncation=True
|
51 |
+
).input_ids
|
52 |
+
|
53 |
+
outputs = st.session_state.t5_model.generate(
|
54 |
+
input_ids,
|
55 |
+
max_length=len(text) + 100, # Dynamic length based on input
|
56 |
+
do_sample=True,
|
57 |
+
temperature=0.7, # Increased creativity
|
58 |
+
top_p=0.9, # Nucleus sampling
|
59 |
+
num_beams=4, # Beam search for better quality
|
60 |
+
no_repeat_ngram_size=2 # Avoid repetition
|
61 |
+
)
|
62 |
+
|
63 |
+
return st.session_state.t5_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
64 |
|
65 |
+
# UI Components
|
66 |
+
st.set_page_config(page_title="Advanced Text Humanizer", page_icon="🤖")
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
+
st.title("🤖 → 🧑 Advanced Text Humanizer")
|
69 |
+
st.markdown("""
|
70 |
+
This app transforms robotic text into more natural, human-like language using
|
71 |
+
advanced AI models. It combines T5 and BART models for better results.
|
72 |
+
""")
|
73 |
+
|
74 |
+
# Input area with expanded capabilities
|
75 |
+
input_text = st.text_area(
|
76 |
+
"Cole seu texto de robô aqui:",
|
77 |
+
height=150,
|
78 |
+
help="Paste your text here to transform it into a more natural, human-like version."
|
79 |
+
)
|
80 |
+
|
81 |
+
# Advanced settings in sidebar
|
82 |
+
with st.sidebar:
|
83 |
+
st.header("Advanced Settings")
|
84 |
+
use_paraphrase = st.checkbox("Enable Paraphrasing", value=True)
|
85 |
+
show_original = st.checkbox("Show Original Text", value=False)
|
86 |
+
|
87 |
+
# Process button with error handling
|
88 |
+
if st.button("Humanizar", type="primary"):
|
89 |
+
if not input_text:
|
90 |
+
st.warning("⚠️ Por favor, cole um texto de robô primeiro!")
|
91 |
else:
|
92 |
+
with st.spinner("Processando o texto..."):
|
93 |
+
try:
|
94 |
+
# First humanization pass
|
95 |
+
humanized_text = humanize_text(input_text)
|
96 |
+
|
97 |
+
# Optional paraphrasing pass
|
98 |
+
if use_paraphrase:
|
99 |
+
final_text = paraphrase_text(humanized_text)
|
100 |
+
else:
|
101 |
+
final_text = humanized_text
|
102 |
+
|
103 |
+
# Display results
|
104 |
+
st.success("✨ Texto humanizado:")
|
105 |
+
if show_original:
|
106 |
+
st.text("Texto original:")
|
107 |
+
st.info(input_text)
|
108 |
+
st.markdown("**Resultado:**")
|
109 |
+
st.write(final_text)
|
110 |
+
|
111 |
+
except Exception as e:
|
112 |
+
st.error(f"❌ Ocorreu um erro durante o processamento: {str(e)}")
|
113 |
+
|
114 |
+
# Footer
|
115 |
+
st.markdown("---")
|
116 |
+
st.markdown(
|
117 |
+
"""
|
118 |
+
<div style='text-align: center'>
|
119 |
+
<small>Desenvolvido com ❤️ usando Streamlit e Transformers</small>
|
120 |
+
</div>
|
121 |
+
""",
|
122 |
+
unsafe_allow_html=True
|
123 |
+
)
|