Update app.py
Browse files
app.py
CHANGED
|
@@ -18,101 +18,88 @@ print("Loading model, please wait...")
|
|
| 18 |
model, tokenizer = load_model()
|
| 19 |
print("Model loaded successfully!")
|
| 20 |
|
| 21 |
-
|
| 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=4096):
|
| 28 |
"""
|
| 29 |
-
|
| 30 |
"""
|
| 31 |
-
if not
|
| 32 |
-
return "
|
| 33 |
|
| 34 |
-
# Create a
|
| 35 |
-
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
{target_lang} translation:"""
|
| 40 |
|
| 41 |
# Create inputs for the model
|
| 42 |
-
inputs = tokenizer(
|
| 43 |
|
| 44 |
-
# Generate
|
| 45 |
with torch.no_grad():
|
| 46 |
outputs = model.generate(
|
| 47 |
**inputs,
|
| 48 |
max_new_tokens=max_length,
|
| 49 |
-
do_sample=
|
| 50 |
-
temperature=0.
|
|
|
|
| 51 |
)
|
| 52 |
|
| 53 |
-
# Extract only the generated part (the
|
| 54 |
-
|
| 55 |
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
# Define the Gradio interface
|
| 59 |
-
def
|
| 60 |
-
with gr.Blocks(title="BitNet
|
| 61 |
-
gr.Markdown("#
|
| 62 |
-
gr.Markdown("A lightweight
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 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 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
)
|
| 94 |
|
|
|
|
|
|
|
| 95 |
# Add some example inputs
|
| 96 |
examples = [
|
| 97 |
-
["Hello, how are you today?"
|
| 98 |
-
["
|
| 99 |
-
["
|
| 100 |
-
["
|
| 101 |
]
|
| 102 |
-
gr.Examples(examples=examples, inputs=[
|
| 103 |
|
| 104 |
gr.Markdown("""
|
| 105 |
## About
|
| 106 |
-
This application uses Microsoft's BitNet b1.58 2B4T, a 1-bit Large Language Model, for
|
| 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:
|
| 111 |
""")
|
| 112 |
|
| 113 |
return demo
|
| 114 |
|
| 115 |
# Create and launch the Gradio interface
|
| 116 |
if __name__ == "__main__":
|
| 117 |
-
demo =
|
| 118 |
demo.launch(share=True) # Set share=False if you don't want a public link
|
|
|
|
| 18 |
model, tokenizer = load_model()
|
| 19 |
print("Model loaded successfully!")
|
| 20 |
|
| 21 |
+
def generate_response(message, chat_history, max_length=4096):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
"""
|
| 23 |
+
Generates a response from the BitNet model based on the user's message
|
| 24 |
"""
|
| 25 |
+
if not message.strip():
|
| 26 |
+
return "", chat_history
|
| 27 |
|
| 28 |
+
# Create a chat prompt based on the history and new message
|
| 29 |
+
full_prompt = ""
|
| 30 |
+
for user_msg, bot_msg in chat_history:
|
| 31 |
+
full_prompt += f"User: {user_msg}\nAssistant: {bot_msg}\n\n"
|
| 32 |
|
| 33 |
+
full_prompt += f"User: {message}\nAssistant:"
|
|
|
|
|
|
|
| 34 |
|
| 35 |
# Create inputs for the model
|
| 36 |
+
inputs = tokenizer(full_prompt, return_tensors="pt").to(model.device)
|
| 37 |
|
| 38 |
+
# Generate response
|
| 39 |
with torch.no_grad():
|
| 40 |
outputs = model.generate(
|
| 41 |
**inputs,
|
| 42 |
max_new_tokens=max_length,
|
| 43 |
+
do_sample=True,
|
| 44 |
+
temperature=0.7, # Slightly higher temperature for more creative responses
|
| 45 |
+
top_p=0.95,
|
| 46 |
)
|
| 47 |
|
| 48 |
+
# Extract only the generated part (the response)
|
| 49 |
+
response = tokenizer.decode(outputs[0][inputs['input_ids'].shape[1]:], skip_special_tokens=True)
|
| 50 |
|
| 51 |
+
# Update chat history
|
| 52 |
+
chat_history.append((message, response.strip()))
|
| 53 |
+
|
| 54 |
+
return "", chat_history
|
| 55 |
|
| 56 |
# Define the Gradio interface
|
| 57 |
+
def create_chat_interface():
|
| 58 |
+
with gr.Blocks(title="BitNet Chat Assistant") as demo:
|
| 59 |
+
gr.Markdown("# 💬 BitNet Chat Assistant")
|
| 60 |
+
gr.Markdown("A lightweight chat application powered by Microsoft's BitNet b1.58 2B4T model.")
|
| 61 |
|
| 62 |
+
chatbot = gr.Chatbot(height=400)
|
| 63 |
+
msg = gr.Textbox(
|
| 64 |
+
show_label=False,
|
| 65 |
+
placeholder="Type your message here...",
|
| 66 |
+
container=False
|
| 67 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
+
clear = gr.Button("Clear Conversation")
|
| 70 |
+
|
| 71 |
+
def clear_convo():
|
| 72 |
+
return "", []
|
| 73 |
+
|
| 74 |
+
msg.submit(
|
| 75 |
+
fn=generate_response,
|
| 76 |
+
inputs=[msg, chatbot],
|
| 77 |
+
outputs=[msg, chatbot]
|
| 78 |
)
|
| 79 |
|
| 80 |
+
clear.click(fn=clear_convo, inputs=[], outputs=[msg, chatbot])
|
| 81 |
+
|
| 82 |
# Add some example inputs
|
| 83 |
examples = [
|
| 84 |
+
["Hello, how are you today?"],
|
| 85 |
+
["Can you tell me about artificial intelligence?"],
|
| 86 |
+
["What's your favorite book?"],
|
| 87 |
+
["Write a short poem about technology."],
|
| 88 |
]
|
| 89 |
+
gr.Examples(examples=examples, inputs=[msg])
|
| 90 |
|
| 91 |
gr.Markdown("""
|
| 92 |
## About
|
| 93 |
+
This application uses Microsoft's BitNet b1.58 2B4T, a 1-bit Large Language Model, for conversational AI.
|
| 94 |
The model runs efficiently on consumer hardware due to its 1-bit architecture, offering significant
|
| 95 |
advantages in memory usage, energy consumption, and latency.
|
| 96 |
|
| 97 |
+
Note: This is a demonstration of the lightweight model's capabilities.
|
| 98 |
""")
|
| 99 |
|
| 100 |
return demo
|
| 101 |
|
| 102 |
# Create and launch the Gradio interface
|
| 103 |
if __name__ == "__main__":
|
| 104 |
+
demo = create_chat_interface()
|
| 105 |
demo.launch(share=True) # Set share=False if you don't want a public link
|