openfree commited on
Commit
763e81e
·
verified ·
1 Parent(s): 66dbc43

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -62
app.py CHANGED
@@ -18,101 +18,88 @@ 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=4096):
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
 
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