Spaces:
Sleeping
Sleeping
Upload api.py
Browse files
api.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from utils import fetch_news, generate_tts_hindi
|
| 4 |
+
import shutil
|
| 5 |
+
|
| 6 |
+
app = FastAPI()
|
| 7 |
+
|
| 8 |
+
class CompanyRequest(BaseModel):
|
| 9 |
+
company_name: str
|
| 10 |
+
|
| 11 |
+
@app.post("/get_news_and_sentiment")
|
| 12 |
+
async def get_news_and_sentiment(request: CompanyRequest):
|
| 13 |
+
company_name = request.company_name
|
| 14 |
+
articles = fetch_news(company_name)
|
| 15 |
+
|
| 16 |
+
# Comparative Sentiment Analysis
|
| 17 |
+
sentiment_distribution = {'Positive': 0, 'Negative': 0, 'Neutral': 0}
|
| 18 |
+
for article in articles:
|
| 19 |
+
sentiment_distribution[article['Sentiment']] += 1
|
| 20 |
+
|
| 21 |
+
# Prepare comparative analysis
|
| 22 |
+
coverage_diff = []
|
| 23 |
+
for i in range(1, len(articles)):
|
| 24 |
+
coverage_diff.append({
|
| 25 |
+
"Comparison": f"Article {i} vs Article {i+1}",
|
| 26 |
+
"Impact": f"Impact of coverage for {articles[i]['Sentiment']} vs {articles[i-1]['Sentiment']}"
|
| 27 |
+
})
|
| 28 |
+
|
| 29 |
+
# Generate TTS in Hindi
|
| 30 |
+
tts_file = generate_tts_hindi(f"Sentiment report for {company_name}.", "output.mp3")
|
| 31 |
+
|
| 32 |
+
return {
|
| 33 |
+
'Company': company_name,
|
| 34 |
+
'Articles': articles,
|
| 35 |
+
'Comparative Sentiment Score': sentiment_distribution,
|
| 36 |
+
'Coverage Differences': coverage_diff,
|
| 37 |
+
'Final Sentiment': f"Final sentiment about {company_name}: Positive" if sentiment_distribution['Positive'] > sentiment_distribution['Negative'] else "Negative",
|
| 38 |
+
'Audio': tts_file
|
| 39 |
+
}
|