Upload 2 files
Browse files- app1.py +78 -0
- requirements.txt +6 -0
app1.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#this is one is using FALCON-7B
|
| 2 |
+
from langchain import HuggingFaceHub, LLMChain, PromptTemplate
|
| 3 |
+
from langchain.memory import ConversationBufferWindowMemory
|
| 4 |
+
from langchain.embeddings.openai import OpenAIEmbeddings
|
| 5 |
+
from langchain.chat_models import ChatOpenAI
|
| 6 |
+
from langchain.chains import ConversationalRetrievalChain
|
| 7 |
+
from langchain.document_loaders.csv_loader import CSVLoader
|
| 8 |
+
from langchain.vectorstores import FAISS
|
| 9 |
+
import tempfile
|
| 10 |
+
from streamlit_chat import message
|
| 11 |
+
import streamlit as st
|
| 12 |
+
|
| 13 |
+
import os
|
| 14 |
+
import sys
|
| 15 |
+
import pandas as pd
|
| 16 |
+
|
| 17 |
+
def conversational_chat(query):
|
| 18 |
+
result = chain({"question": query,
|
| 19 |
+
"chat_history": st.session_state['history']})
|
| 20 |
+
st.session_state['history'].append((query, result["answer"]))
|
| 21 |
+
|
| 22 |
+
return result["answer"]
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
user_api_key = st.sidebar.text_input(
|
| 26 |
+
label="#### Your HuggingFace API key π",
|
| 27 |
+
placeholder="Paste your HuggingGace API key, sk-",
|
| 28 |
+
type="password")
|
| 29 |
+
|
| 30 |
+
if user_api_key is not None and user_api_key.strip() != "":
|
| 31 |
+
huggingfacehub_api_token = os.environ[user_api_key]
|
| 32 |
+
|
| 33 |
+
#setting up the LLM
|
| 34 |
+
repo_id = "tiiuae/falcon-7b-instruct"
|
| 35 |
+
chain = ConversationalRetrievalChain.from_llm(
|
| 36 |
+
llm = HuggingFaceHub(huggingfacehub_api_token=huggingfacehub_api_token,
|
| 37 |
+
repo_id=repo_id,
|
| 38 |
+
model_kwargs={"temperature":0.6, "max_new_tokens":2000}))
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
if 'history' not in st.session_state:
|
| 42 |
+
st.session_state['history'] = []
|
| 43 |
+
|
| 44 |
+
if 'generated' not in st.session_state:
|
| 45 |
+
st.session_state['generated'] = ["Hello ! Ask me anything about " + " π€"]
|
| 46 |
+
|
| 47 |
+
if 'past' not in st.session_state:
|
| 48 |
+
st.session_state['past'] = ["Hey ! π"]
|
| 49 |
+
|
| 50 |
+
#container for the chat history
|
| 51 |
+
response_container = st.container()
|
| 52 |
+
#container for the user's text input
|
| 53 |
+
container = st.container()
|
| 54 |
+
|
| 55 |
+
with container:
|
| 56 |
+
with st.form(key='my_form', clear_on_submit=True):
|
| 57 |
+
|
| 58 |
+
user_input = st.text_input("Query:", placeholder="Talk about your csv data here (:", key='input')
|
| 59 |
+
submit_button = st.form_submit_button(label='Send')
|
| 60 |
+
|
| 61 |
+
if submit_button and user_input:
|
| 62 |
+
output = conversational_chat(user_input)
|
| 63 |
+
|
| 64 |
+
st.session_state['past'].append(user_input)
|
| 65 |
+
st.session_state['generated'].append(output)
|
| 66 |
+
|
| 67 |
+
if st.session_state['generated']:
|
| 68 |
+
with response_container:
|
| 69 |
+
for i in range(len(st.session_state['generated'])):
|
| 70 |
+
message(st.session_state["past"][i], is_user=True, key=str(i) + '_user', avatar_style="big-smile")
|
| 71 |
+
message(st.session_state["generated"][i], key=str(i), avatar_style="thumbs")
|
| 72 |
+
|
| 73 |
+
else:
|
| 74 |
+
st.text("Please enter your OpenAI API key above.")
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
langchain
|
| 2 |
+
streamlit
|
| 3 |
+
openai
|
| 4 |
+
tiktoken
|
| 5 |
+
faiss-cpu
|
| 6 |
+
streamlit_chat
|