Skip to content

Commit 4b9931d

Browse files
committed
rearrange notebook and add toto on 5th notebook
1 parent 9f92ec6 commit 4b9931d

File tree

7 files changed

+186
-74
lines changed

7 files changed

+186
-74
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Machine Translation\n",
8+
"\n",
9+
"What is machine translation\n",
10+
"- Translation converts a sequence of text from one language to another. \n",
11+
"\n",
12+
"\n",
13+
"Reference > https://huggingface.co/docs/transformers/en/tasks/translation"
14+
]
15+
},
16+
{
17+
"cell_type": "code",
18+
"execution_count": null,
19+
"metadata": {},
20+
"outputs": [],
21+
"source": [
22+
"!pip install transformers[sentencepiece]"
23+
]
24+
},
25+
{
26+
"cell_type": "markdown",
27+
"metadata": {},
28+
"source": [
29+
"##### The tranlation model is processing the source group to target group.\n"
30+
]
31+
},
32+
{
33+
"cell_type": "code",
34+
"execution_count": null,
35+
"metadata": {},
36+
"outputs": [],
37+
"source": [
38+
"from transformers import pipeline\n",
39+
"\n",
40+
"# English to French\n",
41+
"translator = pipeline(\"translation_xx_to_yy\", model=\"google-t5/t5-small\")\n",
42+
"\n",
43+
"## Translate English to French\n",
44+
"text = \"translate English to French: Legumes share resources with nitrogen-fixing bacteria.\" \n",
45+
"\n",
46+
"print(translator(text)) "
47+
]
48+
},
49+
{
50+
"cell_type": "markdown",
51+
"metadata": {},
52+
"source": [
53+
"### Translation TH-ENG model"
54+
]
55+
},
56+
{
57+
"cell_type": "code",
58+
"execution_count": null,
59+
"metadata": {},
60+
"outputs": [],
61+
"source": [
62+
"## Load the pre-trained model\n",
63+
"pipeline = pipeline(\"translation\", model=\"Helsinki-NLP/opus-mt-th-en\")"
64+
]
65+
},
66+
{
67+
"cell_type": "markdown",
68+
"metadata": {},
69+
"source": [
70+
"- Model \"Helsinnki-NLP/opus-mt-th-enn\"\n",
71+
"- Source group : Thai\n",
72+
"- Target group : English"
73+
]
74+
},
75+
{
76+
"cell_type": "code",
77+
"execution_count": null,
78+
"metadata": {},
79+
"outputs": [],
80+
"source": [
81+
"## this model is for Thai to Englist translation\n",
82+
"\n",
83+
"text = \"translate Thai to Englist : สวัสดีครับ ยินดีที่ได้รู้จักครับ\"\n",
84+
"print(pipeline(text))"
85+
]
86+
}
87+
],
88+
"metadata": {
89+
"kernelspec": {
90+
"display_name": "biomedparse",
91+
"language": "python",
92+
"name": "python3"
93+
},
94+
"language_info": {
95+
"codemirror_mode": {
96+
"name": "ipython",
97+
"version": 3
98+
},
99+
"file_extension": ".py",
100+
"mimetype": "text/x-python",
101+
"name": "python",
102+
"nbconvert_exporter": "python",
103+
"pygments_lexer": "ipython3",
104+
"version": "3.9.19"
105+
}
106+
},
107+
"nbformat": 4,
108+
"nbformat_minor": 2
109+
}

ml-app-gradio/3_Speech-2-text.ipynb

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,18 @@
2828
},
2929
{
3030
"cell_type": "code",
31-
"execution_count": 2,
31+
"execution_count": null,
32+
"metadata": {},
33+
"outputs": [],
34+
"source": [
35+
"!pip install transformers"
36+
]
37+
},
38+
{
39+
"cell_type": "code",
40+
"execution_count": null,
3241
"metadata": {},
33-
"outputs": [
34-
{
35-
"name": "stderr",
36-
"output_type": "stream",
37-
"text": [
38-
"/home/badboy-005/anaconda3/envs/biomedparse/lib/python3.9/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
39-
" from .autonotebook import tqdm as notebook_tqdm\n",
40-
"Device set to use cuda:0\n"
41-
]
42-
}
43-
],
42+
"outputs": [],
4443
"source": [
4544
"from transformers import pipeline\n",
4645
"import torch\n",
@@ -69,7 +68,46 @@
6968
"metadata": {},
7069
"outputs": [],
7170
"source": [
72-
"text = pipe(\"audio.mp3\")[\"text\"] # give audio mp3 and transcribe text"
71+
"!pip install pytubefix"
72+
]
73+
},
74+
{
75+
"cell_type": "code",
76+
"execution_count": null,
77+
"metadata": {},
78+
"outputs": [],
79+
"source": [
80+
"# importing packages\n",
81+
"from pytubefix import YouTube\n",
82+
"import os\n",
83+
"\n",
84+
"yt = YouTube(\"https://www.youtube.com/watch?v=IoO07mDIBr4&list=RDIoO07mDIBr4&start_radio=1\")\n",
85+
"\n",
86+
"video = yt.streams.filter(only_audio=True).first()\n",
87+
"os.makedirs(\"audio_example\", exist_ok=True)\n",
88+
"out_file = video.download(output_path=\"audio_example\")\n",
89+
"\n",
90+
"new_file = \"audio_example/audio\" + '.mp3'\n",
91+
"os.rename(out_file, new_file)\n"
92+
]
93+
},
94+
{
95+
"cell_type": "code",
96+
"execution_count": null,
97+
"metadata": {},
98+
"outputs": [],
99+
"source": [
100+
"text = pipe(\"audio_example/audio.mp3\")[\"text\"] # give audio mp3 and transcribe text\n",
101+
"## this would take a while to process"
102+
]
103+
},
104+
{
105+
"cell_type": "code",
106+
"execution_count": null,
107+
"metadata": {},
108+
"outputs": [],
109+
"source": [
110+
"print(text)"
73111
]
74112
}
75113
],

ml-app-gradio/4_Text-2-image.ipynb

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,24 @@
99
"!pip install diffusers"
1010
]
1111
},
12+
{
13+
"cell_type": "markdown",
14+
"metadata": {},
15+
"source": [
16+
"## Text-2-image\n",
17+
"\n",
18+
"- Text-to-image is the task of generating images from input text.\n",
19+
"\n",
20+
"Reference : https://huggingface.co/tasks/text-to-image"
21+
]
22+
},
1223
{
1324
"cell_type": "code",
1425
"execution_count": null,
1526
"metadata": {},
1627
"outputs": [],
1728
"source": [
29+
"# Importing the required libraries\n",
1830
"import gradio as gr\n",
1931
"from diffusers import StableDiffusionPipeline\n",
2032
"import torch"
@@ -48,13 +60,6 @@
4860
"# Launch the interface\n",
4961
"demo.launch()"
5062
]
51-
},
52-
{
53-
"cell_type": "code",
54-
"execution_count": null,
55-
"metadata": {},
56-
"outputs": [],
57-
"source": []
5863
}
5964
],
6065
"metadata": {

ml-app-gradio/2_Image-2-text.ipynb renamed to ml-app-gradio/5_Image-2-text.ipynb

Lines changed: 14 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
"cell_type": "markdown",
1212
"metadata": {},
1313
"source": [
14-
"From the previous notebook, we understand the basic concept of gradio application\n",
15-
"and how to use it. \n",
14+
"- Image to text models output a text from a given image.\n",
1615
"\n",
1716
"In this notebook,we will use the LLaVA model which is the multimodal\n",
1817
"model that can generate text from the image. "
@@ -110,18 +109,10 @@
110109
" max_new_tokens=100\n",
111110
")\n",
112111
"\n",
113-
"output_text = processor.batch_decode(generate_ids, skip_special_tokens=True) # Decode the generated text"
114-
]
115-
},
116-
{
117-
"cell_type": "code",
118-
"execution_count": null,
119-
"metadata": {},
120-
"outputs": [],
121-
"source": [
122-
"print(output_text)\n",
112+
"output_text = processor.batch_decode(generate_ids, skip_special_tokens=True) # Decode the generated text\n",
123113
"\n",
124-
"## The output text is contain \"USER\" input and the generated text from the model"
114+
"## The output text is contain \"USER\" input and the generated text from the model\n",
115+
"print(output_text)"
125116
]
126117
},
127118
{
@@ -146,33 +137,17 @@
146137
},
147138
{
148139
"cell_type": "code",
149-
"execution_count": 6,
140+
"execution_count": null,
150141
"metadata": {},
151142
"outputs": [],
152143
"source": [
144+
"# TODO : Try to put image-2-text in gradio platform and see the output\n",
145+
"\n",
153146
"def generate_description(image, prompt = \"What is shown in this image?\", max_new_tokens=200):\n",
154-
" \n",
155-
" conversation = [\n",
156-
" {\n",
157-
" \"role\": \"user\",\n",
158-
" \"content\": [\n",
159-
" {\"type\": \"image\"},\n",
160-
" {\"type\": \"text\", \"text\": prompt},\n",
161-
" ],\n",
162-
" },\n",
163-
" ]\n",
164-
" prompt = processor.apply_chat_template(conversation, add_generation_prompt=True)\n",
165-
" inputs = processor(\n",
166-
" images=[image],\n",
167-
" text=[prompt],\n",
168-
" return_tensors=\"pt\"\n",
169-
" ).to(device=\"cuda\", dtype=torch.float16)\n",
170-
" generate_ids = model.generate(\n",
171-
" **inputs,\n",
172-
" do_sample=True,\n",
173-
" max_new_tokens=max_new_tokens\n",
174-
" )\n",
175-
" generated_description = processor.batch_decode(generate_ids, skip_special_tokens=True)\n",
147+
" \"\"\"\n",
148+
" Generate a description of the image using the model\n",
149+
" \"\"\"\n",
150+
" ## Guide : use the above code as the reference to write the code\n",
176151
"\n",
177152
" return generated_description[0]"
178153
]
@@ -218,13 +193,14 @@
218193
"import gradio as gr\n",
219194
"\n",
220195
"demo = gr.Interface(\n",
221-
" fn=lambda img, prompt: generate_description(img, prompt),\n",
196+
" fn=lambda img, prompt: ... , ## put the function here\n",
222197
" inputs=[gr.Image(type=\"pil\"),\n",
223-
" gr.Textbox(label=\"prompt\", value=\"What is shown in this image?\", lines=3)], # Changed to numpy\n",
198+
" gr.Textbox(label=\"prompt\", value=\"What is shown in this image?\", lines=3)], \n",
224199
" outputs=[gr.Textbox(label=\"Description\", lines=3)],\n",
225200
" title=\"Image Description using LLaVA\",\n",
226201
" description=\"Upload an image to get a detailed description using LLaVA-1.5-7b\",\n",
227202
")\n",
203+
"\n",
228204
"demo.launch()"
229205
]
230206
},

ml-app-gradio/5_Machine_translation.ipynb

Lines changed: 0 additions & 16 deletions
This file was deleted.

ml-app-gradio/audio_example/audio.mp3

1.61 MB
Binary file not shown.

ml-app-gradio/ุุ6_Chatbot.ipynb

Whitespace-only changes.

0 commit comments

Comments
 (0)