Hyeonsieun commited on
Commit
f75d363
·
verified ·
1 Parent(s): 5ba74c1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -154
app.py CHANGED
@@ -1,18 +1,3 @@
1
- import torch
2
-
3
- import gradio as gr
4
-
5
- from transformers import T5ForConditionalGeneration, T5Tokenizer
6
-
7
- import os
8
-
9
- #import whisper
10
-
11
- import matplotlib as plt
12
-
13
- # whisper_model = whisper.load_model('large-v2') # Whisper 모델을 불러오기
14
-
15
-
16
  path = "Hyeonsieun/NTtoGT_1epoch"
17
  tokenizer = T5Tokenizer.from_pretrained(path)
18
  model = T5ForConditionalGeneration.from_pretrained(path)
@@ -42,144 +27,5 @@ def do_correction(text):
42
  )
43
  return corrected_sentence
44
 
45
- # corrected_sentence = do_correction(sentence, model, tokenizer)
46
-
47
 
48
  gr.Interface(fn=do_correction, inputs="text", outputs="text").launch()
49
-
50
-
51
- '''
52
- pipe = pipeline(
53
- task="automatic-speech-recognition",
54
- model=MODEL_NAME,
55
- chunk_length_s=30,
56
- device=device,
57
- )
58
-
59
-
60
- def transcribe(inputs, task):
61
- if inputs is None:
62
- raise gr.Error("No audio file submitted! Please upload or record an audio file before submitting your request.")
63
-
64
- text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"]
65
- return text
66
-
67
-
68
- def _return_yt_html_embed(yt_url):
69
- video_id = yt_url.split("?v=")[-1]
70
- HTML_str = (
71
- f'<center> <iframe width="500" height="320" src="https://www.youtube.com/embed/{video_id}"> </iframe>'
72
- " </center>"
73
- )
74
- return HTML_str
75
-
76
- def download_yt_audio(yt_url, filename):
77
- info_loader = youtube_dl.YoutubeDL()
78
-
79
- try:
80
- info = info_loader.extract_info(yt_url, download=False)
81
- except youtube_dl.utils.DownloadError as err:
82
- raise gr.Error(str(err))
83
-
84
- file_length = info["duration_string"]
85
- file_h_m_s = file_length.split(":")
86
- file_h_m_s = [int(sub_length) for sub_length in file_h_m_s]
87
-
88
- if len(file_h_m_s) == 1:
89
- file_h_m_s.insert(0, 0)
90
- if len(file_h_m_s) == 2:
91
- file_h_m_s.insert(0, 0)
92
- file_length_s = file_h_m_s[0] * 3600 + file_h_m_s[1] * 60 + file_h_m_s[2]
93
-
94
- if file_length_s > YT_LENGTH_LIMIT_S:
95
- yt_length_limit_hms = time.strftime("%HH:%MM:%SS", time.gmtime(YT_LENGTH_LIMIT_S))
96
- file_length_hms = time.strftime("%HH:%MM:%SS", time.gmtime(file_length_s))
97
- raise gr.Error(f"Maximum YouTube length is {yt_length_limit_hms}, got {file_length_hms} YouTube video.")
98
-
99
- ydl_opts = {"outtmpl": filename, "format": "worstvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best"}
100
-
101
- with youtube_dl.YoutubeDL(ydl_opts) as ydl:
102
- try:
103
- ydl.download([yt_url])
104
- except youtube_dl.utils.ExtractorError as err:
105
- raise gr.Error(str(err))
106
-
107
-
108
- def yt_transcribe(yt_url, task, max_filesize=75.0):
109
- html_embed_str = _return_yt_html_embed(yt_url)
110
-
111
- with tempfile.TemporaryDirectory() as tmpdirname:
112
- filepath = os.path.join(tmpdirname, "video.mp4")
113
- download_yt_audio(yt_url, filepath)
114
- with open(filepath, "rb") as f:
115
- inputs = f.read()
116
-
117
- inputs = ffmpeg_read(inputs, pipe.feature_extractor.sampling_rate)
118
- inputs = {"array": inputs, "sampling_rate": pipe.feature_extractor.sampling_rate}
119
-
120
- text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"]
121
-
122
- return html_embed_str, text
123
-
124
-
125
- demo = gr.Blocks()
126
-
127
- mf_transcribe = gr.Interface(
128
- fn=transcribe,
129
- inputs=[
130
- gr.inputs.Audio(source="microphone", type="filepath", optional=True),
131
- gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe"),
132
- ],
133
- outputs="text",
134
- layout="horizontal",
135
- theme="huggingface",
136
- title="Whisper Large V3: Transcribe Audio",
137
- description=(
138
- "Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the OpenAI Whisper"
139
- f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe audio files"
140
- " of arbitrary length."
141
- ),
142
- allow_flagging="never",
143
- )
144
-
145
- file_transcribe = gr.Interface(
146
- fn=transcribe,
147
- inputs=[
148
- gr.inputs.Audio(source="upload", type="filepath", optional=True, label="Audio file"),
149
- gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe"),
150
- ],
151
- outputs="text",
152
- layout="horizontal",
153
- theme="huggingface",
154
- title="Whisper Large V3: Transcribe Audio",
155
- description=(
156
- "Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the OpenAI Whisper"
157
- f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe audio files"
158
- " of arbitrary length."
159
- ),
160
- allow_flagging="never",
161
- )
162
-
163
- yt_transcribe = gr.Interface(
164
- fn=yt_transcribe,
165
- inputs=[
166
- gr.inputs.Textbox(lines=1, placeholder="Paste the URL to a YouTube video here", label="YouTube URL"),
167
- gr.inputs.Radio(["transcribe", "translate"], label="Task", default="transcribe")
168
- ],
169
- outputs=["html", "text"],
170
- layout="horizontal",
171
- theme="huggingface",
172
- title="Whisper Large V3: Transcribe YouTube",
173
- description=(
174
- "Transcribe long-form YouTube videos with the click of a button! Demo uses the OpenAI Whisper checkpoint"
175
- f" [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe video files of"
176
- " arbitrary length."
177
- ),
178
- allow_flagging="never",
179
- )
180
-
181
- with demo:
182
- gr.TabbedInterface([mf_transcribe, file_transcribe, yt_transcribe], ["Microphone", "Audio file", "YouTube"])
183
-
184
- demo.launch(enable_queue=True)
185
- '''
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  path = "Hyeonsieun/NTtoGT_1epoch"
2
  tokenizer = T5Tokenizer.from_pretrained(path)
3
  model = T5ForConditionalGeneration.from_pretrained(path)
 
27
  )
28
  return corrected_sentence
29
 
 
 
30
 
31
  gr.Interface(fn=do_correction, inputs="text", outputs="text").launch()