Spaces:
Sleeping
Sleeping
initial set up
Browse files- app.py +134 -0
- requirements.txt +7 -0
app.py
ADDED
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from langchain_huggingface import HuggingFaceEndpoint
|
3 |
+
import streamlit as st
|
4 |
+
from transformers import pipeline
|
5 |
+
from langchain_core.prompts import PromptTemplate
|
6 |
+
from langchain_core.output_parsers import StrOutputParser
|
7 |
+
|
8 |
+
# Chat parameters
|
9 |
+
first_ia_message = "Hello, there! How can I help you today?"
|
10 |
+
system_message = "You are a friendly AI conversing with a human user."
|
11 |
+
text_placeholder = "Enter your text here."
|
12 |
+
text_waiting_ai_response = "Thinking..."
|
13 |
+
max_response_length = 256
|
14 |
+
reset_button_label = "Reset Chat History"
|
15 |
+
|
16 |
+
# Models and Pipeline
|
17 |
+
model_id="mistralai/Mistral-7B-Instruct-v0.3"
|
18 |
+
translation_model_id = "Helsinki-NLP/opus-mt-tc-big-en-pt"
|
19 |
+
|
20 |
+
translation_pipeline = pipeline(
|
21 |
+
"translation_en_to_pt",
|
22 |
+
model=translation_model_id,
|
23 |
+
token=os.getenv("HF_TOKEN")
|
24 |
+
)
|
25 |
+
|
26 |
+
def get_llm_hf_inference(model_id=model_id, max_new_tokens=128, temperature=0.1):
|
27 |
+
llm = HuggingFaceEndpoint(
|
28 |
+
repo_id=model_id,
|
29 |
+
max_new_tokens=max_new_tokens,
|
30 |
+
temperature=temperature,
|
31 |
+
token = os.getenv("HF_TOKEN")
|
32 |
+
)
|
33 |
+
return llm
|
34 |
+
|
35 |
+
|
36 |
+
def translate_to_portuguese(text):
|
37 |
+
translation = translation_pipeline(text)
|
38 |
+
return translation[0]['translation_text']
|
39 |
+
|
40 |
+
# Configure the Streamlit app
|
41 |
+
st.set_page_config(page_title="Personal ChatBot", page_icon="🤗")
|
42 |
+
st.title("Personal ChatBot")
|
43 |
+
st.markdown(f"* A simple chatbot with {model_id} and {translation_model_id}.*")
|
44 |
+
|
45 |
+
# Initialize session state for avatars
|
46 |
+
if "avatars" not in st.session_state:
|
47 |
+
st.session_state.avatars = {'user': None, 'assistant': None}
|
48 |
+
|
49 |
+
# Initialize session state for user text input
|
50 |
+
if 'user_text' not in st.session_state:
|
51 |
+
st.session_state.user_text = None
|
52 |
+
|
53 |
+
# Initialize session state for model parameters
|
54 |
+
if "max_response_length" not in st.session_state:
|
55 |
+
st.session_state.max_response_length = max_response_length
|
56 |
+
|
57 |
+
# Sidebar for settings
|
58 |
+
with st.sidebar:
|
59 |
+
st.header("System Settings")
|
60 |
+
|
61 |
+
# AI Settings
|
62 |
+
st.session_state.system_message = st.text_area(
|
63 |
+
"System Message", value=system_message
|
64 |
+
)
|
65 |
+
st.session_state.starter_message = st.text_area(
|
66 |
+
'First AI Message', value=first_ia_message
|
67 |
+
)
|
68 |
+
# Model Settings
|
69 |
+
st.session_state.max_response_length = st.number_input(
|
70 |
+
"Max Response Length", value=max_response_length
|
71 |
+
)
|
72 |
+
# Reset Chat History
|
73 |
+
reset_history = st.button(reset_button_label)
|
74 |
+
|
75 |
+
# Initialize or reset chat history
|
76 |
+
if "chat_history" not in st.session_state or reset_history:
|
77 |
+
st.session_state.chat_history = [{"role": "assistant", "content": st.session_state.starter_message}]
|
78 |
+
|
79 |
+
def get_response(system_message, chat_history, user_text,
|
80 |
+
eos_token_id=['User'], max_new_tokens=256, get_llm_hf_kws={}):
|
81 |
+
# Set up model with token and temperature
|
82 |
+
hf = get_llm_hf_inference(max_new_tokens=max_new_tokens, temperature=0.1)
|
83 |
+
|
84 |
+
# Create the prompt template
|
85 |
+
prompt = PromptTemplate.from_template(
|
86 |
+
(
|
87 |
+
"[INST] {system_message}"
|
88 |
+
"\nCurrent Conversation:\n{chat_history}\n\n"
|
89 |
+
"\nUser: {user_text}.\n [/INST]"
|
90 |
+
"\nAI:"
|
91 |
+
)
|
92 |
+
)
|
93 |
+
|
94 |
+
# Response template
|
95 |
+
chat = prompt | hf.bind(skip_prompt=True) | StrOutputParser(output_key='content')
|
96 |
+
|
97 |
+
response = chat.invoke(input=dict(system_message=system_message, user_text=user_text, chat_history=chat_history))
|
98 |
+
response = response.split("AI:")[-1]
|
99 |
+
response = translate_to_portuguese(response)
|
100 |
+
chat_history.append({'role': 'user', 'content': user_text})
|
101 |
+
chat_history.append({'role': 'assistant', 'content': response})
|
102 |
+
return response, chat_history
|
103 |
+
|
104 |
+
# Chat interface
|
105 |
+
chat_interface = st.container(border=True)
|
106 |
+
with chat_interface:
|
107 |
+
output_container = st.container()
|
108 |
+
st.session_state.user_text = st.chat_input(placeholder=text_placeholder)
|
109 |
+
|
110 |
+
# Display chat messages
|
111 |
+
with output_container:
|
112 |
+
for message in st.session_state.chat_history:
|
113 |
+
if message['role'] == 'system':
|
114 |
+
continue
|
115 |
+
with st.chat_message(message['role'],
|
116 |
+
avatar=st.session_state['avatars'][message['role']]):
|
117 |
+
st.markdown(message['content'])
|
118 |
+
|
119 |
+
# User new text:
|
120 |
+
if st.session_state.user_text:
|
121 |
+
with st.chat_message("user",
|
122 |
+
avatar=st.session_state.avatars['user']):
|
123 |
+
st.markdown(st.session_state.user_text)
|
124 |
+
with st.chat_message("assistant",
|
125 |
+
avatar=st.session_state.avatars['assistant']):
|
126 |
+
|
127 |
+
with st.spinner(text_waiting_ai_response):
|
128 |
+
response, st.session_state.chat_history = get_response(
|
129 |
+
system_message=st.session_state.system_message,
|
130 |
+
user_text=st.session_state.user_text,
|
131 |
+
chat_history=st.session_state.chat_history,
|
132 |
+
max_new_tokens=st.session_state.max_response_length,
|
133 |
+
)
|
134 |
+
st.markdown(response)
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
huggingface_hub==0.25.2
|
2 |
+
transformers
|
3 |
+
torch
|
4 |
+
gradio
|
5 |
+
accelerate
|
6 |
+
bitsandbytes
|
7 |
+
|