Shubham0786 commited on
Commit
4d1454e
·
verified ·
1 Parent(s): 7f3a3ea

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +19 -8
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") # Use environment variable or replace with your key
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] # Limit to 512 tokens
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
- inputs = tokenizer(text, return_tensors="pt")
59
- with torch.no_grad():
60
- outputs = model(**inputs).waveform
61
- audio_file = "output.wav"
62
- wav.write(audio_file, rate=model.config.sampling_rate, data=outputs.squeeze().cpu().numpy())
63
- return audio_file
 
 
 
 
 
 
 
 
 
 
 
 
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)}")