Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import gradio as gr
|
3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
4 |
+
|
5 |
+
# Function to load the model and tokenizer (only needs to run once)
|
6 |
+
def load_model():
|
7 |
+
model_id = "microsoft/bitnet-b1.58-2B-4T"
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
9 |
+
model = AutoModelForCausalLM.from_pretrained(
|
10 |
+
model_id,
|
11 |
+
torch_dtype=torch.bfloat16,
|
12 |
+
device_map="auto" # This will use available GPU if present
|
13 |
+
)
|
14 |
+
return model, tokenizer
|
15 |
+
|
16 |
+
# Load the model and tokenizer
|
17 |
+
print("Loading model, please wait...")
|
18 |
+
model, tokenizer = load_model()
|
19 |
+
print("Model loaded successfully!")
|
20 |
+
|
21 |
+
# List of supported languages
|
22 |
+
SUPPORTED_LANGUAGES = [
|
23 |
+
"English", "Spanish", "French", "German", "Chinese",
|
24 |
+
"Japanese", "Russian", "Arabic", "Portuguese", "Italian"
|
25 |
+
]
|
26 |
+
|
27 |
+
def translate_text(input_text, source_lang, target_lang, max_length=150):
|
28 |
+
"""
|
29 |
+
Translates text from source language to target language using the BitNet model
|
30 |
+
"""
|
31 |
+
if not input_text.strip():
|
32 |
+
return "Please enter some text to translate."
|
33 |
+
|
34 |
+
# Create a translation prompt
|
35 |
+
prompt = f"""Translate the following {source_lang} text to {target_lang}.
|
36 |
+
|
37 |
+
{source_lang} text: {input_text}
|
38 |
+
|
39 |
+
{target_lang} translation:"""
|
40 |
+
|
41 |
+
# Create inputs for the model
|
42 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
43 |
+
|
44 |
+
# Generate translation
|
45 |
+
with torch.no_grad():
|
46 |
+
outputs = model.generate(
|
47 |
+
**inputs,
|
48 |
+
max_new_tokens=max_length,
|
49 |
+
do_sample=False, # Use greedy decoding for translation
|
50 |
+
temperature=0.1, # Low temperature for more deterministic output
|
51 |
+
)
|
52 |
+
|
53 |
+
# Extract only the generated part (the translation)
|
54 |
+
translated_text = tokenizer.decode(outputs[0][inputs['input_ids'].shape[1]:], skip_special_tokens=True)
|
55 |
+
|
56 |
+
return translated_text.strip()
|
57 |
+
|
58 |
+
# Define the Gradio interface
|
59 |
+
def create_translation_interface():
|
60 |
+
with gr.Blocks(title="BitNet Multilingual Translation Tool") as demo:
|
61 |
+
gr.Markdown("# 🌍 BitNet Multilingual Translation Tool")
|
62 |
+
gr.Markdown("A lightweight translation application powered by Microsoft's BitNet b1.58 2B4T model.")
|
63 |
+
|
64 |
+
with gr.Row():
|
65 |
+
with gr.Column():
|
66 |
+
source_lang = gr.Dropdown(
|
67 |
+
choices=SUPPORTED_LANGUAGES,
|
68 |
+
value="English",
|
69 |
+
label="Source Language"
|
70 |
+
)
|
71 |
+
input_text = gr.Textbox(
|
72 |
+
lines=5,
|
73 |
+
placeholder="Enter text to translate...",
|
74 |
+
label="Input Text"
|
75 |
+
)
|
76 |
+
|
77 |
+
with gr.Column():
|
78 |
+
target_lang = gr.Dropdown(
|
79 |
+
choices=SUPPORTED_LANGUAGES,
|
80 |
+
value="Spanish",
|
81 |
+
label="Target Language"
|
82 |
+
)
|
83 |
+
output_text = gr.Textbox(
|
84 |
+
lines=5,
|
85 |
+
label="Translated Text"
|
86 |
+
)
|
87 |
+
|
88 |
+
translate_btn = gr.Button("Translate")
|
89 |
+
translate_btn.click(
|
90 |
+
fn=translate_text,
|
91 |
+
inputs=[input_text, source_lang, target_lang],
|
92 |
+
outputs=output_text
|
93 |
+
)
|
94 |
+
|
95 |
+
# Add some example inputs
|
96 |
+
examples = [
|
97 |
+
["Hello, how are you today?", "English", "Spanish"],
|
98 |
+
["I'd like to learn more about artificial intelligence.", "English", "French"],
|
99 |
+
["The weather is beautiful today.", "English", "German"],
|
100 |
+
["Could you please help me find the nearest restaurant?", "English", "Japanese"],
|
101 |
+
]
|
102 |
+
gr.Examples(examples=examples, inputs=[input_text, source_lang, target_lang])
|
103 |
+
|
104 |
+
gr.Markdown("""
|
105 |
+
## About
|
106 |
+
This application uses Microsoft's BitNet b1.58 2B4T, a 1-bit Large Language Model, for translation tasks.
|
107 |
+
The model runs efficiently on consumer hardware due to its 1-bit architecture, offering significant
|
108 |
+
advantages in memory usage, energy consumption, and latency.
|
109 |
+
|
110 |
+
Note: Translation quality may vary by language pair. This is a demonstration of the lightweight model's capabilities.
|
111 |
+
""")
|
112 |
+
|
113 |
+
return demo
|
114 |
+
|
115 |
+
# Create and launch the Gradio interface
|
116 |
+
if __name__ == "__main__":
|
117 |
+
demo = create_translation_interface()
|
118 |
+
demo.launch(share=True) # Set share=False if you don't want a public link
|