|
import streamlit as st |
|
from streamlit_chat import message |
|
import tempfile |
|
from langchain.document_loaders.csv_loader import CSVLoader |
|
from langchain.embeddings import HuggingFaceEmbeddings |
|
from langchain.vectorstores import FAISS |
|
from langchain.llms import CTransformers |
|
from langchain.chains import ConversationalRetrievalChain |
|
from dl_hf_model import dl_hf_model |
|
from ctransformers import AutoModelForCausalLM |
|
from langchain_g4f import G4FLLM |
|
from g4f import Provider, models |
|
import requests |
|
|
|
DB_FAISS_PATH = 'vectorstore/db_faiss' |
|
|
|
|
|
def load_llm(): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
llm = G4FLLM( |
|
model=models.gpt_35_turbo, |
|
provider=Provider.DeepAi, |
|
) |
|
return llm |
|
hide_streamlit_style = """ |
|
<style> |
|
#MainMenu {visibility: hidden;} |
|
footer {visibility: hidden;} |
|
</style> |
|
""" |
|
st.markdown(hide_streamlit_style, unsafe_allow_html=True) |
|
|
|
|
|
st.title("ZendoηΎε₯³γγ£γγγγγ―γΉ") |
|
|
|
csv_url = "https://huggingface.co/spaces/uyen13/chatgirl/raw/main/testchatdata.csv" |
|
|
|
|
|
|
|
tmp_file_path = "testchatdata.csv" |
|
|
|
|
|
response = requests.get(csv_url) |
|
if response.status_code == 200: |
|
with open(tmp_file_path, 'wb') as file: |
|
file.write(response.content) |
|
else: |
|
raise Exception(f"Failed to download the CSV file from {csv_url}") |
|
|
|
|
|
loader = CSVLoader(file_path=tmp_file_path, encoding="utf-8", csv_args={'delimiter': ','}) |
|
data = loader.load() |
|
|
|
|
|
embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2', model_kwargs={'device': 'cpu'}) |
|
|
|
|
|
db = FAISS.from_documents(data, embeddings) |
|
db.save_local(DB_FAISS_PATH) |
|
|
|
|
|
|
|
llm = load_llm() |
|
|
|
|
|
chain = ConversationalRetrievalChain.from_llm(llm=llm, retriever=db.as_retriever()) |
|
|
|
|
|
def conversational_chat(query): |
|
result = chain({"question": query, "chat_history": st.session_state['history']}) |
|
st.session_state['history'].append((query, result["answer"])) |
|
return result["answer"] |
|
|
|
|
|
if 'history' not in st.session_state: |
|
st.session_state['history'] = [] |
|
|
|
|
|
if 'generated' not in st.session_state: |
|
st.session_state['generated'] = ["γγγ«γ‘γ―οΌzendoηΎε₯³γ§γγδ½γγζ’γγ§γγοΌ... π€"] |
|
|
|
if 'past' not in st.session_state: |
|
st.session_state['past'] = ["γγ£γγγ―γγγγ"] |
|
|
|
|
|
response_container = st.container() |
|
container = st.container() |
|
|
|
|
|
with container: |
|
with st.form(key='my_form', clear_on_submit=True): |
|
user_input = st.text_input("ChatBox", placeholder="θ³ͺεγγθ¨ε
₯γγ γγ... ", key='input') |
|
submit_button = st.form_submit_button(label='Send') |
|
|
|
if submit_button and user_input: |
|
output = conversational_chat(user_input) |
|
st.session_state['past'].append(user_input) |
|
st.session_state['generated'].append(output) |
|
|
|
|
|
if st.session_state['generated']: |
|
with response_container: |
|
for i in range(len(st.session_state['generated'])): |
|
message(st.session_state["past"][i], is_user=True, key=str(i) + '_user', avatar_style="big-smile") |
|
message(st.session_state["generated"][i], key=str(i), avatar_style="thumbs") |