Update app.py
Browse files
app.py
CHANGED
@@ -6,14 +6,16 @@ from langchain.embeddings import HuggingFaceEmbeddings
|
|
6 |
from langchain.vectorstores import FAISS
|
7 |
from langchain.llms import CTransformers
|
8 |
from langchain.chains import ConversationalRetrievalChain
|
9 |
-
from dl_hf_model import dl_hf_model
|
10 |
from ctransformers import AutoModelForCausalLM
|
11 |
from langchain_g4f import G4FLLM
|
12 |
from g4f import Provider, models
|
|
|
13 |
import requests
|
14 |
# Define the path for generated embeddings
|
15 |
DB_FAISS_PATH = 'vectorstore/db_faiss'
|
16 |
-
|
|
|
|
|
17 |
# Load the model of choice
|
18 |
def load_llm():
|
19 |
# url = "https://huggingface.co/TheBloke/Llama-2-7B-Chat-GGML/blob/main/llama-2-7b-chat.ggmlv3.q4_K_M.bin" # 2.87G
|
@@ -50,7 +52,7 @@ hide_streamlit_style = """
|
|
50 |
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
|
51 |
|
52 |
# Set the title for the Streamlit app
|
53 |
-
st.title("
|
54 |
|
55 |
csv_url = "https://huggingface.co/spaces/uyen13/chatgirl2/raw/main/testchatdata.csv"
|
56 |
# csv_url="https://docs.google.com/uc?export=download&id=1fQ2v2n9zQcoi6JoOU3lCBDHRt3a1PmaE"
|
@@ -83,10 +85,21 @@ llm = load_llm()
|
|
83 |
|
84 |
# Create a conversational chain
|
85 |
chain = ConversationalRetrievalChain.from_llm(llm=llm, retriever=db.as_retriever())
|
|
|
|
|
86 |
|
87 |
# Function for conversational chat
|
88 |
def conversational_chat(query):
|
|
|
89 |
result = chain({"question": query, "chat_history": st.session_state['history']})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
st.session_state['history'].append((query, result["answer"]))
|
91 |
return result["answer"]
|
92 |
|
@@ -96,10 +109,9 @@ if 'history' not in st.session_state:
|
|
96 |
|
97 |
# Initialize messages
|
98 |
if 'generated' not in st.session_state:
|
99 |
-
st.session_state['generated'] = ["
|
100 |
-
|
101 |
if 'past' not in st.session_state:
|
102 |
-
st.session_state['past'] = ["
|
103 |
|
104 |
# Create containers for chat history and user input
|
105 |
response_container = st.container()
|
@@ -108,7 +120,7 @@ container = st.container()
|
|
108 |
# User input form
|
109 |
with container:
|
110 |
with st.form(key='my_form', clear_on_submit=True):
|
111 |
-
user_input = st.text_input("ChatBox", placeholder="
|
112 |
submit_button = st.form_submit_button(label='Send')
|
113 |
|
114 |
if submit_button and user_input:
|
@@ -121,4 +133,5 @@ if st.session_state['generated']:
|
|
121 |
with response_container:
|
122 |
for i in range(len(st.session_state['generated'])):
|
123 |
message(st.session_state["past"][i], is_user=True, key=str(i) + '_user', avatar_style="big-smile")
|
124 |
-
message(st.session_state["generated"][i], key=str(i), avatar_style="thumbs")
|
|
|
|
6 |
from langchain.vectorstores import FAISS
|
7 |
from langchain.llms import CTransformers
|
8 |
from langchain.chains import ConversationalRetrievalChain
|
|
|
9 |
from ctransformers import AutoModelForCausalLM
|
10 |
from langchain_g4f import G4FLLM
|
11 |
from g4f import Provider, models
|
12 |
+
import unicodedata
|
13 |
import requests
|
14 |
# Define the path for generated embeddings
|
15 |
DB_FAISS_PATH = 'vectorstore/db_faiss'
|
16 |
+
def is_japanese_character(character):
|
17 |
+
return 'CJK UNIFIED' in unicodedata.name(character, '')
|
18 |
+
|
19 |
# Load the model of choice
|
20 |
def load_llm():
|
21 |
# url = "https://huggingface.co/TheBloke/Llama-2-7B-Chat-GGML/blob/main/llama-2-7b-chat.ggmlv3.q4_K_M.bin" # 2.87G
|
|
|
52 |
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
|
53 |
|
54 |
# Set the title for the Streamlit app
|
55 |
+
st.title("ZendoηΎε₯³γγ£γγγγγ―γΉ")
|
56 |
|
57 |
csv_url = "https://huggingface.co/spaces/uyen13/chatgirl2/raw/main/testchatdata.csv"
|
58 |
# csv_url="https://docs.google.com/uc?export=download&id=1fQ2v2n9zQcoi6JoOU3lCBDHRt3a1PmaE"
|
|
|
85 |
|
86 |
# Create a conversational chain
|
87 |
chain = ConversationalRetrievalChain.from_llm(llm=llm, retriever=db.as_retriever())
|
88 |
+
# Initialize spaCy with the Japanese model
|
89 |
+
# nlp = spacy.load("ja_core_news_sm")
|
90 |
|
91 |
# Function for conversational chat
|
92 |
def conversational_chat(query):
|
93 |
+
query = "ζδΎγγγγγΌγΏγ«εΊγ₯γγ¦,"+query
|
94 |
result = chain({"question": query, "chat_history": st.session_state['history']})
|
95 |
+
i = 0
|
96 |
+
while i < len(result["answer"]):
|
97 |
+
character = input_string[i]
|
98 |
+
if is_japanese_character(character):
|
99 |
+
break
|
100 |
+
else:
|
101 |
+
result = chain({"question": query, "chat_history": st.session_state['history']})
|
102 |
+
i += 1
|
103 |
st.session_state['history'].append((query, result["answer"]))
|
104 |
return result["answer"]
|
105 |
|
|
|
109 |
|
110 |
# Initialize messages
|
111 |
if 'generated' not in st.session_state:
|
112 |
+
st.session_state['generated'] = ["γγγ«γ‘γ―οΌzendoηΎε₯³γ§γγδ½γγζ’γγ§γγοΌ... π€"]
|
|
|
113 |
if 'past' not in st.session_state:
|
114 |
+
st.session_state['past'] = ["γγ£γγγ―γγγγ"]
|
115 |
|
116 |
# Create containers for chat history and user input
|
117 |
response_container = st.container()
|
|
|
120 |
# User input form
|
121 |
with container:
|
122 |
with st.form(key='my_form', clear_on_submit=True):
|
123 |
+
user_input = st.text_input("ChatBox", placeholder="θ³ͺεγγθ¨ε
₯γγ γγ... ", key='input')
|
124 |
submit_button = st.form_submit_button(label='Send')
|
125 |
|
126 |
if submit_button and user_input:
|
|
|
133 |
with response_container:
|
134 |
for i in range(len(st.session_state['generated'])):
|
135 |
message(st.session_state["past"][i], is_user=True, key=str(i) + '_user', avatar_style="big-smile")
|
136 |
+
message(st.session_state["generated"][i], key=str(i), avatar_style="thumbs")
|
137 |
+
|