|
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 |
|
import torch |
|
from collections import defaultdict |
|
|
|
|
|
|
|
flask_app = Flask(__name__) |
|
|
|
|
|
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() |
|
|
|
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 = """ |
|
<!DOCTYPE html> |
|
<html lang="en"> |
|
<head> |
|
<meta charset="UTF-8"> |
|
<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
|
<title>Comment Sentiment Analysis</title> |
|
<style> |
|
body { font-family: Arial, sans-serif; background-color: #f5f5f5; margin: 0; padding: 0; color: #333; } |
|
header { background-color: #FF5722; color: white; padding: 20px; text-align: center; } |
|
main { padding: 20px; max-width: 900px; margin: 0 auto; background-color: white; border-radius: 8px; box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.1); } |
|
form { margin: 20px auto; max-width: 600px; display: flex; flex-direction: column; gap: 15px; background-color: #f9f9f9; padding: 20px; border-radius: 8px; box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.1); } |
|
textarea, button { padding: 12px; font-size: 1.1em; border: 1px solid #ccc; border-radius: 6px; } |
|
textarea { background-color: #fff; resize: vertical; min-height: 100px; } |
|
button { background-color: #FF5722; color: white; border: none; cursor: pointer; transition: background-color 0.3s ease; } |
|
button:hover { background-color: #E64A19; } |
|
.result-message { text-align: center; margin-top: 20px; font-size: 18px; font-weight: bold; } |
|
</style> |
|
<script> |
|
document.addEventListener("DOMContentLoaded", function() { |
|
document.getElementById("commentForm").onsubmit = async function(e) { |
|
e.preventDefault(); |
|
const comment = document.getElementById("comment").value; |
|
const resultDiv = document.getElementById("result"); |
|
resultDiv.innerHTML = ""; |
|
try { |
|
const response = await fetch('/analyze', { |
|
method: 'POST', |
|
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, |
|
body: new URLSearchParams({ 'comment': comment }) |
|
}); |
|
const data = await response.json(); |
|
if (data.error) { |
|
resultDiv.innerHTML = `<p class="result-message" style="color:red;">${data.error}</p>`; |
|
} else { |
|
resultDiv.innerHTML = `<p class="result-message" style="color:green;">${data.message}</p>`; |
|
} |
|
} catch (error) { |
|
resultDiv.innerHTML = `<p class="result-message" style="color:red;">Error sending request: ${error.message}</p>`; |
|
console.error('Fetch error:', error); |
|
} |
|
}; |
|
}); |
|
</script> |
|
</head> |
|
<body> |
|
<header> |
|
<h1>Comment Sentiment Analysis</h1> |
|
</header> |
|
<main> |
|
<form id="commentForm"> |
|
<label for="comment">Enter your comment:</label> |
|
<textarea id="comment" name="comment" placeholder="Type your comment here..." required></textarea> |
|
<button type="submit">Analyze Sentiment</button> |
|
</form> |
|
<div id="result"></div> |
|
</main> |
|
</body> |
|
</html> |
|
""" |
|
|
|
@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}.'}) |
|
|
|
|
|
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))) |
|
|