File size: 2,371 Bytes
6646a66
 
 
 
 
 
 
 
f72ae3e
6646a66
 
 
 
 
 
 
 
 
 
 
 
b12fe89
6646a66
 
b12fe89
 
 
6646a66
 
 
 
 
b12fe89
6646a66
 
 
 
b12fe89
6646a66
b12fe89
 
 
 
 
6646a66
 
 
b12fe89
 
 
 
 
 
6646a66
 
 
 
 
 
 
 
 
 
 
 
b12fe89
6646a66
 
 
b12fe89
 
6646a66
b12fe89
6646a66
b12fe89
 
6646a66
b12fe89
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import streamlit as st
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from textblob import TextBlob
import os
from huggingface_hub import login

# Hugging Face Authentication
hf_token = os.getenv("H_API_KEY")

if not hf_token:
    st.error("HUGGINGFACE_TOKEN not found. Please set your Hugging Face token.")
    st.stop()

login(token=hf_token)

# Load Model & Tokenizer
model_name = "meta-llama/Meta-Llama-3-8B"

@st.cache_resource
def load_model():
    tokenizer = AutoTokenizer.from_pretrained(model_name, use_auth_token=hf_token)
    model = AutoModelForCausalLM.from_pretrained(
        model_name,
        torch_dtype=torch.float16,
        device_map="auto",
        use_auth_token=hf_token
    )
    return tokenizer, model

tokenizer, model = load_model()

# Chatbot with Sentiment
def chatbot_with_sentiment(user_input):
    sentiment = TextBlob(user_input).sentiment.polarity
    emotion = "๐Ÿ˜ƒ Positive" if sentiment > 0 else "๐Ÿ˜ž Negative" if sentiment < 0 else "๐Ÿ˜ Neutral"

    inputs = tokenizer(user_input, return_tensors="pt").to(model.device)
    with torch.no_grad():
        output = model.generate(**inputs, max_new_tokens=150)

    generated_tokens = output[0][inputs['input_ids'].shape[-1]:]
    response = tokenizer.decode(generated_tokens, skip_special_tokens=True).strip()

    return emotion, response

# Streamlit UI
st.title("๐Ÿค– LLaMA 3 Chatbot with Sentiment Analysis")
st.write("Ask anything, and see the sentiment of your input.")

# Clear Chat Button
if st.button("๐Ÿงน Clear Chat"):
    st.session_state.messages = []

# Chat History
if "messages" not in st.session_state:
    st.session_state.messages = []

for msg in st.session_state.messages:
    with st.chat_message(msg["role"]):
        st.markdown(msg["content"])

# User Input
user_input = st.chat_input("Type your message here...")
if user_input:
    # Add user message
    st.session_state.messages.append({"role": "user", "content": user_input})
    with st.chat_message("user"):
        st.markdown(user_input)

    # Generate response
    emotion, ai_response = chatbot_with_sentiment(user_input)
    final_response = f"{emotion}\n\n{ai_response}"

    # Add assistant message
    st.session_state.messages.append({"role": "assistant", "content": final_response})
    with st.chat_message("assistant"):
        st.markdown(final_response)