File size: 2,807 Bytes
5cdaf63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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}"