Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,147 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig, pipeline
|
3 |
+
import streamlit as st
|
4 |
+
|
5 |
+
# Define the model repository
|
6 |
+
REPO_NAME = 'schuler/experimental-JP47D20'
|
7 |
+
|
8 |
+
# Load tokenizer and model
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained(REPO_NAME, trust_remote_code=True)
|
10 |
+
generator_conf = GenerationConfig.from_pretrained(REPO_NAME)
|
11 |
+
model = AutoModelForCausalLM.from_pretrained(REPO_NAME, trust_remote_code=True)
|
12 |
+
generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
13 |
+
|
14 |
+
# Configure the Streamlit app
|
15 |
+
st.set_page_config(page_title="HuggingFace ChatBot", page_icon="π€")
|
16 |
+
st.title("Personal HuggingFace ChatBot")
|
17 |
+
st.markdown(f"*This is a simple chatbot that uses the HuggingFace transformers library to generate responses to your text input. It uses the {REPO_NAME} model.*")
|
18 |
+
|
19 |
+
# Initialize session state for avatars
|
20 |
+
if "avatars" not in st.session_state:
|
21 |
+
st.session_state.avatars = {'user': None, 'assistant': None}
|
22 |
+
|
23 |
+
# Initialize session state for user text input
|
24 |
+
if 'user_text' not in st.session_state:
|
25 |
+
st.session_state.user_text = None
|
26 |
+
|
27 |
+
# Initialize session state for model parameters
|
28 |
+
if "max_response_length" not in st.session_state:
|
29 |
+
st.session_state.max_response_length = 256
|
30 |
+
|
31 |
+
if "system_message" not in st.session_state:
|
32 |
+
st.session_state.system_message = "You are a friendly AI conversing with a human user."
|
33 |
+
|
34 |
+
if "starter_message" not in st.session_state:
|
35 |
+
st.session_state.starter_message = "Hello, there! How can I help you today?"
|
36 |
+
|
37 |
+
# Sidebar for settings
|
38 |
+
with st.sidebar:
|
39 |
+
st.header("System Settings")
|
40 |
+
|
41 |
+
# AI Settings
|
42 |
+
st.session_state.system_message = st.text_area(
|
43 |
+
"System Message", value=st.session_state.system_message
|
44 |
+
)
|
45 |
+
st.session_state.starter_message = st.text_area(
|
46 |
+
'First AI Message', value=st.session_state.starter_message
|
47 |
+
)
|
48 |
+
|
49 |
+
# Model Settings
|
50 |
+
st.session_state.max_response_length = st.number_input(
|
51 |
+
"Max Response Length", value=st.session_state.max_response_length
|
52 |
+
)
|
53 |
+
|
54 |
+
# Avatar Selection
|
55 |
+
st.markdown("*Select Avatars:*")
|
56 |
+
col1, col2 = st.columns(2)
|
57 |
+
with col1:
|
58 |
+
st.session_state.avatars['assistant'] = st.selectbox(
|
59 |
+
"AI Avatar", options=["π€", "π¬", "π€"], index=0
|
60 |
+
)
|
61 |
+
with col2:
|
62 |
+
st.session_state.avatars['user'] = st.selectbox(
|
63 |
+
"User Avatar", options=["π€", "π±ββοΈ", "π¨πΎ", "π©", "π§πΎ"], index=0
|
64 |
+
)
|
65 |
+
# Reset Chat History
|
66 |
+
reset_history = st.button("Reset Chat History")
|
67 |
+
|
68 |
+
# Initialize or reset chat history
|
69 |
+
if "chat_history" not in st.session_state or reset_history:
|
70 |
+
st.session_state.chat_history = [{"role": "assistant", "content": st.session_state.starter_message}]
|
71 |
+
|
72 |
+
def get_response(system_message, chat_history, user_text, max_new_tokens=256):
|
73 |
+
"""
|
74 |
+
Generates a response from the chatbot model.
|
75 |
+
|
76 |
+
Args:
|
77 |
+
system_message (str): The system message for the conversation.
|
78 |
+
chat_history (list): The list of previous chat messages.
|
79 |
+
user_text (str): The user's input text.
|
80 |
+
max_new_tokens (int): The maximum number of new tokens to generate.
|
81 |
+
|
82 |
+
Returns:
|
83 |
+
tuple: A tuple containing the generated response and the updated chat history.
|
84 |
+
"""
|
85 |
+
# Build the conversation prompt
|
86 |
+
prompt = ""
|
87 |
+
# f"{system_message}\nCurrent Conversation:\n"
|
88 |
+
for message in chat_history:
|
89 |
+
role = "<|assistant|>" if message['role'] == 'assistant' else "<|user|>"
|
90 |
+
prompt += f"\n{role}\n{message['content']}\n"
|
91 |
+
prompt += f"\n<|user|>\n {user_text}\n<|assistant|>\"
|
92 |
+
|
93 |
+
# Generate the response
|
94 |
+
response_output = generator(
|
95 |
+
prompt,
|
96 |
+
generation_config=generator_conf,
|
97 |
+
max_new_tokens=max_new_tokens,
|
98 |
+
do_sample=True,
|
99 |
+
top_p=0.5,
|
100 |
+
repetition_penalty=1.2
|
101 |
+
)
|
102 |
+
|
103 |
+
generated_text = response_output[0]['generated_text']
|
104 |
+
|
105 |
+
# Extract the assistant's response
|
106 |
+
assistant_response = generated_text[len(prompt):].strip()
|
107 |
+
|
108 |
+
# Update the chat history
|
109 |
+
chat_history.append({'role': 'user', 'content': user_text})
|
110 |
+
chat_history.append({'role': 'assistant', 'content': assistant_response})
|
111 |
+
|
112 |
+
return assistant_response, chat_history
|
113 |
+
|
114 |
+
# Chat interface
|
115 |
+
chat_interface = st.container()
|
116 |
+
with chat_interface:
|
117 |
+
output_container = st.container()
|
118 |
+
st.session_state.user_text = st.chat_input(placeholder="Enter your text here.")
|
119 |
+
|
120 |
+
# Display chat messages
|
121 |
+
with output_container:
|
122 |
+
for message in st.session_state.chat_history:
|
123 |
+
if message['role'] == 'system':
|
124 |
+
continue
|
125 |
+
with st.chat_message(message['role'], avatar=st.session_state.avatars[message['role']]):
|
126 |
+
st.markdown(message['content'])
|
127 |
+
|
128 |
+
# When the user enters new text
|
129 |
+
if st.session_state.user_text:
|
130 |
+
# Display the user's message
|
131 |
+
with st.chat_message("user", avatar=st.session_state.avatars['user']):
|
132 |
+
st.markdown(st.session_state.user_text)
|
133 |
+
|
134 |
+
# Display a spinner while generating the response
|
135 |
+
with st.chat_message("assistant", avatar=st.session_state.avatars['assistant']):
|
136 |
+
with st.spinner("Thinking..."):
|
137 |
+
# Generate the assistant's response
|
138 |
+
response, st.session_state.chat_history = get_response(
|
139 |
+
system_message=st.session_state.system_message,
|
140 |
+
user_text=st.session_state.user_text,
|
141 |
+
chat_history=st.session_state.chat_history,
|
142 |
+
max_new_tokens=st.session_state.max_response_length,
|
143 |
+
)
|
144 |
+
st.markdown(response)
|
145 |
+
|
146 |
+
# Clear the user input
|
147 |
+
st.session_state.user_text = None
|