Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,33 +1,28 @@
|
|
1 |
-
import
|
2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
-
import warnings
|
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 |
-
st.write(f"Translation in {target_lang_index}: {translated_text}")
|
31 |
-
|
32 |
-
if __name__ == "__main__":
|
33 |
-
main()
|
|
|
1 |
+
import gradio as gr
|
2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
|
3 |
|
4 |
+
# Load tokenizer and model
|
5 |
+
tokenizer = AutoTokenizer.from_pretrained("Unbabel/TowerBase-13B-v0.1")
|
6 |
+
model = AutoModelForCausalLM.from_pretrained("Unbabel/TowerBase-13B-v0.1", device="cuda" if gr.utils.is_using_gpu() else "cpu", load_in_4bit=True)
|
7 |
+
|
8 |
+
# Define translation function
|
9 |
+
def translate_text(source_lang, target_lang, text):
|
10 |
+
input_text = f"{source_lang}: {text}\n{target_lang}:"
|
11 |
+
inputs = tokenizer(input_text, return_tensors="pt")
|
12 |
+
outputs = model.generate(**inputs, max_length=150)
|
13 |
+
translated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
14 |
+
return translated_text
|
15 |
+
|
16 |
+
# Define interface
|
17 |
+
iface = gr.Interface(
|
18 |
+
fn=translate_text,
|
19 |
+
inputs=[
|
20 |
+
gr.inputs.Dropdown(["English", "Spanish", "Vietnamese", "French", "Portuguese"], label="Source Language"),
|
21 |
+
gr.inputs.Dropdown(["English", "Spanish", "Vietnamese", "French", "Portuguese"], label="Target Language"),
|
22 |
+
gr.inputs.Textbox(lines=5, label="Input Text")
|
23 |
+
],
|
24 |
+
outputs=gr.outputs.Textbox(label="Translated Text")
|
25 |
+
)
|
26 |
+
|
27 |
+
# Run the interface
|
28 |
+
iface.launch(share=True)
|
|
|
|
|
|
|
|