Spaces:
Sleeping
Sleeping
Update utils.py
Browse files
utils.py
CHANGED
|
@@ -1,43 +1,44 @@
|
|
| 1 |
-
from transformers import pipeline
|
| 2 |
-
from gtts import gTTS
|
| 3 |
-
from
|
| 4 |
-
|
| 5 |
-
# Loading models
|
| 6 |
-
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 7 |
-
sentiment_analyzer = pipeline("sentiment-analysis")
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
result
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
cleaned_text =
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
tts
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
+
from gtts import gTTS
|
| 3 |
+
from deep_translator import GoogleTranslator
|
| 4 |
+
|
| 5 |
+
# Loading models
|
| 6 |
+
summarizer = pipeline("summarization", model="facebook/bart-large-cnn") # Load summarizer
|
| 7 |
+
sentiment_analyzer = pipeline("sentiment-analysis") # Load sentiment analyzer
|
| 8 |
+
|
| 9 |
+
def analyze_sentiment(text):
|
| 10 |
+
result = sentiment_analyzer(text[:500])[0]
|
| 11 |
+
return result['label']
|
| 12 |
+
|
| 13 |
+
def summarize_text(text):
|
| 14 |
+
cleaned_text = text.strip().replace("\n", " ")
|
| 15 |
+
cleaned_text = cleaned_text[:3000] # Limit to avoid token overflow
|
| 16 |
+
|
| 17 |
+
result = summarizer(
|
| 18 |
+
cleaned_text,
|
| 19 |
+
max_length=130,
|
| 20 |
+
min_length=30,
|
| 21 |
+
do_sample=False
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
summary_text = result[0]['summary_text']
|
| 25 |
+
return summary_text
|
| 26 |
+
|
| 27 |
+
def translate_to_hindi(text):
|
| 28 |
+
try:
|
| 29 |
+
translated_text = GoogleTranslator(source='auto', target='hi').translate(text)
|
| 30 |
+
return translated_text
|
| 31 |
+
except Exception as e:
|
| 32 |
+
print(f"Translation Error: {e}")
|
| 33 |
+
return text # Fallback to original text if translation fails
|
| 34 |
+
|
| 35 |
+
def generate_hindi_tts(text, filename="output.mp3"):
|
| 36 |
+
try:
|
| 37 |
+
hindi_text = translate_to_hindi(text)
|
| 38 |
+
tts = gTTS(text=hindi_text, lang='hi')
|
| 39 |
+
tts.save(filename)
|
| 40 |
+
print(f"Hindi audio saved to {filename}")
|
| 41 |
+
return filename
|
| 42 |
+
except Exception as e:
|
| 43 |
+
print(f"Error in generating the TTS: {e}")
|
| 44 |
+
return None
|