cyberandy commited on
Commit
9c1cc06
·
verified ·
1 Parent(s): f03955d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -31
app.py CHANGED
@@ -1,28 +1,31 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
  from transformers import SynthIDTextWatermarkingConfig
4
 
5
  class SynthIDApp:
6
  def __init__(self):
7
- self.client = None
 
8
  self.watermarking_config = 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 inference client with authentication."""
13
  try:
14
- # Initialize the inference client
15
- self.client = InferenceClient(
16
- model="google/gemma-2b",
17
- token=hf_token
 
 
 
18
  )
 
19
 
20
- # Test the connection with a simple generation
21
- _ = self.client.text_generation("Test", max_new_tokens=1)
22
- return "Inference client initialized successfully!"
23
  except Exception as e:
24
- self.client = None
25
- return f"Error initializing client: {str(e)}"
26
 
27
  def update_watermark_config(self, ngram_len):
28
  """Update the watermarking configuration with new ngram_len."""
@@ -36,32 +39,44 @@ class SynthIDApp:
36
  return f"Error updating config: {str(e)}"
37
 
38
  def apply_watermark(self, text, ngram_len):
39
- """Apply SynthID watermark to input text using the inference endpoint."""
40
- if not self.client:
41
- return text, "Error: Client not initialized. Please login first."
42
 
43
  try:
44
  # Update watermark config with current ngram_len
45
  self.update_watermark_config(ngram_len)
46
 
47
- # Convert watermarking config to dict for the API call
48
- watermark_dict = {
49
- "keys": self.watermarking_config.keys,
50
- "ngram_len": self.watermarking_config.ngram_len
 
 
 
 
 
 
 
 
 
51
  }
52
 
53
- # Make the API call with watermarking config
54
- response = self.client.text_generation(
55
- text,
56
- max_new_tokens=100,
57
- do_sample=True,
58
- temperature=0.7,
59
- top_p=0.9,
60
- watermarking_config=watermark_dict,
61
- return_full_text=False
62
  )
 
63
 
64
- watermarked_text = response
 
 
 
 
 
 
65
  return watermarked_text, f"Watermark applied successfully! (ngram_len: {ngram_len})"
66
  except Exception as e:
67
  return text, f"Error applying watermark: {str(e)}"
@@ -89,7 +104,7 @@ app_instance = SynthIDApp()
89
 
90
  with gr.Blocks(title="SynthID Text Watermarking Tool") as app:
91
  gr.Markdown("# SynthID Text Watermarking Tool")
92
- gr.Markdown("Using Hugging Face Inference Endpoints for faster processing")
93
 
94
  # Login section
95
  with gr.Row():
@@ -153,7 +168,7 @@ with gr.Blocks(title="SynthID Text Watermarking Tool") as app:
153
  3. Adjust the N-gram Length slider to control watermark characteristics
154
 
155
  ### Notes:
156
- - This version uses Hugging Face's Inference Endpoints for faster processing
157
  - No model download required - everything runs in the cloud
158
  - The watermark is designed to be imperceptible to humans
159
  - This demo only implements watermark application
 
1
  import gradio as gr
2
+ import requests
3
  from transformers import SynthIDTextWatermarkingConfig
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
  try:
15
+ self.headers = {"Authorization": f"Bearer {hf_token}"}
16
+
17
+ # Test the connection with a simple query
18
+ response = requests.post(
19
+ self.api_url,
20
+ headers=self.headers,
21
+ json={"inputs": "Test", "parameters": {"max_new_tokens": 1}}
22
  )
23
+ response.raise_for_status()
24
 
25
+ return "API connection initialized successfully!"
 
 
26
  except Exception as e:
27
+ self.headers = None
28
+ return f"Error initializing API: {str(e)}"
29
 
30
  def update_watermark_config(self, ngram_len):
31
  """Update the watermarking configuration with new ngram_len."""
 
39
  return f"Error updating config: {str(e)}"
40
 
41
  def apply_watermark(self, text, ngram_len):
42
+ """Apply SynthID watermark to input text using the inference API."""
43
+ if not self.headers:
44
+ return text, "Error: API not initialized. Please login first."
45
 
46
  try:
47
  # Update watermark config with current ngram_len
48
  self.update_watermark_config(ngram_len)
49
 
50
+ # Prepare the API request parameters
51
+ params = {
52
+ "inputs": text,
53
+ "parameters": {
54
+ "max_new_tokens": 100,
55
+ "do_sample": True,
56
+ "temperature": 0.7,
57
+ "top_p": 0.9,
58
+ "watermarking_config": {
59
+ "keys": self.watermarking_config.keys,
60
+ "ngram_len": self.watermarking_config.ngram_len
61
+ }
62
+ }
63
  }
64
 
65
+ # Make the API call
66
+ response = requests.post(
67
+ self.api_url,
68
+ headers=self.headers,
69
+ json=params
 
 
 
 
70
  )
71
+ response.raise_for_status()
72
 
73
+ # Extract the generated text
74
+ result = response.json()
75
+ if isinstance(result, list) and len(result) > 0:
76
+ watermarked_text = result[0].get('generated_text', text)
77
+ else:
78
+ watermarked_text = text
79
+
80
  return watermarked_text, f"Watermark applied successfully! (ngram_len: {ngram_len})"
81
  except Exception as e:
82
  return text, f"Error applying watermark: {str(e)}"
 
104
 
105
  with gr.Blocks(title="SynthID Text Watermarking Tool") as app:
106
  gr.Markdown("# SynthID Text Watermarking Tool")
107
+ gr.Markdown("Using Hugging Face Inference API for faster processing")
108
 
109
  # Login section
110
  with gr.Row():
 
168
  3. Adjust the N-gram Length slider to control watermark characteristics
169
 
170
  ### Notes:
171
+ - This version uses Hugging Face's Inference API for faster processing
172
  - No model download required - everything runs in the cloud
173
  - The watermark is designed to be imperceptible to humans
174
  - This demo only implements watermark application