doublelotus commited on
Commit
749b55f
·
1 Parent(s): ca77260
Files changed (2) hide show
  1. main.py +8 -30
  2. requirements.txt +13 -13
main.py CHANGED
@@ -1,38 +1,16 @@
1
- import os
2
- from flask import Flask, jsonify, request
3
- from flask_cors import CORS
4
  from transformers import pipeline
5
 
6
- # Set the TRANSFORMERS_CACHE environment variable to a writable directory
7
- os.environ['TRANSFORMERS_CACHE'] = '/tmp/huggingface/cache'
8
-
9
  app = Flask(__name__)
10
- CORS(app)
11
-
12
- # Initialize the sentiment analysis pipeline
13
- classifier = pipeline("sentiment-analysis")
14
 
15
- @app.route('/')
16
- def hello():
17
- return 'hello world'
18
 
19
- @app.route('/analyze-sentiment', methods=['POST'])
20
- def analyze_sentiment():
21
- try:
22
- # Get the text from the request
23
- data = request.get_json()
24
- if 'text' not in data:
25
- return jsonify({"error": "No text provided"}), 400
26
-
27
- text = data['text']
28
- # Run sentiment analysis
29
- result = classifier(text)
30
- return jsonify(result)
31
- except Exception as e:
32
- # Log the error message if needed
33
- print(f"Error processing the text: {e}")
34
- # Return a JSON response with the error message and a 400 Bad Request status
35
- return jsonify({"error": "Error processing the text", "details": str(e)}), 400
36
 
37
  if __name__ == '__main__':
38
  app.run(debug=True)
 
1
+ from flask import Flask, jsonify
 
 
2
  from transformers import pipeline
3
 
 
 
 
4
  app = Flask(__name__)
 
 
 
 
5
 
6
+ # Load the sentiment-analysis pipeline from Hugging Face
7
+ sentiment_analyzer = pipeline('sentiment-analysis')
 
8
 
9
+ @app.route('/analyze', methods=['GET'])
10
+ def analyze():
11
+ sentence = "hey there"
12
+ result = sentiment_analyzer(sentence)
13
+ return jsonify(result)
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
  if __name__ == '__main__':
16
  app.run(debug=True)
requirements.txt CHANGED
@@ -1,16 +1,16 @@
1
  flask
2
  gunicorn
3
  flask-cors
4
- transformers
5
- # numpy
6
- # opencv-python
7
- # Pillow
8
- # requests
9
- # git+https://github.com/facebookresearch/segment-anything.git
10
- # --extra-index-url https://download.pytorch.org/whl/cu113
11
- # torch
12
- # torchvision
13
- # matplotlib # Required for image processing and mask visualization
14
- # onnxruntime
15
- # onnx
16
- # pycocotools
 
1
  flask
2
  gunicorn
3
  flask-cors
4
+ numpy
5
+ opencv-python
6
+ Pillow
7
+ requests
8
+ git+https://github.com/facebookresearch/segment-anything.git
9
+ --extra-index-url https://download.pytorch.org/whl/cu113
10
+ torch
11
+ torchvision
12
+ matplotlib # Required for image processing and mask visualization
13
+ onnxruntime
14
+ onnx
15
+ pycocotools
16
+ transformers