Spaces:
Sleeping
Sleeping
| import os | |
| import requests | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| HF_API_TOKEN = os.getenv("HF_API_TOKEN") | |
| SUMMARIZATION_MODEL = "facebook/bart-large-cnn" | |
| TAGS_MODEL = "dslim/bert-base-NER" | |
| SENTIMENT_MODEL = "cardiffnlp/twitter-roberta-base-sentiment" | |
| LABEL_MAP = {"LABEL_0": "Negative", "LABEL_1": "Neutral", "LABEL_2": "Positive"} | |
| ERROR_400_MSG = "Oops! Error 400 — this model might not understand that language. Try using English! If you did, then something went wrong on our end. Sorry x(" | |
| TIMEOUT = 30 | |
| def summarize_text(text): | |
| headers = {"Authorization": f"Bearer {HF_API_TOKEN}"} | |
| payload = {"inputs": text, "parameters": {"min_length": 30, "max_length": 130}} | |
| try: | |
| resp = requests.post( | |
| f"https://api-inference.huggingface.co/models/{SUMMARIZATION_MODEL}", | |
| headers=headers, | |
| json=payload, | |
| timeout=TIMEOUT | |
| ) | |
| resp.raise_for_status() | |
| out = resp.json() | |
| if isinstance(out, list) and out: | |
| return out[0]["summary_text"] | |
| return str(out) | |
| except requests.exceptions.HTTPError as e: | |
| if e.response.status_code == 400: | |
| return ERROR_400_MSG | |
| return f"Hugging Face API inference failed: {e}" | |
| def extract_tags(text): | |
| headers = {"Authorization": f"Bearer {HF_API_TOKEN}"} | |
| payload = {"inputs": text} | |
| try: | |
| resp = requests.post( | |
| f"https://api-inference.huggingface.co/models/{TAGS_MODEL}", | |
| headers=headers, | |
| json=payload, | |
| timeout=TIMEOUT | |
| ) | |
| resp.raise_for_status() | |
| out = resp.json() | |
| entities = [item.get("word") for item in out if "word" in item] | |
| tags = list(set(entities)) | |
| return ", ".join(tags) if tags else "No tags found." | |
| except requests.exceptions.HTTPError as e: | |
| if e.response.status_code == 400: | |
| return ERROR_400_MSG | |
| return f"Hugging Face API inference failed: {e}" | |
| def detect_sentiment(text): | |
| headers = {"Authorization": f"Bearer {HF_API_TOKEN}"} | |
| payload = {"inputs": text} | |
| try: | |
| resp = requests.post( | |
| f"https://api-inference.huggingface.co/models/{SENTIMENT_MODEL}", | |
| headers=headers, | |
| json=payload, | |
| timeout=TIMEOUT | |
| ) | |
| resp.raise_for_status() | |
| out = resp.json() | |
| if isinstance(out, list) and len(out) > 0: | |
| result = out[0] if isinstance(out[0], list) else out | |
| best = max(result, key=lambda x: x["score"]) | |
| return LABEL_MAP.get(best["label"], "Unknown") | |
| return "Unknown" | |
| except requests.exceptions.HTTPError as e: | |
| if e.response.status_code == 400: | |
| return ERROR_400_MSG | |
| return f"Hugging Face API inference failed: {e}" |