Abineshkumar77 commited on
Commit
4171ec2
·
1 Parent(s): 30c467a

Add application file

Browse files
Files changed (1) hide show
  1. app.py +34 -19
app.py CHANGED
@@ -1,25 +1,40 @@
1
  from fastapi import FastAPI, Query
2
- from transformers import pipeline
3
-
4
- # Initialize the sentiment analysis pipeline
5
- pipe = pipeline("text-classification", model="cardiffnlp/twitter-roberta-base-sentiment")
6
 
7
  app = FastAPI()
8
 
9
- @app.get('/')
10
- def home():
11
- return {"message": "Welcome to the Sentiment Analysis API"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
- @app.get('/analyze')
14
- def analyze(tweet: str = Query(..., description="The tweet to analyze")):
15
- # Use the pipeline to analyze the sentiment of the tweet
16
- results = pipe(tweet)
17
-
18
- # Format the output to include labels and their corresponding scores
19
- sentiment_scores = {result['label']: result['score'] for result in results}
20
-
21
- return sentiment_scores
22
 
23
- # To run this app on Hugging Face Spaces:
24
- # 1. Save this code as `app.py`.
25
- # 2. Deploy it to a new Hugging Face Space, and the Space will automatically start your FastAPI app.
 
1
  from fastapi import FastAPI, Query
2
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
+ from scipy.special import softmax
4
+ from typing import Dict
 
5
 
6
  app = FastAPI()
7
 
8
+ # Load model and tokenizer
9
+ roberta = "cardiffnlp/twitter-roberta-base-sentiment"
10
+ model = AutoModelForSequenceClassification.from_pretrained(roberta)
11
+ tokenizer = AutoTokenizer.from_pretrained(roberta)
12
+
13
+ labels = ['Negative', 'Neutral', 'Positive']
14
+
15
+ def preprocess_tweet(tweet: str) -> str:
16
+ tweet_words = []
17
+ for word in tweet.split(' '):
18
+ if word.startswith('@') and len(word) > 1:
19
+ word = '@user'
20
+ elif word.startswith('http'):
21
+ word = "http"
22
+ tweet_words.append(word)
23
+ return " ".join(tweet_words)
24
+
25
+ def analyze_sentiment(tweet: str) -> Dict[str, float]:
26
+ tweet_proc = preprocess_tweet(tweet)
27
+ encoded_tweet = tokenizer(tweet_proc, return_tensors='pt')
28
+ output = model(**encoded_tweet)
29
+ scores = output[0][0].detach().numpy()
30
+ scores = softmax(scores)
31
+ return {labels[i]: float(scores[i]) for i in range(len(scores))}
32
 
33
+ @app.get("/analyze-sentiment/")
34
+ async def analyze_sentiment_endpoint(tweet: str = Query(..., description="The tweet to analyze")):
35
+ sentiment_scores = analyze_sentiment(tweet)
36
+ return {"sentiment": sentiment_scores}
 
 
 
 
 
37
 
38
+ # To run the application:
39
+ # 1. Save this code as `main.py`.
40
+ # 2. Run the command `uvicorn main:app --reload`.