Upload app (1).py
Browse files- app (1).py +124 -0
app (1).py
ADDED
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 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
|
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("Coloring Anime ChatBot")
|
54 |
+
|
55 |
+
csv_url = "https://huggingface.co/spaces/uyen13/chatbot/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 |
+
|
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 |
+
|
93 |
+
# Initialize chat history
|
94 |
+
if 'history' not in st.session_state:
|
95 |
+
st.session_state['history'] = []
|
96 |
+
|
97 |
+
# Initialize messages
|
98 |
+
if 'generated' not in st.session_state:
|
99 |
+
st.session_state['generated'] = ["Hello ! Ask me about this page like coloring book,how to buy ... 🤗"]
|
100 |
+
|
101 |
+
if 'past' not in st.session_state:
|
102 |
+
st.session_state['past'] = ["your chat here"]
|
103 |
+
|
104 |
+
# Create containers for chat history and user input
|
105 |
+
response_container = st.container()
|
106 |
+
container = st.container()
|
107 |
+
|
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="Ask anything... ", key='input')
|
112 |
+
submit_button = st.form_submit_button(label='Send')
|
113 |
+
|
114 |
+
if submit_button and user_input:
|
115 |
+
output = conversational_chat(user_input)
|
116 |
+
st.session_state['past'].append(user_input)
|
117 |
+
st.session_state['generated'].append(output)
|
118 |
+
|
119 |
+
# Display chat history
|
120 |
+
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")
|