sheonhan commited on
Commit
316e0e6
1 Parent(s): 8262899

udpate gitignore

Browse files
.gitignore CHANGED
@@ -1,4 +1,6 @@
 
1
  flagged/
2
  __pycache__/
3
  .python-version
4
  .DS_Store
 
 
1
+ .ipynb_checkpoints/
2
  flagged/
3
  __pycache__/
4
  .python-version
5
  .DS_Store
6
+ *ipynb
.ipynb_checkpoints/Untitled-checkpoint.ipynb DELETED
@@ -1,6 +0,0 @@
1
- {
2
- "cells": [],
3
- "metadata": {},
4
- "nbformat": 4,
5
- "nbformat_minor": 5
6
- }
 
 
 
 
 
 
 
Untitled.ipynb DELETED
@@ -1,150 +0,0 @@
1
- {
2
- "cells": [
3
- {
4
- "cell_type": "code",
5
- "execution_count": 2,
6
- "id": "f3def174",
7
- "metadata": {},
8
- "outputs": [
9
- {
10
- "name": "stdout",
11
- "output_type": "stream",
12
- "text": [
13
- "Requirement already satisfied: gradio in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (3.13.1)\n",
14
- "Requirement already satisfied: h11<0.13,>=0.11 in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from gradio) (0.12.0)\n",
15
- "Requirement already satisfied: pyyaml in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from gradio) (6.0)\n",
16
- "Requirement already satisfied: httpx in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from gradio) (0.23.3)\n",
17
- "Requirement already satisfied: numpy in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from gradio) (1.21.6)\n",
18
- "Requirement already satisfied: matplotlib in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from gradio) (3.5.3)\n",
19
- "Requirement already satisfied: pillow in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from gradio) (9.4.0)\n",
20
- "Requirement already satisfied: altair in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from gradio) (4.2.0)\n",
21
- "Requirement already satisfied: fsspec in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from gradio) (2023.1.0)\n",
22
- "Requirement already satisfied: jinja2 in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from gradio) (3.1.2)\n",
23
- "Requirement already satisfied: pydantic in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from gradio) (1.10.4)\n",
24
- "Requirement already satisfied: ffmpy in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from gradio) (0.3.0)\n",
25
- "Requirement already satisfied: pydub in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from gradio) (0.25.1)\n",
26
- "Requirement already satisfied: python-multipart in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from gradio) (0.0.5)\n",
27
- "Requirement already satisfied: markdown-it-py[linkify,plugins] in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from gradio) (2.1.0)\n",
28
- "Requirement already satisfied: websockets>=10.0 in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from gradio) (10.4)\n",
29
- "Requirement already satisfied: aiohttp in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from gradio) (3.8.3)\n",
30
- "Requirement already satisfied: uvicorn in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from gradio) (0.20.0)\n",
31
- "Requirement already satisfied: pandas in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from gradio) (1.3.5)\n",
32
- "Requirement already satisfied: orjson in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from gradio) (3.8.5)\n",
33
- "Requirement already satisfied: fastapi in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from gradio) (0.89.1)\n",
34
- "Requirement already satisfied: requests in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from gradio) (2.28.2)\n",
35
- "Requirement already satisfied: pycryptodome in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from gradio) (3.16.0)\n",
36
- "Requirement already satisfied: typing-extensions>=3.7.4 in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from aiohttp->gradio) (4.4.0)\n",
37
- "Requirement already satisfied: yarl<2.0,>=1.0 in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from aiohttp->gradio) (1.8.2)\n",
38
- "Requirement already satisfied: asynctest==0.13.0 in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from aiohttp->gradio) (0.13.0)\n",
39
- "Requirement already satisfied: aiosignal>=1.1.2 in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from aiohttp->gradio) (1.3.1)\n",
40
- "Requirement already satisfied: async-timeout<5.0,>=4.0.0a3 in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from aiohttp->gradio) (4.0.2)\n",
41
- "Requirement already satisfied: charset-normalizer<3.0,>=2.0 in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from aiohttp->gradio) (2.1.1)\n",
42
- "Requirement already satisfied: multidict<7.0,>=4.5 in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from aiohttp->gradio) (6.0.4)\n",
43
- "Requirement already satisfied: attrs>=17.3.0 in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from aiohttp->gradio) (22.2.0)\n",
44
- "Requirement already satisfied: frozenlist>=1.1.1 in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from aiohttp->gradio) (1.3.3)\n",
45
- "Requirement already satisfied: toolz in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from altair->gradio) (0.12.0)\n",
46
- "Requirement already satisfied: entrypoints in /usr/local/Cellar/ipython/8.3.0/libexec/lib/python3.10/site-packages (from altair->gradio) (0.4)\n",
47
- "Requirement already satisfied: jsonschema>=3.0 in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from altair->gradio) (4.17.3)\n",
48
- "Requirement already satisfied: pytz>=2017.3 in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from pandas->gradio) (2022.7.1)\n",
49
- "Requirement already satisfied: python-dateutil>=2.7.3 in /usr/local/Cellar/ipython/8.3.0/libexec/lib/python3.10/site-packages (from pandas->gradio) (2.8.2)\n",
50
- "Requirement already satisfied: starlette==0.22.0 in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from fastapi->gradio) (0.22.0)\n",
51
- "Requirement already satisfied: anyio<5,>=3.4.0 in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from starlette==0.22.0->fastapi->gradio) (3.6.2)\n",
52
- "Requirement already satisfied: sniffio in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from httpx->gradio) (1.3.0)\n",
53
- "Requirement already satisfied: rfc3986[idna2008]<2,>=1.3 in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from httpx->gradio) (1.5.0)\n",
54
- "Requirement already satisfied: certifi in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from httpx->gradio) (2022.12.7)\n",
55
- "Requirement already satisfied: httpcore<0.17.0,>=0.15.0 in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from httpx->gradio) (0.15.0)\n",
56
- "Requirement already satisfied: MarkupSafe>=2.0 in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from jinja2->gradio) (2.1.2)\n",
57
- "Requirement already satisfied: mdurl~=0.1 in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from markdown-it-py[linkify,plugins]->gradio) (0.1.2)\n",
58
- "Requirement already satisfied: mdit-py-plugins in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from markdown-it-py[linkify,plugins]->gradio) (0.3.3)\n",
59
- "Requirement already satisfied: linkify-it-py~=1.0 in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from markdown-it-py[linkify,plugins]->gradio) (1.0.3)\n",
60
- "Requirement already satisfied: cycler>=0.10 in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from matplotlib->gradio) (0.11.0)\n",
61
- "Requirement already satisfied: fonttools>=4.22.0 in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from matplotlib->gradio) (4.38.0)\n",
62
- "Requirement already satisfied: pyparsing>=2.2.1 in /usr/local/Cellar/ipython/8.3.0/libexec/lib/python3.10/site-packages (from matplotlib->gradio) (3.0.8)\n",
63
- "Requirement already satisfied: packaging>=20.0 in /usr/local/Cellar/ipython/8.3.0/libexec/lib/python3.10/site-packages (from matplotlib->gradio) (21.3)\n",
64
- "Requirement already satisfied: kiwisolver>=1.0.1 in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from matplotlib->gradio) (1.4.4)\n"
65
- ]
66
- },
67
- {
68
- "name": "stdout",
69
- "output_type": "stream",
70
- "text": [
71
- "Requirement already satisfied: six>=1.4.0 in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from python-multipart->gradio) (1.16.0)\n",
72
- "Requirement already satisfied: idna<4,>=2.5 in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from requests->gradio) (3.4)\n",
73
- "Requirement already satisfied: urllib3<1.27,>=1.21.1 in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from requests->gradio) (1.26.14)\n",
74
- "Requirement already satisfied: click>=7.0 in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from uvicorn->gradio) (8.1.3)\n",
75
- "Requirement already satisfied: importlib-metadata in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from click>=7.0->uvicorn->gradio) (6.0.0)\n",
76
- "Requirement already satisfied: importlib-resources>=1.4.0 in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from jsonschema>=3.0->altair->gradio) (5.10.2)\n",
77
- "Requirement already satisfied: pyrsistent!=0.17.0,!=0.17.1,!=0.17.2,>=0.14.0 in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from jsonschema>=3.0->altair->gradio) (0.19.3)\n",
78
- "Requirement already satisfied: pkgutil-resolve-name>=1.3.10 in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from jsonschema>=3.0->altair->gradio) (1.3.10)\n",
79
- "Requirement already satisfied: uc-micro-py in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from linkify-it-py~=1.0->markdown-it-py[linkify,plugins]->gradio) (1.0.1)\n",
80
- "Requirement already satisfied: zipp>=3.1.0 in /Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/lib/python3.7/site-packages (from importlib-resources>=1.4.0->jsonschema>=3.0->altair->gradio) (3.11.0)\n",
81
- "\u001b[33mWARNING: You are using pip version 22.0.2; however, version 22.3.1 is available.\n",
82
- "You should consider upgrading via the '/Users/sheonhan/.pyenv/versions/3.7.9/envs/language-demo/bin/python -m pip install --upgrade pip' command.\u001b[0m\u001b[33m\n",
83
- "\u001b[0m"
84
- ]
85
- }
86
- ],
87
- "source": [
88
- "!pip install gradio"
89
- ]
90
- },
91
- {
92
- "cell_type": "code",
93
- "execution_count": 4,
94
- "id": "25f4c73e",
95
- "metadata": {},
96
- "outputs": [
97
- {
98
- "ename": "ModuleNotFoundError",
99
- "evalue": "No module named 'gradio'",
100
- "output_type": "error",
101
- "traceback": [
102
- "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
103
- "\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)",
104
- "Input \u001b[0;32mIn [4]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mgradio\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mgr\u001b[39;00m\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mgreet\u001b[39m(name):\n\u001b[1;32m 4\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mHello \u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;241m+\u001b[39m name \u001b[38;5;241m+\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m!\u001b[39m\u001b[38;5;124m\"\u001b[39m\n",
105
- "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'gradio'"
106
- ]
107
- }
108
- ],
109
- "source": [
110
- "import gradio as gr\n",
111
- "\n",
112
- "def greet(name):\n",
113
- " return \"Hello \" + name + \"!\"\n",
114
- "\n",
115
- "demo = gr.Interface(fn=greet, inputs=\"text\", outputs=\"text\")\n",
116
- "\n",
117
- "demo.launch() "
118
- ]
119
- },
120
- {
121
- "cell_type": "code",
122
- "execution_count": null,
123
- "id": "a740eff3",
124
- "metadata": {},
125
- "outputs": [],
126
- "source": []
127
- }
128
- ],
129
- "metadata": {
130
- "kernelspec": {
131
- "display_name": "Python 3 (ipykernel)",
132
- "language": "python",
133
- "name": "python3"
134
- },
135
- "language_info": {
136
- "codemirror_mode": {
137
- "name": "ipython",
138
- "version": 3
139
- },
140
- "file_extension": ".py",
141
- "mimetype": "text/x-python",
142
- "name": "python",
143
- "nbconvert_exporter": "python",
144
- "pygments_lexer": "ipython3",
145
- "version": "3.10.4"
146
- }
147
- },
148
- "nbformat": 4,
149
- "nbformat_minor": 5
150
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app.py CHANGED
@@ -12,10 +12,6 @@ When comments are created in the community tab, detect the language of the conte
12
  Then, if the detected language is different from the user's language, display an option to translate it.
13
  """
14
 
15
- LANG_ID_API_URL = "https://q5esh83u7boq5qwd.us-east-1.aws.endpoints.huggingface.cloud"
16
- ACCESS_TOKEN = os.environ.get("ACCESS_TOKEN")
17
- headers = {"Authorization": f"Bearer {ACCESS_TOKEN}"}
18
-
19
  model = AutoModelForSeq2SeqLM.from_pretrained("facebook/nllb-200-distilled-600M")
20
  tokenizer = AutoTokenizer.from_pretrained("facebook/nllb-200-distilled-600M")
21
  device = 0 if torch.cuda.is_available() else -1
@@ -30,16 +26,16 @@ language_code_map = {
30
  "Japanese": "jpn_Jpan"
31
  }
32
 
 
33
  def identify_language(text):
34
  model_file = "lid218e.bin"
35
  model_full_path = os.path.join(os.path.dirname(__file__), model_file)
36
  model = fasttext.load_model(model_full_path)
37
- predictions = model.predict(text, k=1) # e.g., (('__label__eng_Latn',), array([0.81148803]))
38
-
39
- PREFIX_LENGTH = 9 # To strip away '__label__' from language code
40
  language_code = predictions[0][0][PREFIX_LENGTH:]
41
  return language_code
42
-
43
 
44
 
45
  def translate(text, src_lang, tgt_lang):
@@ -52,15 +48,13 @@ def translate(text, src_lang, tgt_lang):
52
  return result[0]['translation_text']
53
 
54
 
55
- def query(text, src_lang, tgt_lang):
56
- translation = translate(text, src_lang, tgt_lang)
57
- lang_id_response = requests.post(LANG_ID_API_URL, headers=headers, json={
58
- "inputs": text, "wait_for_model": True, "use_cache": True})
59
- lang_id = lang_id_response.json()[0]
60
-
61
- language_code = identify_language(text)
62
 
63
- return [language_code, translation]
64
 
65
 
66
  examples = [
@@ -73,12 +67,12 @@ examples = [
73
  gr.Interface(
74
  query,
75
  [
76
- gr.Textbox(lines=2),
77
- gr.Radio(["English", "Spanish", "Korean"], value="English", label="Source Language"),
78
- gr.Radio(["French", "German", "Japanese"], value="French", label="Target Language")
79
  ],
80
  outputs=[
81
- gr.Textbox(lines=3, label="Detected Language"),
82
  gr.Textbox(lines=3, label="Translation")
83
  ],
84
  title=title,
 
12
  Then, if the detected language is different from the user's language, display an option to translate it.
13
  """
14
 
 
 
 
 
15
  model = AutoModelForSeq2SeqLM.from_pretrained("facebook/nllb-200-distilled-600M")
16
  tokenizer = AutoTokenizer.from_pretrained("facebook/nllb-200-distilled-600M")
17
  device = 0 if torch.cuda.is_available() else -1
 
26
  "Japanese": "jpn_Jpan"
27
  }
28
 
29
+
30
  def identify_language(text):
31
  model_file = "lid218e.bin"
32
  model_full_path = os.path.join(os.path.dirname(__file__), model_file)
33
  model = fasttext.load_model(model_full_path)
34
+ predictions = model.predict(text, k=1) # e.g., (('__label__eng_Latn',), array([0.81148803]))
35
+
36
+ PREFIX_LENGTH = 9 # To strip away '__label__' from language code
37
  language_code = predictions[0][0][PREFIX_LENGTH:]
38
  return language_code
 
39
 
40
 
41
  def translate(text, src_lang, tgt_lang):
 
48
  return result[0]['translation_text']
49
 
50
 
51
+ def query(text, user_lang):
52
+ detected_lang = identify_language(text)
53
+
54
+ translation = translate(
55
+ text, src_lang, tgt_lang) if detected_lang != user_lang else "User's content language is the same as the language of the input"
 
 
56
 
57
+ return [detected_lang, translation]
58
 
59
 
60
  examples = [
 
67
  gr.Interface(
68
  query,
69
  [
70
+ gr.Textbox(lines=3, label="User Input"),
71
+ gr.Radio(["English", "Spanish", "Korean", "French", "German", "Japanese"],
72
+ value="English", label="User's Content Language"),
73
  ],
74
  outputs=[
75
+ gr.Textbox(lines=1, label="Detected Language"),
76
  gr.Textbox(lines=3, label="Translation")
77
  ],
78
  title=title,