from transformers import TextClassificationPipeline from transformers import AutoTokenizer from transformers import pipeline import evaluate import gradio as gr import torch import random from transformers.file_utils import is_tf_available, is_torch_available, is_torch_tpu_available from transformers import AutoTokenizer, AutoModelForSequenceClassification, Trainer, TrainingArguments from datasets import load_metric from sklearn.model_selection import train_test_split import pandas as pd import numpy as np import streamlit as st from textblob import TextBlob from streamlit_extras.switch_page_button import switch_page from transformers import YolosImageProcessor, YolosForObjectDetection from PIL import Image import torch import requests import numpy as np import torchvision from torchvision.io import read_image from torchvision.utils import draw_bounding_boxes from transformers import DetrImageProcessor, DetrForObjectDetection from transformers import DetrImageProcessor, DetrForObjectDetection from transformers import pipeline import torch from transformers import PegasusForConditionalGeneration, PegasusTokenizer st.set_page_config(layout="wide") def get_models(prompt): #prompt = input("Enter your AI task idea:") response = pipe(prompt) print("AI Model Idea: ", prompt,"\n") x = pd.json_normalize(response[0]) # x.nlargest(3,['score'])["label"].values knowledge_base_tasks = ['depth-estimation', 'image-classification', 'image-segmentation', 'image-to-image', 'object-detection', 'video-classification', 'unconditional-image-generation', 'zero-shot-image-classification', 'conversational', 'fill-mask', 'question-answering', 'sentence-similarity', 'summarization', 'table-question-answering', 'text-classification', 'text-generation', 'token-classification', 'translation', 'zero-shot-classification'] temp = [] for label_code in x.nlargest(3,['score'])["label"].values: temp.append(label_code[6:]) # temp cat_to_model = {} top_cats = [] for i in range(len(temp)): print("Possible Category ",i+1," : ",knowledge_base_tasks[int(temp[i])]) print("Top three models for this category are:",models_list[models_list["pipeline_tag"] == knowledge_base_tasks[int(temp[i])]].nlargest(3,"downloads")["modelId"].values) cat_to_model[knowledge_base_tasks[int(temp[i])]] = models_list[models_list["pipeline_tag"] == knowledge_base_tasks[int(temp[i])]].nlargest(3,"downloads")["modelId"].values top_cats.append(knowledge_base_tasks[int(temp[i])]) # models_list[models_list["pipeline_tag"] == "image-classification"].nlargest(3,"downloads")["modelId"].values print() print("Returning category-models dictionary..") return top_cats,cat_to_model def get_top_3(top_cat): top_3_df = pd.read_csv("./Top_3_models.csv") top_3 = [] for i in range(top_3_df.shape[0]): if top_3_df["Category"].iloc[i].lower() == top_cat: top_3.append(top_3_df["Model_1"].iloc[i]) top_3.append(top_3_df["Model_2"].iloc[i]) top_3.append(top_3_df["Model_3"].iloc[i]) break return top_3 def get_response(input_text,model_name): torch_device = 'cuda' if torch.cuda.is_available() else 'cpu' tokenizer = PegasusTokenizer.from_pretrained(model_name) model = PegasusForConditionalGeneration.from_pretrained(model_name).to(torch_device) batch = tokenizer([input_text],truncation=True,padding='longest',max_length=1024, return_tensors="pt").to(torch_device) gen_out = model.generate(**batch,max_length=128,num_beams=5, num_return_sequences=1, temperature=1.5) output_text = tokenizer.batch_decode(gen_out, skip_special_tokens=True) return output_text def summarizer (models, data): model_Eval = {} for i in range (len(models)): # print(models[i]) if models[i] == 'tuner007/pegasus_summarizer': model_name = 'tuner007/pegasus_summarizer' result = get_response(data,model_name) rouge = evaluate.load('rouge') # print("345",rouge.compute(predictions=[result],references=[data])) print(type(result), type([data])) quality = rouge.compute(predictions=[result[0]],references=[data]) model_Eval[models[i]] = {"Score":quality,"Result": result} else: summarizer_model = pipeline("summarization", model = models[i]) print(models[i], summarizer_model(data)) try: result = summarizer_model(data)[0]["summary_text"] rouge = evaluate.load('rouge') # print("345",rouge.compute(predictions=[result],references=[data])) quality = rouge.compute(predictions=[result],references=[data]) model_Eval[models[i]] = {"Score":quality,"Result": result} except: print("Model {} has issues.".format(models[i])) return model_Eval def best_model (analysis, data): best_model_score = 0 best_model_name = "" best_model_result = "" temp2 = 0 for model in analysis.keys(): temp1 = analysis[model]["Score"]["rougeLsum"] if temp1 > temp2: temp2 = analysis[model]["Score"]["rougeLsum"] best_model_score = analysis[model]["Score"] best_model_name = model best_model_result = analysis[model]["Result"] return best_model_name, best_model_score,data[:50],best_model_result.replace("\n","") def text_summarization(): top_models = get_top_3("summarization") # st.write("Upload your file: ") # uploaded_files = "" # uploaded_files = st.file_uploader("Choose your file", accept_multiple_files=True) option = st.selectbox( 'What text would you like AI to summarize for you?', ("Choose text files below:",'How to Win friends - Text', 'mocktext', '--')) #add 2 other options of files here if option == 'How to Win friends - Text' or option == 'mocktext' or option == '--':### update book text files here st.write('You selected:', option) if option == 'How to Win friends - Text': # add text name = "How_to_win_friends.txt" st.write("Selected file for analyis is: How_to_win_friends.txt") if option == 'mocktext': name = "mocktext.txt" st.write("Selected file for analyis is: mocktext.txt") if option == '--': name = "--" st.write("--") if st.button("Done"): global file_data # st.write("filename:", uploaded_files) # for uploaded_file in uploaded_files: # # print("here") # file_data = open(uploaded_file.name,encoding="utf8").read() # st.write("filename:", uploaded_file.name) # # st.write(file_data[:500]) # # print("before summarizer") # print(file_data[:50]) file_data = open(name,encoding="utf8").read() analysis = summarizer(models = top_models, data = file_data[:500]) x,c,v,b = best_model(analysis,file_data[:500]) # st.write("Best model for Task: ",z) st.markdown(f'
{"Best Model with Summarization Results"}
', unsafe_allow_html=True) st.write("\nBest model name: ",x) # st.write("\nBest model Score: ",c) st.write("Best Model Rouge Scores: ") st.write("Rouge 1 Score: ",c["rouge1"]) st.write("Rouge 2 Score: ",c["rouge2"]) st.write("Rouge L Score: ",c["rougeL"]) st.write("Rouge LSum Score: ",c["rougeLsum"]) st.write("\nOriginal Data first 50 characters: ", v) st.write("\nBest Model Result: ",b) # print("between summarizer analysis") st.markdown(f'{"Summarization Results for Model 1"}
', unsafe_allow_html=True) # st.write("Summarization Results for Model 1") st.write("Model name: facebook/bart-large-cnn") st.write("Rouge Scores: ") st.write("Rouge 1 Score: ",analysis["facebook/bart-large-cnn"]["Score"]["rouge1"]) st.write("Rouge 2 Score: ",analysis["facebook/bart-large-cnn"]["Score"]["rouge2"]) st.write("Rouge L Score: ",analysis["facebook/bart-large-cnn"]["Score"]["rougeL"]) st.write(f"Rouge LSum Score: ",analysis["facebook/bart-large-cnn"]["Score"]["rougeLsum"]) st.write("Result: ", analysis["facebook/bart-large-cnn"]["Result"]) st.markdown(f'{"Summarization Results for Model 2"}
', unsafe_allow_html=True) # st.write("Summarization Results for Model 2") st.write("Model name: tuner007/pegasus_summarizer") st.write("Rouge Scores: ") st.write("Rouge 1 Score: ",analysis["tuner007/pegasus_summarizer"]["Score"]["rouge1"]) st.write("Rouge 2 Score: ",analysis["tuner007/pegasus_summarizer"]["Score"]["rouge2"]) st.write("Rouge L Score: ",analysis["tuner007/pegasus_summarizer"]["Score"]["rougeL"]) st.write("Rouge LSum Score: ",analysis["tuner007/pegasus_summarizer"]["Score"]["rougeLsum"]) st.write("Result: ", analysis["tuner007/pegasus_summarizer"]["Result"][0]) st.markdown(f'{"Summarization Results for Model 3"}
', unsafe_allow_html=True) # st.write("Summarization Results for Model 3") st.write("Model name: sshleifer/distilbart-cnn-12-6") st.write("Rouge Scores: ") st.write("Rouge 1 Score: ",analysis["sshleifer/distilbart-cnn-12-6"]["Score"]["rouge1"]) st.write("Rouge 2 Score: ",analysis["sshleifer/distilbart-cnn-12-6"]["Score"]["rouge2"]) st.write("Rouge L Score: ",analysis["sshleifer/distilbart-cnn-12-6"]["Score"]["rougeL"]) st.write("Rouge LSum Score: ",analysis["sshleifer/distilbart-cnn-12-6"]["Score"]["rougeLsum"]) st.write("Result: ", analysis["sshleifer/distilbart-cnn-12-6"]["Result"]) #OBJECT DETECTION def yolo_tiny(name): image = read_image(name) model = YolosForObjectDetection.from_pretrained('hustvl/yolos-tiny') image_processor = YolosImageProcessor.from_pretrained("hustvl/yolos-tiny") inputs = image_processor(images=image, return_tensors="pt") outputs = model(**inputs) # model predicts bounding boxes and corresponding COCO classes logits = outputs.logits bboxes = outputs.pred_boxes # print results target_sizes = torch.tensor([image.shape[::-1][:2]]) results = image_processor.post_process_object_detection(outputs, threshold=0.7, target_sizes=target_sizes)[0] label_ = [] bboxes = [] for score, label, box in zip(results["scores"], results["labels"], results["boxes"]): box = [round(i, 2) for i in box.tolist()] print( f"Detected {model.config.id2label[label.item()]} with confidence " f"{round(score.item(), 3)} at location {box}" ) label_.append(model.config.id2label[label.item()]) bboxes.append(np.asarray(box,dtype="int")) bboxes = torch.tensor(bboxes, dtype=torch.int) img=draw_bounding_boxes(image, bboxes,labels = label_, width=3) img = torchvision.transforms.ToPILImage()(img) return img # img.show() def resnet_101(name): image = read_image(name) processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-101") model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-101") inputs = processor(images=image, return_tensors="pt") outputs = model(**inputs) # convert outputs (bounding boxes and class logits) to COCO API # let's only keep detections with score > 0.9 target_sizes = torch.tensor([image.shape[::-1][:2]]) results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.7)[0] label_ = [] bboxes = [] for score, label, box in zip(results["scores"], results["labels"], results["boxes"]): box = [round(i, 2) for i in box.tolist()] print( f"Detected {model.config.id2label[label.item()]} with confidence " f"{round(score.item(), 3)} at location {box}") label_.append(model.config.id2label[label.item()]) bboxes.append(np.asarray(box,dtype="int")) bboxes = torch.tensor(bboxes, dtype=torch.int) bboxes = torch.tensor(bboxes, dtype=torch.int) img=draw_bounding_boxes(image, bboxes,labels = label_, width=3) img = torchvision.transforms.ToPILImage()(img) return img def resnet_50(name): image = read_image(name) processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50") model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50") inputs = processor(images=image, return_tensors="pt") outputs = model(**inputs) # convert outputs (bounding boxes and class logits) to COCO API # let's only keep detections with score > 0.9 target_sizes = torch.tensor([image.shape[::-1][:2]]) results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.7)[0] label_ = [] bboxes = [] for score, label, box in zip(results["scores"], results["labels"], results["boxes"]): box = [round(i, 2) for i in box.tolist()] print( f"Detected {model.config.id2label[label.item()]} with confidence " f"{round(score.item(), 3)} at location {box}" ) label_.append(model.config.id2label[label.item()]) bboxes.append(np.asarray(box,dtype="int")) bboxes = torch.tensor(bboxes, dtype=torch.int) bboxes = torch.tensor(bboxes, dtype=torch.int) img=draw_bounding_boxes(image, bboxes,labels = label_, width=3) img = torchvision.transforms.ToPILImage()(img) return img def object_detection(): # st.write("Upload your image: ") # uploaded_files = "" # uploaded_files = st.file_uploader("Choose a image file", accept_multiple_files=True) option = st.selectbox( 'What image you want for analysis?', ("Choose an image for object detection analysis from the options below:",'Cat and Dog', '2 lazy cats chilling on a couch', 'An astronaut riding wild horse')) if option == 'Cat and Dog' or option == '2 lazy cats chilling on a couch' or option == 'An astronaut riding wild horse': st.write('You selected:', option) if option == 'Cat and Dog': name = "cat_dog.jpg" st.image("cat_dog.jpg") if option == '2 lazy cats chilling on a couch': name = "cat_remote.jpg" st.image("cat_remote.jpg") if option == 'An astronaut riding wild horse': name = "astronaut_rides_horse.png" st.image("astronaut_rides_horse.png") if st.button("Done"): # global file_data # st.write("filename:", uploaded_files) # for uploaded_file in uploaded_files: # print("here") # file_data = open(uploaded_file.name).read() st.write("filename:", name) # name = uploaded_file.name st.image([yolo_tiny(name),resnet_101(name),resnet_50(name)],caption=["hustvl/yolos-tiny","facebook/detr-resnet-101","facebook/detr-resnet-50"]) def task_categorization_model_predictions(): st.image("./panelup.png") # st.title("Text Analysis App") data = "" classifier = pipeline("zero-shot-classification",model="facebook/bart-large-mnli") global check st.markdown(f'{"Write down below the description of your AI application in few sentences:"}
', unsafe_allow_html=True) prompt = st.text_input(" ") st.write("") st.write("") if prompt != "": # sbert_saved_model = torch.load("Sbert_saved_model", map_location=torch.device('cpu')).to("cpu") # model = sbert_saved_model.to("cpu") # tokenizer = AutoTokenizer.from_pretrained("sentence-transformers/all-mpnet-base-v2") # pipe = TextClassificationPipeline(model= model, tokenizer=tokenizer, return_all_scores=True) # # outputs a list of dicts like [[{'label': 'NEGATIVE', 'score': 0.0001223755971295759}, {'label': 'POSITIVE', 'score': 0.9998776316642761}]] # # prompt = ["What is the the best ai for putting text report into data table?","How can I generate car sales agreement with ai model?","AI model to detect burglar on 48 hours of cctv video footage","I need Ai model help me with rewriting 50 financial statements emails into one summary report ?","I need a model for extracting person from an image"] # # responses = pipe(prompt) # models_list = pd.read_csv("models.csv") # # st.write(get_top_3(prompt)) # top_cat, top_models = get_top_3(prompt) # # prompt = input("Enter your AI task idea:") # # top_cats,cat_to_models = get_models(prompt) # # top_models = cat_to_models[top_cats[0]] # top_cat = " " + top_cat[0].upper() + top_cat[1:] st.markdown(f'{"Recognized AI Domain: "}
', unsafe_allow_html=True) domains = ["Computer Vision Task","Natural Language Processing Problem","Audio Operations Problem","Tabular Data Task","Reinforcement Learning Problem","Time Series Forecasting Problem"] #st.write(classifier(prompt, domains)) domain = classifier(prompt, domains)["labels"][0] st.markdown(f'{domain}
', unsafe_allow_html=True) # st.write("Recommended AI Domain Type: ",top_cat) check = 0 if st.button("This seems accurate"): check = 1 if st.button("Show me other likely category recommendations:"): if domain == "Tabular Data Problem": if st.button("Computer Vision Task"): domain = "Computer Vision Task" check = 1 if st.button("Natural Language Processing Problem"): domain = "Natural Language Processing Problem" check = 1 if st.button("Multimodal AI Model"): domain = "Multimodal AI Model" check = 1 if st.button("Audio Operations Problem"): domain = "Audio Operations Problem" check = 1 # if st.button("Tabular Data Task"): # domain = "Tabular Data Task" if st.button("Reinforcement Learning Problem"): domain = "Reinforcement Learning Problem" check = 1 if st.button("Time Series Forecasting Problem"): domain = "Time Series Forecasting Problem" check = 1 if domain == "Computer Vision Task": # if st.button("Computer Vision Task"): # domain = "Computer Vision Task" if st.button("Natural Language Processing Problem"): domain = "Natural Language Processing Problem" check = 1 if st.button("Multimodal AI Model"): domain = "Multimodal AI Model" check = 1 if st.button("Audio Operations Problem"): domain = "Audio Operations Problem" check = 1 if st.button("Tabular Data Task"): domain = "Tabular Data Task" check = 1 if st.button("Reinforcement Learning Problem"): domain = "Reinforcement Learning Problem" check = 1 if st.button("Time Series Forecasting Problem"): domain = "Time Series Forecasting Problem" check = 1 if domain == "Natural Language Processing Problem": if st.button("Computer Vision Task"): domain = "Computer Vision Task" check = 1 # if st.button("Natural Language Processing Problem"): # domain = "Natural Language Processing Problem" if st.button("Multimodal AI Model"): domain = "multimodal" check = 1 if st.button("Audio Operations Problem"): domain = "Audio Operations Problem" check = 1 if st.button("Tabular Data Task"): domain = "Tabular Data Task" check = 1 if st.button("Reinforcement Learning Problem"): domain = "Reinforcement Learning Problem" check = 1 if st.button("Time Series Forecasting Problem"): domain = "Time Series Forecasting Problem" check = 1 if domain == "Multimodal AI Model": if st.button("Computer Vision Task"): domain = "Computer Vision Task" check = 1 if st.button("Natural Language Processing Problem"): domain = "Natural Language Processing Problem" check = 1 # if st.button("Multimodal AI Model"): # domain = "Multimodal AI Model" if st.button("Audio Operations Problem"): domain = "Audio Operations Problem" check = 1 if st.button("Tabular Data Task"): domain = "Tabular Data Task" check = 1 if st.button("Reinforcement Learning Problem"): domain = "Reinforcement Learning Problem" check = 1 if st.button("Time Series Forecasting Problem"): domain = "Time Series Forecasting Problem" check = 1 if domain == "audio": if st.button("Computer Vision Task"): domain = "Computer Vision Task" check = 1 if st.button("Natural Language Processing Problem"): domain = "Natural Language Processing Problem" check = 1 if st.button("Multimodal AI Model"): domain = "Multimodal AI Model" check = 1 # if st.button("Audio Operations Problem"): # domain = "Audio Operations Problem" if st.button("Tabular Data Task"): domain = "Tabular Data Task" check = 1 if st.button("Reinforcement Learning Problem"): domain = "Reinforcement Learning Problem" check = 1 if st.button("Time Series Forecasting Problem"): domain = "Time Series Forecasting Problem" check = 1 if domain == "reinforcement-learning": if st.button("Computer Vision Task"): domain = "Computer Vision Task" check = 1 if st.button("Natural Language Processing Problem"): domain = "Natural Language Processing Problem" check = 1 if st.button("Multimodal AI Model"): domain = "multimodal" check = 1 if st.button("Audio Operations Problem"): domain = "Audio Operations Problem" check = 1 if st.button("Tabular Data Task"): domain = "Tabular Data Task" check = 1 # if st.button("Reinforcement Learning Problem"): # domain = "Reinforcement Learning Problem" if st.button("Time Series Forecasting Problem"): domain = "Time Series Forecasting Problem" check = 1 if domain == "Time Series Forecasting": if st.button("Computer Vision Task"): domain = "Computer Vision Task" check = 1 if st.button("Natural Language Processing Problem"): domain = "Natural Language Processing Problem" check = 1 if st.button("Multimodal AI Model"): domain = "Multimodal AI Model" check = 1 if st.button("Audio Operations Problem"): domain = "Audio Operations Problem" check = 1 if st.button("Tabular Data Task"): domain = "Tabular Data Task" check = 1 if st.button("Reinforcement Learning Problem"): domain = "Reinforcement Learning Problem" check = 1 # if st.button("Time Series Forecasting Problem"): # domain = "Time Series Forecasting Problem" # st.write("Recommended Models for category: ",top_cats[0], " are:",top_models) # st.write("Recommended Task category: ",top_models[0]) knowledge_base_tasks = {"Computer Vision Task":['depth-estimation', 'image-classification', 'image-segmentation', 'image-to-image', 'object-detection', 'video-classification', 'unconditional-image-generation', 'zero-shot-image-classification'],"Natural Language Processing Problem":[ 'conversational', 'fill-mask', 'question-answering', 'sentence-similarity', 'summarization', 'table-question-answering', 'text-classification', 'text-generation', 'token-classification', 'translation', 'zero-shot-classification'],"Audio Operations Problem":["audio-classification","audio-to-audio","automatic-speech-recognition", "text-to-speech"],"Tabular Data Task":["tabular-classification","tabular-regression"],"others":["document-question-answering", "feature-extraction","image-to-text","text-to-image","text-to-video","visual-question-answering"], "Reinforcement Learning Problem":["reinforcement-learning"],"time-series-forecasting":["time-series-forecasting"]} # st.write(check) # st.write(domain) if check == 1: category = classifier(prompt, knowledge_base_tasks[domain])["labels"][0] st.markdown(f'{"Recognized sub category in Domain: "+domain}
', unsafe_allow_html=True) st.markdown(f'{category}
', unsafe_allow_html=True) top_models = get_top_3(category) #st.write(top_models) st.markdown(f'{"The best models selected for this domain:"}
', unsafe_allow_html=True) st.markdown(f'{"1- "+top_models[0]}
', unsafe_allow_html=True) st.image("./buttons1.png") # if st.button("Show more"): st.markdown(f'{"2- "+top_models[1]}
', unsafe_allow_html=True) st.image("./buttons1.png") st.markdown(f'{"3- "+top_models[2]}
', unsafe_allow_html=True) st.image("./buttons1.png") page_names_to_funcs = { "Pick the best Model for your AI app":task_categorization_model_predictions, "Compare Object Detection Performance": object_detection, "Compare Document Summarization Performance": text_summarization } demo_name = st.sidebar.selectbox("Pick the best model for your next AI task or directly compare models' performance", page_names_to_funcs.keys()) page_names_to_funcs[demo_name]() # st.write("Recommended Most Popular Model for category ",top_cat, " is:",top_models[0]) # if st.button("Show more"): # for i in range(1,len(top_models)): # st.write("Model#",str(i+1),top_models[i]) # data = prompt # # print("before len data") # if len(data) != 0: # # print("after len data") # st.write("Recommended Task category: ",top_cats[0]) # st.write("Recommended Most Popular Model for category ",top_cats[0], " is:",top_models[0]) # if st.button("Show more"): # for i in range(1,len(top_models)): # st.write("Model#",str(i+1),top_models[i]) # st.write("Upload your file: ") # uploaded_files = "" # uploaded_files = st.file_uploader("Choose a text file", accept_multiple_files=True) # if st.button("Done"): # global file_data # st.write("filename:", uploaded_files) # for uploaded_file in uploaded_files: # # print("here") # file_data = open(uploaded_file.name,encoding="utf8").read() # st.write("filename:", uploaded_file.name) # # st.write(file_data[:500]) # # print("before summarizer") # print(file_data[:500]) # analysis = summarizer(models = top_models, data = file_data[:500]) # # print("between summarizer analysis") # z,x,c,v,b = best_model(analysis,file_data[:500]) # st.write("Best model for Task: ",z) # st.write("\nBest model name: ",x) # st.write("\nBest model Score: ",c) # st.write("\nOriginal Data first 500 characters: ", v) # st.write("\nBest Model Result: ",b) # st.success(result)