File size: 2,146 Bytes
b98ed7e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Gradio interface for Hugging Face inference."""

import gradio as gr
from huggingface_hub import InferenceClient

try:
    import spaces
    SPACES_AVAILABLE = True
except ImportError:
    SPACES_AVAILABLE = False

from app.config import get_settings

settings = get_settings()
client = InferenceClient(model=settings.model_name, token=settings.api_token)


def _predict(text: str) -> str:
    """Run inference on the input text."""
    if not text.strip():
        return "Please enter some text."

    task = settings.task

    try:
        if task in ("text-classification", "sentiment-analysis"):
            results = client.text_classification(text)
            output = "\n".join(
                [f"{r['label']}: {r['score']:.2%}" for r in results]
            )
        elif task == "text-generation":
            output = client.text_generation(text, max_new_tokens=100)
        elif task == "summarization":
            output = client.summarization(text)
        elif task == "translation":
            output = client.translation(text)
        elif task == "fill-mask":
            results = client.fill_mask(text)
            output = "\n".join(
                [f"{r['token_str']}: {r['score']:.2%}" for r in results]
            )
        else:
            output = str(client.post(json={"inputs": text}))

        return output
    except Exception as e:
        return f"Error: {e}"


# Apply @spaces.GPU decorator only on HF Spaces
if SPACES_AVAILABLE:
    predict = spaces.GPU(duration=60)(_predict)
else:
    predict = _predict


demo = gr.Interface(
    fn=predict,
    inputs=gr.Textbox(
        label="Input Text",
        placeholder="Enter text here...",
        lines=4,
    ),
    outputs=gr.Textbox(label="Result", lines=6),
    title="Hugging Face Inference",
    description=f"Model: **{settings.model_name}** | Task: **{settings.task}**",
    examples=[
        ["I love this product! It's amazing."],
        ["This is the worst experience ever."],
        ["The weather is nice today."],
    ],
    flagging_mode="never",
)

if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=7860)