MD1998 commited on
Commit
5c54470
·
1 Parent(s): 4ae4b01

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -162
app.py CHANGED
@@ -1,174 +1,40 @@
1
- # import gradio as gr
2
- # from langchain.llms import HuggingFacePipeline
3
- # from transformers import AutoTokenizer, AutoModel
4
- # import transformers
5
- # import torch
6
- # from langchain.prompts import PromptTemplate
7
- # from langchain.chains import LLMChain
8
- # import warnings
9
- # warnings.filterwarnings('ignore')
10
-
11
-
12
- # model = 'MD1998/FLAN-T5-V1'
13
- # tokenizer=AutoTokenizer.from_pretrained(model)
14
-
15
- # prompt_template=PromptTemplate(input_variables=["conversation"],
16
- # template="""\
17
- # You are a helpful, respectful, and honest assistant designed to improve English language skills. Always provide accurate and helpful responses to language improvement tasks, while ensuring safety and ethical standards. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased, positive, and focused on enhancing language skills.
18
- # If a question does not make sense or is not factually coherent, explain why instead of answering something incorrect. If you don't know the answer to a question, please don't share false information.
19
- # 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. Always encourage users to try different approaches and provide constructive feedback to help them progress.
20
-
21
- # {conversation}
22
- # """)
23
-
24
-
25
- # pipeline=transformers.pipeline(
26
- # "text-generation",
27
- # model=model,
28
- # tokenizer=tokenizer,
29
- # torch_dtype=torch.bfloat16,
30
- # trust_remote_code=True,
31
- # device_map="auto",
32
- # max_length=15,
33
- # do_sample=True,
34
- # top_k=10,
35
- # top_p=5,
36
- # num_return_sequences=1,
37
- # eos_token_id=tokenizer.eos_token_id
38
- # )
39
-
40
- # llm=HuggingFacePipeline(pipeline=pipeline, model_kwargs={'temperature':0.1})
41
-
42
-
43
- # chain = LLMChain(llm=llm, prompt=prompt_template, verbose=True)
44
-
45
-
46
- ##########################
47
-
48
- # from transformers import T5Tokenizer
49
- # from transformers import T5ForConditionalGeneration
50
-
51
- # finetuned_model = T5ForConditionalGeneration.from_pretrained("MD1998/chating_beginner_v2")
52
- # tokenizer = T5Tokenizer.from_pretrained("MD1998/chating_beginner_v2")
53
-
54
- # # Initialize conversation history
55
- # conversation_history = "System_prompt: You establish that the assistant is intelligent and helpful, and that you want to have an engaging conversation.\n"
56
- # generation_params = {
57
- # "max_length": 100,
58
- # "repetition_penalty": 1.2,
59
- # "temperature": 0.2,
60
- # "top_p": 0.99,
61
- # "top_k": 1
62
- # }
63
-
64
- # # Function to handle conversation
65
- # def chat_with_model(input_text):
66
- # global conversation_history
67
-
68
- # # Combine the new input with the conversation history
69
- # my_inputs = conversation_history + input_text
70
-
71
- # # Encode the inputs
72
- # inputs = tokenizer(my_inputs, return_tensors="pt")
73
-
74
- # # Generate outputs using the model
75
- # outputs = finetuned_model.generate(**inputs, **generation_params)
76
-
77
- # # Decode the outputs to get the answer
78
- # answer = tokenizer.decode(outputs[0], skip_special_tokens=True)
79
-
80
- # # Update conversation history (append the new input and answer)
81
- # conversation_history += f"\nUser: {input_text}\nAssistant: {answer}\n"
82
-
83
- # # Display the answer using text wrapping for readability
84
- # print(fill(answer, width=80))
85
-
86
- # # Return the answer for further use (if needed)
87
- # return answer
88
-
89
- # # Example usage
90
- # # user_input = "What is the weather like today?"
91
- # # chat_with_model(user_input)
92
-
93
-
94
- # def greet(user_input):
95
- # response = chat_with_model(user_input)
96
-
97
- # return response
98
-
99
- # iface = gr.Interface(fn=greet, inputs="text", outputs="text")
100
- # iface.launch()
101
- ##########################
102
-
103
-
104
-
105
-
106
-
107
-
108
-
109
-
110
-
111
-
112
-
113
 
114
- # def greet(prompt):
115
- # response = chain.run(prompt)
116
-
117
- # return response
118
 
 
 
 
 
 
 
 
 
 
119
 
 
120
 
121
- # iface = gr.Interface(fn=greet, inputs="text", outputs="text")
122
- # iface.launch()
123
 
124
- #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
125
- from transformers import pipeline, T5Tokenizer
126
 
127
- # Load the tokenizer and the pipeline for text generation
128
- tokenizer = T5Tokenizer.from_pretrained("MD1998/chating_beginner_v2")
129
- chat_pipeline = pipeline("text2text-generation", model="MD1998/chating_beginner_v2", tokenizer=tokenizer)
130
 
131
- # Initialize conversation history
132
- conversation_history = "System_prompt: You are a helpful, respectful, and honest assistant designed to improve English language skills. Always provide accurate and helpful responses to language improvement tasks, while ensuring safety and ethical standards. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased, positive, and focused on enhancing language skills. If a question does not make sense or is not factually coherent, explain why instead of answering something incorrect. If you don't know the answer to a question, please don't share false information. 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. Always encourage users to try different approaches and provide constructive feedback to help them progress.\n"
133
- generation_params = {
134
- "max_length": 100,
135
- "repetition_penalty": 1.2,
136
- "temperature": 0.2,
137
- "top_p": 0.99,
138
- "top_k": 1
139
- }
140
 
141
- # Function to handle conversation
142
- def chat_with_model(input_text):
143
- global conversation_history
144
-
145
- # Combine the new input with the conversation history
146
- prompt = conversation_history + input_text
147
-
148
- # Generate outputs using the pipeline with the provided prompt and generation parameters
149
- response = chat_pipeline(prompt, **generation_params)[0]["generated_text"]
150
-
151
- # Update conversation history (append the new input and answer)
152
- conversation_history += f"\nUser: {input_text}\nAssistant: {response}\n"
153
-
154
- # Display the answer using text wrapping for readability
155
- print(fill(response, width=80))
156
-
157
- # Return the answer for further use (if needed)
158
- return response
159
 
160
- # Example usage
161
- # user_input = "What is the weather like today?"
162
- # chat_with_model(user_input)
163
 
164
- def greet(user_input):
165
- response = chat_with_model(user_input)
166
- return response
167
 
168
- # Launch a Gradio interface
169
- import gradio as gr
170
  iface = gr.Interface(fn=greet, inputs="text", outputs="text")
171
- iface.launch()
172
-
173
-
174
-
 
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
 
19
+ instruction = "Have a good conversation: \n\n {text}"
20
 
 
 
21
 
22
+ SYSTEM_PROMPT = B_SYS + DEFAULT_SYSTEM_PROMPT + E_SYS
 
23
 
24
+ template = B_INST + SYSTEM_PROMPT + instruction + E_INST
 
 
25
 
26
+ prompt = PromptTemplate(template=template, input_variables=["text"])
 
 
 
 
 
 
 
 
27
 
28
+ llm = CTransformers(model="TheBloke/Llama-2-7B-Chat-GGUF", model_file="llama-2-7b-chat.Q3_K_S.gguf",
29
+ model_type='llama',
30
+ config={'max_new_tokens': 128,
31
+ 'temperature': 0.01}
32
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
+ LLM_Chain = LLMChain(prompt=prompt, llm=llm)
 
 
35
 
36
+ def greet(prompt):
37
+ return LLM_Chain.run(prompt)
 
38
 
 
 
39
  iface = gr.Interface(fn=greet, inputs="text", outputs="text")
40
+ iface.launch()