File size: 3,272 Bytes
de2e147
a58b274
3803398
6e64769
6ee5d7e
e636dcd
8c60672
de2e147
 
 
8c60672
de2e147
8c60672
 
de2e147
 
e636dcd
de2e147
8c60672
de2e147
6e64769
8c60672
de2e147
 
 
8c60672
 
 
 
 
 
 
 
 
 
 
 
de2e147
 
 
e636dcd
6ee5d7e
8c60672
 
e636dcd
6ee5d7e
 
 
e636dcd
8c60672
e636dcd
 
 
 
de2e147
8c60672
a0616bd
e636dcd
8c60672
e636dcd
 
 
8c60672
 
3803398
8c60672
 
 
 
 
e636dcd
 
a58b274
 
 
ee05df2
a58b274
ee05df2
 
 
a58b274
 
 
 
 
 
 
ee05df2
a58b274
ee05df2
3803398
6599173
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from fastapi import FastAPI, HTTPException
from fastapi.responses import JSONResponse, StreamingResponse
import uvicorn
from src.utils import extract_news, analyze_sentiment, extract_keywords_keybert, generate_hindi_speech
from src.comparison import comparison_analysis
from src.summarization import summarize_overall_sentiment
from typing import Dict

app = FastAPI()


@app.get("/")
def home() -> Dict[str, str]:
    """Home route for API"""
    return {"message": "Welcome to the News Analysis API!"}


@app.get("/news-analysis/")
def get_news_analysis(company: str) -> JSONResponse:
    """Extracts news, analyzes sentiment, and provides a JSON response."""
    articles = extract_news(company)[:10]  # Extract first 10 articles
    
    if not articles:
        raise HTTPException(status_code=404, detail="No articles found for the given company.")

    news_data = {
        "Company": company,
        "Articles": [
            {
                "Title": article.get("title", "No Title"),
                "Summary": article.get("summary", "No Summary"),
                "Sentiment": analyze_sentiment(article.get("summary", "")),  # Sentiment analysis
                "Topics": extract_keywords_keybert(article.get("summary", ""))  # Extract topics
            }
            for article in articles
        ]
    }

    return JSONResponse(content=news_data)


@app.get("/comparative-analyst/")
def get_comparative_analysis(company: str) -> JSONResponse:
    """Performs comparative sentiment analysis for a given company."""
    articles = extract_news(company)[:10]
    
    if len(articles) < 10:
        raise HTTPException(status_code=400, detail="Not enough articles for a full comparison.")

    comparison_data = comparison_analysis(articles)  # Perform comparative analysis

    return JSONResponse(content=comparison_data)


@app.get("/generate-audio/")
def generate_audio(company: str) -> StreamingResponse:
    """Generates a Hindi audio summary using LLM response."""
    articles = extract_news(company)[:10]

    if not articles:
        raise HTTPException(status_code=404, detail="No articles found for the given company.")

    summary_text = summarize_overall_sentiment(articles)  # Generate summary text
    audio_buffer = generate_hindi_speech(summary_text)  # Convert summary to speech

    return StreamingResponse(
        audio_buffer,
        media_type="audio/mpeg",
        headers={"Content-Disposition": "attachment; filename=hindi_summary.mp3"}
    )


@app.get("/search-news/")
def search_news(company: str, keyword: str = None, sentiment: str = None):
    """Search for articles based on keyword or sentiment."""

    articles = extract_news(company)[:10]
    if not articles:
        raise HTTPException(status_code=404, detail="No articles found.")

    filtered_articles = []
    for article in articles:
        if keyword and keyword.lower() not in article["summary"].lower():
            continue
        if sentiment and analyze_sentiment(article["summary"]).lower() != sentiment.lower():
            continue
        filtered_articles.append(article)

    return JSONResponse(content={"Company": company, "Filtered Articles": filtered_articles})

if __name__ == "__main__":
    uvicorn.run(app, host="127.0.0.1", port=8000)