junaidbaber's picture
Update app.py
ef628bc verified
raw
history blame
1.97 kB
from huggingface_hub import login
import os
token = os.environ.get("hf")
login(token)
import streamlit as st
from transformers import pipeline
import torch
# Model ID
MODEL_ID = "meta-llama/Meta-Llama-3.1-8B-Instruct"
@st.cache_resource
def load_pipeline():
try:
st.write("Loading the instruct pipeline...")
instruct_pipeline = pipeline(
"text-generation",
model=MODEL_ID,
model_kwargs={"torch_dtype": torch.bfloat16},
device_map="auto",
)
st.write("Pipeline successfully loaded.")
return instruct_pipeline
except Exception as e:
st.error(f"Error loading pipeline: {e}")
return None
# Load the pipeline
instruct_pipeline = load_pipeline()
# Streamlit UI
st.title("Instruction Chatbot")
st.write("Chat with the instruction-tuned model!")
if instruct_pipeline is None:
st.error("Pipeline failed to load. Please check the configuration.")
else:
# Message-based interaction
system_message = st.text_area("System Message", value="You are a helpful assistant.", height=100)
user_input = st.text_input("User:", placeholder="Ask a question or provide an instruction...")
if st.button("Send"):
if user_input.strip():
try:
messages = [
{"role": "system", "content": system_message},
{"role": "user", "content": user_input},
]
# Generate response
outputs = instruct_pipeline(
messages,
max_new_tokens=150, # Limit response length
)
# Display the generated response
response = outputs[0]["generated_text"]
st.write(f"**Assistant:** {response}")
except Exception as e:
st.error(f"Error generating response: {e}")
else:
st.warning("Please enter a valid message.")