import os import time import gradio as gr import torch from transformers import AutoTokenizer from model import get_sentiment, make_model device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # Load the tokenizer and model tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased") model = make_model( device=device, tokenizer=tokenizer, n_layers=4, d_model=768, num_labels=5, n_heads=8, dropout=0.1, max_length=32, ) model.to(device) model_path = "sentiment_analysis_model.pt" if os.path.exists(model_path): print(f"Loading model from {model_path}...") model.load_state_dict(torch.load(model_path, map_location=device)) else: print("No pretrained model found. Using randomly initialized weights.") def predict_sentiment(text): sentiment = get_sentiment(text, model, tokenizer, device, max_length=32) return sentiment css_str = """ body { background-color: #121212; color: #e0e0e0; } .container { max-width: 750px; margin: 10px auto; } h1 { font-size: 36px; font-weight: bold; text-align: center; color: #ffffff; } .description { font-size: 18px; text-align: center; color: #b0b0b0; } """ with gr.Blocks(css=css_str) as demo: gr.HTML("