Spaces:
No application file
No application file
gradio bigtranslate
Browse files- gradio_bigtranslate.py +64 -0
- requirements.txt +4 -0
gradio_bigtranslate.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""gradio-bigtranslate.ipynb
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colab.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1Rtw0lupjDrxW3bRiuFmxFlxKO40X6AuU
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
# ! pip install gradio
|
| 11 |
+
|
| 12 |
+
# ! pip install transformers
|
| 13 |
+
|
| 14 |
+
from huggingface_hub import notebook_login
|
| 15 |
+
|
| 16 |
+
notebook_login()
|
| 17 |
+
|
| 18 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
# Load the model and tokenizer
|
| 22 |
+
# ! pip install optimum auto-gptq
|
| 23 |
+
|
| 24 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, GPTQConfig # Import necessary modules
|
| 25 |
+
|
| 26 |
+
# Load the model and tokenizer
|
| 27 |
+
model_name = "TheBloke/BigTranslate-13B-GPTQ"
|
| 28 |
+
# Configure GPTQ to disable Exllama and use the CUDA backend
|
| 29 |
+
quantization_config = GPTQConfig(bits=4, disable_exllama=True)
|
| 30 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, quantization_config=quantization_config)
|
| 31 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 32 |
+
|
| 33 |
+
import gradio as gr
|
| 34 |
+
|
| 35 |
+
supported_languages = {
|
| 36 |
+
"English": "en",
|
| 37 |
+
"French": "fr",
|
| 38 |
+
"Spanish": "es",
|
| 39 |
+
"German": "de",
|
| 40 |
+
# Add more languages and their codes as needed
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
def translate_text(input_text, output_language):
|
| 44 |
+
# Prefix the input text with the target language code
|
| 45 |
+
prefixed_input_text = f">>{output_language}<< {input_text}"
|
| 46 |
+
# Tokenize the input text
|
| 47 |
+
inputs = tokenizer(prefixed_input_text, return_tensors="pt")
|
| 48 |
+
# Generate translation
|
| 49 |
+
outputs = model.generate(inputs['input_ids'], max_length=40, num_beams=4, early_stopping=True)
|
| 50 |
+
# Decode the output
|
| 51 |
+
translated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 52 |
+
return translated_text
|
| 53 |
+
|
| 54 |
+
# Create the Gradio interface
|
| 55 |
+
iface = gr.Interface(
|
| 56 |
+
fn=translate_text,
|
| 57 |
+
inputs=[
|
| 58 |
+
gr.Textbox(lines=2, placeholder="Enter text here..."),
|
| 59 |
+
gr.Dropdown(choices=list(supported_languages.keys()), label="Select output language")
|
| 60 |
+
],
|
| 61 |
+
outputs="text"
|
| 62 |
+
)
|
| 63 |
+
# Launch the interface
|
| 64 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
transformers
|
| 3 |
+
optimum
|
| 4 |
+
auto-gptq
|