2.0 / app.py
Oliver2406's picture
Upload 2 files
182fe1e verified
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="/")