Spaces:
Sleeping
Sleeping
import numpy as np | |
import pandas as pd | |
import torch | |
from transformers import BertTokenizer, BertForSequenceClassification | |
import gradio as gr | |
#task1 | |
model_name = "bert-base-multilingual-cased" | |
tokenizer = BertTokenizer.from_pretrained(model_name) | |
model_load=BertForSequenceClassification.from_pretrained("https://huggingface.co/spaces/rima357/FinArgBengali/tree/main/bert-base-multilingual-cased_finetune_FinArgBengali",num_labels=2) | |
def fun_task1(Text): | |
if(Text==""): | |
return "অনুগ্রহ করে প্রথমে টেক্সট লিখুন। (Please enter the text first)" | |
tokenized_text = tokenizer(Text, padding=True, truncation=True, max_length=512, return_tensors="pt") | |
logits = model_load(**tokenized_text).logits | |
output=torch.argmax(logits, axis=1) | |
label=output.tolist() | |
if(label[0]==0): | |
return "ভিত্তি (Premise)" | |
elif(label[0]==1): | |
return "দাবি (Claim)" | |
#task2 | |
from sentence_transformers.cross_encoder import CrossEncoder | |
model_load_task2=CrossEncoder("https://huggingface.co/spaces/rima357/FinArgBengali/tree/main/task2-crossEncoder-bert-base-multilingual-cased",num_labels=3) | |
def fun_task2(Text1,Text2): | |
if(Text1=="" or Text2==""): | |
return "অনুগ্রহ করে প্রথমে টেক্সট লিখুন। (Please enter the text first.)" | |
scores = model_load_task2.predict([Text1,Text2]) | |
label=np.argmax(scores) | |
if(label==0): | |
return "দুটি পাঠ্যের মধ্যে কোন সনাক্ত করা সম্পর্ক নেই। (There is no relation detected between two texts.)" | |
elif(label==1): | |
return "পাঠ্য 1 থেকে পাঠ্য 2 এর সাথে একটি সমর্থন সম্পর্ক রয়েছে। (There is an “support” relation from text 1 to text 2.)" | |
elif(label==2): | |
return "পাঠ্য 1 থেকে পাঠ্য 2 এর সাথে একটি আক্রমণ সম্পর্ক রয়েছে। (There is an “Attack” relation from text 1 to text 2.)" | |
#gradio app | |
with gr.Blocks(theme=gr.themes.Soft()) as demo: | |
gr.Markdown("# **The demo toolkit for Financial Argument Analysis in Bengali**") | |
with gr.Tab("Task1-Classify a bengali argumative text into Premise and Claim"): | |
text_input = gr.Textbox(lines=5, placeholder="Enter the text in Bengali...",label="Text") | |
text_button = gr.Button("Classify") | |
text_output = gr.Textbox(label="output") | |
gr.ClearButton([text_input,text_output]) | |
example_task1 = gr.Examples(examples=[["আমি বলতে চাচ্ছি, কখনও কখনও এমন হয় না যে আপনি কোনও উজ্জ্বল কৌশল নিয়ে এসেছিলেন, এটি দীর্ঘ সময় ধরে ধারাবাহিকভাবে সত্যিই ভাল কাজ করার মতো।"], | |
["দেখুন, সবার আগে, আমি বলব আমাদের শেয়ারহোল্ডারদের জন্য সুযোগ যখন তারা মাইক্রোসফটের কথা চিন্তা করে তখন আগে কখনও এত ভালো ছিল না।"]],inputs=[text_input]) | |
with gr.Tab("Task2-Detection the relation between two bengali argumative texts"): | |
text1_input = gr.Textbox(lines=5, placeholder="Enter the first text in Bengali...",label="Text-1") | |
text2_input = gr.Textbox(lines=5, placeholder="Enter the second text in Bengali...",label="Text-2") | |
task2_button = gr.Button("Detect the relation") | |
task2_output = gr.Textbox(label="output") | |
gr.ClearButton([text1_input,text2_input,task2_output]) | |
example_task2 =gr.Examples(examples=[["ইনস্টাগ্রাম আরও দ্রুত বৃদ্ধি পাচ্ছে এবং বৃদ্ধিতে অবদান রাখছে।","আর ইনস্টাগ্রাম যেভাবে এগিয়ে চলেছে তাতে আমরা খুশি।"], | |
["আর এটাই আমাদের দৃষ্টিভঙ্গির মূল কারণ যে আপনারা একটি পার্থক্য দেখতে পাচ্ছেন।","চাহিদা পরিমাপ করা খুব কঠিন, যেমন আপনি জানেন, যখন আপনি আপনার তৈরি করা সবকিছু বিক্রি করছেন।"]], | |
inputs=[text1_input,text2_input]) | |
text_button.click(fun_task1, inputs=text_input, outputs=text_output) | |
task2_button.click(fun_task2, inputs=[text1_input,text2_input], outputs=task2_output) | |
demo.launch() |