File size: 1,246 Bytes
182fe1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import gradio as gr
import torch
from fastapi import FastAPI
from transformers import AutoTokenizer, AutoModelForSequenceClassification

# You can set POPBERT_MODEL in the Space settings to your own model ID
MODEL_ID = os.getenv("POPBERT_MODEL", "luerhard/PopBERT")

tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
model = AutoModelForSequenceClassification.from_pretrained(MODEL_ID)
model.eval()

def classify(text: str):
    if not text.strip():
        return {"message": "Please paste some text."}
    inputs = tokenizer(text, return_tensors="pt", truncation=True)
    with torch.no_grad():
        logits = model(**inputs).logits
        probs = torch.sigmoid(logits)[0].tolist()
    labels = ["Anti‑Elitism", "People‑Centrism", "Left‑Ideology", "Right‑Ideology"]
    return {lab: f"{p*100:.1f}%" for lab, p in zip(labels, probs)}

app = FastAPI()

@app.post("/predict")
async def predict_endpoint(text: str):
    return classify(text)

demo = gr.Interface(
    fn=classify,
    inputs=gr.Textbox(lines=10, placeholder="Paste speech here..."),
    outputs=gr.Label(),
    title="Populism Analysis Tool",
    description="Detects populist rhetoric in English speeches."
)

app = gr.mount_gradio_app(app, demo, path="/")