File size: 3,686 Bytes
73c9569
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c7f62e
b317f13
 
 
73c9569
ccf2ab1
b1318a8
ccf2ab1
03c0a2b
78eb20d
 
ccf2ab1
 
f902b4a
b1318a8
c3ecef9
73c9569
 
 
 
 
 
 
 
e9b987a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ccf2ab1
56253bf
73c9569
 
ccf2ab1
73c9569
 
 
5347e77
 
56253bf
73c9569
 
5347e77
73c9569
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
from datasets import load_dataset
dataset = load_dataset("Namitg02/Test")
print(dataset)

from langchain.docstore.document import Document as LangchainDocument
from langchain.text_splitter import RecursiveCharacterTextSplitter
splitter = RecursiveCharacterTextSplitter(chunk_size=100, chunk_overlap=15,separators=["\n\n", "\n", " ", ""])
docs = splitter.create_documents(str(dataset))


from langchain_community.embeddings import HuggingFaceEmbeddings
embedding_model = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")

from langchain_community.vectorstores import Chroma
persist_directory = 'docs/chroma/'

vectordb = Chroma.from_documents(
    documents=docs,
    embedding=embedding_model,
    persist_directory=persist_directory
)



retriever = vectordb.as_retriever(
    search_type="similarity", search_kwargs={"k": 2}
)


from langchain.prompts import PromptTemplate
from langchain.chains import ConversationalRetrievalChain
from langchain.memory import ConversationBufferMemory

memory = ConversationBufferMemory(
    memory_key="chat_history",
    return_messages=True
)

from transformers import pipeline
from langchain_community.llms.huggingface_pipeline import HuggingFacePipeline
from langchain_core.messages import SystemMessage
from langchain_core.prompts import HumanMessagePromptTemplate
from langchain_core.prompts import ChatPromptTemplate
from langchain.prompts import PromptTemplate

print("check1")
question = "How can I reverse Diabetes?"


#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. 
#{context}
#Question: {question}
#Helpful Answer:"""

#QA_CHAIN_PROMPT = PromptTemplate(input_variables=["context", "question"],template=template)

from transformers import AutoTokenizer
from transformers import AutoModelForCausalLM

llm_model = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
tokenizer = AutoTokenizer.from_pretrained(llm_model)
model = AutoModelForCausalLM.from_pretrained(llm_model)
pipe = pipeline(model = llm_model, tokenizer = tokenizer, task = "text-generation", temperature=0.5)

#question = "How can I reverse diabetes?"
#docs1 = retriever.invoke(question)
#docs1 = retriever.similarity_search(question)
#print(docs1[0].page_content)

import pandas as pd
#df = pd.DataFrame(docs1, columns=["text"])
#context =  df.to_string()
#print(context)

#print(docs1)[0]['generated_text'][-1]

print("check2")


#question = "How can I reverse diabetes?"
print("result")

print("check3")

messages = [
    {
        "role": "system",
        "content": "You are a friendly chatbot who responds in the style of a  doctor",
    },
    {"role": "user", "content": "How can I reverse diabetes?"},
]
prompt = pipe.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
chain = pipe(prompt, max_new_tokens=256, do_sample=True)
print(chain[0]["generated_text"])

#chain = pipe(question = question,context = "Use the following information to answer the question. Diabetes can be cured by eating apples.") 





#chain = pipe(question = question,context = "Use the following information to answer the question. {context}.") 
#context = "Use the following information to answer the question. Diabetes can be cured by eating apples."

print("check3A")
#print(chain)[0]['generated_text'][-1]
print("check3B")

import gradio as gr
#ragdemo = gr.Interface.from_pipeline(chain)

interface = gr.Interface.from_pipeline(chain).launch(share=True)

print("check4")
#ragdemo.launch()
print("check5")