Sentiment_Analyzer / Sample_Sentiment_Analyzer.py
pavankumarhm's picture
Upload folder using huggingface_hub
2f879b8
import os
import requests
from textblob import TextBlob
import matplotlib.pyplot as plt
import os
# Replace with your API endpoint and parameters
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
headers = {
"X-RapidAPI-Key": os.getenv("PARAM1"),
"X-RapidAPI-Host": os.getenv("PARAM2")
}
# Make the API request
url = "https://twinword-sentiment-analysis.p.rapidapi.com/analyze/"
querystring = {"text":input("Enter the text to analyze:")}
# Make the API request
response = requests.get(url, headers=headers, params=querystring)
# Check if the request was successful
if response.status_code == 200:
# Extract the text data from the API response
text_data = response.text
# Analyze sentiment using TextBlob
blob = TextBlob(text_data)
sentiment = blob.sentiment.polarity
# Determine sentiment category
if sentiment > 0:
sentiment_category = "positive"
elif sentiment < 0:
sentiment_category = "negative"
else:
sentiment_category = "neutral"
print(f"Sentiment: {sentiment_category} ({sentiment})")
else:
print(f"Error: API request failed with status code {response.status_code}")