from flask import Flask, request, render_template_string, jsonify, send_from_directory import requests import pandas as pd import re import time from random import randint, choice import os from transformers import XLMRobertaForSequenceClassification, XLMRobertaTokenizer from peft import PeftModel, PeftConfig # Ensure peft library is installed import torch from collections import defaultdict # Define the Flask app flask_app = Flask(__name__) #Load the base XLM-RoBERTa model with the correct number of labels (3 labels for classification) tokenizer = XLMRobertaTokenizer.from_pretrained("letijo03/lora-adapter-32",use_fast=True, trust_remote_code=True) base_model = XLMRobertaForSequenceClassification.from_pretrained("xlm-roberta-base", num_labels=3) config = PeftConfig.from_pretrained("letijo03/lora-adapter-32") model = PeftModel.from_pretrained(base_model, "letijo03/lora-adapter-32") model.eval() # Set the model to evaluation mode def classify_sentiment(text): inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=512) outputs = model(**inputs) prediction = torch.argmax(outputs.logits, dim=-1) return prediction.item() # HTML template for user input html_template = """ Comment Sentiment Analysis

Comment Sentiment Analysis

""" @flask_app.route('/') def index(): return render_template_string(html_template) @flask_app.route('/analyze', methods=['POST']) def analyze(): comment = request.form.get('comment') if not comment or comment.strip() == "": return jsonify({'error': 'Please provide a valid comment.'}) sentiment = classify_sentiment(comment) sentiment_label = "Positive" if sentiment == 2 else "Neutral" if sentiment == 1 else "Negative" return jsonify({'message': f'Sentiment analysis complete. The sentiment is: {sentiment_label}.'}) # Wrap the Flask app as an ASGI app so that the module-level variable 'app' is ASGI-compatible from asgiref.wsgi import WsgiToAsgi app = WsgiToAsgi(flask_app) if __name__ == '__main__': import uvicorn uvicorn.run(app, host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))