|
import streamlit as st |
|
from transformers import AutoTokenizer, AutoModelForCausalLM |
|
import torch |
|
from huggingface_hub import login |
|
|
|
|
|
|
|
|
|
|
|
model_id = "meta-llama/Meta-Llama-3-8B-Instruct" |
|
tokenizer = AutoTokenizer.from_pretrained(model_id) |
|
model = AutoModelForCausalLM.from_pretrained(model_id) |
|
|
|
|
|
st.title("Chat with LLaMA") |
|
st.write("Enter your message below:") |
|
|
|
|
|
user_input = st.text_input("Your Message:") |
|
|
|
|
|
if 'conversation' not in st.session_state: |
|
st.session_state.conversation = [] |
|
|
|
if st.button("Send"): |
|
if user_input: |
|
|
|
st.session_state.conversation.append({"role": "user", "content": user_input}) |
|
|
|
|
|
conversation_history = "\n".join( |
|
[f"{msg['role']}: {msg['content']}" for msg in st.session_state.conversation] |
|
) |
|
|
|
|
|
input_ids = tokenizer(conversation_history, return_tensors="pt").input_ids |
|
|
|
|
|
with torch.no_grad(): |
|
output = model.generate(input_ids, max_new_tokens=100, do_sample=True) |
|
|
|
|
|
response = tokenizer.decode(output[0], skip_special_tokens=True) |
|
|
|
|
|
st.session_state.conversation.append({"role": "assistant", "content": response}) |
|
|
|
|
|
for msg in st.session_state.conversation: |
|
if msg['role'] == 'user': |
|
st.write(f"**User:** {msg['content']}") |
|
else: |
|
st.write(f"**Assistant:** {msg['content']}") |
|
|
|
|
|
if st.button("Clear Conversation"): |
|
st.session_state.conversation = [] |
|
|