from transformers import BertTokenizerFast,TFBertForSequenceClassification,TextClassificationPipeline, AutoTokenizer, T5ForConditionalGeneration, pipeline, AutoModelForQuestionAnswering
import numpy as np
import tensorflow as tf
import gradio as gr
import openai
import os
import torch
#Summarization Fine Tune Model
def summarize_text(text, model_path="leadingbridge/summarization"):
# Load the tokenizer and model
tokenizer = AutoTokenizer.from_pretrained(model_path)
model = T5ForConditionalGeneration.from_pretrained(model_path)
# Tokenize the input text
inputs = tokenizer.encode(text, return_tensors="pt")
# Generate the summary
summary_ids = model.generate(inputs)
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
return summary
# Sentiment Analysis Pre-Trained Model
def sentiment_analysis(text, model_path="leadingbridge/sentiment-analysis", id2label={0: 'negative', 1: 'positive'}):
tokenizer = BertTokenizerFast.from_pretrained(model_path)
model = TFBertForSequenceClassification.from_pretrained(model_path, id2label=id2label)
pipe = TextClassificationPipeline(model=model, tokenizer=tokenizer)
result = pipe(text)
return result
# Open AI Model
openai.api_key = os.environ['openai_api']
def openai_chatbot(prompt):
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role":"system","content":"You are a general chatbot that can answer anything in Chinese"},
{"role":"user","content":prompt}
],
temperature=0.8,
max_tokens=3000,
top_p=1,
frequency_penalty=0,
presence_penalty=0.6
)
return response.choices[0].message.content
def openai_translation_ec(prompt):
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role":"system","content":"As a professional translator, your task is to translate the following article to Chinese, ensuring that the original tone, meaning, and context are preserved. It's important to provide an accurate and culturally appropriate translation for the target audience."},
{"role":"user","content":prompt}
],
temperature=0.8,
max_tokens=3000,
top_p=1,
frequency_penalty=0,
presence_penalty=1
)
return response.choices[0].message.content
def openai_translation_ce(prompt):
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role":"system","content":"As a professional translator, your task is to translate the following article to English, ensuring that the original tone, meaning, and context are preserved. It's important to provide an accurate and culturally appropriate translation for the target audience."},
{"role":"user","content":prompt}
],
temperature=0.8,
max_tokens=3000,
top_p=1,
frequency_penalty=0,
presence_penalty=1
)
return response.choices[0].message.content
def chatgpt_clone(input, history):
history = history or []
s = list(sum(history, ()))
s.append(input)
inp = ' '.join(s)
output = openai_chatbot(inp)
history.append((input, output))
return history, history
# Pretrained Question Answering Model
model = AutoModelForQuestionAnswering.from_pretrained('uer/roberta-base-chinese-extractive-qa')
tokenizer = AutoTokenizer.from_pretrained('uer/roberta-base-chinese-extractive-qa')
QA = pipeline('question-answering', model=model, tokenizer=tokenizer)
model.eval()
def cqa(question,context): #Chinese QA model function
QA_input = {'question': question,
'context': context}
return QA(QA_input)
"""# **Gradio Model**"""
# Gradio Output Model
with gr.Blocks() as demo:
gr.Markdown('Welcome to the Chinese NLP Demo! Please select a model tab to interact with:')
with gr.Tab("🤖Chatbot"):
gr.Markdown("""
🤖Chatbot
""")
chatbot = gr.Chatbot()
message = gr.Textbox(placeholder="You can discuss any topic with the Chinese Chatbot assistant by typing any natural language in here",lines=3)
state = gr.State()
submit = gr.Button("Send")
submit.click(chatgpt_clone, inputs=[message, state], outputs=[chatbot, state])
gr.Markdown("This is a Chinese chatbot powered by the OpenAI language model. Enter your message above in Chinese and the chatbot will respond.")
with gr.Tab("🤗Sentiment Analysis"):
gr.Markdown("""🤗Sentiment Analysis
""")
inputs = gr.Textbox(placeholder="Type a Chinese sentence here, either positive or negative in sentiment.",lines=3)
outputs = gr.Textbox(label="Sentiment Analysis")
proceed_button = gr.Button("Proceed")
proceed_button.click(fn=sentiment_analysis, inputs=inputs, outputs=outputs)
gr.Markdown("This is a self-trained fine-tuned model using Chinese BERT for sentiment analysis. Enter a sentence in Chinese in the input box and click the 'proceed' button to get the sentiment analysis result.")
with gr.Tab("🀄Chinese Translation"):
gr.Markdown("""🀄Chinese Translation
""")
inputs = gr.Textbox(placeholder="Enter a short English sentence to translate to Chinese here.",lines=3)
outputs = gr.Textbox(label="Translation Result")
proceed_button = gr.Button("Translate")
proceed_button.click(fn=openai_translation_ec, inputs=inputs, outputs=outputs)
gr.Markdown("This model translate an English sentence to Chinese using the OpenAI engine. Enter an English short sentence in the input box and click the 'Translate' button to get the translation result in Chinese.")
with gr.Tab("🔤English Translation"):
gr.Markdown("""🔤English Translation
""")
inputs = gr.Textbox(placeholder="Enter a short Chinese sentence to translate to English here.",lines=3)
outputs = gr.Textbox(label="Translation Result")
proceed_button = gr.Button("Translate")
proceed_button.click(fn=openai_translation_ce, inputs=inputs, outputs=outputs)
gr.Markdown("This model translate a Chinese sentence to English using the OpenAI engine. Enter a Chinese short sentence in the input box and click the 'Translate' button to get the translation result in English.")
with gr.Tab("đź“‘Text Summarization"):
gr.Markdown("""đź“‘Text Summarization
""")
inputs = gr.Textbox(placeholder="Enter a Chinese text to summarize here.",lines=3)
outputs = gr.Textbox(label="Summary")
proceed_button = gr.Button("Summarize")
proceed_button.click(fn=summarize_text, inputs=inputs, outputs=outputs)
gr.Markdown("This self-trained fine-tuned model summarizes Chinese text using the MT5 language model. Enter a Chinese text in the input box and click the 'Summarize' button to get the summary.")
with gr.Tab("âť“Chinese Q&A"):
gr.Markdown("""âť“Chinese Q&A
""")
text_button = gr.Button("proceed")
text_button.click(fn=cqa, inputs=[gr.Textbox(lines=1,label="Question Input", placeholder="Enter the question you want to ask"),gr.Textbox(lines=9,label="Answer Soruce", placeholder="Enter the answer source article in here")],outputs=gr.Textbox(label="Answer Output"))
gr.Markdown("This is a pre-trained Roberta Base Chinese Extractive question answering model. Enter the answer source and the question you want to ask and click the 'Proceed' button to get the answer of your question")
gr.Markdown('''
We are happy to share with you some Chinese language models that we've made using NLP. When we looked online, we noticed that there weren't many resources available for Chinese NLP, so we hope that our models can be useful to you.
We want to mention that these models aren't perfect and there is still room for improvement. Because of limited resources, there might be some mistakes or limitations in the models.
However, We hope that you find them helpful and that you can help make them even better.
''')
demo.launch ()