CoolFace
Apppublic

namnh113/Question_Answering

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
app.py115 linesDownload Raw Back to root
1import os, time, transformers2import streamlit as st3from model import MRCQuestionAnswering4from relevance_ranking import rel_ranking5from huggingface_hub import login6from infer import *7from gg_search import GoogleSearch, getContent8ggsearch = GoogleSearch()9 10 11 12class Chatbot():13    def __init__(self):14        st.header('๐Ÿฆœ Question answering')15        st.warning("Warning: the processing may take long cause I have no any GPU now...")16        st.info("This app uses google search engine for each input question...")17        st.info("About me: namnh113")        18        19        self.API_KEY = st.sidebar.text_input(20            'API key (not necessary for now)',21            type='password',22            help="Type in your HuggingFace API key to use this app")23        24        self.model_checkpoint = 'namnh113/vi-mrc-large'25 26        self.checkpoint = st.sidebar.selectbox(27            label = "Choose model",28            options = [self.model_checkpoint],29            help="List available model to predict"30            )31 32 33    def generate_response(self, question):34        try:35            links, documents = ggsearch.search(question)36            if not documents:37                try:38                    for url in links:39                        docs = getContent(url)40                        if len(docs) > 20 and 'The security system for this website has been triggered. Completing the challenge below verifies you are a human and gives you access.' not in doc:41                            documents += [docs]42                except:43                    pass44        except:45            pass46        passages = rel_ranking(question, documents)47        # get top 40 relevant passages48        passages = '. '.join([p.replace('\n',', ') for p in passages[:40]])49        QA_input = {50            'question': question,51            'context': passages }52        if len(QA_input['question'].strip()) > 0:53            start = time.time()54            inputs = [tokenize_function(QA_input, tokenizer)]55            inputs_ids = data_collator(inputs, tokenizer)56            outputs = model(**inputs_ids)57            answer = extract_answer(inputs, outputs, tokenizer)[0]58            during = time.time() - start59            print("answer: {}. \nScore start: {}, Score end: {}, Time: {}".format(answer['answer'],60                                                                      answer['score_start'],61                                                                      answer['score_end'], during))62            answer = ' '.join([_.strip() for _ in answer['answer'].split()])63        return answer if answer else 'No answer found !'64        65    66    def form_data(self):67        # with st.form('my_form'):68            try:69                # if not self.API_KEY.startswith('hf_'):70                #     st.warning('Please enter your API key!', icon='โš ')71                72                73                if "messages" not in st.session_state:74                    st.session_state.messages = []75 76                st.write(f"You are using {self.checkpoint} model")77 78                for message in st.session_state.messages:79                    with st.chat_message(message.get('role')):80                        st.write(message.get("content"))81                text = st.chat_input(disabled=False)82                83                if text:84                    st.session_state.messages.append(85                        {86                            "role":"user",87                            "content": text88                        }89                    )90                    with st.chat_message("user"):91                        st.write(text)92                    93                    if text.lower() == "clear":94                        del st.session_state.messages95                        return96                        97                    result = self.generate_response(text)98                    st.session_state.messages.append(99                        {100                            "role": "assistant",101                            "content": result102                        }103                    )104                    with st.chat_message('assistant'):105                        st.markdown(result)106                107            except Exception as e:108                st.error(e, icon="๐Ÿšจ")109 110chatbot = Chatbot()111login(token=os.environ['hf_api_key'])112os.environ["TOKENIZERS_PARALLELISM"] = "false"113tokenizer = transformers.AutoTokenizer.from_pretrained(chatbot.model_checkpoint)114model = MRCQuestionAnswering.from_pretrained(chatbot.model_checkpoint)115chatbot.form_data()