Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,15 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
-
|
| 4 |
|
| 5 |
class SynthIDApp:
|
| 6 |
def __init__(self):
|
| 7 |
self.api_url = "https://api-inference.huggingface.co/models/google/gemma-2b"
|
| 8 |
self.headers = None
|
| 9 |
-
self.watermarking_config = None
|
| 10 |
self.WATERMARK_KEYS = [654, 400, 836, 123, 340, 443, 597, 160, 57, 789]
|
| 11 |
|
| 12 |
def login(self, hf_token):
|
| 13 |
"""Initialize the API headers with authentication."""
|
| 14 |
-
if not hf_token or not hf_token.startswith('hf_'):
|
| 15 |
-
return "Error: Please enter a valid Hugging Face token (starts with 'hf_')"
|
| 16 |
try:
|
| 17 |
self.headers = {"Authorization": f"Bearer {hf_token}"}
|
| 18 |
|
|
@@ -20,34 +17,14 @@ class SynthIDApp:
|
|
| 20 |
response = requests.post(
|
| 21 |
self.api_url,
|
| 22 |
headers=self.headers,
|
| 23 |
-
json={"inputs": "Test", "parameters": {"max_new_tokens": 1}}
|
| 24 |
-
timeout=10 # Add 10 second timeout
|
| 25 |
)
|
| 26 |
response.raise_for_status()
|
| 27 |
|
| 28 |
return "API connection initialized successfully!"
|
| 29 |
except Exception as e:
|
| 30 |
self.headers = None
|
| 31 |
-
|
| 32 |
-
if "timeout" in error_msg.lower():
|
| 33 |
-
return "Error: API connection timed out. Please try again."
|
| 34 |
-
elif "forbidden" in error_msg.lower():
|
| 35 |
-
return "Error: Invalid token or insufficient permissions."
|
| 36 |
-
elif "not found" in error_msg.lower():
|
| 37 |
-
return "Error: Model not found or unavailable."
|
| 38 |
-
else:
|
| 39 |
-
return f"Error initializing API: {error_msg}"
|
| 40 |
-
|
| 41 |
-
def update_watermark_config(self, ngram_len):
|
| 42 |
-
"""Update the watermarking configuration with new ngram_len."""
|
| 43 |
-
try:
|
| 44 |
-
self.watermarking_config = SynthIDTextWatermarkingConfig(
|
| 45 |
-
keys=self.WATERMARK_KEYS,
|
| 46 |
-
ngram_len=ngram_len
|
| 47 |
-
)
|
| 48 |
-
return f"Watermark config updated: ngram_len = {ngram_len}"
|
| 49 |
-
except Exception as e:
|
| 50 |
-
return f"Error updating config: {str(e)}"
|
| 51 |
|
| 52 |
def apply_watermark(self, text, ngram_len):
|
| 53 |
"""Apply SynthID watermark to input text using the inference API."""
|
|
@@ -55,9 +32,6 @@ class SynthIDApp:
|
|
| 55 |
return text, "Error: API not initialized. Please login first."
|
| 56 |
|
| 57 |
try:
|
| 58 |
-
# Update watermark config with current ngram_len
|
| 59 |
-
self.update_watermark_config(ngram_len)
|
| 60 |
-
|
| 61 |
# Prepare the API request parameters
|
| 62 |
params = {
|
| 63 |
"inputs": text,
|
|
@@ -67,8 +41,8 @@ class SynthIDApp:
|
|
| 67 |
"temperature": 0.7,
|
| 68 |
"top_p": 0.9,
|
| 69 |
"watermarking_config": {
|
| 70 |
-
"keys": self.
|
| 71 |
-
"ngram_len":
|
| 72 |
}
|
| 73 |
}
|
| 74 |
}
|
|
@@ -77,8 +51,7 @@ class SynthIDApp:
|
|
| 77 |
response = requests.post(
|
| 78 |
self.api_url,
|
| 79 |
headers=self.headers,
|
| 80 |
-
json=params
|
| 81 |
-
timeout=30 # Add 30 second timeout for generation
|
| 82 |
)
|
| 83 |
response.raise_for_status()
|
| 84 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
+
import json
|
| 4 |
|
| 5 |
class SynthIDApp:
|
| 6 |
def __init__(self):
|
| 7 |
self.api_url = "https://api-inference.huggingface.co/models/google/gemma-2b"
|
| 8 |
self.headers = None
|
|
|
|
| 9 |
self.WATERMARK_KEYS = [654, 400, 836, 123, 340, 443, 597, 160, 57, 789]
|
| 10 |
|
| 11 |
def login(self, hf_token):
|
| 12 |
"""Initialize the API headers with authentication."""
|
|
|
|
|
|
|
| 13 |
try:
|
| 14 |
self.headers = {"Authorization": f"Bearer {hf_token}"}
|
| 15 |
|
|
|
|
| 17 |
response = requests.post(
|
| 18 |
self.api_url,
|
| 19 |
headers=self.headers,
|
| 20 |
+
json={"inputs": "Test", "parameters": {"max_new_tokens": 1}}
|
|
|
|
| 21 |
)
|
| 22 |
response.raise_for_status()
|
| 23 |
|
| 24 |
return "API connection initialized successfully!"
|
| 25 |
except Exception as e:
|
| 26 |
self.headers = None
|
| 27 |
+
return f"Error initializing API: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
def apply_watermark(self, text, ngram_len):
|
| 30 |
"""Apply SynthID watermark to input text using the inference API."""
|
|
|
|
| 32 |
return text, "Error: API not initialized. Please login first."
|
| 33 |
|
| 34 |
try:
|
|
|
|
|
|
|
|
|
|
| 35 |
# Prepare the API request parameters
|
| 36 |
params = {
|
| 37 |
"inputs": text,
|
|
|
|
| 41 |
"temperature": 0.7,
|
| 42 |
"top_p": 0.9,
|
| 43 |
"watermarking_config": {
|
| 44 |
+
"keys": self.WATERMARK_KEYS,
|
| 45 |
+
"ngram_len": int(ngram_len) # Ensure integer
|
| 46 |
}
|
| 47 |
}
|
| 48 |
}
|
|
|
|
| 51 |
response = requests.post(
|
| 52 |
self.api_url,
|
| 53 |
headers=self.headers,
|
| 54 |
+
json=params
|
|
|
|
| 55 |
)
|
| 56 |
response.raise_for_status()
|
| 57 |
|