Spaces:
Runtime error
Runtime error
Woo Jun Jung
commited on
Upload 4 files
Browse files- README.md +8 -7
- app.py +192 -0
- hfkr_logo.png +0 -0
- requirements.txt +2 -0
README.md
CHANGED
|
@@ -1,13 +1,14 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
app_file: app.py
|
| 9 |
-
pinned:
|
| 10 |
license: apache-2.0
|
|
|
|
| 11 |
---
|
| 12 |
|
| 13 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: HuggingFace docs i18n
|
| 3 |
+
emoji: 🌍
|
| 4 |
+
colorFrom: yellow
|
| 5 |
+
colorTo: pink
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 3.28.0
|
| 8 |
app_file: app.py
|
| 9 |
+
pinned: true
|
| 10 |
license: apache-2.0
|
| 11 |
+
duplicated_from: Hyeonseo/ChatGPT-ko-translation-prompt
|
| 12 |
---
|
| 13 |
|
| 14 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import subprocess
|
| 2 |
+
import requests
|
| 3 |
+
import string
|
| 4 |
+
import time
|
| 5 |
+
import re
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
import openai
|
| 9 |
+
import gradio as gr
|
| 10 |
+
|
| 11 |
+
def get_content(filepath: str) -> str:
|
| 12 |
+
url = string.Template(
|
| 13 |
+
"https://raw.githubusercontent.com/huggingface/"
|
| 14 |
+
"huggingface_hub/main/docs/source/en/$filepath"
|
| 15 |
+
).safe_substitute(filepath=filepath)
|
| 16 |
+
response = requests.get(url)
|
| 17 |
+
if response.status_code == 200:
|
| 18 |
+
content = response.text
|
| 19 |
+
return content
|
| 20 |
+
else:
|
| 21 |
+
raise ValueError("Failed to retrieve content from the URL.", url)
|
| 22 |
+
|
| 23 |
+
def preprocess_content(content: str) -> str:
|
| 24 |
+
# Extract text to translate from document
|
| 25 |
+
|
| 26 |
+
## ignore top license comment
|
| 27 |
+
to_translate = content[content.find('#'):]
|
| 28 |
+
## remove code blocks from text
|
| 29 |
+
to_translate = re.sub(r'```.*?```', '', to_translate, flags=re.DOTALL)
|
| 30 |
+
## remove markdown tables from text
|
| 31 |
+
to_translate = re.sub(r'^\|.*\|$\n?', '', to_translate, flags=re.MULTILINE)
|
| 32 |
+
## remove empty lines from text
|
| 33 |
+
to_translate = re.sub(r'\n\n+', '\n\n', to_translate)
|
| 34 |
+
|
| 35 |
+
return to_translate
|
| 36 |
+
|
| 37 |
+
def get_full_prompt(language: str, filepath: str) -> str:
|
| 38 |
+
content = get_content(filepath)
|
| 39 |
+
to_translate = preprocess_content(content)
|
| 40 |
+
|
| 41 |
+
prompt = string.Template(
|
| 42 |
+
"What do these sentences about Hugging Face Hub "
|
| 43 |
+
"(a machine learning library) mean in $language? "
|
| 44 |
+
"Please do not translate the word after a 🤗 emoji "
|
| 45 |
+
"as it is a product name.\n```md"
|
| 46 |
+
).safe_substitute(language=language)
|
| 47 |
+
return '\n'.join([prompt, to_translate.strip(), "```"])
|
| 48 |
+
|
| 49 |
+
def split_markdown_sections(markdown: str) -> list:
|
| 50 |
+
# Find all titles using regular expressions
|
| 51 |
+
return re.split(r'^(#+\s+)(.*)$', markdown, flags=re.MULTILINE)[1:]
|
| 52 |
+
# format is like [level, title, content, level, title, content, ...]
|
| 53 |
+
|
| 54 |
+
def get_anchors(divided: list) -> list:
|
| 55 |
+
anchors = []
|
| 56 |
+
# from https://github.com/huggingface/doc-builder/blob/01b262bae90d66e1150cdbf58c83c02733ed4366/src/doc_builder/build_doc.py#L300-L302
|
| 57 |
+
for title in divided[1::3]:
|
| 58 |
+
anchor = re.sub(r"[^a-z0-9\s]+", "", title.lower())
|
| 59 |
+
anchor = re.sub(r"\s{2,}", " ", anchor.strip()).replace(" ", "-")
|
| 60 |
+
anchors.append(f"[[{anchor}]]")
|
| 61 |
+
return anchors
|
| 62 |
+
|
| 63 |
+
def make_scaffold(content: str, to_translate: str) -> string.Template:
|
| 64 |
+
scaffold = content
|
| 65 |
+
for i, text in enumerate(to_translate.split('\n\n')):
|
| 66 |
+
scaffold = scaffold.replace(text, f'$hf_i18n_placeholder{i}', 1)
|
| 67 |
+
return string.Template(scaffold)
|
| 68 |
+
|
| 69 |
+
def fill_scaffold(filepath: str, translated: str) -> list[str]:
|
| 70 |
+
content = get_content(filepath)
|
| 71 |
+
to_translate = preprocess_content(content)
|
| 72 |
+
|
| 73 |
+
scaffold = make_scaffold(content, to_translate)
|
| 74 |
+
divided = split_markdown_sections(to_translate)
|
| 75 |
+
anchors = get_anchors(divided)
|
| 76 |
+
|
| 77 |
+
translated = split_markdown_sections(translated)
|
| 78 |
+
translated[1::3] = [
|
| 79 |
+
f"{korean_title} {anchors[i]}"
|
| 80 |
+
for i, korean_title in enumerate(translated[1::3])
|
| 81 |
+
]
|
| 82 |
+
translated = ''.join([
|
| 83 |
+
''.join(translated[i*3:i*3+3])
|
| 84 |
+
for i in range(len(translated) // 3)
|
| 85 |
+
]).split('\n\n')
|
| 86 |
+
if (newlines := scaffold.template.count('$hf_i18n_placeholder') - len(translated)):
|
| 87 |
+
return [
|
| 88 |
+
content,
|
| 89 |
+
f"Please {'recover' if newlines > 0 else 'remove'} "
|
| 90 |
+
f"{abs(newlines)} incorrectly inserted double newlines."
|
| 91 |
+
]
|
| 92 |
+
translated_doc = scaffold.safe_substitute({
|
| 93 |
+
f"hf_i18n_placeholder{i}": text
|
| 94 |
+
for i, text in enumerate(translated)
|
| 95 |
+
})
|
| 96 |
+
|
| 97 |
+
return [content, translated_doc]
|
| 98 |
+
|
| 99 |
+
def translate_openai(language: str, filepath: str, api_key: str) -> list[str]:
|
| 100 |
+
content = get_content(filepath)
|
| 101 |
+
return [content, "Please use the web UI for now."]
|
| 102 |
+
raise NotImplementedError("Currently debugging output.")
|
| 103 |
+
|
| 104 |
+
openai.api_key = api_key
|
| 105 |
+
prompt = string.Template(
|
| 106 |
+
"What do these sentences about Hugging Face Transformers "
|
| 107 |
+
"(a machine learning library) mean in $language? "
|
| 108 |
+
"Please do not translate the word after a 🤗 emoji "
|
| 109 |
+
"as it is a product name.\n```md"
|
| 110 |
+
).safe_substitute(language=language)
|
| 111 |
+
|
| 112 |
+
to_translate = preprocess_content(content)
|
| 113 |
+
|
| 114 |
+
scaffold = make_scaffold(content, to_translate)
|
| 115 |
+
divided = split_markdown_sections(to_translate)
|
| 116 |
+
anchors = get_anchors(divided)
|
| 117 |
+
|
| 118 |
+
sections = [''.join(divided[i*3:i*3+3]) for i in range(len(divided) // 3)]
|
| 119 |
+
reply = []
|
| 120 |
+
|
| 121 |
+
for i, section in enumerate(sections):
|
| 122 |
+
chat = openai.ChatCompletion.create(
|
| 123 |
+
model = "gpt-3.5-turbo",
|
| 124 |
+
messages=[{
|
| 125 |
+
"role": "user",
|
| 126 |
+
"content": "\n".join([prompt, section, '```'])
|
| 127 |
+
},]
|
| 128 |
+
)
|
| 129 |
+
print(f"{i} out of {len(sections)} complete. Estimated time remaining ~{len(sections) - i} mins")
|
| 130 |
+
|
| 131 |
+
reply.append(chat.choices[0].message.content)
|
| 132 |
+
|
| 133 |
+
translated = split_markdown_sections('\n\n'.join(reply))
|
| 134 |
+
print(translated[1::3], anchors)
|
| 135 |
+
translated[1::3] = [
|
| 136 |
+
f"{korean_title} {anchors[i]}"
|
| 137 |
+
for i, korean_title in enumerate(translated[1::3])
|
| 138 |
+
]
|
| 139 |
+
translated = ''.join([
|
| 140 |
+
''.join(translated[i*3:i*3+3])
|
| 141 |
+
for i in range(len(translated) // 3)
|
| 142 |
+
]).split('\n\n')
|
| 143 |
+
translated_doc = scaffold.safe_substitute({
|
| 144 |
+
f"hf_i18n_placeholder{i}": text
|
| 145 |
+
for i, text in enumerate(translated)
|
| 146 |
+
})
|
| 147 |
+
return translated_doc
|
| 148 |
+
|
| 149 |
+
demo = gr.Blocks()
|
| 150 |
+
|
| 151 |
+
with demo:
|
| 152 |
+
gr.Markdown(
|
| 153 |
+
'<img style="display: block; margin-left: auto; margin-right: auto; height: 10em;"'
|
| 154 |
+
' src="file/hfkr_logo.png"/>\n\n'
|
| 155 |
+
'<h1 style="text-align: center;">HuggingFace i18n made easy</h1>'
|
| 156 |
+
)
|
| 157 |
+
with gr.Row():
|
| 158 |
+
language_input = gr.Textbox(
|
| 159 |
+
value="Korean",
|
| 160 |
+
label=" / ".join([
|
| 161 |
+
"Target language", "langue cible",
|
| 162 |
+
"目标语", "Idioma Objetivo",
|
| 163 |
+
"도착어", "língua alvo"
|
| 164 |
+
])
|
| 165 |
+
)
|
| 166 |
+
filepath_input = gr.Textbox(
|
| 167 |
+
value="tasks/masked_language_modeling.md",
|
| 168 |
+
label="File path of transformers document"
|
| 169 |
+
)
|
| 170 |
+
with gr.Tabs():
|
| 171 |
+
with gr.TabItem("Web UI"):
|
| 172 |
+
prompt_button = gr.Button("Show Full Prompt", variant="primary")
|
| 173 |
+
# TODO: add with_prompt_checkbox so people can freely use other services such as DeepL or Papago.
|
| 174 |
+
gr.Markdown("1. Copy with the button right-hand side and paste into [chat.openai.com](https://chat.openai.com).")
|
| 175 |
+
prompt_output = gr.Textbox(label="Full Prompt", lines=3).style(show_copy_button=True)
|
| 176 |
+
# TODO: add check for segments, indicating whether user should add or remove new lines from their input. (gr.Row)
|
| 177 |
+
gr.Markdown("2. After getting the complete translation, remove randomly inserted newlines on your favorite text editor and paste the result below.")
|
| 178 |
+
ui_translated_input = gr.Textbox(label="Cleaned ChatGPT initial translation")
|
| 179 |
+
fill_button = gr.Button("Fill in scaffold", variant="primary")
|
| 180 |
+
with gr.TabItem("API (Not Implemented)"):
|
| 181 |
+
with gr.Row():
|
| 182 |
+
api_key_input = gr.Textbox(label="Your OpenAI API Key")
|
| 183 |
+
api_call_button = gr.Button("Translate (Call API)", variant="primary")
|
| 184 |
+
with gr.Row():
|
| 185 |
+
content_output = gr.Textbox(label="Original content").style(show_copy_button=True)
|
| 186 |
+
final_output = gr.Textbox(label="Draft for review").style(show_copy_button=True)
|
| 187 |
+
|
| 188 |
+
prompt_button.click(get_full_prompt, inputs=[language_input, filepath_input], outputs=prompt_output)
|
| 189 |
+
fill_button.click(fill_scaffold, inputs=[filepath_input, ui_translated_input], outputs=[content_output, final_output])
|
| 190 |
+
api_call_button.click(translate_openai, inputs=[language_input, filepath_input, api_key_input], outputs=[content_output, final_output])
|
| 191 |
+
|
| 192 |
+
demo.launch()
|
hfkr_logo.png
ADDED
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
openai
|
| 2 |
+
gradio
|