|
|
import gradio as gr |
|
|
import spaces |
|
|
from inference import SentimentClassifier |
|
|
from huggingface_hub import snapshot_download |
|
|
from huggingface_hub import hf_hub_download |
|
|
import os |
|
|
|
|
|
MODEL_REPO = "vuminhtue/qwen3_sentiment_tinystories" |
|
|
FILENAME = "Qwen3_200k_model_params.pt" |
|
|
|
|
|
LOCAL_DIR = os.path.join(os.getcwd(), "models", "qwen3_sentiment_tinystories") |
|
|
|
|
|
weights_path = hf_hub_download( |
|
|
repo_id=MODEL_REPO, |
|
|
filename=FILENAME, |
|
|
local_dir="qwen3_sentiment_tinystories", |
|
|
local_dir_use_symlinks=None |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
classifier = SentimentClassifier(model_dir="qwen3_sentiment_tinystories", |
|
|
weights_path=weights_path) |
|
|
|
|
|
def predict_sentiment(text): |
|
|
"""Predict sentiment and return results""" |
|
|
result = classifier.predict(text) |
|
|
|
|
|
return { |
|
|
"Sentiment": result["sentiment"].upper(), |
|
|
"Confidence": f"{result['confidence']:.2%}", |
|
|
"Negative Probability": f"{result['probabilities']['negative']:.2%}", |
|
|
"Positive Probability": f"{result['probabilities']['positive']:.2%}" |
|
|
} |
|
|
|
|
|
@spaces.GPU(duration=120) |
|
|
def generate(prompt): |
|
|
return pipe(prompt).images |
|
|
|
|
|
|
|
|
demo = gr.Interface( |
|
|
fn=predict_sentiment, |
|
|
inputs=gr.Textbox( |
|
|
label="Enter text to analyze", |
|
|
placeholder="Type your text here...", |
|
|
lines=3 |
|
|
), |
|
|
outputs=gr.JSON(label="Prediction Results"), |
|
|
title="🎭 Sentiment Analyzer", |
|
|
description="Classify text as positive or negative using Qwen3:0.6B embeddings + Logistic Regression", |
|
|
examples=[ |
|
|
["This movie was absolutely wonderful!"], |
|
|
["Terrible experience, complete waste of time."], |
|
|
["It's okay, nothing special."] |
|
|
] |
|
|
) |
|
|
|
|
|
demo.launch() |