Spaces:
Runtime error
Runtime error
| from flask import Flask, request, jsonify | |
| from transformers import pipeline | |
| import os | |
| # Initialize Flask app | |
| app = Flask(__name__) | |
| # Load pre-trained sentiment analysis pipeline | |
| model_name = "peace4ever/roberta-large-finetuned-mongolian_v4" | |
| nlp_pipeline = pipeline(task="sentiment-analysis", model=model_name) | |
| def analyze_sentiment(text): | |
| """ | |
| This function takes user input, performs sentiment analysis using your fine-tuned model, | |
| maps the predicted labels to desired sentiment categories, and returns the sentiment. | |
| """ | |
| predictions = nlp_pipeline(text) | |
| label = predictions[0]["label"] | |
| probability = predictions[0]["score"] | |
| sentiment_map = { | |
| "entailment": "Negative", # Map based on your fine-tuned model's labels | |
| "contradiction": "Neutral", | |
| "neutral": "Positive", | |
| # Add more mappings if needed | |
| } | |
| sentiment = sentiment_map.get(label.lower(), "Unknown") | |
| return {"sentiment": sentiment, "label": label, "probability": probability} | |
| def analyze(): | |
| """ | |
| This endpoint receives text data and returns the sentiment analysis result. | |
| """ | |
| data = request.json | |
| if 'text' not in data: | |
| return jsonify({"error": "No text provided"}), 400 | |
| text = data['text'] | |
| result = analyze_sentiment(text) | |
| return jsonify(result) | |
| def home(): | |
| return "Welcome to the Sentiment Analysis API!" | |
| if __name__ == "__main__": | |
| port = int(os.environ.get("PORT", 7860)) | |
| app.run(host="0.0.0.0", port=port) | |