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|
|
|
|
|
40 |
)
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
snapshot_download(
|
46 |
-
repo_id=RESULTS_REPO, local_dir=EVAL_RESULTS_PATH, repo_type="dataset", tqdm_class=None, etag_timeout=30, token=TOKEN
|
47 |
)
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
(
|
55 |
-
finished_eval_queue_df,
|
56 |
-
running_eval_queue_df,
|
57 |
-
pending_eval_queue_df,
|
58 |
-
) = get_evaluation_queue_df(EVAL_REQUESTS_PATH, EVAL_COLS)
|
59 |
-
|
60 |
-
def init_leaderboard(dataframe):
|
61 |
-
if dataframe is None or dataframe.empty:
|
62 |
-
raise ValueError("Leaderboard DataFrame is empty or None.")
|
63 |
-
return Leaderboard(
|
64 |
-
value=dataframe,
|
65 |
-
datatype=[c.type for c in fields(AutoEvalColumn)],
|
66 |
-
select_columns=SelectColumns(
|
67 |
-
default_selection=[c.name for c in fields(AutoEvalColumn) if c.displayed_by_default],
|
68 |
-
cant_deselect=[c.name for c in fields(AutoEvalColumn) if c.never_hidden],
|
69 |
-
label="Select Columns to Display:",
|
70 |
-
),
|
71 |
-
search_columns=[AutoEvalColumn.model.name, AutoEvalColumn.license.name],
|
72 |
-
hide_columns=[c.name for c in fields(AutoEvalColumn) if c.hidden],
|
73 |
-
filter_columns=[
|
74 |
-
ColumnFilter(AutoEvalColumn.model_type.name, type="checkboxgroup", label="Model types"),
|
75 |
-
ColumnFilter(AutoEvalColumn.precision.name, type="checkboxgroup", label="Precision"),
|
76 |
-
ColumnFilter(
|
77 |
-
AutoEvalColumn.params.name,
|
78 |
-
type="slider",
|
79 |
-
min=0.01,
|
80 |
-
max=150,
|
81 |
-
label="Select the number of parameters (B)",
|
82 |
-
),
|
83 |
-
ColumnFilter(
|
84 |
-
AutoEvalColumn.still_on_hub.name, type="boolean", label="Deleted/incomplete", default=True
|
85 |
-
),
|
86 |
-
],
|
87 |
-
bool_checkboxgroup_label="Hide models",
|
88 |
-
interactive=False,
|
89 |
)
|
90 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
|
92 |
-
|
93 |
-
|
94 |
-
gr.HTML(TITLE)
|
95 |
-
gr.Markdown(INTRODUCTION_TEXT, elem_classes="markdown-text")
|
96 |
-
|
97 |
-
with gr.Tabs(elem_classes="tab-buttons") as tabs:
|
98 |
-
with gr.TabItem("🏅 LLM Benchmark", elem_id="llm-benchmark-tab-table", id=0):
|
99 |
-
leaderboard = init_leaderboard(LEADERBOARD_DF)
|
100 |
-
|
101 |
-
with gr.TabItem("📝 About", elem_id="llm-benchmark-tab-table", id=2):
|
102 |
-
gr.Markdown(LLM_BENCHMARKS_TEXT, elem_classes="markdown-text")
|
103 |
-
|
104 |
-
with gr.TabItem("🚀 Submit here! ", elem_id="llm-benchmark-tab-table", id=3):
|
105 |
-
with gr.Column():
|
106 |
-
with gr.Row():
|
107 |
-
gr.Markdown(EVALUATION_QUEUE_TEXT, elem_classes="markdown-text")
|
108 |
-
|
109 |
-
with gr.Column():
|
110 |
-
with gr.Accordion(
|
111 |
-
f"✅ Finished Evaluations ({len(finished_eval_queue_df)})",
|
112 |
-
open=False,
|
113 |
-
):
|
114 |
-
with gr.Row():
|
115 |
-
finished_eval_table = gr.components.Dataframe(
|
116 |
-
value=finished_eval_queue_df,
|
117 |
-
headers=EVAL_COLS,
|
118 |
-
datatype=EVAL_TYPES,
|
119 |
-
row_count=5,
|
120 |
-
)
|
121 |
-
with gr.Accordion(
|
122 |
-
f"🔄 Running Evaluation Queue ({len(running_eval_queue_df)})",
|
123 |
-
open=False,
|
124 |
-
):
|
125 |
-
with gr.Row():
|
126 |
-
running_eval_table = gr.components.Dataframe(
|
127 |
-
value=running_eval_queue_df,
|
128 |
-
headers=EVAL_COLS,
|
129 |
-
datatype=EVAL_TYPES,
|
130 |
-
row_count=5,
|
131 |
-
)
|
132 |
-
|
133 |
-
with gr.Accordion(
|
134 |
-
f"⏳ Pending Evaluation Queue ({len(pending_eval_queue_df)})",
|
135 |
-
open=False,
|
136 |
-
):
|
137 |
-
with gr.Row():
|
138 |
-
pending_eval_table = gr.components.Dataframe(
|
139 |
-
value=pending_eval_queue_df,
|
140 |
-
headers=EVAL_COLS,
|
141 |
-
datatype=EVAL_TYPES,
|
142 |
-
row_count=5,
|
143 |
-
)
|
144 |
-
with gr.Row():
|
145 |
-
gr.Markdown("# ✉️✨ Submit your model here!", elem_classes="markdown-text")
|
146 |
-
|
147 |
-
with gr.Row():
|
148 |
-
with gr.Column():
|
149 |
-
model_name_textbox = gr.Textbox(label="Model name")
|
150 |
-
revision_name_textbox = gr.Textbox(label="Revision commit", placeholder="main")
|
151 |
-
model_type = gr.Dropdown(
|
152 |
-
choices=[t.to_str(" : ") for t in ModelType if t != ModelType.Unknown],
|
153 |
-
label="Model type",
|
154 |
-
multiselect=False,
|
155 |
-
value=None,
|
156 |
-
interactive=True,
|
157 |
-
)
|
158 |
-
|
159 |
-
with gr.Column():
|
160 |
-
precision = gr.Dropdown(
|
161 |
-
choices=[i.value.name for i in Precision if i != Precision.Unknown],
|
162 |
-
label="Precision",
|
163 |
-
multiselect=False,
|
164 |
-
value="float16",
|
165 |
-
interactive=True,
|
166 |
-
)
|
167 |
-
weight_type = gr.Dropdown(
|
168 |
-
choices=[i.value.name for i in WeightType],
|
169 |
-
label="Weights type",
|
170 |
-
multiselect=False,
|
171 |
-
value="Original",
|
172 |
-
interactive=True,
|
173 |
-
)
|
174 |
-
base_model_name_textbox = gr.Textbox(label="Base model (for delta or adapter weights)")
|
175 |
-
|
176 |
-
submit_button = gr.Button("Submit Eval")
|
177 |
-
submission_result = gr.Markdown()
|
178 |
-
submit_button.click(
|
179 |
-
add_new_eval,
|
180 |
-
[
|
181 |
-
model_name_textbox,
|
182 |
-
base_model_name_textbox,
|
183 |
-
revision_name_textbox,
|
184 |
-
precision,
|
185 |
-
weight_type,
|
186 |
-
model_type,
|
187 |
-
],
|
188 |
-
submission_result,
|
189 |
-
)
|
190 |
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
show_copy_button=True,
|
199 |
-
)
|
200 |
-
|
201 |
-
scheduler = BackgroundScheduler()
|
202 |
-
scheduler.add_job(restart_space, "interval", seconds=1800)
|
203 |
-
scheduler.start()
|
204 |
-
demo.queue(default_concurrency_limit=40).launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import uuid
|
4 |
+
import os
|
5 |
+
from typing import Optional
|
6 |
+
import tempfile
|
7 |
+
from pydub import AudioSegment
|
8 |
+
import re
|
9 |
+
|
10 |
+
ASR_API = "http://astarwiz.com:9998/asr"
|
11 |
+
TTS_SPEAK_SERVICE = 'http://astarwiz.com:9603/speak'
|
12 |
+
TTS_WAVE_SERVICE = 'http://astarwiz.com:9603/wave'
|
13 |
+
|
14 |
+
LANGUAGE_MAP = {
|
15 |
+
"en": "English",
|
16 |
+
"ma": "Malay",
|
17 |
+
"ta": "Tamil",
|
18 |
+
"zh": "Chinese"
|
19 |
+
}
|
20 |
+
|
21 |
+
# Add a password for developer mode
|
22 |
+
DEVELOPER_PASSWORD = os.getenv("DEV_PWD")
|
23 |
+
|
24 |
+
# Add this constant for the RapidAPI key
|
25 |
+
RAPID_API_KEY = os.getenv("RAPID_API_KEY")
|
26 |
+
|
27 |
+
# Add this constant for available speakers
|
28 |
+
AVAILABLE_SPEAKERS = {
|
29 |
+
"en": ["MS"],
|
30 |
+
"ma": ["msFemale"],
|
31 |
+
"ta": ["ta_female1"],
|
32 |
+
"zh": ["childChinese2"]
|
33 |
+
}
|
34 |
+
|
35 |
+
def fetch_youtube_id(youtube_url: str) -> str:
|
36 |
+
if 'v=' in youtube_url:
|
37 |
+
return youtube_url.split("v=")[1].split("&")[0]
|
38 |
+
elif 'youtu.be/' in youtube_url:
|
39 |
+
return youtube_url.split("youtu.be/")[1]
|
40 |
+
elif 'shorts' in youtube_url:
|
41 |
+
return youtube_url.split("/")[-1]
|
42 |
+
else:
|
43 |
+
raise Exception("Unsupported URL format")
|
44 |
+
|
45 |
+
def download_youtube_audio(youtube_url: str, output_dir: Optional[str] = None) -> Optional[str]:
|
46 |
+
video_id = fetch_youtube_id(youtube_url)
|
47 |
+
|
48 |
+
if not video_id:
|
49 |
+
return None
|
50 |
+
|
51 |
+
if output_dir is None:
|
52 |
+
output_dir = tempfile.gettempdir()
|
53 |
+
|
54 |
+
output_filename = os.path.join(output_dir, f"{video_id}.mp3")
|
55 |
+
|
56 |
+
if os.path.exists(output_filename):
|
57 |
+
return output_filename # Return if the file already exists
|
58 |
+
|
59 |
+
url = "https://youtube86.p.rapidapi.com/api/youtube/links"
|
60 |
+
headers = {
|
61 |
+
'Content-Type': 'application/json',
|
62 |
+
'x-rapidapi-host': 'youtube86.p.rapidapi.com',
|
63 |
+
'x-rapidapi-key': RAPID_API_KEY
|
64 |
+
}
|
65 |
+
data = {
|
66 |
+
"url": youtube_url
|
67 |
+
}
|
68 |
+
|
69 |
+
response = requests.post(url, headers=headers, json=data)
|
70 |
+
print('Fetched audio links')
|
71 |
+
|
72 |
+
if response.status_code == 200:
|
73 |
+
result = response.json()
|
74 |
+
for url in result[0]['urls']:
|
75 |
+
if url.get('isBundle'):
|
76 |
+
audio_url = url['url']
|
77 |
+
extension = url['extension']
|
78 |
+
audio_response = requests.get(audio_url)
|
79 |
+
|
80 |
+
if audio_response.status_code == 200:
|
81 |
+
temp_filename = os.path.join(output_dir, f"{video_id}.{extension}")
|
82 |
+
with open(temp_filename, 'wb') as audio_file:
|
83 |
+
audio_file.write(audio_response.content)
|
84 |
+
|
85 |
+
# Convert to MP3 and downsample to 16000 Hz
|
86 |
+
audio = AudioSegment.from_file(temp_filename, format=extension)
|
87 |
+
audio = audio.set_frame_rate(16000)
|
88 |
+
audio.export(output_filename, format="mp3", parameters=["-ar", "16000"])
|
89 |
+
|
90 |
+
os.remove(temp_filename) # Remove the temporary file
|
91 |
+
return output_filename # Return the final MP3 filename
|
92 |
+
|
93 |
+
return None # Return None if no successful download occurs
|
94 |
+
else:
|
95 |
+
print("Error:", response.status_code, response.text)
|
96 |
+
return None # Return None on failure
|
97 |
+
|
98 |
+
def inference_via_llm_api(input_text, min_new_tokens=2, max_new_tokens=64):
|
99 |
+
print(input_text)
|
100 |
+
one_vllm_input = f"<|im_start|>system\nYou are a translation expert.<|im_end|>\n<|im_start|>user\n{input_text}<|im_end|>\n<|im_start|>assistant"
|
101 |
+
vllm_api = 'http://astarwiz.com:2333/' + "v1/completions"
|
102 |
+
data = {
|
103 |
+
"prompt": one_vllm_input,
|
104 |
+
'model': "./Edu-4B-NewTok-V2-20240904/",
|
105 |
+
'min_tokens': min_new_tokens,
|
106 |
+
'max_tokens': max_new_tokens,
|
107 |
+
'temperature': 0.1,
|
108 |
+
'top_p': 0.75,
|
109 |
+
'repetition_penalty': 1.1,
|
110 |
+
"stop_token_ids": [151645, ],
|
111 |
+
}
|
112 |
+
response = requests.post(vllm_api, headers={"Content-Type": "application/json"}, json=data).json()
|
113 |
+
print(response)
|
114 |
+
if "choices" in response.keys():
|
115 |
+
return response["choices"][0]['text'].strip()
|
116 |
+
else:
|
117 |
+
return "The system got some error during vLLM generation. Please try it again."
|
118 |
+
|
119 |
+
def transcribe_and_speak(audio, source_lang, target_lang, youtube_url=None, target_speaker=None):
|
120 |
+
if youtube_url:
|
121 |
+
audio = download_youtube_audio(youtube_url)
|
122 |
+
if not audio:
|
123 |
+
return "Failed to download YouTube audio.", None, None
|
124 |
+
|
125 |
+
if not audio:
|
126 |
+
return "Please provide an audio input or a valid YouTube URL.", None, None
|
127 |
+
|
128 |
+
# ASR
|
129 |
+
file_id = str(uuid.uuid4())
|
130 |
+
files = {'file': open(audio, 'rb')}
|
131 |
+
data = {
|
132 |
+
'language': 'ms' if source_lang == 'ma' else source_lang,
|
133 |
+
'model_name': 'whisper-large-v2-local-cs',
|
134 |
+
'with_timestamp': False
|
135 |
+
}
|
136 |
+
|
137 |
+
asr_response = requests.post(ASR_API, files=files, data=data)
|
138 |
+
print(asr_response.json())
|
139 |
+
if asr_response.status_code == 200:
|
140 |
+
transcription = asr_response.json()['text']
|
141 |
+
else:
|
142 |
+
return "ASR failed", None, None
|
143 |
+
|
144 |
+
translation_prompt = f"Translate the following text from {LANGUAGE_MAP[source_lang]} to {LANGUAGE_MAP[target_lang]}: {transcription}"
|
145 |
+
translated_text = inference_via_llm_api(translation_prompt)
|
146 |
+
print(f"Translation: {translated_text}")
|
147 |
+
|
148 |
+
# TTS
|
149 |
+
tts_params = {
|
150 |
+
'language': target_lang,
|
151 |
+
'speed': 1.1,
|
152 |
+
'speaker': target_speaker or AVAILABLE_SPEAKERS[target_lang][0], # Use the first speaker as default
|
153 |
+
'text': translated_text
|
154 |
+
}
|
155 |
+
|
156 |
+
tts_response = requests.get(TTS_SPEAK_SERVICE, params=tts_params)
|
157 |
+
if tts_response.status_code == 200:
|
158 |
+
audio_file = tts_response.text.strip()
|
159 |
+
audio_url = f"{TTS_WAVE_SERVICE}?file={audio_file}"
|
160 |
+
return transcription, translated_text, audio_url
|
161 |
+
else:
|
162 |
+
return transcription, translated_text, "TTS failed"
|
163 |
+
|
164 |
+
def check_password(password):
|
165 |
+
return password == DEVELOPER_PASSWORD
|
166 |
+
|
167 |
+
def run_speech_translation(audio, source_lang, target_lang, youtube_url, target_speaker):
|
168 |
+
transcription, translated_text, audio_url = transcribe_and_speak(audio, source_lang, target_lang, youtube_url, target_speaker)
|
169 |
+
|
170 |
+
return transcription, translated_text, audio_url
|
171 |
+
|
172 |
+
with gr.Blocks() as demo:
|
173 |
+
gr.Markdown("# Speech Translation")
|
174 |
+
|
175 |
+
# with gr.Tab("User Mode"):
|
176 |
+
gr.Markdown("Speak into the microphone, upload an audio file, or provide a YouTube URL. The app will translate and speak it back to you.")
|
177 |
+
|
178 |
+
with gr.Row():
|
179 |
+
user_audio_input = gr.Audio(sources=["microphone", "upload"], type="filepath")
|
180 |
+
user_youtube_url = gr.Textbox(label="YouTube URL (optional)")
|
181 |
+
|
182 |
+
with gr.Row():
|
183 |
+
user_source_lang = gr.Dropdown(choices=["en", "ma", "ta", "zh"], label="Source Language", value="en")
|
184 |
+
user_target_lang = gr.Dropdown(choices=["en", "ma", "ta", "zh"], label="Target Language", value="zh")
|
185 |
+
user_target_speaker = gr.Dropdown(choices=AVAILABLE_SPEAKERS['zh'], label="Target Speaker", value="childChinese2")
|
186 |
+
|
187 |
+
with gr.Row():
|
188 |
+
user_button = gr.Button("Translate and Speak", interactive=False)
|
189 |
+
|
190 |
+
with gr.Row():
|
191 |
+
user_transcription_output = gr.Textbox(label="Transcription")
|
192 |
+
user_translation_output = gr.Textbox(label="Translation")
|
193 |
+
user_audio_output = gr.Audio(label="Translated Speech")
|
194 |
+
|
195 |
+
user_video_output = gr.HTML(label="YouTube Video")
|
196 |
+
|
197 |
+
def update_button_state(audio, youtube_url):
|
198 |
+
print(audio, youtube_url)
|
199 |
+
return gr.Button(interactive=bool(audio) or bool(youtube_url))
|
200 |
+
|
201 |
+
user_audio_input.change(
|
202 |
+
fn=update_button_state,
|
203 |
+
inputs=[user_audio_input, user_youtube_url],
|
204 |
+
outputs=user_button
|
205 |
)
|
206 |
+
user_youtube_url.change(
|
207 |
+
fn=update_button_state,
|
208 |
+
inputs=[user_audio_input, user_youtube_url],
|
209 |
+
outputs=user_button
|
|
|
|
|
210 |
)
|
211 |
+
|
212 |
+
user_button.click(
|
213 |
+
fn=run_speech_translation,
|
214 |
+
inputs=[user_audio_input, user_source_lang, user_target_lang, user_youtube_url, user_target_speaker],
|
215 |
+
outputs=[user_transcription_output, user_translation_output, user_audio_output]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
216 |
)
|
217 |
|
218 |
+
def update_video_embed(youtube_url):
|
219 |
+
if youtube_url:
|
220 |
+
try:
|
221 |
+
video_id = fetch_youtube_id(youtube_url)
|
222 |
+
return f'<iframe width="560" height="315" src="https://www.youtube.com/embed/{video_id}" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>'
|
223 |
+
except Exception as e:
|
224 |
+
print(f"Error embedding video: {e}")
|
225 |
+
return ""
|
226 |
+
|
227 |
+
user_youtube_url.change(
|
228 |
+
fn=update_video_embed,
|
229 |
+
inputs=[user_youtube_url],
|
230 |
+
outputs=[user_video_output]
|
231 |
+
)
|
232 |
|
233 |
+
def update_target_speakers(target_lang):
|
234 |
+
return gr.Dropdown(choices=AVAILABLE_SPEAKERS[target_lang], value=AVAILABLE_SPEAKERS[target_lang][0])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
235 |
|
236 |
+
user_target_lang.change(
|
237 |
+
fn=update_target_speakers,
|
238 |
+
inputs=[user_target_lang],
|
239 |
+
outputs=[user_target_speaker]
|
240 |
+
)
|
241 |
+
|
242 |
+
demo.launch(auth=("test", "test")
|
|
|
|
|
|
|
|
|
|
|
|
|
|