code / app.py
thanhdov's picture
Support ZeroGPU with @spaces.GPU decorator
2c6ab64
Raw
History Blame Contribute Delete
945 Bytes
import gradio as gr
import spaces
from transformers import pipeline
# Load pipeline mô hình (ví dụ: mô hình phân loại cảm xúc văn bản)
classifier = pipeline(
"sentiment-analysis",
model="distilbert-base-uncased-finetuned-sst-2-english",
)
@spaces.GPU
def analyze_text(text):
if not text.strip():
return "Vui lòng nhập văn bản cần phân tích."
result = classifier(text)[0]
label = result["label"]
score = round(result["score"], 4)
return f"Kết quả: {label} (Độ tin cậy: {score})"
# Tạo giao diện người dùng
demo = gr.Interface(
fn=analyze_text,
inputs=gr.Textbox(
lines=3, placeholder="Nhập câu tiếng Anh cần kiểm tra..."
),
outputs="text",
title="Sentiment Analysis Demo",
description="Ứng dụng phân tích cảm xúc sử dụng mô hình Hugging Face Transformers.",
)
if __name__ == "__main__":
demo.launch()