Update app.py
Browse files
app.py
CHANGED
@@ -25,3 +25,32 @@ demo = gr.Interface(
|
|
25 |
)
|
26 |
|
27 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
)
|
26 |
|
27 |
demo.launch()
|
28 |
+
|
29 |
+
|
30 |
+
from transformers import WhisperProcessor, WhisperForConditionalGeneration
|
31 |
+
import torch
|
32 |
+
from datasets import load_dataset
|
33 |
+
|
34 |
+
# Load model and processor
|
35 |
+
processor = WhisperProcessor.from_pretrained("openai/whisper-small")
|
36 |
+
model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-small")
|
37 |
+
|
38 |
+
# Optional: Use GPU if available
|
39 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
40 |
+
model = model.to(device)
|
41 |
+
|
42 |
+
# Load sample audio (here using a dummy dataset, aap apni audio file bhi use kar sakte hain)
|
43 |
+
ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation")
|
44 |
+
sample = ds[0]["audio"]
|
45 |
+
|
46 |
+
# Prepare audio input
|
47 |
+
input_features = processor(sample["array"], sampling_rate=sample["sampling_rate"], return_tensors="pt").input_features
|
48 |
+
input_features = input_features.to(device)
|
49 |
+
|
50 |
+
# Generate transcription
|
51 |
+
predicted_ids = model.generate(input_features)
|
52 |
+
|
53 |
+
# Decode transcription
|
54 |
+
transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)
|
55 |
+
print(transcription)
|
56 |
+
|