venkat charan commited on
Commit
b3c1a68
1 Parent(s): 1cc056f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -0
app.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
3
+
4
+
5
+ device = "cuda:0" if torch.cuda.is_available() else "cpu"
6
+ torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
7
+
8
+ model_id = "openai/whisper-large-v3"
9
+
10
+ model = AutoModelForSpeechSeq2Seq.from_pretrained(
11
+ model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=False, use_safetensors=True
12
+ )
13
+ model.to(device)
14
+
15
+ processor = AutoProcessor.from_pretrained(model_id)
16
+
17
+ pipe = pipeline(
18
+ "automatic-speech-recognition",
19
+ model=model,
20
+ tokenizer=processor.tokenizer,
21
+ feature_extractor=processor.feature_extractor,
22
+ max_new_tokens=128,
23
+ chunk_length_s=30,
24
+ batch_size=16,
25
+ return_timestamps=True,
26
+ torch_dtype=torch_dtype,
27
+ device=device,
28
+ )
29
+ result = pipe("/content/BryanThe_Ideal_Republic.ogg", generate_kwargs={"language": "french"})
30
+ print(result["text"]) # transcritpion
31
+ print(result["chunks"]) # translation
32
+
33
+ from transformers import RagTokenizer, RagRetriever, RagSequenceForGeneration
34
+
35
+
36
+ tokenizer = RagTokenizer.from_pretrained("facebook/rag-token-nq")
37
+ retriever = RagRetriever.from_pretrained("facebook/rag-token-nq", index_name="exact", use_dummy_dataset=True)
38
+ rag_model = RagSequenceForGeneration.from_pretrained("facebook/rag-token-nq", retriever=retriever)
39
+
40
+ def retrieve_and_generate_response(transcribed_text):
41
+ # Tokenize the transcribed text
42
+ input_ids = tokenizer(transcribed_text, return_tensors="pt").input_ids
43
+
44
+ # Generate response
45
+ outputs = rag_model.generate(input_ids)
46
+ response = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
47
+
48
+ return response
49
+
50
+ response = retrieve_and_generate_response(result["text"])
51
+ print("Response:", response)
52
+