Update utils.py
Browse files
utils.py
CHANGED
|
@@ -7,7 +7,7 @@ import os
|
|
| 7 |
|
| 8 |
# News Extraction
|
| 9 |
def fetch_news(company_name):
|
| 10 |
-
api_key = os.getenv("NEWSAPI_KEY", "14c0c7c5415b48e093fad78a1fd9581b")
|
| 11 |
url = f"https://newsapi.org/v2/everything?q={company_name}&apiKey={api_key}"
|
| 12 |
response = requests.get(url)
|
| 13 |
|
|
@@ -32,7 +32,7 @@ def fetch_news(company_name):
|
|
| 32 |
# Sentiment Analysis
|
| 33 |
sentiment_analyzer = pipeline("sentiment-analysis")
|
| 34 |
def analyze_sentiment(text):
|
| 35 |
-
result = sentiment_analyzer(text[:512])[0]
|
| 36 |
label = result['label']
|
| 37 |
if label == "POSITIVE":
|
| 38 |
return "Positive"
|
|
@@ -55,9 +55,20 @@ model = VitsModel.from_pretrained("facebook/mms-tts-hin")
|
|
| 55 |
tokenizer = AutoTokenizer.from_pretrained("facebook/mms-tts-hin")
|
| 56 |
|
| 57 |
def generate_hindi_tts(text):
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# News Extraction
|
| 9 |
def fetch_news(company_name):
|
| 10 |
+
api_key = os.getenv("NEWSAPI_KEY", "14c0c7c5415b48e093fad78a1fd9581b")
|
| 11 |
url = f"https://newsapi.org/v2/everything?q={company_name}&apiKey={api_key}"
|
| 12 |
response = requests.get(url)
|
| 13 |
|
|
|
|
| 32 |
# Sentiment Analysis
|
| 33 |
sentiment_analyzer = pipeline("sentiment-analysis")
|
| 34 |
def analyze_sentiment(text):
|
| 35 |
+
result = sentiment_analyzer(text[:512])[0]
|
| 36 |
label = result['label']
|
| 37 |
if label == "POSITIVE":
|
| 38 |
return "Positive"
|
|
|
|
| 55 |
tokenizer = AutoTokenizer.from_pretrained("facebook/mms-tts-hin")
|
| 56 |
|
| 57 |
def generate_hindi_tts(text):
|
| 58 |
+
try:
|
| 59 |
+
# Tokenize input text (limit length to avoid model issues)
|
| 60 |
+
inputs = tokenizer(text[:200], return_tensors="pt") # Limit to 200 chars for stability
|
| 61 |
+
with torch.no_grad():
|
| 62 |
+
outputs = model(**inputs).waveform
|
| 63 |
+
|
| 64 |
+
# Normalize audio data
|
| 65 |
+
audio_data = outputs.squeeze().cpu().numpy()
|
| 66 |
+
if audio_data.ndim > 1:
|
| 67 |
+
audio_data = audio_data[0] # Ensure 1D array
|
| 68 |
+
|
| 69 |
+
# Save as WAV file
|
| 70 |
+
audio_file = "output.wav"
|
| 71 |
+
wav.write(audio_file, rate=model.config.sampling_rate, data=audio_data)
|
| 72 |
+
return audio_file
|
| 73 |
+
except Exception as e:
|
| 74 |
+
raise Exception(f"TTS generation failed: {str(e)}")
|