Nikhillmahesh701 commited on
Commit
b047091
·
verified ·
1 Parent(s): e751473

Update tts.py

Browse files
Files changed (1) hide show
  1. tts.py +11 -90
tts.py CHANGED
@@ -1,93 +1,14 @@
1
- import yake
2
- from textblob import TextBlob
3
- from sentence_transformers import SentenceTransformer, util
4
- from services.news_fetcher import fetch_news
5
- from tts import generate_tts
6
 
7
- # Initialize SBERT & YAKE
8
- sbert_model = SentenceTransformer("all-MiniLM-L6-v2")
9
- yake_extractor = yake.KeywordExtractor(lan="en", n=2, dedupLim=0.9, top=5)
 
10
 
11
- def analyze_sentiment(text):
12
- """Perform sentiment analysis using TextBlob."""
13
- if not text or not isinstance(text, str) or not text.strip():
14
- return "Neutral"
15
 
16
- blob = TextBlob(text)
17
- polarity = blob.sentiment.polarity
18
- return "Positive" if polarity > 0 else "Negative" if polarity < 0 else "Neutral"
19
-
20
- def extract_topics(text):
21
- """Extract key topics using YAKE."""
22
- if not text or not isinstance(text, str) or not text.strip():
23
- return []
24
-
25
- keywords = yake_extractor.extract_keywords(text)
26
- return [kw[0] for kw in keywords]
27
-
28
- def extract_common_and_unique_topics(articles):
29
- """Identify common topics appearing in multiple articles and extract unique topics."""
30
- topic_sets = {f"Article {i+1}": set(extract_topics(article["Summary"])) for i, article in enumerate(articles)}
31
-
32
- all_topics = [topic for topics in topic_sets.values() for topic in topics]
33
- common_topics = {topic for topic in all_topics if all_topics.count(topic) > 1}
34
-
35
- unique_topics = {
36
- f"Unique Topics in Article {i+1}": list(topics - common_topics) for i, topics in enumerate(topic_sets.values())
37
- }
38
-
39
- return {"Common Topics": list(common_topics), **unique_topics}
40
-
41
- def compare_articles(articles):
42
- """Generate coverage differences by comparing specific article pairs."""
43
- comparisons = []
44
- pairs = [(0,1), (2,3), (4,5), (6,7), (8,9)]
45
-
46
- summaries = [article["Summary"] for article in articles]
47
- embeddings = sbert_model.encode(summaries, convert_to_tensor=True)
48
-
49
- for i, j in pairs:
50
- if i < len(articles) and j < len(articles):
51
- similarity = util.pytorch_cos_sim(embeddings[i], embeddings[j]).item()
52
-
53
- comparison_text = f"Article {i+1} discusses '{articles[i]['Title']}', while Article {j+1} focuses on '{articles[j]['Title']}'."
54
- impact_text = (
55
- f"The first article has a {articles[i]['Sentiment']} sentiment, whereas the second has a {articles[j]['Sentiment']} sentiment. "
56
- f"This contrast may influence public perception differently."
57
- )
58
- comparisons.append({"Comparison": comparison_text, "Impact": impact_text})
59
-
60
- return comparisons
61
-
62
- def process_articles(company):
63
- """Processes articles to extract sentiment, topics, and comparisons."""
64
- articles = fetch_news(company)
65
- structured_data = {"Company": company, "Articles": []}
66
- sentiments = []
67
-
68
- for i, article in enumerate(articles):
69
- summary = article.get("Summary", "No summary available")
70
- sentiment = analyze_sentiment(summary)
71
- topics = extract_topics(summary)
72
- sentiments.append(sentiment)
73
-
74
- structured_data["Articles"].append({
75
- "Title": article["Title"],
76
- "Summary": summary,
77
- "Sentiment": sentiment,
78
- "Topics": topics
79
- })
80
-
81
- structured_data["Comparative Sentiment Score"] = {
82
- "Sentiment Distribution": {s: sentiments.count(s) for s in ["Positive", "Negative", "Neutral"]},
83
- "Coverage Differences": compare_articles(structured_data["Articles"]),
84
- "Topic Overlap": extract_common_and_unique_topics(structured_data["Articles"])
85
- }
86
-
87
- max_sentiment = max(structured_data["Comparative Sentiment Score"]["Sentiment Distribution"],
88
- key=structured_data["Comparative Sentiment Score"]["Sentiment Distribution"].get)
89
- structured_data["Final Sentiment Analysis"] = f"{company} latest news coverage is mostly {max_sentiment}."
90
-
91
- structured_data["Audio"] = generate_tts(structured_data["Final Sentiment Analysis"])
92
-
93
- return structured_data
 
1
+ from gtts import gTTS
2
+ from deep_translator import GoogleTranslator
3
+ import os
 
 
4
 
5
+ def generate_tts(text, lang="hi"):
6
+ """Translate text to Hindi and generate TTS audio."""
7
+ translator = GoogleTranslator(source="auto", target="hi") # Translate to Hindi
8
+ translated_text = translator.translate(text)
9
 
10
+ output_path = "static/output.mp3"
11
+ tts = gTTS(translated_text, lang=lang)
12
+ tts.save(output_path)
 
13
 
14
+ return output_path # Return file path for playback