uyen13 commited on
Commit
856d534
Β·
1 Parent(s): 3b9a55a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +126 -0
app.py ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from streamlit_chat import message
3
+ import tempfile
4
+ from langchain.document_loaders.csv_loader import CSVLoader
5
+ 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 ctransformers import AutoModelForCausalLM
10
+ from langchain_g4f import G4FLLM
11
+ from g4f import Provider, models
12
+ # import spacy
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
20
+
21
+ # model_loc, file_size = dl_hf_model(url)
22
+
23
+ # llm = CTransformers(
24
+ # model=model_loc,
25
+ # temperature=0.2,
26
+ # model_type="llama",
27
+ # top_k=10,
28
+ # top_p=0.9,
29
+ # repetition_penalty=1.0,
30
+ # max_new_tokens=512, # adjust as needed
31
+ # seed=42,
32
+ # reset=True, # reset history (cache)
33
+ # stream=False,
34
+ # # threads=cpu_count,
35
+ # # stop=prompt_prefix[1:2],
36
+
37
+
38
+ # )
39
+ llm = G4FLLM(
40
+ model=models.gpt_35_turbo,
41
+ provider=Provider.DeepAi,
42
+ )
43
+ return llm
44
+ hide_streamlit_style = """
45
+ <style>
46
+ #MainMenu {visibility: hidden;}
47
+ footer {visibility: hidden;}
48
+ </style>
49
+ """
50
+ st.markdown(hide_streamlit_style, unsafe_allow_html=True)
51
+
52
+ # Set the title for the Streamlit app
53
+ st.title("Zendo美ε₯³γƒγƒ£γƒƒγƒˆγƒœγƒƒγ‚―γ‚Ή")
54
+
55
+ csv_url = "https://huggingface.co/spaces/uyen13/chatzendo/raw/main/testchatdata.csv"
56
+ # csv_url="https://docs.google.com/uc?export=download&id=1fQ2v2n9zQcoi6JoOU3lCBDHRt3a1PmaE"
57
+
58
+ # Define the path where you want to save the downloaded file
59
+ tmp_file_path = "testchatdata.csv"
60
+
61
+ # Download the CSV file
62
+ response = requests.get(csv_url)
63
+ if response.status_code == 200:
64
+ with open(tmp_file_path, 'wb') as file:
65
+ file.write(response.content)
66
+ else:
67
+ raise Exception(f"Failed to download the CSV file from {csv_url}")
68
+
69
+ # Load CSV data using CSVLoader
70
+ loader = CSVLoader(file_path=tmp_file_path, encoding="utf-8", csv_args={'delimiter': ','})
71
+ data = loader.load()
72
+
73
+ # Create embeddings using Sentence Transformers
74
+ embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2', model_kwargs={'device': 'cpu'})
75
+
76
+ # Create a FAISS vector store and save embeddings
77
+ db = FAISS.from_documents(data, embeddings)
78
+ db.save_local(DB_FAISS_PATH)
79
+
80
+
81
+ # Load the language model
82
+ llm = load_llm()
83
+
84
+ # Create a conversational chain
85
+ chain = ConversationalRetrievalChain.from_llm(llm=llm, retriever=db.as_retriever())
86
+ # Initialize spaCy with the Japanese model
87
+ # nlp = spacy.load("ja_core_news_sm")
88
+
89
+ # Function for conversational chat
90
+ def conversational_chat(query):
91
+ query = "ζδΎ›γ•γ‚ŒγŸγƒ‡γƒΌγ‚Ώγ«εŸΊγ₯いて,"+query
92
+ result = chain({"question": query, "chat_history": st.session_state['history']})
93
+ st.session_state['history'].append((query, result["answer"]))
94
+ return result["answer"]
95
+
96
+ # Initialize chat history
97
+ if 'history' not in st.session_state:
98
+ st.session_state['history'] = []
99
+
100
+ # Initialize messages
101
+ if 'generated' not in st.session_state:
102
+ st.session_state['generated'] = ["こんにけは!zendo美ε₯³γ§γ™γ€‚δ½•γ‹γŠζŽ’γ—γ§γ™γ‹οΌŸ... πŸ€—"]
103
+ if 'past' not in st.session_state:
104
+ st.session_state['past'] = ["γƒγƒ£γƒƒγƒˆγ―γ“γ“γ‹γ‚‰"]
105
+
106
+ # Create containers for chat history and user input
107
+ response_container = st.container()
108
+ container = st.container()
109
+
110
+ # User input form
111
+ with container:
112
+ with st.form(key='my_form', clear_on_submit=True):
113
+ user_input = st.text_input("ChatBox", placeholder="θ³ͺε•γ‚’γ”θ¨˜ε…₯ください... ", key='input')
114
+ submit_button = st.form_submit_button(label='Send')
115
+
116
+ if submit_button and user_input:
117
+ output = conversational_chat(user_input)
118
+ st.session_state['past'].append(user_input)
119
+ st.session_state['generated'].append(output)
120
+
121
+ # Display chat history
122
+ if st.session_state['generated']:
123
+ with response_container:
124
+ for i in range(len(st.session_state['generated'])):
125
+ message(st.session_state["past"][i], is_user=True, key=str(i) + '_user', avatar_style="big-smile")
126
+ message(st.session_state["generated"][i], key=str(i), avatar_style="thumbs")