Native_Bot / app.py
MD1998's picture
Update app.py
6f2882b verified
raw
history blame
2.03 kB
import gradio as gr
from langchain.llms import HuggingFacePipeline
from transformers import AutoTokenizer, AutoModel
import transformers
import torch
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
import warnings
warnings.filterwarnings('ignore')
model = 'MD1998/FLAN-T5-V1'
tokenizer=AutoTokenizer.from_pretrained(model)
prompt_template=PromptTemplate(input_variables=["conversation"],
template="""\
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.
{conversation}
""")
pipeline=transformers.pipeline(
"text-generation",
model=model,
tokenizer=tokenizer,
torch_dtype=torch.bfloat16,
trust_remote_code=True,
device_map="auto",
max_length=64,
do_sample=True,
top_k=10,
top_p=5,
num_return_sequences=1,
eos_token_id=tokenizer.eos_token_id
)
llm=HuggingFacePipeline(pipeline=pipeline, model_kwargs={'temperature':0.1})
chain = LLMChain(llm=llm, prompt=prompt_template, verbose=True)
def greet(prompt):
response = chain.run(prompt)
return response
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
iface.launch()