Spaces:
Runtime error
Runtime error
File size: 8,996 Bytes
601e2c7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
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")
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)
|