CoolFace
Apppublic

DevBM/ChatBot-with-llama3

sourceHugging Faceapache-2.0updated 2y agoView on Hugging Face
0likes
app.py41 linesDownload Raw Back to root
1import streamlit as st
2from transformers import pipeline
3from transformers import AutoConfig, pipeline
4from langchain_community.llms import Ollama
5import time
6
7st.title('*ChatBot clone*')
8
9llm = Ollama(model='llama3:latest')
10
11def response_generator(prompt):
12    response = llm.invoke(prompt, stop=['<|eot_id|>'])
13    for word in response.split():
14        yield word + " "
15        time.sleep(0.05)
16
17
18# init chat history
19if "messages" not in st.session_state:
20    st.session_state.messages = []
21
22# display chat history
23for message in st.session_state.messages:
24    with st.chat_message(message['role']):
25        st.markdown(message['content'])
26
27# accept user input
28if prompt := st.chat_input("What is up?"):
29    # add user message to user history
30    st.session_state.messages.append({'role':'user','content':prompt})
31    # display user message
32    with st.chat_message('user'):
33        st.markdown(prompt)
34    
35    # display assistant response
36    with st.chat_message('assistant'):
37        ans = llm.invoke(prompt, stop=['<|eot_id|>'])
38        respose = st.write_stream(response_generator(prompt))
39    st.session_state.messages.append({'role':'assistant', 'content':respose})
40
41