Spaces:
Sleeping
Sleeping
Upload api.py
Browse files
api.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException
|
2 |
+
from fastapi.responses import FileResponse
|
3 |
+
import time
|
4 |
+
from utils import (
|
5 |
+
get_bing_news_articles,
|
6 |
+
analyze_sentiment,
|
7 |
+
extract_topics,
|
8 |
+
comparative_analysis,
|
9 |
+
convert_text_to_hindi_tts,
|
10 |
+
)
|
11 |
+
|
12 |
+
app = FastAPI(title="News Summarization & TTS API")
|
13 |
+
|
14 |
+
@app.get("/news")
|
15 |
+
def get_news(company: str, num_articles: int = 10):
|
16 |
+
articles = get_bing_news_articles(company, num_articles=num_articles)
|
17 |
+
if not articles:
|
18 |
+
raise HTTPException(status_code=404, detail="No articles found.")
|
19 |
+
for article in articles:
|
20 |
+
combined_text = article["title"]
|
21 |
+
if article["summary"]:
|
22 |
+
combined_text += ". " + article["summary"]
|
23 |
+
sentiment, scores = analyze_sentiment(combined_text)
|
24 |
+
article["sentiment"] = sentiment
|
25 |
+
article["sentiment_scores"] = scores
|
26 |
+
article["topics"] = extract_topics(combined_text)
|
27 |
+
time.sleep(0.5)
|
28 |
+
analysis = comparative_analysis(articles)
|
29 |
+
return {"articles": articles, "analysis": analysis}
|
30 |
+
|
31 |
+
@app.get("/tts")
|
32 |
+
def get_tts(text: str):
|
33 |
+
output_file = "output.mp3"
|
34 |
+
convert_text_to_hindi_tts(text, output_file=output_file)
|
35 |
+
return FileResponse(output_file, media_type="audio/mpeg", filename=output_file)
|