|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import gradio as gr |
|
|
|
|
|
|
|
|
|
|
|
|
|
import langchain |
|
import transformers |
|
|
|
|
|
|
|
from langchain.llms import HuggingFaceHub |
|
|
|
|
|
|
|
from langchain.prompts import PromptTemplate |
|
from langchain.chains import LLMChain, SimpleSequentialChain |
|
|
|
|
|
|
|
llm = HuggingFaceHub( |
|
|
|
|
|
repo_id="google/flan-t5-small", |
|
model_kwargs={"temperature":0.1, |
|
"max_new_tokens":250}) |
|
|
|
|
|
|
|
template = """{question}\n\n""" |
|
prompt_template = PromptTemplate(input_variables=["question"], template=template) |
|
question_chain = LLMChain(llm=llm, prompt=prompt_template) |
|
|
|
|
|
template = """Here is a statement: |
|
{statement} |
|
Make a bullet point list of the assumptions you made when producing the above statement.\n\n""" |
|
prompt_template = PromptTemplate(input_variables=["statement"], template=template) |
|
assumptions_chain = LLMChain(llm=llm, prompt=prompt_template) |
|
assumptions_chain_seq = SimpleSequentialChain( |
|
chains=[question_chain, assumptions_chain], verbose=True |
|
) |
|
|
|
|
|
template = """Here is a bullet point list of assertions: |
|
{assertions} |
|
For each assertion, determine whether it is true or false. If it is false, explain why.\n\n""" |
|
prompt_template = PromptTemplate(input_variables=["assertions"], template=template) |
|
fact_checker_chain = LLMChain(llm=llm, prompt=prompt_template) |
|
fact_checker_chain_seq = SimpleSequentialChain( |
|
chains=[question_chain, assumptions_chain, fact_checker_chain], verbose=True |
|
) |
|
|
|
|
|
template = """In light of the above facts, how would you answer the question '{}'""".format( |
|
user_question |
|
) |
|
template = """{facts}\n""" + template |
|
prompt_template = PromptTemplate(input_variables=["facts"], template=template) |
|
answer_chain = LLMChain(llm=llm, prompt=prompt_template) |
|
overall_chain = SimpleSequentialChain( |
|
chains=[question_chain, assumptions_chain, fact_checker_chain, answer_chain], |
|
verbose=True, |
|
) |
|
|
|
print(overall_chain.run("What is the capitol of the usa?")) |
|
|
|
|
|
|
|
|
|
|
|
|
|
from transformers import BlenderbotTokenizer, BlenderbotForConditionalGeneration |
|
|
|
|
|
|
|
|
|
|
|
|
|
def prompt_human_instruct(system_msg, history): |
|
return system_msg.strip() + "\n" + \ |
|
"\n".join(["\n".join(["###Human: "+item[0], "###Assistant: "+item[1]]) |
|
for item in history]) |
|
|
|
|
|
def prompt_instruct(system_msg, history): |
|
return system_msg.strip() + "\n" + \ |
|
"\n".join(["\n".join(["### Instruction: "+item[0], "### Response: "+item[1]]) |
|
for item in history]) |
|
|
|
|
|
def prompt_chat(system_msg, history): |
|
return system_msg.strip() + "\n" + \ |
|
"\n".join(["\n".join(["USER: "+item[0], "ASSISTANT: "+item[1]]) |
|
for item in history]) |
|
|
|
|
|
def prompt_roleplay(system_msg, history): |
|
return "<|system|>" + system_msg.strip() + "\n" + \ |
|
"\n".join(["\n".join(["<|user|>"+item[0], "<|model|>"+item[1]]) |
|
for item in history]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
model_id_1 = "nlptown/bert-base-multilingual-uncased-sentiment" |
|
model_id_2 = "microsoft/deberta-xlarge-mnli" |
|
model_id_3 = "distilbert-base-uncased-finetuned-sst-2-english" |
|
model_id_4 = "lordtt13/emo-mobilebert" |
|
model_id_5 = "juliensimon/reviews-sentiment-analysis" |
|
model_id_6 = "sbcBI/sentiment_analysis_model" |
|
model_id_7 = "oliverguhr/german-sentiment-bert" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from transformers import pipeline |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pipe = pipeline("sentiment-analysis", model=model_id_7) |
|
|
|
|
|
def predict(text): |
|
sentiment_result = pipe(text) |
|
print(sentiment_result) |
|
return sentiment_result |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
chat_model_facebook_blenderbot_400M_distill = "facebook/blenderbot-400M-distill" |
|
chat_model_HenryJJ_vincua_13b = "HenryJJ/vincua-13b" |
|
|
|
|
|
|
|
|
|
|
|
text = "Why did the chicken cross the road?" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
llm_factextract = HuggingFaceHub( |
|
|
|
|
|
repo_id="google/flan-t5-small", |
|
model_kwargs={"temperature":0.1, |
|
"max_new_tokens":250}) |
|
|
|
fact_extraction_prompt = PromptTemplate( |
|
input_variables=["text_input"], |
|
template="Extract the key facts out of this text. Don't include opinions. Give each fact a number and keep them short sentences. :\n\n {text_input}" |
|
) |
|
|
|
def factextraction (message): |
|
fact_extraction_chain = LLMChain(llm=llm_factextract, prompt=fact_extraction_prompt) |
|
facts = fact_extraction_chain.run(message) |
|
print(facts) |
|
return facts |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
model_name = 'facebook/blenderbot-400M-distill' |
|
tokenizer = BlenderbotTokenizer.from_pretrained(model_name) |
|
model = BlenderbotForConditionalGeneration.from_pretrained(model_name) |
|
|
|
def func (message, checkbox, numb): |
|
inputs = tokenizer(message, return_tensors="pt") |
|
result = model.generate(**inputs) |
|
return tokenizer.decode(result[0]),"0.9" |
|
|
|
|
|
|
|
app = gr.Interface( |
|
fn=func, |
|
title="Conversation Bota", |
|
inputs=["text", "checkbox", gr.Slider(0, 100)], |
|
outputs=["text", "number"], |
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
classifier = pipeline("zero-shot-classification") |
|
text = "This is a tutorial about Hugging Face." |
|
candidate_labels = ["tech", "education", "business"] |
|
|
|
def topic_sale_inform (text): |
|
res = classifier(text, candidate_labels) |
|
print (res) |
|
return res |
|
|
|
app_topic = gr.Interface(fn=topic_sale_inform , inputs="textbox", outputs="textbox", title="Conversation Bots") |
|
|
|
app_topic.launch() |
|
|
|
|
|
|
|
app_facts = gr.Interface(fn=factextraction , inputs="textbox", outputs="textbox", title="Conversation Bots") |
|
|
|
|
|
|
|
|
|
|
|
|