File size: 4,417 Bytes
3d2ccf5
 
 
 
cf322df
3d2ccf5
 
050bcdd
 
 
39fa1f9
 
 
3252678
3d2ccf5
1b8a80e
19d6ff0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d2ccf5
0b96e65
296d90c
8ae824c
296d90c
c52039d
51739ac
850e742
 
 
 
 
 
51739ac
3252678
3d2ccf5
 
 
 
1b8a80e
3d2ccf5
 
19d6ff0
 
3252678
c52039d
3252678
19d6ff0
 
44409cc
2fc5351
19d6ff0
 
 
 
 
 
 
 
7c00320
5d4dfc8
 
 
da31b09
5d4dfc8
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
import streamlit as st
import requests
import os
from streamlit_chat import message
import random

@st.cache
def query(payload):
    api_token = os.getenv("api_token")
    model_id = "deepset/roberta-base-squad2"
    headers = {"Authorization": f"Bearer {api_token}"}
    API_URL = f"https://api-inference.huggingface.co/models/{model_id}"
    response = requests.post(API_URL, headers=headers, json=payload)
    return response.json(), response


context = "To extract information from documents, use sentence similarity task. To do sentiment analysis from tweets, use text classification task. To detect masks from images, use object detection task. To extract name or address from documents, use named entity recognition from token classification task. To build voice enabled applications, you can use automatic speech recognition. You can retrieve information from documents using sentence similarity models. You can summarize papers using summarization models. 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. To see if a text is grammatically correct, you can use text classification. To augment your training data, you can use text classification. For pedestrian detection, you can use object detection."


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",
    "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",
    "image classification": "https://huggingface.co/tasks/image-classification",
    "image segmentation": "https://huggingface.co/tasks/image-segmentation",
    "object detection": "https://huggingface.co/tasks/object-detection"}
    

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"])   # display all the previous message

placeholder = st.empty()  # placeholder for latest message

input = st.text_input("Ask me πŸ€—")
message_history.append({"text":input, "is_user" : True})



data, resp = query(
    {
        "inputs": {
            "question": input,
            "context": context,
        }
    }
)


if resp.status_code == 200:

    model_answer = data["answer"]
    for key in link_dict:
        if key in model_answer:
            url = link_dict[key]
            response_templates = [f"I think that {model_answer} is the best task for this 🀩 Check out the page πŸ‘‰πŸΌ {url}", f"I think you should use {model_answer} πŸͺ„ Check it out here πŸ‘‰πŸΌ {url}", f"I think {model_answer} should work for you πŸ€“ Check out the page πŸ‘‰πŸΌ {url}"]
    
            bot_answer = random.choice(response_templates)
            message_history.append({"text":bot_answer, "is_user" : False})
        else:
            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)
 
with placeholder.container():
    last_message = message_history[-1]
    if last_message != "":
        message(last_message["text"], last_message["is_user"])