Update app.py
Browse files
app.py
CHANGED
@@ -33,20 +33,11 @@ class SynthIDApp:
|
|
33 |
|
34 |
try:
|
35 |
# Prepare the API request parameters
|
36 |
-
# Calculate input length in tokens first
|
37 |
-
test_response = requests.post(
|
38 |
-
self.api_url,
|
39 |
-
headers=self.headers,
|
40 |
-
json={"inputs": text}
|
41 |
-
)
|
42 |
-
input_length = len(test_response.json()[0]['tokens'])
|
43 |
-
|
44 |
# Prepare the API request parameters for watermarking
|
45 |
params = {
|
46 |
"inputs": text,
|
47 |
"parameters": {
|
48 |
-
"
|
49 |
-
"min_length": input_length, # Force exact length
|
50 |
"do_sample": True,
|
51 |
"temperature": 0.7,
|
52 |
"top_p": 0.9,
|
@@ -69,24 +60,30 @@ class SynthIDApp:
|
|
69 |
response = requests.post(
|
70 |
self.api_url,
|
71 |
headers=self.headers,
|
72 |
-
json=params
|
|
|
73 |
)
|
74 |
response.raise_for_status()
|
75 |
|
76 |
# Extract the watermarked text
|
77 |
result = response.json()
|
78 |
if isinstance(result, list) and len(result) > 0:
|
|
|
|
|
|
|
79 |
watermarked_text = result[0].get('generated_text', '').strip()
|
80 |
if not watermarked_text:
|
81 |
return text, "Error: No watermarked text generated"
|
82 |
|
83 |
-
#
|
84 |
-
if len(watermarked_text
|
85 |
-
return text, "Error: Generated text too
|
|
|
|
|
86 |
|
87 |
return watermarked_text, f"Watermark applied successfully! (ngram_len: {ngram_len})"
|
88 |
else:
|
89 |
-
return text, "Error: Unexpected API response format"
|
90 |
|
91 |
return watermarked_text, f"Watermark applied successfully! (ngram_len: {ngram_len})"
|
92 |
except Exception as e:
|
|
|
33 |
|
34 |
try:
|
35 |
# Prepare the API request parameters
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
# Prepare the API request parameters for watermarking
|
37 |
params = {
|
38 |
"inputs": text,
|
39 |
"parameters": {
|
40 |
+
"max_new_tokens": 0, # Don't generate new tokens
|
|
|
41 |
"do_sample": True,
|
42 |
"temperature": 0.7,
|
43 |
"top_p": 0.9,
|
|
|
60 |
response = requests.post(
|
61 |
self.api_url,
|
62 |
headers=self.headers,
|
63 |
+
json=params,
|
64 |
+
timeout=30 # Add timeout
|
65 |
)
|
66 |
response.raise_for_status()
|
67 |
|
68 |
# Extract the watermarked text
|
69 |
result = response.json()
|
70 |
if isinstance(result, list) and len(result) > 0:
|
71 |
+
if 'error' in result[0]:
|
72 |
+
return text, f"API Error: {result[0]['error']}"
|
73 |
+
|
74 |
watermarked_text = result[0].get('generated_text', '').strip()
|
75 |
if not watermarked_text:
|
76 |
return text, "Error: No watermarked text generated"
|
77 |
|
78 |
+
# Add basic length validation
|
79 |
+
if len(watermarked_text) < len(text) * 0.5:
|
80 |
+
return text, "Error: Generated text too short"
|
81 |
+
if len(watermarked_text) > len(text) * 1.5:
|
82 |
+
return text, "Error: Generated text too long"
|
83 |
|
84 |
return watermarked_text, f"Watermark applied successfully! (ngram_len: {ngram_len})"
|
85 |
else:
|
86 |
+
return text, f"Error: Unexpected API response format: {str(result)}"
|
87 |
|
88 |
return watermarked_text, f"Watermark applied successfully! (ngram_len: {ngram_len})"
|
89 |
except Exception as e:
|