Spaces:
Sleeping
Sleeping
File size: 5,280 Bytes
3d2ccf5 cf322df 97f5741 b3d2c60 818627e 3d2ccf5 5fdd7f8 3d2ccf5 1b8a80e 75e25ba 19d6ff0 e752a6d 19d6ff0 e752a6d 702ffe4 19d6ff0 3d2ccf5 b3d2c60 1093800 b3d2c60 1093800 b3d2c60 2c706e6 b3d2c60 488b0e4 b87574d 01aa20e 488b0e4 b3d2c60 518d051 51739ac 3f0d99a 850e742 e413986 3f0d99a b3d2c60 3f0d99a 51739ac 43d8f7d 19d6ff0 6009075 8080ad9 19d6ff0 6009075 3f0d99a 6009075 3f0d99a |
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 |
import streamlit as st
import requests
import os
from streamlit_chat import message
import random
from sentence_transformers import SentenceTransformer, util
import nltk
import numpy as np
import pandas as pd
nltk.download("punkt")
context = "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."
link_dict = {
"audio-to-audio": "https://huggingface.co/tasks/audio-to-audio",
"audio classification": "https://huggingface.co/tasks/audio-classification",
"automatic speech recognition": "https://huggingface.co/tasks/automatic-speech-recognition",
"fill-mask":"https://huggingface.co/tasks/fill-mask",
"image classification": "https://huggingface.co/tasks/image-classification",
"image segmentation": "https://huggingface.co/tasks/image-segmentation",
"question answering":"https://huggingface.co/tasks/question-answering",
"text-to-speech":"https://huggingface.co/tasks/text-to-speech",
"sentence similarity": "https://huggingface.co/tasks/sentence-similarity",
"summarization":"https://huggingface.co/tasks/summarization",
"text generation": "https://huggingface.co/tasks/text-generation",
"translation": "https://huggingface.co/tasks/translation",
"token classification": "https://huggingface.co/tasks/token-classification",
"text classification":"https://huggingface.co/tasks/text-classification",
"object detection": "https://huggingface.co/tasks/object-detection"}
model_name = 'sentence-transformers/msmarco-distilbert-base-v4'
max_sequence_length = 512
model = SentenceTransformer(model_name)
model.max_seq_length = max_sequence_length
corpus = []
sentence_count = []
for sent in context.split("."):
sentences = nltk.tokenize.sent_tokenize(str(sent), language='english')
sentence_count.append(len(sentences))
for _,s in enumerate(sentences):
corpus.append(s)
corpus_embeddings = model.encode(corpus)
def find_sentences(query):
query_embedding = model.encode(query)
hits = util.semantic_search(query_embedding, corpus_embeddings, top_k=1)
hit = hits[0][0]
corpus_id = hit['corpus_id']
saved = corpus[corpus_id]
return saved
st.subheader("If you don't know how to build your machine learning product for your use case, Taskmaster is here to help you! πͺβ¨")
message("Let's find out the best task for your use case! Tell me about your use case :)")
#message_history = [{"text":"Let's find out the best task for your use case! Tell me about your use case :)", "is_user":False}]
#for msg in message_history:
# message(msg["text"], is_user = msg["is_user"])
#placeholder = st.empty() # placeholder for latest message
input = st.text_input("Ask me π€")
if input:
message(input, is_user = True)
#message_history.append({"text":input, "is_user" : True})
model_answer = find_sentences(input)
key_exists = False
for key in link_dict:
if key in model_answer:
key_exists = True
url = link_dict[key]
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}"]
bot_answer = random.choice(response_templates)
message(bot_answer)
#message_history.append({"text":bot_answer, "is_user" : False})
if key_exists == False:
fallback_template = ["I didn't get the question π§ Could you please ask again? Try 'What should I use for detecting masks in an image?'",
"Hmm, not sure I know the answer, maybe you could ask differently? π€",
"Sorry, I didn't understand you, maybe you could ask differently? π€ Try asking 'What should I use to extract name in a document' π€"]
bot_answer = random.choice(fallback_template)
message(bot_answer)
#message_history.append({"text":bot_answer, "is_user" : False})
|