-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Anantha Raju C
committed
Feb 25, 2024
1 parent
0e12c7f
commit 210690c
Showing
2 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import torch | ||
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline | ||
from datasets import load_dataset | ||
import speech_recognition as sr | ||
import numpy as np | ||
import sys | ||
import json | ||
|
||
# Access command-line arguments | ||
arguments = sys.argv[1:] | ||
|
||
# Print the arguments passed from Java | ||
for arg in arguments: | ||
print("Argument:", arg) | ||
|
||
# device = "cuda:0" if torch.cuda.is_available() else "cpu" | ||
device = "cpu" | ||
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32 | ||
|
||
model_id = "openai/whisper-large-v3" | ||
|
||
model = AutoModelForSpeechSeq2Seq.from_pretrained( | ||
model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True | ||
) | ||
model.to(device) | ||
|
||
processor = AutoProcessor.from_pretrained(model_id) | ||
|
||
pipe = pipeline( | ||
"automatic-speech-recognition", | ||
model=model, | ||
tokenizer=processor.tokenizer, | ||
feature_extractor=processor.feature_extractor, | ||
max_new_tokens=128, | ||
chunk_length_s=30, | ||
batch_size=16, | ||
return_timestamps=True, | ||
torch_dtype=torch_dtype, | ||
device=device, | ||
) | ||
|
||
|
||
resultTranscription = pipe("/home/anantha/PCF/AIML/SpeechToText/HIN_M_AbhishekS.mp3", generate_kwargs={"language": None}) | ||
print("") | ||
print(resultTranscription["text"]) | ||
|
||
|
||
resultTranslation = pipe("/home/anantha/PCF/AIML/SpeechToText/HIN_M_AbhishekS.mp3", generate_kwargs={"task": "translate"}) | ||
print("") | ||
print(resultTranslation["text"]) | ||
|
||
|
||
classifier = pipeline("sentiment-analysis",model="distilbert/distilbert-base-uncased-finetuned-sst-2-english",revision ="af0f99b") | ||
resultClassifier = classifier(resultTranslation["text"]) | ||
print("") | ||
print(resultClassifier) | ||
|
||
# Create a Python dictionary | ||
data = { | ||
"resultTranscription": resultTranscription, | ||
"resultTranslation": resultTranslation, | ||
"resultClassifier": resultClassifier | ||
} | ||
|
||
# Convert the dictionary to a JSON string | ||
json_string = json.dumps(data) | ||
|
||
# Print the JSON string | ||
print(json_string) |