Karthick1087 commited on
Commit
544d94c
Β·
verified Β·
1 Parent(s): 6699bc2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +224 -0
app.py ADDED
@@ -0,0 +1,224 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from docx import Document
3
+ import io
4
+ import os
5
+ import re
6
+ from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
7
+ from gingerit.gingerit import GingerIt
8
+
9
+ # Initialize counter
10
+ if not os.path.exists("counter.txt"):
11
+ with open("counter.txt", "w") as f:
12
+ f.write("0")
13
+
14
+ def get_counter():
15
+ with open("counter.txt", "r") as f:
16
+ return int(f.read())
17
+
18
+ def increment_counter():
19
+ with open("counter.txt", "r+") as f:
20
+ count = int(f.read())
21
+ count += 1
22
+ f.seek(0)
23
+ f.write(str(count))
24
+ f.truncate()
25
+ return count
26
+
27
+ # Load advanced paraphrasing model
28
+ MODEL_NAME = "humarin/chatgpt_paraphraser_on_T5_base"
29
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
30
+ model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME)
31
+
32
+ def grammar_check(text):
33
+ try:
34
+ parser = GingerIt()
35
+ result = parser.parse(text)
36
+ return result['result']
37
+ except Exception as e:
38
+ st.error(f"Grammar check failed: {str(e)}")
39
+ return text
40
+
41
+ def improve_text(text):
42
+ # Preprocessing
43
+ text = re.sub(r'\s+', ' ', text).strip()
44
+
45
+ # Split into manageable chunks
46
+ sentences = re.split(r'(?<=[.!?]) +', text)
47
+ chunks = []
48
+ current_chunk = []
49
+ char_count = 0
50
+
51
+ for sentence in sentences:
52
+ if char_count + len(sentence) < 400:
53
+ current_chunk.append(sentence)
54
+ char_count += len(sentence)
55
+ else:
56
+ chunks.append(" ".join(current_chunk))
57
+ current_chunk = [sentence]
58
+ char_count = len(sentence)
59
+ if current_chunk:
60
+ chunks.append(" ".join(current_chunk))
61
+
62
+ # Process each chunk
63
+ improved_chunks = []
64
+ for chunk in chunks:
65
+ inputs = tokenizer(
66
+ f"paraphrase: {chunk}",
67
+ return_tensors="pt",
68
+ max_length=512,
69
+ truncation=True
70
+ )
71
+
72
+ outputs = model.generate(
73
+ inputs["input_ids"],
74
+ max_length=512,
75
+ num_beams=5,
76
+ num_return_sequences=1,
77
+ temperature=1.2,
78
+ do_sample=True,
79
+ top_p=0.95,
80
+ early_stopping=True
81
+ )
82
+
83
+ paraphrased = tokenizer.decode(outputs[0], skip_special_tokens=True)
84
+ improved_chunks.append(paraphrased)
85
+
86
+ # Post-processing
87
+ final_text = " ".join(improved_chunks)
88
+ final_text = grammar_check(final_text)
89
+
90
+ # Ensure coherence
91
+ final_text = re.sub(r'\s+([.,!?])', r'\1', final_text)
92
+ final_text = re.sub(r'\s+', ' ', final_text)
93
+
94
+ return final_text
95
+
96
+ # Page configuration
97
+ st.set_page_config(
98
+ page_title="⚑ Rapid Humanize AI",
99
+ page_icon="⚑",
100
+ layout="wide",
101
+ initial_sidebar_state="expanded"
102
+ )
103
+
104
+ # Custom CSS
105
+ st.markdown("""
106
+ <style>
107
+ @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&display=swap');
108
+ html, body, [class*="css"] {
109
+ font-family: 'Poppins', sans-serif;
110
+ }
111
+ .stTextArea textarea {min-height: 300px; border-radius: 10px; padding: 15px!important;}
112
+ .download-btn {margin-top: 20px;}
113
+ .counter {font-size: 1.4em; color: #FF4B4B; font-weight: 700; padding: 10px 20px; background: #FFE3E3; border-radius: 10px;}
114
+ .header {color: #2c3e50; border-bottom: 3px solid #FF4B4B; padding-bottom: 10px;}
115
+ .stButton button {background: #FF4B4B!important; color: white!important; border-radius: 8px!important; padding: 12px 24px!important;}
116
+ .stButton button:hover {background: #FF2B2B!important; color: white!important;}
117
+ .logo {text-align: center; margin-bottom: 30px;}
118
+ .logo h1 {color: #FF4B4B; font-size: 2.8em; margin-bottom: 0;}
119
+ .logo p {color: #6c757d; font-size: 1.2em;}
120
+ </style>
121
+ """, unsafe_allow_html=True)
122
+
123
+ # Sidebar
124
+ with st.sidebar:
125
+ st.markdown('<div class="header">βš™οΈ Settings</div>', unsafe_allow_html=True)
126
+ language = st.selectbox("Language Version", ["English (US)", "English (UK)", "English (AU)", "English (CA)"])
127
+ mode = st.radio("Processing Mode", ["Basic", "Advanced"])
128
+ st.markdown(f'<div class="counter">πŸš€ Total Conversions: {get_counter()}</div>', unsafe_allow_html=True)
129
+ st.markdown("---")
130
+ st.markdown("**Made with ❀️ by RHAI Team**")
131
+
132
+ # Main content
133
+ st.markdown("""
134
+ <div class="logo">
135
+ <h1>⚑ Rapid Humanize AI</h1>
136
+ <p>Transform AI-generated text into undetectable human-like content</p>
137
+ </div>
138
+ """, unsafe_allow_html=True)
139
+
140
+ col1, col2 = st.columns(2, gap="large")
141
+
142
+ with col1:
143
+ st.markdown('<div class="header">πŸ“₯ Input Section</div>', unsafe_allow_html=True)
144
+ uploaded_file = st.file_uploader("Upload DOC/DOCX File", type=["docx", "doc"], help="Max file size: 25MB")
145
+
146
+ input_text = st.text_area("Paste your AI-generated text here:", height=400,
147
+ placeholder="Enter text or upload file... (No word limit)")
148
+
149
+ if uploaded_file:
150
+ doc = Document(io.BytesIO(uploaded_file.getvalue()))
151
+ input_text = "\n".join([para.text for para in doc.paragraphs])
152
+
153
+ with col2:
154
+ st.markdown('<div class="header">πŸ“€ Output Section</div>', unsafe_allow_html=True)
155
+ output_text = st.text_area("Humanized text will appear here:",
156
+ value=st.session_state.get("output_text", ""),
157
+ height=400, key="output")
158
+
159
+ if output_text:
160
+ col_d1, col_d2 = st.columns(2)
161
+ with col_d1:
162
+ st.download_button(
163
+ label="πŸ“₯ Download TXT",
164
+ data=output_text,
165
+ file_name="humanized_text.txt",
166
+ mime="text/plain",
167
+ use_container_width=True
168
+ )
169
+ with col_d2:
170
+ doc = Document()
171
+ doc.add_paragraph(output_text)
172
+ bio = io.BytesIO()
173
+ doc.save(bio)
174
+ st.download_button(
175
+ label="πŸ“₯ Download DOCX",
176
+ data=bio.getvalue(),
177
+ file_name="humanized_text.docx",
178
+ mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document",
179
+ use_container_width=True
180
+ )
181
+
182
+ # Humanize Button
183
+ if st.button("✨ HUMANIZE NOW", use_container_width=True, type="primary"):
184
+ if input_text.strip():
185
+ with st.spinner("πŸš€ Humanizing your text... (This usually takes 10-30 seconds)"):
186
+ output_text = improve_text(input_text)
187
+ st.session_state.output_text = output_text
188
+ increment_counter()
189
+ st.experimental_rerun()
190
+ else:
191
+ st.error("Please enter some text or upload a file to humanize")
192
+
193
+ # Features Section
194
+ with st.expander("🌟 Key Features", expanded=True):
195
+ col_f1, col_f2, col_f3 = st.columns(3)
196
+ with col_f1:
197
+ st.markdown("""
198
+ ### πŸ”’ AI Detection Proof
199
+ Advanced algorithms remove all AI patterns
200
+ """)
201
+ with col_f2:
202
+ st.markdown("""
203
+ ### 🌐 Multi-language Support
204
+ Process text in various English variants
205
+ """)
206
+ with col_f3:
207
+ st.markdown("""
208
+ ### ⚑ Instant Processing
209
+ Quick conversion with real-time preview
210
+ """)
211
+
212
+ # Instructions
213
+ with st.expander("πŸ“˜ How to Use RHAI"):
214
+ st.markdown("""
215
+ 1. **Input Method** - Paste text directly or upload Word document
216
+ 2. **Settings** - Choose language variant and processing mode
217
+ 3. **Humanize** - Click the HUMANIZE NOW button
218
+ 4. **Review & Download** - Check output and download preferred format
219
+ """)
220
+
221
+ st.markdown("""
222
+ ---
223
+ > **Note**: Current version uses GingerIt for grammar checking. For enterprise solutions with enhanced checking, contact RHAI Team.
224
+ """)