Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,87 +1,49 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
# from transformers import pipeline
|
| 4 |
-
|
| 5 |
-
# model_name = "peace4ever/roberta-large-finetuned-mongolian_v4"
|
| 6 |
-
# pipeline = pipeline(task="sentiment-analysis", model=model_name) # Load pre-trained pipeline
|
| 7 |
-
|
| 8 |
-
# st.title("Эерэг? Сөрөг эсвэл аль нь ч биш?")
|
| 9 |
-
# text = st.text_area("Өгүүлбэр оруулна уу?")
|
| 10 |
-
|
| 11 |
-
# if text is not None:
|
| 12 |
-
# predictions = pipeline(text)
|
| 13 |
-
# label = predictions[0]["label"]
|
| 14 |
-
# probability = predictions[0]["score"]
|
| 15 |
-
# col1, col2 = st.columns(2)
|
| 16 |
-
# col1.header("Sentiment")
|
| 17 |
-
# col2.header("Probability")
|
| 18 |
-
|
| 19 |
-
# if label == "entailment":
|
| 20 |
-
# sentiment = "Negative"
|
| 21 |
-
# elif label == "contradiction":
|
| 22 |
-
# sentiment = "Neutral"
|
| 23 |
-
# elif label == "neutral":
|
| 24 |
-
# sentiment = "Positive"
|
| 25 |
-
|
| 26 |
-
# col1.write(sentiment)
|
| 27 |
-
# col2.write(f"{probability:.2f}")
|
| 28 |
-
|
| 29 |
from transformers import pipeline
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
# Load pre-trained sentiment analysis pipeline
|
| 33 |
model_name = "peace4ever/roberta-large-finetuned-mongolian_v4"
|
| 34 |
-
|
| 35 |
|
| 36 |
def analyze_sentiment(text):
|
| 37 |
-
"""
|
| 38 |
-
This function takes user input, performs sentiment analysis using your fine-tuned model,
|
| 39 |
-
maps the predicted labels to desired sentiment categories, and returns the sentiment.
|
| 40 |
-
"""
|
| 41 |
-
predictions = pipeline(text)
|
| 42 |
-
label = predictions[0]["label"]
|
| 43 |
-
probability = predictions[0]["score"]
|
| 44 |
-
|
| 45 |
-
sentiment_map = {
|
| 46 |
-
"entailment": "Negative", # Map based on your fine-tuned model's labels
|
| 47 |
-
"contradiction": "Neutral",
|
| 48 |
-
"neutral": "Positive",
|
| 49 |
-
# Add more mappings if needed
|
| 50 |
-
}
|
| 51 |
-
|
| 52 |
-
sentiment = sentiment_map.get(label.upper(), "Unknown")
|
| 53 |
-
return sentiment
|
| 54 |
-
|
| 55 |
-
def main():
|
| 56 |
-
"""
|
| 57 |
-
This function creates the main window and handles user interaction.
|
| 58 |
-
"""
|
| 59 |
-
window = Tk()
|
| 60 |
-
window.title("Эерэг? Сөрөг эсвэл аль нь ч биш?")
|
| 61 |
-
|
| 62 |
-
# Text box for user input
|
| 63 |
-
text_box = Text(window)
|
| 64 |
-
text_box.pack(padx=10, pady=10)
|
| 65 |
-
|
| 66 |
-
# Button to trigger sentiment analysis
|
| 67 |
-
analyze_button = Button(window, text="Анализ хийх", command=lambda: update_sentiment(text_box.get("1.0", END)))
|
| 68 |
-
analyze_button.pack(pady=10)
|
| 69 |
-
|
| 70 |
-
# Label to display sentiment result
|
| 71 |
-
sentiment_label = Label(window, text="")
|
| 72 |
-
sentiment_label.pack()
|
| 73 |
-
|
| 74 |
-
def update_sentiment(text):
|
| 75 |
"""
|
| 76 |
-
This function performs sentiment analysis
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
"""
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
-
|
|
|
|
|
|
|
| 82 |
|
| 83 |
if __name__ == "__main__":
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from transformers import pipeline
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# Initialize Flask app
|
| 6 |
+
app = Flask(__name__)
|
| 7 |
|
| 8 |
+
# Load pre-trained sentiment analysis pipeline
|
| 9 |
model_name = "peace4ever/roberta-large-finetuned-mongolian_v4"
|
| 10 |
+
nlp_pipeline = pipeline(task="sentiment-analysis", model=model_name)
|
| 11 |
|
| 12 |
def analyze_sentiment(text):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
"""
|
| 14 |
+
This function takes user input, performs sentiment analysis using your fine-tuned model,
|
| 15 |
+
maps the predicted labels to desired sentiment categories, and returns the sentiment.
|
| 16 |
+
"""
|
| 17 |
+
predictions = nlp_pipeline(text)
|
| 18 |
+
label = predictions[0]["label"]
|
| 19 |
+
probability = predictions[0]["score"]
|
| 20 |
+
|
| 21 |
+
sentiment_map = {
|
| 22 |
+
"entailment": "Negative", # Map based on your fine-tuned model's labels
|
| 23 |
+
"contradiction": "Neutral",
|
| 24 |
+
"neutral": "Positive",
|
| 25 |
+
# Add more mappings if needed
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
sentiment = sentiment_map.get(label.lower(), "Unknown")
|
| 29 |
+
return {"sentiment": sentiment, "label": label, "probability": probability}
|
| 30 |
+
|
| 31 |
+
@app.route('/analyze', methods=['POST'])
|
| 32 |
+
def analyze():
|
| 33 |
"""
|
| 34 |
+
This endpoint receives text data and returns the sentiment analysis result.
|
| 35 |
+
"""
|
| 36 |
+
data = request.json
|
| 37 |
+
if 'text' not in data:
|
| 38 |
+
return jsonify({"error": "No text provided"}), 400
|
| 39 |
+
text = data['text']
|
| 40 |
+
result = analyze_sentiment(text)
|
| 41 |
+
return jsonify(result)
|
| 42 |
|
| 43 |
+
@app.route('/')
|
| 44 |
+
def home():
|
| 45 |
+
return "Welcome to the Sentiment Analysis API!"
|
| 46 |
|
| 47 |
if __name__ == "__main__":
|
| 48 |
+
port = int(os.environ.get("PORT", 7860))
|
| 49 |
+
app.run(host="0.0.0.0", port=port)
|
|
|
|
|
|