Spaces:
Runtime error
Runtime error
Download Sample_Sentiment_Analyzer.py from pavankumarhm/Sentiment_Analyzer: direct link, hf CLI and curl.
- Browser
- Download file 1.21 kB
-
https://huggingface.co/spaces/pavankumarhm/Sentiment_Analyzer/resolve/main/Sample_Sentiment_Analyzer.py
- Command line
-
hf download hf://spaces/pavankumarhm/Sentiment_Analyzer/Sample_Sentiment_Analyzer.py
-
curl -L -o Sample_Sentiment_Analyzer.py https://huggingface.co/spaces/pavankumarhm/Sentiment_Analyzer/resolve/main/Sample_Sentiment_Analyzer.py
1.21 kB
| 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}") | |