aaravlovescodes commited on
Commit
50845fb
1 Parent(s): 1cc3bab

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -0
app.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+ import streamlit as st
3
+ import requests
4
+
5
+ API_URL = "https://api-inference.huggingface.co/models/distilbert-base-cased-distilled-squad"
6
+ headers = {"Authorization": "Bearer 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"}
7
+
8
+ st.title("distilbert-demo")
9
+
10
+ text_input = st.text_area("Enter some context👇")
11
+ text_question = st.text_input("Enter a question regarding that context👇")
12
+
13
+
14
+ def query(payload):
15
+ retries = 0
16
+ while True:
17
+ response = requests.post(API_URL, headers=headers, json=payload)
18
+ if response.status_code == 200:
19
+ return response.json()
20
+ elif response.status_code == 429:
21
+ retries += 1
22
+ wait_time = 2 ** retries
23
+ print(f"Too many requests. Retrying in {wait_time} seconds...")
24
+ time.sleep(wait_time)
25
+ else:
26
+ print(f"Request failed with status code {response.status_code}.")
27
+ return None
28
+
29
+
30
+ if st.button("Send"):
31
+ output = query({"inputs": {"question": text_question, "context": text_input}})
32
+ if output:
33
+ answer = output["answer"]
34
+ st.header(f"Answer: {answer}")
35
+