Spaces:
Runtime error
Runtime error
Commit
·
5a42448
1
Parent(s):
e9cd1d1
analyze sentiment
Browse files
main.py
CHANGED
@@ -1,12 +1,34 @@
|
|
1 |
-
from flask import Flask
|
2 |
from flask_cors import CORS
|
|
|
3 |
|
4 |
app = Flask(__name__)
|
5 |
CORS(app)
|
6 |
|
|
|
|
|
|
|
7 |
@app.route('/')
|
8 |
def hello():
|
9 |
return 'hello world'
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
if __name__ == '__main__':
|
12 |
app.run(debug=True)
|
|
|
1 |
+
from flask import Flask, jsonify, request
|
2 |
from flask_cors import CORS
|
3 |
+
from transformers import pipeline
|
4 |
|
5 |
app = Flask(__name__)
|
6 |
CORS(app)
|
7 |
|
8 |
+
# Initialize the sentiment analysis pipeline
|
9 |
+
classifier = pipeline("sentiment-analysis")
|
10 |
+
|
11 |
@app.route('/')
|
12 |
def hello():
|
13 |
return 'hello world'
|
14 |
|
15 |
+
@app.route('/analyze-sentiment', methods=['POST'])
|
16 |
+
def analyze_sentiment():
|
17 |
+
try:
|
18 |
+
# Get the text from the request
|
19 |
+
data = request.get_json()
|
20 |
+
if 'text' not in data:
|
21 |
+
return jsonify({"error": "No text provided"}), 400
|
22 |
+
|
23 |
+
text = data['text']
|
24 |
+
# Run sentiment analysis
|
25 |
+
result = classifier(text)
|
26 |
+
return jsonify(result)
|
27 |
+
except Exception as e:
|
28 |
+
# Log the error message if needed
|
29 |
+
print(f"Error processing the text: {e}")
|
30 |
+
# Return a JSON response with the error message and a 400 Bad Request status
|
31 |
+
return jsonify({"error": "Error processing the text", "details": str(e)}), 400
|
32 |
+
|
33 |
if __name__ == '__main__':
|
34 |
app.run(debug=True)
|