Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Custom CSS for styling
|
| 6 |
+
st.markdown("""
|
| 7 |
+
<style>
|
| 8 |
+
.main {
|
| 9 |
+
background-color: #f5f5f5;
|
| 10 |
+
}
|
| 11 |
+
.stTextInput>div>div>input {
|
| 12 |
+
background-color: #ffffff;
|
| 13 |
+
color: #000000;
|
| 14 |
+
}
|
| 15 |
+
.stButton>button {
|
| 16 |
+
background-color: #4CAF50;
|
| 17 |
+
color: white;
|
| 18 |
+
border-radius: 5px;
|
| 19 |
+
border: none;
|
| 20 |
+
padding: 10px 24px;
|
| 21 |
+
}
|
| 22 |
+
.stButton>button:hover {
|
| 23 |
+
background-color: #45a049;
|
| 24 |
+
}
|
| 25 |
+
.title {
|
| 26 |
+
font-size: 2.5em;
|
| 27 |
+
color: #2c3e50;
|
| 28 |
+
text-align: center;
|
| 29 |
+
margin-bottom: 0.5em;
|
| 30 |
+
}
|
| 31 |
+
.sidebar .sidebar-content {
|
| 32 |
+
background-color: #2c3e50;
|
| 33 |
+
color: white;
|
| 34 |
+
}
|
| 35 |
+
</style>
|
| 36 |
+
""", unsafe_allow_html=True)
|
| 37 |
+
|
| 38 |
+
# App Title
|
| 39 |
+
st.markdown('<p class="title">💬 Fine-Tuned LLM Chat</p>', unsafe_allow_html=True)
|
| 40 |
+
|
| 41 |
+
# Sidebar for settings
|
| 42 |
+
with st.sidebar:
|
| 43 |
+
st.header("⚙️ Settings")
|
| 44 |
+
model_name = st.selectbox(
|
| 45 |
+
"Select Model",
|
| 46 |
+
["mistralai/Mistral-7B-v0.1", "meta-llama/Llama-2-7b-chat-hf"],
|
| 47 |
+
help="Choose a pre-trained model to fine-tune."
|
| 48 |
+
)
|
| 49 |
+
temperature = st.slider(
|
| 50 |
+
"Temperature",
|
| 51 |
+
min_value=0.1,
|
| 52 |
+
max_value=1.0,
|
| 53 |
+
value=0.7,
|
| 54 |
+
help="Controls randomness (lower = more deterministic)."
|
| 55 |
+
)
|
| 56 |
+
max_length = st.slider(
|
| 57 |
+
"Max Response Length",
|
| 58 |
+
min_value=50,
|
| 59 |
+
max_value=500,
|
| 60 |
+
value=150,
|
| 61 |
+
help="Maximum number of tokens in the response."
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
# Load model (cached to avoid reloading)
|
| 65 |
+
@st.cache_resource
|
| 66 |
+
def load_model(model_name):
|
| 67 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 68 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16)
|
| 69 |
+
return tokenizer, model
|
| 70 |
+
|
| 71 |
+
tokenizer, model = load_model(model_name)
|
| 72 |
+
|
| 73 |
+
# Initialize chat history
|
| 74 |
+
if "messages" not in st.session_state:
|
| 75 |
+
st.session_state.messages = []
|
| 76 |
+
|
| 77 |
+
# Display chat messages
|
| 78 |
+
for message in st.session_state.messages:
|
| 79 |
+
with st.chat_message(message["role"]):
|
| 80 |
+
st.markdown(message["content"])
|
| 81 |
+
|
| 82 |
+
# Chat input
|
| 83 |
+
if prompt := st.chat_input("Ask me anything..."):
|
| 84 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 85 |
+
with st.chat_message("user"):
|
| 86 |
+
st.markdown(prompt)
|
| 87 |
+
|
| 88 |
+
# Generate response
|
| 89 |
+
with st.chat_message("assistant"):
|
| 90 |
+
with st.spinner("Thinking..."):
|
| 91 |
+
inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
|
| 92 |
+
outputs = model.generate(
|
| 93 |
+
**inputs,
|
| 94 |
+
max_new_tokens=max_length,
|
| 95 |
+
temperature=temperature,
|
| 96 |
+
do_sample=True
|
| 97 |
+
)
|
| 98 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 99 |
+
st.markdown(response)
|
| 100 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|