Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,18 +1,34 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
# Get token from environment (set in HF Space secrets)
|
| 6 |
HF_TOKEN = os.environ.get("HF_TOKEN", "")
|
|
|
|
|
|
|
| 7 |
client = InferenceClient(token=HF_TOKEN) if HF_TOKEN else InferenceClient()
|
|
|
|
| 8 |
|
| 9 |
|
| 10 |
def analyze(text: str) -> tuple[str, dict]:
|
| 11 |
"""Return emoji + label and confidence scores."""
|
|
|
|
|
|
|
| 12 |
if not text.strip():
|
|
|
|
| 13 |
return "π€ Enter some text!", {}
|
| 14 |
|
| 15 |
try:
|
|
|
|
| 16 |
result = client.text_classification(
|
| 17 |
text,
|
| 18 |
model="distilbert-base-uncased-finetuned-sst-2-english",
|
|
@@ -20,12 +36,17 @@ def analyze(text: str) -> tuple[str, dict]:
|
|
| 20 |
|
| 21 |
label = result.label
|
| 22 |
score = result.score
|
|
|
|
|
|
|
| 23 |
emoji = "π" if label == "POSITIVE" else "π"
|
| 24 |
return f"{emoji} {label} ({score:.1%})", {label: score, "OTHER": 1 - score}
|
| 25 |
except Exception as e:
|
|
|
|
| 26 |
return f"β Error: {e}", {}
|
| 27 |
|
| 28 |
|
|
|
|
|
|
|
| 29 |
with gr.Blocks(title="Sentiment Explorer") as demo:
|
| 30 |
gr.Markdown("# π Sentiment Explorer\nType anything and see if it's positive or negative!")
|
| 31 |
|
|
@@ -54,4 +75,5 @@ with gr.Blocks(title="Sentiment Explorer") as demo:
|
|
| 54 |
)
|
| 55 |
|
| 56 |
demo.queue()
|
|
|
|
| 57 |
demo.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
+
import logging
|
| 3 |
import gradio as gr
|
| 4 |
from huggingface_hub import InferenceClient
|
| 5 |
|
| 6 |
+
# Configure logging
|
| 7 |
+
logging.basicConfig(
|
| 8 |
+
level=logging.INFO,
|
| 9 |
+
format="%(asctime)s | %(levelname)s | %(message)s",
|
| 10 |
+
datefmt="%Y-%m-%d %H:%M:%S",
|
| 11 |
+
)
|
| 12 |
+
logger = logging.getLogger(__name__)
|
| 13 |
+
|
| 14 |
# Get token from environment (set in HF Space secrets)
|
| 15 |
HF_TOKEN = os.environ.get("HF_TOKEN", "")
|
| 16 |
+
logger.info(f"HF_TOKEN configured: {bool(HF_TOKEN)}")
|
| 17 |
+
|
| 18 |
client = InferenceClient(token=HF_TOKEN) if HF_TOKEN else InferenceClient()
|
| 19 |
+
logger.info("InferenceClient initialized")
|
| 20 |
|
| 21 |
|
| 22 |
def analyze(text: str) -> tuple[str, dict]:
|
| 23 |
"""Return emoji + label and confidence scores."""
|
| 24 |
+
logger.info(f"analyze() called with text length: {len(text)}")
|
| 25 |
+
|
| 26 |
if not text.strip():
|
| 27 |
+
logger.warning("Empty text received")
|
| 28 |
return "π€ Enter some text!", {}
|
| 29 |
|
| 30 |
try:
|
| 31 |
+
logger.info("Calling text_classification API...")
|
| 32 |
result = client.text_classification(
|
| 33 |
text,
|
| 34 |
model="distilbert-base-uncased-finetuned-sst-2-english",
|
|
|
|
| 36 |
|
| 37 |
label = result.label
|
| 38 |
score = result.score
|
| 39 |
+
logger.info(f"Result: {label} ({score:.1%})")
|
| 40 |
+
|
| 41 |
emoji = "π" if label == "POSITIVE" else "π"
|
| 42 |
return f"{emoji} {label} ({score:.1%})", {label: score, "OTHER": 1 - score}
|
| 43 |
except Exception as e:
|
| 44 |
+
logger.error(f"API error: {e}")
|
| 45 |
return f"β Error: {e}", {}
|
| 46 |
|
| 47 |
|
| 48 |
+
logger.info("Building Gradio interface...")
|
| 49 |
+
|
| 50 |
with gr.Blocks(title="Sentiment Explorer") as demo:
|
| 51 |
gr.Markdown("# π Sentiment Explorer\nType anything and see if it's positive or negative!")
|
| 52 |
|
|
|
|
| 75 |
)
|
| 76 |
|
| 77 |
demo.queue()
|
| 78 |
+
logger.info("Starting Gradio server...")
|
| 79 |
demo.launch()
|