MD1998 commited on
Commit
8f1972a
1 Parent(s): 4741047

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +94 -30
app.py CHANGED
@@ -1,46 +1,110 @@
1
- from langchain import PromptTemplate
2
- from langchain import LLMChain
3
- from langchain.llms import CTransformers
4
- import gradio as gr
5
 
6
- B_INST, E_INST = "[INST]", "[/INST]"
7
- B_SYS, E_SYS = "<<SYS>>\n", "\n<</SYS>>\n\n"
8
 
 
 
 
 
 
 
 
 
 
9
  # DEFAULT_SYSTEM_PROMPT="\
10
  # You are a helpful, respectful, and honest assistant designed to improve English language skills. Your name is Nemo\
11
- # Always provide accurate and helpful responses to language improvement tasks, while ensuring safety and ethical standards. \
12
- # Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. \
13
- # Please ensure that your responses are socially unbiased, positive, and focused on enhancing language skills. \
14
- # If a question does not make sense or is not factually coherent, explain why instead of answering something incorrect. \
15
  # If you don't know the answer to a question, please don't share false information. \
16
  # Your role is to guide users through various language exercises and challenges, helping them to practice and improve their English skills in a fun and engaging way. \
17
  # Always encourage users to try different approaches and provide constructive feedback to help them progress."
18
- DEFAULT_SYSTEM_PROMPT="\
19
- You are a helpful, respectful, and honest assistant designed to improve English language skills. Your name is Nemo\
20
- If you don't know the answer to a question, please don't share false information. \
21
- Your role is to guide users through various language exercises and challenges, helping them to practice and improve their English skills in a fun and engaging way. \
22
- Always encourage users to try different approaches and provide constructive feedback to help them progress."
23
 
24
- instruction = "Have a good conversation: \n\n {text}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
 
 
26
 
27
- SYSTEM_PROMPT = B_SYS + DEFAULT_SYSTEM_PROMPT + E_SYS
 
 
 
 
 
 
 
 
 
28
 
29
- template = B_INST + SYSTEM_PROMPT + instruction + E_INST
 
 
 
 
 
 
 
30
 
31
- prompt = PromptTemplate(template=template, input_variables=["text"])
 
 
 
 
32
 
33
- # llm = CTransformers(model="TheBloke/Llama-2-7B-Chat-GGUF", model_file="llama-2-7b-chat.Q3_K_S.gguf",
34
- llm = CTransformers(model="NousResearch/Llama-2-7b-chat-hf",
35
- model_type='llama',
36
- config={'max_new_tokens': 128,
37
- 'temperature': 0.01}
38
- )
39
 
40
- LLM_Chain = LLMChain(prompt=prompt, llm=llm)
41
 
42
- def greet(prompt):
43
- return LLM_Chain.run(prompt)
44
 
45
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
46
- iface.launch()
 
1
+ # from langchain import PromptTemplate
2
+ # from langchain import LLMChain
3
+ # from langchain.llms import CTransformers
4
+ # import gradio as gr
5
 
6
+ # B_INST, E_INST = "[INST]", "[/INST]"
7
+ # B_SYS, E_SYS = "<<SYS>>\n", "\n<</SYS>>\n\n"
8
 
9
+ # # DEFAULT_SYSTEM_PROMPT="\
10
+ # # You are a helpful, respectful, and honest assistant designed to improve English language skills. Your name is Nemo\
11
+ # # Always provide accurate and helpful responses to language improvement tasks, while ensuring safety and ethical standards. \
12
+ # # Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. \
13
+ # # Please ensure that your responses are socially unbiased, positive, and focused on enhancing language skills. \
14
+ # # If a question does not make sense or is not factually coherent, explain why instead of answering something incorrect. \
15
+ # # If you don't know the answer to a question, please don't share false information. \
16
+ # # Your role is to guide users through various language exercises and challenges, helping them to practice and improve their English skills in a fun and engaging way. \
17
+ # # Always encourage users to try different approaches and provide constructive feedback to help them progress."
18
  # DEFAULT_SYSTEM_PROMPT="\
19
  # You are a helpful, respectful, and honest assistant designed to improve English language skills. Your name is Nemo\
 
 
 
 
20
  # If you don't know the answer to a question, please don't share false information. \
21
  # Your role is to guide users through various language exercises and challenges, helping them to practice and improve their English skills in a fun and engaging way. \
22
  # Always encourage users to try different approaches and provide constructive feedback to help them progress."
 
 
 
 
 
23
 
24
+ # instruction = "Have a good conversation: \n\n {text}"
25
+
26
+
27
+ # SYSTEM_PROMPT = B_SYS + DEFAULT_SYSTEM_PROMPT + E_SYS
28
+
29
+ # template = B_INST + SYSTEM_PROMPT + instruction + E_INST
30
+
31
+ # prompt = PromptTemplate(template=template, input_variables=["text"])
32
+
33
+ # # llm = CTransformers(model="TheBloke/Llama-2-7B-Chat-GGUF", model_file="llama-2-7b-chat.Q3_K_S.gguf",
34
+ # llm = CTransformers(model="NousResearch/Llama-2-7b-chat-hf",
35
+ # model_type='llama',
36
+ # config={'max_new_tokens': 128,
37
+ # 'temperature': 0.01}
38
+ # )
39
+
40
+ # LLM_Chain = LLMChain(prompt=prompt, llm=llm)
41
+
42
+ # def greet(prompt):
43
+ # return LLM_Chain.run(prompt)
44
+
45
+ # iface = gr.Interface(fn=greet, inputs="text", outputs="text")
46
+ # iface.launch()
47
+
48
+
49
+
50
+
51
+
52
+
53
+
54
+
55
+
56
+
57
+ ########################3
58
+ from langchain.chains import RetrievalQA, ConversationalRetrievalChain
59
+ from langchain.vectorstores import Chroma
60
+ from langchain.text_splitter import CharacterTextSplitter
61
+ from langchain.document_loaders import DirectoryLoader, TextLoader,PyPDFLoader
62
+ from transformers import pipeline, AutoModelForCausalLM
63
+ from langchain.llms import HuggingFacePipeline
64
+ from langchain.embeddings import HuggingFaceInstructEmbeddings
65
+ import gradio as gr
66
+ from InstructorEmbedding import INSTRUCTOR
67
+ import torch
68
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
69
+
70
+ from transformers import AutoModelForSequenceClassification, AutoTokenizer
71
+
72
 
73
+ tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-base")
74
+ model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-base")
75
 
76
+ pipe = pipeline(
77
+ "text2text-generation",
78
+ model=model,
79
+ tokenizer=tokenizer,
80
+ max_length=200,
81
+ temperature=0.8,
82
+ top_p=0.95,
83
+ repetition_penalty=1.15,
84
+ do_sample=True
85
+ )
86
 
87
+ local_llm = HuggingFacePipeline(pipeline=pipe)
88
+ loader = PyPDFLoader('conv.pdf')
89
+ # loader = TextLoader('info.txt')
90
+ document = loader.load()
91
+ text_spliter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
92
+ texts = text_spliter.split_documents(document)
93
+ embedding = HuggingFaceInstructEmbeddings()
94
+ docsearch = Chroma.from_documents(texts, embedding, persist_directory='db')
95
 
96
+ retriever = docsearch.as_retriever(search_kwargs={"k": 3})
97
+ qa_chain = RetrievalQA.from_chain_type(llm=local_llm,
98
+ chain_type="stuff",
99
+ retriever=retriever,
100
+ return_source_documents=True)
101
 
102
+ def gradinterface(query,history):
103
+ result = qa_chain({'query': query})
104
+ return result['result']
 
 
 
105
 
 
106
 
107
+ demo = gr.ChatInterface(fn=gradinterface, title='OUR_OWN_BOT')
 
108
 
109
+ if __name__ == "__main__":
110
+ demo.launch(share=True)