Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,56 +1,128 @@
|
|
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 |
-
|
49 |
|
50 |
|
51 |
|
52 |
-
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
53 |
-
iface.launch()
|
54 |
|
55 |
|
56 |
|
|
|
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 |
+
from langchain import PromptTemplate
|
48 |
+
from langchain import LLMChain
|
49 |
+
from langchain.llms import CTransformers
|
50 |
+
|
51 |
+
# Update the model path to use the Hugging Face model identifier
|
52 |
+
llm = CTransformers(model='TheBloke/Llama-2-7B-Chat-GGML',
|
53 |
+
model_file='llama-2-7b-chat.ggmlv3.q3_K_S.bin',
|
54 |
+
model_type='llama',
|
55 |
+
config={'max_new_tokens': 15,
|
56 |
+
'temperature': 0.01}
|
57 |
+
)
|
58 |
+
|
59 |
+
def greet(my_prompt):
|
60 |
+
|
61 |
+
B_INST, E_INST = "[INST]", "[/INST]"
|
62 |
+
B_SYS, E_SYS = "<<SYS>>\n", "\n<</SYS>>\n\n"
|
63 |
+
|
64 |
+
DEFAULT_SYSTEM_PROMPT="""\
|
65 |
+
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.
|
66 |
+
|
67 |
+
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.
|
68 |
+
|
69 |
+
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.
|
70 |
+
"""
|
71 |
+
|
72 |
+
|
73 |
+
|
74 |
+
|
75 |
+
|
76 |
+
|
77 |
+
# DEFAULT_SYSTEM_PROMPT = """\
|
78 |
+
# You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.
|
79 |
+
|
80 |
+
# If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information."""
|
81 |
+
|
82 |
+
# instruction = "Convert the following text from English to French: \n\n {text}"
|
83 |
+
instruction = "Have a good conversation: \n\n {text}"
|
84 |
+
|
85 |
+
SYSTEM_PROMPT = B_SYS + DEFAULT_SYSTEM_PROMPT + E_SYS
|
86 |
+
|
87 |
+
template = B_INST + SYSTEM_PROMPT + instruction + E_INST
|
88 |
+
|
89 |
+
# print(template)
|
90 |
+
prompt = PromptTemplate(template=template, input_variables=["text"])
|
91 |
+
|
92 |
+
|
93 |
+
LLM_Chain = LLMChain(prompt=prompt, llm=llm)
|
94 |
+
|
95 |
+
# print(LLM_Chain.run("How are you"))
|
96 |
+
|
97 |
+
|
98 |
+
|
99 |
+
|
100 |
+
return LLM_Chain.run(my_prompt)
|
101 |
+
|
102 |
+
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
103 |
+
iface.launch()
|
104 |
+
##########################
|
105 |
+
|
106 |
|
107 |
|
|
|
|
|
108 |
|
|
|
|
|
|
|
|
|
|
|
109 |
|
|
|
|
|
110 |
|
111 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
|
|
|
113 |
|
114 |
|
|
|
115 |
|
116 |
+
|
117 |
+
# def greet(prompt):
|
118 |
+
# response = chain.run(prompt)
|
119 |
|
120 |
+
# return response
|
121 |
|
122 |
|
123 |
|
124 |
+
# iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
125 |
+
# iface.launch()
|
126 |
|
127 |
|
128 |
|