File size: 1,860 Bytes
50845fb
 
 
 
1ebd87c
c424053
1ebd87c
 
 
 
50845fb
 
 
1ebd87c
 
 
 
 
 
 
 
 
50845fb
 
 
 
 
 
 
 
 
 
 
 
1ebd87c
50845fb
 
 
 
 
1ebd87c
 
 
 
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
import time
import streamlit as st
import requests

headers = {"Authorization": "Bearer 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"}

st.title("extractive-qa")
option = st.selectbox(
    'Select a model👇',
    ('distilbert-base-cased-distilled-squad', 'roberta-base-squad2-distilled', 'xlm-roberta-large-squad2', 'bert-large-cased-whole-word-masking-finetuned-squad'))
text_input = st.text_area("Enter some context👇")
text_question = st.text_input("Enter a question regarding that context👇")

if option == 'distilbert-base-cased-distilled-squad':
        API_URL = "https://api-inference.huggingface.co/models/distilbert-base-cased-distilled-squad"
elif option == "roberta-base-squad2-distilled":
    API_URL="https://api-inference.huggingface.co/models/deepset/roberta-base-squad2-distilled"
elif option == 'xlm-roberta-large-squad2':
    API_URL="https://api-inference.huggingface.co/models/deepset/xlm-roberta-large-squad2"
if option == "bert-large-cased-whole-word-masking-finetuned-squad":
    API_URL = "https://api-inference.huggingface.co/models/bert-large-cased-whole-word-masking-finetuned-squad"
    
def query(payload):
    retries = 0
    while True:
        response = requests.post(API_URL, headers=headers, json=payload)
        if response.status_code == 200:
            return response.json()
        elif response.status_code == 429:
            retries += 1
            wait_time = 2 ** retries
            print(f"Too many requests. Retrying in {wait_time} seconds...")
            time.sleep(wait_time)
        else:
            st.error(f"Request failed with status code {response.status_code}. Try Again!", icon="🚨")
            return None


if st.button("Send"):
    output = query({"inputs": {"question": text_question, "context": text_input}})
    if output:
        answer = output["answer"]
        st.write(output)