Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,33 +1,43 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
|
|
3 |
|
4 |
-
#
|
|
|
5 |
|
6 |
-
|
7 |
-
|
|
|
8 |
|
9 |
-
#
|
10 |
-
backend_servers = [
|
11 |
-
pipeline("question-answering", model="google-bert/bert-large-uncased-whole-word-masking-finetuned-squad"),
|
12 |
-
pipeline("question-answering", model='distilbert-base-uncased-distilled-squad'),
|
13 |
-
pipe = pipeline("question-answering", model="deepset/roberta-base-squad2")
|
14 |
-
]
|
15 |
-
|
16 |
-
# Function to select a backend server in a round-robin manner
|
17 |
def select_backend():
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
# Define the function to answer questions
|
21 |
def get_answer(text):
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
# Define the Gradio interface
|
27 |
def chatbot_interface(text):
|
28 |
answer = get_answer(text)
|
29 |
return answer
|
30 |
|
|
|
31 |
knowledge_base_text = """
|
32 |
- British American Tobacco Bangladesh (BATB) is a leading multinational corporation operating in Bangladesh, listed on both the Dhaka Stock Exchange and Chittagong Stock Exchange.
|
33 |
- Established in 1910, BATB has over a century of business history in Bangladesh.
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
+
import threading
|
4 |
|
5 |
+
# Number of backend servers (simulated instances of the pipeline)
|
6 |
+
NUM_BACKEND_SERVERS = 3
|
7 |
|
8 |
+
# Initialize backend servers (pipelines) and availability flags
|
9 |
+
backend_servers = [pipeline("question-answering", model="google-bert/bert-large-uncased-whole-word-masking-finetuned-squad") for _ in range(NUM_BACKEND_SERVERS)]
|
10 |
+
backend_locks = [threading.Lock() for _ in range(NUM_BACKEND_SERVERS)]
|
11 |
|
12 |
+
# Function to select a backend server (pipeline) based on availability
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
def select_backend():
|
14 |
+
for i in range(NUM_BACKEND_SERVERS):
|
15 |
+
if backend_locks[i].acquire(blocking=False):
|
16 |
+
return i
|
17 |
+
return None
|
18 |
+
|
19 |
+
# Function to release a backend server (pipeline) after processing a request
|
20 |
+
def release_backend(index):
|
21 |
+
backend_locks[index].release()
|
22 |
|
23 |
# Define the function to answer questions
|
24 |
def get_answer(text):
|
25 |
+
backend_index = select_backend()
|
26 |
+
if backend_index is not None:
|
27 |
+
try:
|
28 |
+
result = backend_servers[backend_index](question=text, context=knowledge_base_text)
|
29 |
+
return result["answer"]
|
30 |
+
finally:
|
31 |
+
release_backend(backend_index)
|
32 |
+
else:
|
33 |
+
return "All pipelines are busy. Please try again later."
|
34 |
|
35 |
# Define the Gradio interface
|
36 |
def chatbot_interface(text):
|
37 |
answer = get_answer(text)
|
38 |
return answer
|
39 |
|
40 |
+
|
41 |
knowledge_base_text = """
|
42 |
- British American Tobacco Bangladesh (BATB) is a leading multinational corporation operating in Bangladesh, listed on both the Dhaka Stock Exchange and Chittagong Stock Exchange.
|
43 |
- Established in 1910, BATB has over a century of business history in Bangladesh.
|