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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -3
app.py CHANGED
@@ -11,6 +11,8 @@ class SynthIDApp:
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
 
@@ -18,14 +20,23 @@ class SynthIDApp:
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."""
@@ -66,7 +77,8 @@ class SynthIDApp:
66
  response = requests.post(
67
  self.api_url,
68
  headers=self.headers,
69
- json=params
 
70
  )
71
  response.raise_for_status()
72
 
 
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
  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
+ error_msg = str(e)
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."""
 
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