Update app.py
Browse files
app.py
CHANGED
|
@@ -97,16 +97,26 @@ def get_client():
|
|
| 97 |
logger.info("π Initializing HuggingFace client for Spaces...")
|
| 98 |
client = InferenceClient(token=api_token)
|
| 99 |
|
| 100 |
-
#
|
| 101 |
logger.info("π§ͺ Testing client connectivity...")
|
| 102 |
-
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
|
|
|
| 105 |
return client
|
| 106 |
|
| 107 |
except Exception as e:
|
| 108 |
logger.error(f"β Client initialization failed: {e}")
|
| 109 |
-
logger.error(f"β Token used: {api_token[:15]}..." if api_token else "No token")
|
| 110 |
return None
|
| 111 |
|
| 112 |
# Initialize client globally for Spaces
|
|
@@ -172,7 +182,12 @@ def run_fill_mask(text):
|
|
| 172 |
return "β Please enter text with [MASK]"
|
| 173 |
|
| 174 |
def fill_mask_call():
|
| 175 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
if isinstance(result, list):
|
| 178 |
output = "π **Predictions:**\n"
|
|
@@ -231,11 +246,24 @@ def run_text_classification(text):
|
|
| 231 |
return "β Please enter text to classify"
|
| 232 |
|
| 233 |
def classification_call():
|
| 234 |
-
#
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
|
| 240 |
if isinstance(result, list):
|
| 241 |
output = "π·οΈ **Sentiment Analysis:**\n"
|
|
@@ -243,7 +271,7 @@ def run_text_classification(text):
|
|
| 243 |
label = pred.get("label", "Unknown")
|
| 244 |
score = pred.get("score", 0)
|
| 245 |
# Clean up label names
|
| 246 |
-
clean_label = label.replace("LABEL_", "").title()
|
| 247 |
output += f"{i}. **{clean_label}** ({score:.3f})\n"
|
| 248 |
return output
|
| 249 |
return str(result)
|
|
@@ -661,5 +689,4 @@ if __name__ == "__main__":
|
|
| 661 |
server_port=7860,
|
| 662 |
show_error=True,
|
| 663 |
quiet=False
|
| 664 |
-
)
|
| 665 |
-
|
|
|
|
| 97 |
logger.info("π Initializing HuggingFace client for Spaces...")
|
| 98 |
client = InferenceClient(token=api_token)
|
| 99 |
|
| 100 |
+
# Simple connectivity test (optional)
|
| 101 |
logger.info("π§ͺ Testing client connectivity...")
|
| 102 |
+
try:
|
| 103 |
+
# Try fill-mask with bert-base-uncased (more likely to be available)
|
| 104 |
+
test_result = client.fill_mask("Paris is the [MASK] of France.", model="bert-base-uncased")
|
| 105 |
+
logger.info(f"β
Client test successful with bert-base-uncased - got {len(test_result)} results")
|
| 106 |
+
except Exception as e1:
|
| 107 |
+
try:
|
| 108 |
+
# Fallback: try text classification (almost always available)
|
| 109 |
+
test_result = client.text_classification("This is a test.", model="distilbert-base-uncased-finetuned-sst-2-english")
|
| 110 |
+
logger.info(f"β
Client test successful with sentiment model")
|
| 111 |
+
except Exception as e2:
|
| 112 |
+
# If tests fail, client is still valid - just skip testing
|
| 113 |
+
logger.info("β
Client initialized (skipping model tests - they may be loading)")
|
| 114 |
|
| 115 |
+
logger.info("β
HuggingFace client ready for use")
|
| 116 |
return client
|
| 117 |
|
| 118 |
except Exception as e:
|
| 119 |
logger.error(f"β Client initialization failed: {e}")
|
|
|
|
| 120 |
return None
|
| 121 |
|
| 122 |
# Initialize client globally for Spaces
|
|
|
|
| 182 |
return "β Please enter text with [MASK]"
|
| 183 |
|
| 184 |
def fill_mask_call():
|
| 185 |
+
# Try bert-base-uncased first, then fallback
|
| 186 |
+
try:
|
| 187 |
+
result = CLIENT.fill_mask(str(text).strip(), model="bert-base-uncased")
|
| 188 |
+
except:
|
| 189 |
+
# Fallback to distilbert if bert-base-uncased not available
|
| 190 |
+
result = CLIENT.fill_mask(str(text).strip(), model="distilbert-base-uncased")
|
| 191 |
|
| 192 |
if isinstance(result, list):
|
| 193 |
output = "π **Predictions:**\n"
|
|
|
|
| 246 |
return "β Please enter text to classify"
|
| 247 |
|
| 248 |
def classification_call():
|
| 249 |
+
# Try multiple models for better reliability
|
| 250 |
+
models_to_try = [
|
| 251 |
+
"cardiffnlp/twitter-roberta-base-sentiment-latest",
|
| 252 |
+
"distilbert-base-uncased-finetuned-sst-2-english",
|
| 253 |
+
"cardiffnlp/twitter-roberta-base-sentiment"
|
| 254 |
+
]
|
| 255 |
+
|
| 256 |
+
result = None
|
| 257 |
+
for model in models_to_try:
|
| 258 |
+
try:
|
| 259 |
+
result = CLIENT.text_classification(str(text).strip(), model=model)
|
| 260 |
+
break
|
| 261 |
+
except Exception as e:
|
| 262 |
+
logger.warning(f"Model {model} failed: {e}")
|
| 263 |
+
continue
|
| 264 |
+
|
| 265 |
+
if result is None:
|
| 266 |
+
return "β All sentiment models unavailable. Please try again later."
|
| 267 |
|
| 268 |
if isinstance(result, list):
|
| 269 |
output = "π·οΈ **Sentiment Analysis:**\n"
|
|
|
|
| 271 |
label = pred.get("label", "Unknown")
|
| 272 |
score = pred.get("score", 0)
|
| 273 |
# Clean up label names
|
| 274 |
+
clean_label = label.replace("LABEL_", "").replace("_", " ").title()
|
| 275 |
output += f"{i}. **{clean_label}** ({score:.3f})\n"
|
| 276 |
return output
|
| 277 |
return str(result)
|
|
|
|
| 689 |
server_port=7860,
|
| 690 |
show_error=True,
|
| 691 |
quiet=False
|
| 692 |
+
)
|
|
|