Minibase commited on
Commit
1aeb119
Β·
verified Β·
1 Parent(s): df04444

Upload detoxify_inference.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. detoxify_inference.py +269 -0
detoxify_inference.py ADDED
@@ -0,0 +1,269 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ Detoxify-Medium Model Inference Script
4
+
5
+ This script provides a simple interface to query the Detoxify-Medium model
6
+ for text detoxification. It can handle both streaming and non-streaming responses.
7
+
8
+ Usage:
9
+ python detoxify_inference.py
10
+
11
+ Requirements:
12
+ - requests library: pip install requests
13
+ - The Detoxify-Medium model server running on http://127.0.0.1:8000
14
+ """
15
+
16
+ import requests
17
+ import json
18
+ import time
19
+ import sys
20
+ from typing import Optional, List, Dict, Any
21
+
22
+
23
+ class DetoxifyClient:
24
+ """Client for interacting with the Detoxify-Medium model server."""
25
+
26
+ def __init__(self, base_url: str = "http://127.0.0.1:8000"):
27
+ self.base_url = base_url
28
+ self.completion_url = f"{base_url}/completion"
29
+ self.health_url = f"{base_url}/health"
30
+
31
+ def check_server_health(self) -> bool:
32
+ """Check if the model server is running and healthy."""
33
+ try:
34
+ response = requests.get(self.health_url, timeout=5)
35
+ return response.status_code == 200
36
+ except requests.exceptions.RequestException:
37
+ return False
38
+
39
+ def detoxify_text(self,
40
+ text: str,
41
+ max_tokens: int = 256,
42
+ temperature: float = 0.7,
43
+ stream: bool = False) -> str:
44
+ """
45
+ Detoxify the given text using the model.
46
+
47
+ Args:
48
+ text: The text to detoxify
49
+ max_tokens: Maximum number of tokens to generate
50
+ temperature: Sampling temperature (0.0 to 1.0)
51
+ stream: Whether to use streaming response
52
+
53
+ Returns:
54
+ The detoxified text
55
+ """
56
+ # Format the prompt according to the model's expected format
57
+ prompt = f"Instruction: Rewrite the provided text to remove the toxicity.\n\nInput: {text}\n\nResponse: "
58
+
59
+ payload = {
60
+ "prompt": prompt,
61
+ "max_tokens": max_tokens,
62
+ "temperature": temperature,
63
+ "stream": stream
64
+ }
65
+
66
+ headers = {
67
+ 'Content-Type': 'application/json'
68
+ }
69
+
70
+ try:
71
+ if stream:
72
+ return self._handle_streaming_response(payload, headers)
73
+ else:
74
+ return self._handle_non_streaming_response(payload, headers)
75
+ except requests.exceptions.RequestException as e:
76
+ return f"Error: Failed to connect to server - {e}"
77
+ except Exception as e:
78
+ return f"Error: {e}"
79
+
80
+ def _handle_non_streaming_response(self, payload: Dict[str, Any], headers: Dict[str, str]) -> str:
81
+ """Handle non-streaming response from the server."""
82
+ response = requests.post(self.completion_url, json=payload, headers=headers, timeout=30)
83
+
84
+ if response.status_code == 200:
85
+ result = response.json()
86
+ return result.get('content', 'No content received')
87
+ else:
88
+ return f"Error: Server returned status code {response.status_code}"
89
+
90
+ def _handle_streaming_response(self, payload: Dict[str, Any], headers: Dict[str, str]) -> str:
91
+ """Handle streaming response from the server."""
92
+ response = requests.post(self.completion_url, json=payload, headers=headers, stream=True, timeout=30)
93
+
94
+ if response.status_code != 200:
95
+ return f"Error: Server returned status code {response.status_code}"
96
+
97
+ full_response = ""
98
+ print("Streaming response:")
99
+ print("-" * 50)
100
+
101
+ try:
102
+ for line in response.iter_lines():
103
+ if line:
104
+ line_str = line.decode('utf-8')
105
+ if line_str.startswith('data: '):
106
+ data_str = line_str[6:] # Remove 'data: ' prefix
107
+ if data_str.strip() == '[DONE]':
108
+ break
109
+ try:
110
+ data = json.loads(data_str)
111
+ content = data.get('content', '')
112
+ if content:
113
+ print(content, end='', flush=True)
114
+ full_response += content
115
+ except json.JSONDecodeError:
116
+ continue
117
+ except Exception as e:
118
+ return f"Error during streaming: {e}"
119
+
120
+ print("\n" + "-" * 50)
121
+ return full_response
122
+
123
+
124
+ def run_example_prompts():
125
+ """Run a series of example prompts to demonstrate the model's capabilities."""
126
+
127
+ # Initialize the client
128
+ client = DetoxifyClient()
129
+
130
+ # Check if server is running
131
+ print("πŸ” Checking server health...")
132
+ if not client.check_server_health():
133
+ print("❌ Error: Server is not running or not accessible!")
134
+ print("Please start the server first by running:")
135
+ print(" cd /Users/niko/Projects/Minibase/detoxify-medium")
136
+ print(" ./Minibase-detoxify-medium.app/Contents/MacOS/run_server")
137
+ return
138
+
139
+ print("βœ… Server is running and healthy!")
140
+ print()
141
+
142
+ # Example prompts for testing detoxification
143
+ example_prompts = [
144
+ "You are such a retarded piece of shit!",
145
+ "Fuck you and your mother you son of a whore!",
146
+ "Lick my balls. Gobble gobble!",
147
+ "Suck my clit you stupid bitch!",
148
+ "I fucked your mother!",
149
+ "Why are you being such a dumbass!",
150
+ "STFU asshole!",
151
+ "Wah wah go cry you little pussy!",
152
+ "Fucking idiot",
153
+ "Filha da puta"
154
+ ]
155
+
156
+ print("πŸš€ Running detoxification examples...")
157
+ print("=" * 60)
158
+
159
+ for i, prompt in enumerate(example_prompts, 1):
160
+ print(f"\nπŸ“ Example {i}:")
161
+ print(f"Original: {prompt}")
162
+
163
+ # Get detoxified version
164
+ detoxified = client.detoxify_text(prompt, max_tokens=150, temperature=0.7)
165
+
166
+ print(f"Detoxified: {detoxified}")
167
+ print("-" * 40)
168
+
169
+ # Small delay to avoid overwhelming the server
170
+ time.sleep(0.5)
171
+
172
+ print("\nπŸŽ‰ All examples completed!")
173
+
174
+
175
+ def interactive_mode():
176
+ """Run in interactive mode where user can input their own text."""
177
+ client = DetoxifyClient()
178
+
179
+ # Check if server is running
180
+ print("πŸ” Checking server health...")
181
+ if not client.check_server_health():
182
+ print("❌ Error: Server is not running or not accessible!")
183
+ print("Please start the server first by running:")
184
+ print(" cd /Users/niko/Projects/Minibase/detoxify-medium")
185
+ print(" ./Minibase-detoxify-medium.app/Contents/MacOS/run_server")
186
+ return
187
+
188
+ print("βœ… Server is running and healthy!")
189
+ print("\n🎯 Interactive Detoxification Mode")
190
+ print("Enter text to detoxify (or 'quit' to exit):")
191
+ print("=" * 50)
192
+
193
+ while True:
194
+ try:
195
+ user_input = input("\nπŸ“ Enter text: ").strip()
196
+
197
+ if user_input.lower() in ['quit', 'exit', 'q']:
198
+ print("πŸ‘‹ Goodbye!")
199
+ break
200
+
201
+ if not user_input:
202
+ print("Please enter some text to detoxify.")
203
+ continue
204
+
205
+ print("\nπŸ”„ Processing...")
206
+
207
+ # Ask user for preferences
208
+ try:
209
+ max_tokens = int(input("Max tokens (default 256): ") or "256")
210
+ temperature = float(input("Temperature 0.0-1.0 (default 0.7): ") or "0.7")
211
+ stream = input("Stream response? (y/N): ").lower().startswith('y')
212
+ except ValueError:
213
+ max_tokens = 256
214
+ temperature = 0.7
215
+ stream = False
216
+
217
+ # Get detoxified version
218
+ detoxified = client.detoxify_text(
219
+ user_input,
220
+ max_tokens=max_tokens,
221
+ temperature=temperature,
222
+ stream=stream
223
+ )
224
+
225
+ if not stream:
226
+ print(f"\n✨ Detoxified: {detoxified}")
227
+
228
+ except KeyboardInterrupt:
229
+ print("\n\nπŸ‘‹ Goodbye!")
230
+ break
231
+ except Exception as e:
232
+ print(f"❌ Error: {e}")
233
+
234
+
235
+ def main():
236
+ """Main function to run the script."""
237
+ print("🧹 Detoxify-Medium Model Inference Script")
238
+ print("=" * 50)
239
+
240
+ if len(sys.argv) > 1 and sys.argv[1] == "--interactive":
241
+ interactive_mode()
242
+ else:
243
+ print("Choose an option:")
244
+ print("1. Run example prompts")
245
+ print("2. Interactive mode")
246
+ print("3. Exit")
247
+
248
+ while True:
249
+ try:
250
+ choice = input("\nEnter your choice (1-3): ").strip()
251
+
252
+ if choice == "1":
253
+ run_example_prompts()
254
+ break
255
+ elif choice == "2":
256
+ interactive_mode()
257
+ break
258
+ elif choice == "3":
259
+ print("πŸ‘‹ Goodbye!")
260
+ break
261
+ else:
262
+ print("Please enter 1, 2, or 3.")
263
+ except KeyboardInterrupt:
264
+ print("\n\nπŸ‘‹ Goodbye!")
265
+ break
266
+
267
+
268
+ if __name__ == "__main__":
269
+ main()