Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,44 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
-
#
|
| 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 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
chatbot = gr.Chatbot()
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
msg = gr.Textbox(visible=False, elem_id="quill_input")
|
| 37 |
-
submit_btn = gr.Button("Submit Hidden", visible=False, elem_id="quill_hidden_submit")
|
| 38 |
state = gr.State([])
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
demo.launch(share=True,
|
|
|
|
| 1 |
+
# llama158_chatbot.py
|
| 2 |
+
|
| 3 |
+
# π§ͺ INSTALLATION (run this separately in terminal before launching)
|
| 4 |
+
# pip install torch --index-url https://download.pytorch.org/whl/cu121
|
| 5 |
+
# pip install git+https://github.com/huggingface/transformers.git@refs/pull/33410/head
|
| 6 |
+
# pip install gradio
|
| 7 |
+
|
| 8 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 9 |
+
import torch
|
| 10 |
import gradio as gr
|
| 11 |
|
| 12 |
+
# π§ Load tokenizer and model
|
| 13 |
+
model_id = "HF1BitLLM/Llama3-8B-1.58-100B-tokens"
|
| 14 |
+
tokenizer_id = "meta-llama/Meta-Llama-3-8B-Instruct"
|
| 15 |
+
|
| 16 |
+
print("π Loading tokenizer...")
|
| 17 |
+
tokenizer = AutoTokenizer.from_pretrained(tokenizer_id)
|
| 18 |
+
|
| 19 |
+
print("π§ Loading 1.58-bit model...")
|
| 20 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 21 |
+
model_id,
|
| 22 |
+
device_map="auto",
|
| 23 |
+
torch_dtype=torch.bfloat16 # Ensure GPU supports BF16 (e.g. A100/4090)
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
# π£οΈ Chat function
|
| 27 |
+
def chat(user_input, history):
|
| 28 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 29 |
+
|
| 30 |
+
# Assemble prompt from history
|
| 31 |
+
full_input = ""
|
| 32 |
+
for turn in history:
|
| 33 |
+
full_input += f"User: {turn[0]}\nAssistant: {turn[1]}\n"
|
| 34 |
+
full_input += f"User: {user_input}\nAssistant:"
|
| 35 |
+
|
| 36 |
+
# Tokenize and truncate if needed
|
| 37 |
+
input_ids = tokenizer.encode(full_input, return_tensors="pt", truncation=True, max_length=4000).to(device)
|
| 38 |
+
model.to(device)
|
| 39 |
+
|
| 40 |
+
try:
|
| 41 |
+
with torch.no_grad():
|
| 42 |
+
output = model.generate(
|
| 43 |
+
input_ids,
|
| 44 |
+
max_new_tokens=100,
|
| 45 |
+
do_sample=True,
|
| 46 |
+
temperature=0.7
|
| 47 |
+
)
|
| 48 |
+
response = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 49 |
+
reply = response.split("Assistant:")[-1].strip()
|
| 50 |
+
except Exception as e:
|
| 51 |
+
reply = f"β οΈ Error: {str(e)}"
|
| 52 |
+
|
| 53 |
+
history.append((user_input, reply))
|
| 54 |
+
return reply, history
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
# π§πΎββοΈ Launch Gradio Chat Interface
|
| 58 |
+
with gr.Blocks(title="π¦ Llama3-8B-1.58 Chatbot") as demo:
|
| 59 |
+
gr.Markdown("## π¦ Llama3-8B-1.58 Chatbot\nChat with a super-efficient 1-bit model!")
|
| 60 |
chatbot = gr.Chatbot()
|
| 61 |
+
msg = gr.Textbox(label="Your message", placeholder="Ask me anything...")
|
| 62 |
+
clear = gr.Button("Clear")
|
| 63 |
+
|
|
|
|
|
|
|
| 64 |
state = gr.State([])
|
| 65 |
|
| 66 |
+
def respond(user_message, history):
|
| 67 |
+
reply, new_history = chat(user_message, history)
|
| 68 |
+
return new_history, new_history
|
| 69 |
+
|
| 70 |
+
msg.submit(respond, [msg, state], [chatbot, state])
|
| 71 |
+
clear.click(lambda: ([], []), None, [chatbot, state])
|
| 72 |
|
| 73 |
+
demo.launch(share=True,debug=True)
|