Spaces:
Sleeping
Sleeping
File size: 7,702 Bytes
5fdb69e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "kayiMLgsBnVt"
},
"outputs": [],
"source": [
"!pip install -q requests torch bitsandbytes transformers sentencepiece accelerate openai gradio"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"executionInfo": {
"elapsed": 15255,
"status": "ok",
"timestamp": 1744678358807,
"user": {
"displayName": "Kenneth Andales",
"userId": "04047926009324958530"
},
"user_tz": -480
},
"id": "ByKEQHyhiLl7"
},
"outputs": [],
"source": [
"import os\n",
"import requests\n",
"from IPython.display import Markdown, display, update_display\n",
"from openai import OpenAI\n",
"from google.colab import drive, userdata\n",
"from huggingface_hub import login\n",
"from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig, TextStreamer\n",
"import torch\n",
"import gradio as gr"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"executionInfo": {
"elapsed": 2,
"status": "ok",
"timestamp": 1744678358815,
"user": {
"displayName": "Kenneth Andales",
"userId": "04047926009324958530"
},
"user_tz": -480
},
"id": "9tzK_t3jiOo1"
},
"outputs": [],
"source": [
"AUDIO_MODEL = 'whisper-1'\n",
"LLAMA = \"meta-llama/Meta-Llama-3.1-8B-Instruct\""
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"executionInfo": {
"elapsed": 737,
"status": "ok",
"timestamp": 1744678360474,
"user": {
"displayName": "Kenneth Andales",
"userId": "04047926009324958530"
},
"user_tz": -480
},
"id": "PYNmGaQniW73"
},
"outputs": [],
"source": [
"hf_token = userdata.get('HF_TOKEN')\n",
"login(hf_token, add_to_git_credential=True)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"executionInfo": {
"elapsed": 555,
"status": "ok",
"timestamp": 1744678362522,
"user": {
"displayName": "Kenneth Andales",
"userId": "04047926009324958530"
},
"user_tz": -480
},
"id": "yGjVTeMEig-b"
},
"outputs": [],
"source": [
"openai_api_key = userdata.get(\"OPENAI_API_KEY\")\n",
"openai = OpenAI(api_key=openai_api_key)"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {
"executionInfo": {
"elapsed": 9,
"status": "ok",
"timestamp": 1744679561600,
"user": {
"displayName": "Kenneth Andales",
"userId": "04047926009324958530"
},
"user_tz": -480
},
"id": "6jboyASHilLz"
},
"outputs": [],
"source": [
"def message_prompt(transciption):\n",
" system_message = \"\"\"\n",
" You are an assistant that translate japanese text into two different languages like 'English' and 'Filipino',\n",
" please display the translated text into markdown and include the original text from japanese using 'Romaji',\n",
" sample format would be - original text (converted to romaji): orignal_translated_text_here \\n\\n translated to english: translated_english_text_here \\n\\n translated to filipino: translated_filipino_text_here\"\n",
" \"\"\"\n",
"\n",
" user_propmpt = f\"Here is the transcripted japanese audio and translate it into two languages: '{transciption}'. No explaination just the translated languages only.\"\n",
"\n",
" messages = [\n",
" {\"role\": \"system\", \"content\": system_message},\n",
" {\"role\": \"user\", \"content\": user_propmpt}\n",
" ]\n",
"\n",
" return messages"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"executionInfo": {
"elapsed": 7,
"status": "ok",
"timestamp": 1744678366113,
"user": {
"displayName": "Kenneth Andales",
"userId": "04047926009324958530"
},
"user_tz": -480
},
"id": "nYrf_wKmmoUs"
},
"outputs": [],
"source": [
"quant_config = BitsAndBytesConfig(\n",
" load_in_4bit=True,\n",
" bnb_4bit_use_double_quant=True,\n",
" bnb_4bit_quant_type=\"nf4\",\n",
" bnb_4bit_compute_dtype=torch.bfloat16\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"executionInfo": {
"elapsed": 7,
"status": "ok",
"timestamp": 1744678367778,
"user": {
"displayName": "Kenneth Andales",
"userId": "04047926009324958530"
},
"user_tz": -480
},
"id": "ESlOaRGioqUQ"
},
"outputs": [],
"source": [
"def translation(messages):\n",
" tokenizer = AutoTokenizer.from_pretrained(LLAMA)\n",
" tokenizer.pad_token = tokenizer.eos_token\n",
" inputs = tokenizer.apply_chat_template(messages, return_tensors=\"pt\").to(\"cuda\")\n",
" streamer = TextStreamer(tokenizer)\n",
" model = AutoModelForCausalLM.from_pretrained(LLAMA, device_map=\"auto\", quantization_config=quant_config)\n",
" outputs = model.generate(inputs, max_new_tokens=2000, streamer=streamer)\n",
"\n",
" return tokenizer.decode(outputs[0])"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {
"executionInfo": {
"elapsed": 6,
"status": "ok",
"timestamp": 1744679567326,
"user": {
"displayName": "Kenneth Andales",
"userId": "04047926009324958530"
},
"user_tz": -480
},
"id": "FSGFTvIEys0j"
},
"outputs": [],
"source": [
"def translate_text(file):\n",
" try:\n",
" audio_file = open(file, \"rb\")\n",
"\n",
" transciption = openai.audio.transcriptions.create(\n",
" model=AUDIO_MODEL,\n",
" file=audio_file,\n",
" response_format=\"text\",\n",
" language=\"ja\"\n",
" )\n",
"\n",
" messages = message_prompt(transciption)\n",
" response = translation(messages)\n",
"\n",
" return response\n",
" except Exception as e:\n",
" return f\"Unexpected error: {str(e)}\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "bexgSsWuvUmU"
},
"outputs": [],
"source": [
"with gr.Blocks() as demo:\n",
" gr.Markdown(\"# 🎙️ Anime Audio Translator\")\n",
" with gr.Row():\n",
" with gr.Column():\n",
" audio_file = gr.Audio(type=\"filepath\", label=\"Upload Audio\")\n",
" button = gr.Button(\"Translate\", variant=\"primary\")\n",
"\n",
" with gr.Column():\n",
" gr.Label(value=\"Result of translated text to 'English' and 'Filipino'\", label=\"Character\")\n",
" output_text = gr.Markdown()\n",
"\n",
" button.click(\n",
" fn=translate_text,\n",
" inputs=audio_file,\n",
" outputs=output_text,\n",
" trigger_mode=\"once\"\n",
" )\n",
"demo.launch()"
]
}
],
"metadata": {
"accelerator": "GPU",
"colab": {
"authorship_tag": "ABX9TyO+HrhlkaVchpoGIfmYAHdf",
"gpuType": "T4",
"provenance": []
},
"kernelspec": {
"display_name": "Python [conda env:base] *",
"language": "python",
"name": "conda-base-py"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
|