Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,3 @@
|
|
1 |
-
#from langchain_community.document_loaders import PyPDFLoader
|
2 |
-
|
3 |
from datasets import load_dataset
|
4 |
dataset = load_dataset("Namitg02/Test")
|
5 |
print(dataset)
|
@@ -28,93 +26,46 @@ vectordb = Chroma.from_documents(
|
|
28 |
persist_directory=persist_directory
|
29 |
)
|
30 |
|
31 |
-
retriever = vectordb.as_retriever()
|
32 |
-
|
33 |
#docs_ss = vectordb.similarity_search(question,k=3)
|
34 |
|
35 |
-
|
36 |
-
#
|
37 |
-
#
|
38 |
-
# retriever=vectordb.as_retriever()
|
39 |
-
#)
|
40 |
-
|
41 |
-
from transformers import pipeline
|
42 |
-
from transformers import BitsAndBytesConfig
|
43 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
44 |
-
|
45 |
-
|
46 |
-
READER_MODEL = "HuggingFaceH4/zephyr-7b-beta"
|
47 |
-
bnb_config = BitsAndBytesConfig(bnb_4bit_use_double_quant=True, bnb_4bit_quant_type="nf4")
|
48 |
-
model = AutoModelForCausalLM.from_pretrained(READER_MODEL,quantization_config=bnb_config)
|
49 |
-
tokenizer = AutoTokenizer.from_pretrained(READER_MODEL)
|
50 |
-
|
51 |
-
from langchain.llms import HuggingFacePipeline
|
52 |
-
from langchain.prompts import PromptTemplate
|
53 |
-
from transformers import pipeline
|
54 |
-
from langchain_core.output_parsers import StrOutputParser
|
55 |
-
|
56 |
-
|
57 |
-
text_generation_pipeline = pipeline(
|
58 |
-
model=model,
|
59 |
-
tokenizer=tokenizer,
|
60 |
-
task="text-generation",
|
61 |
-
temperature=0.2,
|
62 |
-
do_sample=True,
|
63 |
-
repetition_penalty=1.1,
|
64 |
-
return_full_text=True,
|
65 |
-
max_new_tokens=100,
|
66 |
-
)
|
67 |
-
|
68 |
-
llm = HuggingFacePipeline(pipeline=text_generation_pipeline)
|
69 |
-
|
70 |
|
71 |
#from langchain_community.output_parsers.rail_parser import GuardrailsOutputParser
|
|
|
72 |
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
{context}
|
78 |
-
|
79 |
-
</s>
|
80 |
-
<|user|>
|
81 |
-
{question}
|
82 |
-
</s>
|
83 |
-
<|assistant|>
|
84 |
|
85 |
-
|
86 |
|
87 |
-
|
88 |
-
|
89 |
-
|
|
|
|
|
90 |
)
|
91 |
|
|
|
|
|
92 |
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
from langchain_core.runnables import RunnablePassthrough
|
97 |
-
|
98 |
-
|
99 |
-
retriever=vectordb.as_retriever()
|
100 |
-
|
101 |
-
rag_chain = {"context": retriever, "question": RunnablePassthrough()} | llm_chain
|
102 |
|
|
|
103 |
|
104 |
-
#from langchain.chains import ConversationalRetrievalChain
|
105 |
-
#from langchain.memory import ConversationBufferMemory
|
106 |
-
#memory = ConversationBufferMemory(
|
107 |
-
# memory_key="chat_history",
|
108 |
-
# return_messages=True
|
109 |
-
#)
|
110 |
|
111 |
-
question = "Can I reverse Diabetes?"
|
112 |
-
print("template")
|
113 |
|
|
|
114 |
#qa = ConversationalRetrievalChain.from_llm(llm=READER_MODEL,retriever=retriever,memory=memory)
|
115 |
-
|
|
|
116 |
import gradio as gr
|
117 |
gr.load("READER_MODEL").launch()
|
118 |
|
119 |
#result = ({"query": question})
|
120 |
-
|
|
|
|
|
|
|
1 |
from datasets import load_dataset
|
2 |
dataset = load_dataset("Namitg02/Test")
|
3 |
print(dataset)
|
|
|
26 |
persist_directory=persist_directory
|
27 |
)
|
28 |
|
|
|
|
|
29 |
#docs_ss = vectordb.similarity_search(question,k=3)
|
30 |
|
31 |
+
# Create placeholders for the login form widgets using st.empty()
|
32 |
+
#user_input_placeholder = st.empty()
|
33 |
+
#pass_input_placeholder = st.empty()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
#from langchain_community.output_parsers.rail_parser import GuardrailsOutputParser
|
36 |
+
from langchain.prompts import PromptTemplate
|
37 |
|
38 |
+
#template = """Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer. Use three sentences maximum. Keep the answer as concise as possible. Always say "thanks for asking!" at the end of the answer.
|
39 |
+
#{You are a helpful dietician}
|
40 |
+
#Question: {question}
|
41 |
+
#Helpful Answer:"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
+
#QA_CHAIN_PROMPT = PromptTemplate.from_template(template)
|
44 |
|
45 |
+
from langchain.chains import ConversationalRetrievalChain
|
46 |
+
from langchain.memory import ConversationBufferMemory
|
47 |
+
memory = ConversationBufferMemory(
|
48 |
+
memory_key="chat_history",
|
49 |
+
return_messages=True
|
50 |
)
|
51 |
|
52 |
+
question = "How can I reverse Diabetes?"
|
53 |
+
print("template")
|
54 |
|
55 |
+
retriever = vectordb.as_retriever(
|
56 |
+
search_type="similarity", search_kwargs={"k": 2}
|
57 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
+
from langchain.chains import RetrievalQA
|
60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
|
|
|
|
62 |
|
63 |
+
READER_MODEL = "HuggingFaceH4/zephyr-7b-beta"
|
64 |
#qa = ConversationalRetrievalChain.from_llm(llm=READER_MODEL,retriever=retriever,memory=memory)
|
65 |
+
qa = RetrievalQA.from_chain_type(llm=READER_MODEL,chain_type="map_reduce",retriever=retriever,verbose=True)
|
66 |
+
result = qa(question)
|
67 |
import gradio as gr
|
68 |
gr.load("READER_MODEL").launch()
|
69 |
|
70 |
#result = ({"query": question})
|
71 |
+
print("qa")
|