merve/taskmaster
5
1import streamlit as st2import requests3import os4from streamlit_chat import message5import random6from sentence_transformers import SentenceTransformer, util7import nltk8import numpy as np9import pandas as pd10 11nltk.download("punkt")12 13 14context = "To extract information from documents, use sentence similarity task. To classify sentiments, use text classification task. To do sentiment analysis, use text classification task. To detect masks from images, use object detection task. To extract name or address from documents use token classification task. To extract name or address from invoices, use token classification task. To build voice enabled applications, you can use automatic speech recognition task. You can retrieve information from documents using sentence similarity task. You can summarize papers using summarization task. You can convert text to speech using text-to-speech task. To detect language spoken in an audio, you can use audio classification task. To detect emotion in an audio, you can use audio classification task. To detect commands in an audio, you can use audio classification task. To decompose sounds in a recording, use audio-to-audio task. To answer questions from a document, you can use question answering task. To answer FAQs from your customers, you can use question answering task. To see if a text is grammatically correct, you can use text classification task. To augment your training data, you can use text classification task. To detect pedestrians, you can use object detection task."15 16 17link_dict = {18 "audio-to-audio": "https://huggingface.co/tasks/audio-to-audio",19 "audio classification": "https://huggingface.co/tasks/audio-classification",20 "automatic speech recognition": "https://huggingface.co/tasks/automatic-speech-recognition",21 "fill-mask":"https://huggingface.co/tasks/fill-mask",22 "image classification": "https://huggingface.co/tasks/image-classification",23 "image segmentation": "https://huggingface.co/tasks/image-segmentation",24 "question answering":"https://huggingface.co/tasks/question-answering",25 "text-to-speech":"https://huggingface.co/tasks/text-to-speech",26 "sentence similarity": "https://huggingface.co/tasks/sentence-similarity",27 "summarization":"https://huggingface.co/tasks/summarization",28 "text generation": "https://huggingface.co/tasks/text-generation",29 "translation": "https://huggingface.co/tasks/translation",30 "token classification": "https://huggingface.co/tasks/token-classification",31 "text classification":"https://huggingface.co/tasks/text-classification",32 "object detection": "https://huggingface.co/tasks/object-detection"}33 34 35 36model_name = 'sentence-transformers/msmarco-distilbert-base-v4'37max_sequence_length = 51238 39model = SentenceTransformer(model_name)40model.max_seq_length = max_sequence_length41corpus = []42sentence_count = []43 44for sent in context.split("."):45 46 sentences = nltk.tokenize.sent_tokenize(str(sent), language='english')47 sentence_count.append(len(sentences))48 for _,s in enumerate(sentences):49 corpus.append(s)50 51corpus_embeddings = model.encode(corpus)52 53def find_sentences(query):54 query_embedding = model.encode(query)55 hits = util.semantic_search(query_embedding, corpus_embeddings, top_k=1)56 hit = hits[0][0]57 corpus_id = hit['corpus_id']58 saved = corpus[corpus_id]59 return saved60 61st.subheader("If you don't know how to build your machine learning product for your use case, Taskmaster is here to help you! ๐ชโจ")62 63message("Let's find out the best task for your use case! Tell me about your use case :)")64 65#message_history = [{"text":"Let's find out the best task for your use case! Tell me about your use case :)", "is_user":False}]66#for msg in message_history:67# message(msg["text"], is_user = msg["is_user"]) 68#placeholder = st.empty() # placeholder for latest message69 70 71input = st.text_input("Ask me ๐ค")72if input:73 message(input, is_user = True)74 75 #message_history.append({"text":input, "is_user" : True})76 77 model_answer = find_sentences(input)78 79 key_exists = False80 for key in link_dict:81 if key in model_answer:82 key_exists = True83 url = link_dict[key]84 response_templates = [f"I think that {key} is the best task for this ๐คฉ Check out the page ๐๐ผ {url}", f"I think you should use {key} ๐ช Check it out here ๐๐ผ {url}", f"I think {key} should work for you ๐ค Check out the page ๐๐ผ {url}"]85 86 bot_answer = random.choice(response_templates)87 message(bot_answer)88 #message_history.append({"text":bot_answer, "is_user" : False})89 if key_exists == False:90 fallback_template = ["I didn't get the question ๐ง Could you please ask again? Try 'What should I use for detecting masks in an image?'",91 "Hmm, not sure I know the answer, maybe you could ask differently? ๐ค",92 "Sorry, I didn't understand you, maybe you could ask differently? ๐ค Try asking 'What should I use to extract name in a document' ๐ค"]93 bot_answer = random.choice(fallback_template)94 message(bot_answer)95 #message_history.append({"text":bot_answer, "is_user" : False})96 97 