Spaces:
Runtime error
Runtime error
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 | |
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(prompt): | |
response = pipe(prompt) | |
x = pd.json_normalize(response[0]) | |
temp = [] | |
for label_code in x.nlargest(3,['score'])["label"].values: | |
temp.append(label_code[6:]) | |
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'] | |
top_cat = knowledge_base_tasks[int(temp[0])] | |
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] == 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_cat,top_3 | |
def summarizer (models, data): | |
model_Eval = {} | |
for i in range (len(models)): | |
# print(models[i]) | |
summarizer_model = pipeline("summarization", model = models[i]) | |
print(summarizer_model(data)) | |
try: | |
print(summarizer_model(data)) | |
result = summarizer_model(data)[0]["summary_text"] | |
print("123",result) | |
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): | |
print("BESTTTTTTTTTTTTTTTTTTTTT") | |
print(analysis) | |
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 top_cats[0], best_model_name, best_model_score,data[:500],best_model_result.replace("\n","") | |
st.image("./Autumn8_logo.jpg") | |
# st.title("Text Analysis App") | |
data = "" | |
st.markdown(f'<p style="color: #36454F;font-size:36px;border-radius:%;">{"Please enter your task:"}</p>', unsafe_allow_html=True) | |
prompt = st.text_input(" ") | |
if prompt != "": | |
sbert_saved_model = torch.load("Sbert_saved_model",map_location=torch.device('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'<p style="color: #36454F;font-size:36px;border-radius:%;">{"Recommended AI Domain Type: "}</p>', unsafe_allow_html=True) | |
st.markdown(f'<p style="background-color:#0066cc; color:#FFFFFF;font-size:24px;border-radius:%;">{top_cat}</p>', unsafe_allow_html=True) | |
# st.write("Recommended AI Domain Type: ",top_cat) | |
# st.write("Recommended Models for category: ",top_cats[0], " are:",top_models) | |
# st.write("Recommended Task category: ",top_models[0]) | |
st.markdown(f'<p style=" margin-left: 25px;color: #36454F;font-size:36px;border-radius:%;">{"Top 3 best models selected:"}</p>', unsafe_allow_html=True) | |
st.markdown(f'<p style="margin-left: 45px;background-color:#0066cc; color:#FFFFFF;font-size:24px;border-radius:%;">{"1- "+top_models[0]}</p>', unsafe_allow_html=True) | |
st.markdown(f'<p style="margin-left: 45px;background-color:#0066cc; color:#FFFFFF;font-size:24px;border-radius:%;">{"2- "+top_models[1]}</p>', unsafe_allow_html=True) | |
st.markdown(f'<p style="margin-left: 45px;background-color:#0066cc; color:#FFFFFF;font-size:24px;border-radius:%;">{"3- "+top_models[2]}</p>', unsafe_allow_html=True) | |
# 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) | |