Kushtrim commited on
Commit
d13b017
·
verified ·
1 Parent(s): 3e2ce7f

Upload 4 files

Browse files
Files changed (4) hide show
  1. README.md +6 -4
  2. app.py +131 -0
  3. packages.txt +1 -0
  4. requirements.txt +5 -0
README.md CHANGED
@@ -1,12 +1,14 @@
1
  ---
2
- title: Whisper Large V3 Turbo Shqip Radio Transcriber
3
- emoji: 📈
4
- colorFrom: yellow
5
  colorTo: red
6
  sdk: gradio
7
  sdk_version: 5.9.1
8
  app_file: app.py
9
  pinned: false
 
 
10
  ---
11
 
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: Transkriptim i Radios Shqip
3
+ emoji: 🎤
4
+ colorFrom: indigo
5
  colorTo: red
6
  sdk: gradio
7
  sdk_version: 5.9.1
8
  app_file: app.py
9
  pinned: false
10
+ tags:
11
+ - whisper-event
12
  ---
13
 
14
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,131 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import WhisperProcessor, WhisperForConditionalGeneration
2
+ from huggingface_hub import login
3
+ import gradio as gr
4
+ import numpy as np
5
+ import ffmpeg
6
+ import torch
7
+ import time
8
+ import os
9
+
10
+ login(os.environ["HF"], add_to_git_credential=True)
11
+
12
+ # Load Whisper Tiny Model
13
+ model_name = "Kushtrim/whisper-large-v3-turbo-shqip-115h"
14
+ processor = WhisperProcessor.from_pretrained(model_name)
15
+ model = WhisperForConditionalGeneration.from_pretrained(model_name)
16
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
17
+ model.to(device)
18
+
19
+ def transcribe_chunk(audio_chunk):
20
+ """Përkthe një pjesë të audios duke përdorur Whisper."""
21
+ audio_array = np.frombuffer(audio_chunk, dtype=np.int16).astype(np.float32) / 32768.0
22
+ inputs = processor(audio_array, sampling_rate=16000, return_tensors="pt").to(device)
23
+ generated_ids = model.generate(
24
+ inputs.input_features,
25
+ forced_decoder_ids=processor.get_decoder_prompt_ids(language="en") # English language
26
+ )
27
+ return processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
28
+
29
+ def stream_and_transcribe_radio(stream_url, chunk_duration=5):
30
+ """
31
+ Transkripto audio të drejtpërdrejtë nga një stream radioje.
32
+
33
+ Args:
34
+ stream_url (str): URL i stream-it audio.
35
+ chunk_duration (int): Kohëzgjatja e secilës pjesë për t'u përpunuar në sekonda.
36
+ """
37
+ process = (
38
+ ffmpeg.input(stream_url)
39
+ .output('pipe:', format='wav', acodec='pcm_s16le', ac=1, ar='16000')
40
+ .run_async(pipe_stdout=True, pipe_stderr=True)
41
+ )
42
+
43
+ chunk_size = chunk_duration * 16000 * 2 # 16000 mostra për sekondë, 2 byte për mostër
44
+ transcription_log = ""
45
+ try:
46
+ for i in range(60): # Përpunoni për 5 minuta (60 pjesë nga 5 sekonda)
47
+ audio_chunk = process.stdout.read(chunk_size)
48
+ if not audio_chunk:
49
+ break
50
+ transcription = transcribe_chunk(audio_chunk)
51
+ current_time = time.strftime("%H:%M:%S", time.localtime())
52
+ new_transcription = f"[{current_time}] {transcription}"
53
+ transcription_log += new_transcription + "\n"
54
+ yield transcription_log # Dërgo përditësimin e transkriptimit
55
+ finally:
56
+ process.kill()
57
+
58
+ # Gradio Interface
59
+ def transcribe_app(stream_url):
60
+ return stream_and_transcribe_radio(stream_url, chunk_duration=5)
61
+
62
+ with gr.Blocks() as app:
63
+ gr.Markdown("""
64
+ <h1 style="text-align: center;">🎤 Transkriptim i Radios në Shqip</h1>
65
+ <p style="text-align: center;">
66
+ Ky aplikacion ju lejon të transkriptoni audio të drejtpërdrejtë nga një stream radioje për 5 minuta.
67
+ Vendosni URL-në e stream-it dhe klikoni "Fillo Transkriptimin".
68
+ </p>
69
+ <p style="text-align: center;">Autori: Kushtrim Visoka</p>
70
+ <p style="text-align: center;">
71
+ Modeli i përdorur për transkriptim:
72
+ <a href="https://huggingface.co/openai/whisper-tiny" target="_blank">openai/whisper-tiny</a>
73
+ </p>
74
+ """)
75
+
76
+ # Futja e URL-së së stream-it
77
+ stream_url_input = gr.Textbox(label="Vendosni URL-në e stream-it audio", value="https://s2.voscast.com:8825/radiodukagjini")
78
+
79
+ # Butoni për fillimin e transkriptimit
80
+ transcribe_button = gr.Button("Fillo Transkriptimin")
81
+
82
+ # Luajtësi i audios
83
+ audio_player = gr.HTML("""
84
+ <div style="text-align: center; margin-top: 20px;">
85
+ <audio controls autoplay style="width: 80%; max-width: 600px;">
86
+ <source src="https://s2.voscast.com:8825/radiodukagjini" type="audio/mpeg">
87
+ Shfletuesi juaj nuk e mbështet elementin audio.
88
+ </audio>
89
+ </div>
90
+ """)
91
+
92
+ # Dalja e transkriptimit
93
+ transcription_output = gr.Textbox(label="Dalja e Transkriptimit", lines=20, interactive=False)
94
+
95
+ # Përditëso transkriptimin dinamikisht
96
+ def update_transcription(stream_url):
97
+ for transcription in transcribe_app(stream_url):
98
+ yield transcription
99
+
100
+ # Përditëso luajtësin e audios dinamikisht
101
+ def update_audio_player(stream_url):
102
+ return f"""
103
+ <div style="text-align: center; margin-top: 20px;">
104
+ <audio controls autoplay style="width: 80%; max-width: 600px;">
105
+ <source src="{stream_url}" type="audio/mpeg">
106
+ Shfletuesi juaj nuk e mbështet elementin audio.
107
+ </audio>
108
+ </div>
109
+ """
110
+
111
+ # Lidhja e eventeve
112
+ transcribe_button.click(
113
+ update_transcription,
114
+ inputs=stream_url_input,
115
+ outputs=transcription_output
116
+ )
117
+
118
+ stream_url_input.change(
119
+ update_audio_player,
120
+ inputs=stream_url_input,
121
+ outputs=audio_player
122
+ )
123
+
124
+ # Rreshtimi i komponentëve
125
+ stream_url_input
126
+ transcribe_button
127
+ audio_player
128
+ transcription_output
129
+
130
+ # Nis aplikacionin
131
+ app.launch()
packages.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ ffmpeg
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ transformers
2
+ torch
3
+ ffmpeg-python
4
+ huggingface-hub
5
+ pydub