Pratik Dwivedi
commited on
Commit
·
a2c0a8e
1
Parent(s):
d44db4f
added app.py
Browse files- app.py +65 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from langchain.document_loaders import PyPDFLoader
|
| 3 |
+
from langchain.indexes import VectorstoreIndexCreator
|
| 4 |
+
from langchain.chains import RetrievalQA, RetrievalQAWithSourcesChain
|
| 5 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
| 6 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 7 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def respond_to_question(question, model, tokenizer):
|
| 12 |
+
|
| 13 |
+
prompt = [{'role': 'user', 'content': question}]
|
| 14 |
+
inputs = tokenizer.apply_chat_template(
|
| 15 |
+
prompt,
|
| 16 |
+
add_generation_prompt=True,
|
| 17 |
+
return_tensors='pt'
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
tokens = model.generate(
|
| 21 |
+
inputs.to(model.device),
|
| 22 |
+
max_new_tokens=1024,
|
| 23 |
+
temperature=0.8,
|
| 24 |
+
do_sample=True
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
print(tokenizer.decode(tokens[0], skip_special_tokens=False))
|
| 28 |
+
return tokenizer.decode(tokens[0], skip_special_tokens=False)
|
| 29 |
+
# prompt = "write me a python function that prints the fibonacci sequence"
|
| 30 |
+
# messages = [
|
| 31 |
+
# {
|
| 32 |
+
# "role": "system",
|
| 33 |
+
# "content": "You are a friendly chatbot who can code",
|
| 34 |
+
# },
|
| 35 |
+
# {"role": "user", "content": prompt},
|
| 36 |
+
# ]
|
| 37 |
+
# prompt = pipe.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 38 |
+
# outputs = pipe(prompt, max_new_tokens=256, do_sample=True, temperature=0.7, top_k=50, top_p=0.95)
|
| 39 |
+
# print(outputs[0]["generated_text"].split("<|assistant|>")[1])
|
| 40 |
+
# return outputs[0]["generated_text"].split("<|assistant|>")[1]
|
| 41 |
+
|
| 42 |
+
def main():
|
| 43 |
+
st.title("LangChain Demo")
|
| 44 |
+
|
| 45 |
+
tokenizer = AutoTokenizer.from_pretrained('stabilityai/stablelm-zephyr-3b')
|
| 46 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 47 |
+
'stabilityai/stablelm-zephyr-3b',
|
| 48 |
+
trust_remote_code=True,
|
| 49 |
+
device_map="auto"
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
if 'messages' not in st.session_state:
|
| 53 |
+
st.session_state.messages = []
|
| 54 |
+
|
| 55 |
+
prompt = st.text_input("Enter your question here:")
|
| 56 |
+
|
| 57 |
+
for message in st.session_state.messages:
|
| 58 |
+
st.chat_message(message['role']).markdown(message['text'])
|
| 59 |
+
|
| 60 |
+
if prompt:
|
| 61 |
+
st.session_state.messages.append({'role': 'user', 'text': prompt})
|
| 62 |
+
st.chat_message("user").markdown(prompt)
|
| 63 |
+
model_response = respond_to_question(prompt, model, tokenizer)
|
| 64 |
+
st.session_state.messages.append({'role': 'Assistant', 'text': model_response})
|
| 65 |
+
st.chat_message("system").markdown(model_response)
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
langchain
|
| 3 |
+
transformers
|
| 4 |
+
git+https://github.com/huggingface/transformers.git
|
| 5 |
+
accelerate
|