Nur Arifin Akbar commited on
Commit
4cd0e28
·
0 Parent(s):

Initial commit: Human-like text generator with Kimi-K2-Thinking

Browse files
Files changed (6) hide show
  1. .gitattributes +35 -0
  2. .gitignore +4 -0
  3. README.md +40 -0
  4. app.py +167 -0
  5. prompt.txt +26 -0
  6. requirements.txt +3 -0
.gitattributes ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
.gitignore ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ .env
2
+ __pycache__/
3
+ *.pyc
4
+ .DS_Store
README.md ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Human Text Rewriter - Kimi K2
3
+ emoji: ✍️
4
+ colorFrom: blue
5
+ colorTo: green
6
+ sdk: gradio
7
+ sdk_version: 5.9.1
8
+ app_file: app.py
9
+ pinned: false
10
+ ---
11
+
12
+ # Human-Like Text Generator
13
+
14
+ Rewrite AI-generated or formal content to sound more natural and human-like using **Kimi-K2-Thinking**.
15
+
16
+ ## Setup
17
+
18
+ Set these in the Space's **Settings → Repository secrets**:
19
+
20
+ - `API_KEY` - Your API key (Chutes AI compatible)
21
+ - `API_URL` - API endpoint (default: https://llm.chutes.ai/v1/chat/completions)
22
+ - `MODEL` - Model name: `moonshotai/Kimi-K2-Thinking`
23
+
24
+ ## Model
25
+
26
+ **Kimi-K2-Thinking** - Moonshot AI's reasoning model with thinking capabilities
27
+
28
+ ## How It Works
29
+
30
+ 1. Sends your content to Kimi-K2-Thinking
31
+ 2. Applies a natural rewriting prompt
32
+ 3. Filters out thinking tokens automatically
33
+ 4. Streams the humanized output in real-time
34
+
35
+ ## Usage
36
+
37
+ 1. Paste your formal/AI-generated content
38
+ 2. Click "Generate Human Version"
39
+ 3. Watch the text appear in real-time
40
+ 4. Copy the humanized result
app.py ADDED
@@ -0,0 +1,167 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import os
4
+ import re
5
+ import json
6
+ from dotenv import load_dotenv
7
+
8
+ # Load environment variables
9
+ load_dotenv()
10
+
11
+ API_KEY = os.getenv("API_KEY", "")
12
+ API_URL = os.getenv("API_URL", "https://openrouter.ai/api/v1/chat/completions")
13
+ MODEL = os.getenv("MODEL", "moonshotai/Kimi-K2-Thinking")
14
+
15
+ # Load the prompt template
16
+ def load_prompt():
17
+ with open("prompt.txt", "r") as f:
18
+ return f.read()
19
+
20
+ PROMPT_TEMPLATE = load_prompt()
21
+
22
+ def generate_human_text(content: str, progress=gr.Progress()):
23
+ """Generate human-like text using Kimi-K2-Thinking model."""
24
+
25
+ if not content.strip():
26
+ yield "⚠️ Please enter some content!"
27
+ return
28
+
29
+ if not API_KEY:
30
+ yield "⚠️ API key not configured. Please contact the administrator."
31
+ return
32
+
33
+ progress(0.1, desc="Preparing request...")
34
+
35
+ # Build the prompt
36
+ prompt = PROMPT_TEMPLATE.replace("{content}", content)
37
+
38
+ headers = {
39
+ "Authorization": f"Bearer {API_KEY}",
40
+ "Content-Type": "application/json"
41
+ }
42
+
43
+ payload = {
44
+ "model": MODEL,
45
+ "messages": [
46
+ {"role": "user", "content": prompt}
47
+ ],
48
+ "temperature": 0.8,
49
+ "max_tokens": 16000,
50
+ "stream": True
51
+ }
52
+
53
+ progress(0.3, desc="Connecting to AI...")
54
+
55
+ try:
56
+ response = requests.post(
57
+ API_URL,
58
+ headers=headers,
59
+ json=payload,
60
+ timeout=180,
61
+ stream=True
62
+ )
63
+
64
+ response.raise_for_status()
65
+
66
+ progress(0.5, desc="Receiving response...")
67
+
68
+ full_content = ""
69
+ is_streaming_answer = False
70
+
71
+ # Process streaming response
72
+ for line in response.iter_lines():
73
+ if not line:
74
+ continue
75
+
76
+ line = line.decode('utf-8')
77
+
78
+ if line.startswith('data: '):
79
+ data_str = line[6:]
80
+
81
+ if data_str == '[DONE]':
82
+ break
83
+
84
+ try:
85
+ data = json.loads(data_str)
86
+ if 'choices' in data and len(data['choices']) > 0:
87
+ delta = data['choices'][0].get('delta', {})
88
+ chunk = delta.get('content', '')
89
+
90
+ if chunk:
91
+ full_content += chunk
92
+
93
+ # Wait until thinking is done
94
+ if '</think>' in full_content and not is_streaming_answer:
95
+ is_streaming_answer = True
96
+ progress(0.7, desc="Generating human-like text...")
97
+
98
+ # Only stream if we're past the thinking phase
99
+ if is_streaming_answer:
100
+ # Remove all thinking content
101
+ clean_content = re.sub(r'<think>.*?</think>', '', full_content, flags=re.DOTALL)
102
+ clean_content = clean_content.strip()
103
+
104
+ if clean_content:
105
+ yield clean_content
106
+ except:
107
+ continue
108
+
109
+ progress(1.0, desc="Done!")
110
+
111
+ # Final cleanup - remove all thinking tokens
112
+ final_content = re.sub(r'<think>.*?</think>', '', full_content, flags=re.DOTALL)
113
+ # Also remove any remaining unclosed think tags
114
+ final_content = re.sub(r'<think>.*$', '', final_content, flags=re.DOTALL)
115
+ final_content = final_content.strip()
116
+
117
+ yield final_content if final_content else "⚠️ No content generated. Please try again with different text."
118
+
119
+ except requests.exceptions.Timeout:
120
+ yield "⏱️ Request timed out. The AI is taking too long to respond. Try with shorter text or try again later."
121
+ except requests.exceptions.RequestException as e:
122
+ yield f"❌ API Error: {str(e)}"
123
+ except Exception as e:
124
+ yield f"❌ Unexpected error: {str(e)}"
125
+
126
+ # Create the Gradio interface
127
+ with gr.Blocks(title="Human-Like Text Generator - Kimi K2") as app:
128
+ gr.Markdown("""
129
+ # ✍️ Human-Like Text Generator
130
+ Transform AI-generated or formal text into natural, human-sounding content using Open Source Model.
131
+
132
+ ⚡ **Streaming enabled!** You'll see the text appear in real-time as the AI writes.
133
+ """)
134
+
135
+ with gr.Row():
136
+ with gr.Column():
137
+ content = gr.Textbox(
138
+ label="Your Content",
139
+ placeholder="Paste the content you want rewritten in a human style...",
140
+ lines=12,
141
+ max_lines=20
142
+ )
143
+ generate_btn = gr.Button("🚀 Generate Human Version", variant="primary", size="lg")
144
+
145
+ with gr.Column():
146
+ output = gr.Textbox(
147
+ label="Human-Like Output",
148
+ lines=12,
149
+ max_lines=20
150
+ )
151
+
152
+ gr.Markdown("💡 **Tip:** For best results, paste clear, complete paragraphs. The output will match the length and topic of your input.")
153
+
154
+ generate_btn.click(
155
+ fn=generate_human_text,
156
+ inputs=[content],
157
+ outputs=output
158
+ )
159
+
160
+ content.submit(
161
+ fn=generate_human_text,
162
+ inputs=[content],
163
+ outputs=output
164
+ )
165
+
166
+ if __name__ == "__main__":
167
+ app.launch()
prompt.txt ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Rewrite the following content to sound more natural and human-like, while preserving the core message and meaning.
2
+
3
+ IMPORTANT: Provide only the rewritten text. No explanations, no meta-commentary, no thinking process.
4
+
5
+ Guidelines:
6
+ - Maintain similar length and structure to the original
7
+ - Use varied sentence lengths naturally
8
+ - Write as if explaining to an informed colleague, not lecturing
9
+ - Replace overly formal or academic language with clearer alternatives
10
+ - Keep technical terms when necessary, but explain them naturally
11
+ - Avoid unnecessary questions or rhetorical devices
12
+ - Use straightforward transitions between ideas
13
+ - Present information directly without excessive casual interjections
14
+
15
+ Avoid:
16
+ - Overly formal academic language ("Furthermore", "In conclusion", "Moreover")
17
+ - Perfectly structured, symmetrical paragraphs
18
+ - Robotic or repetitive sentence patterns
19
+ - Excessive use of questions or exclamations
20
+ - Talking down to the reader or over-explaining basic concepts
21
+ - Adding filler words just to sound casual ("like", "you know", "I mean")
22
+
23
+ Goal: Make the text feel like it was written by a knowledgeable person communicating clearly, not by an AI or an academic paper. The result should be professional but accessible, informative but not stiff.
24
+
25
+ Content to rewrite:
26
+ {content}
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio
2
+ requests
3
+ python-dotenv