|
|
|
from langchain import HuggingFaceHub, LLMChain, PromptTemplate |
|
from langchain.memory import ConversationBufferWindowMemory |
|
from langchain.embeddings.openai import OpenAIEmbeddings |
|
from langchain.chat_models import ChatOpenAI |
|
from langchain.chains import ConversationalRetrievalChain |
|
from langchain.document_loaders.csv_loader import CSVLoader |
|
from langchain.vectorstores import FAISS |
|
import tempfile |
|
from streamlit_chat import message |
|
import streamlit as st |
|
|
|
import os |
|
import re |
|
import sys |
|
import pandas as pd |
|
|
|
def extract_text_from_html(html): |
|
cleanr = re.compile('<.*?>') |
|
cleantext = re.sub(cleanr, '', html) |
|
return cleantext.strip() |
|
|
|
def conversational_chat(query): |
|
output = llm_chain.predict(human_input=query) |
|
return extract_text_from_html(output) |
|
|
|
|
|
user_api_key = st.sidebar.text_input( |
|
label="#### Your HuggingFace API key π", |
|
placeholder="Paste your HuggingGace API key, sk-", |
|
type="password") |
|
|
|
if user_api_key is not None and user_api_key.strip() != "": |
|
|
|
|
|
|
|
repo_id = "tiiuae/falcon-7b-instruct" |
|
template = """ |
|
|
|
Your custon promp |
|
{history} |
|
Me:{human_input} |
|
Jack: |
|
""" |
|
prompt = PromptTemplate( |
|
input_variables=["history", "human_input"], |
|
template=template |
|
) |
|
llm_chain = LLMChain( |
|
llm=HuggingFaceHub(huggingfacehub_api_token=user_api_key, repo_id="tiiuae/falcon-7b-instruct", model_kwargs={"temperature": 0.2}), |
|
prompt=prompt, |
|
verbose=True, |
|
memory=ConversationBufferWindowMemory(k=2) |
|
) |
|
|
|
|
|
if 'history' not in st.session_state: |
|
st.session_state['history'] = [] |
|
|
|
if 'generated' not in st.session_state: |
|
st.session_state['generated'] = ["Hello ! Ask me anything about " + " π€"] |
|
|
|
if 'past' not in st.session_state: |
|
st.session_state['past'] = ["Hey ! π"] |
|
|
|
|
|
response_container = st.container() |
|
|
|
container = st.container() |
|
|
|
with container: |
|
with st.form(key='my_form', clear_on_submit=True): |
|
|
|
user_input = st.text_input("Query:", placeholder="Lets talk about something General", 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") |
|
|
|
else: |
|
st.text("Please enter your HuggingFace API key above.") |
|
|
|
|
|
|
|
|
|
|