File size: 1,968 Bytes
7077c22
7ec6449
ef628bc
f72e6fb
 
ef628bc
 
 
 
 
 
 
 
 
 
7077c22
ef628bc
 
 
 
 
 
7077c22
ef628bc
 
7077c22
ef628bc
 
7077c22
ef628bc
 
7077c22
 
ef628bc
 
7077c22
ef628bc
 
7077c22
ef628bc
 
 
7077c22
 
 
 
ef628bc
 
 
 
 
 
 
 
7077c22
ef628bc
 
 
7077c22
 
 
ef628bc
 
1
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

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.")