Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
-
from transformers.pipelines import PipelineException
|
| 4 |
|
| 5 |
# Preload models
|
| 6 |
models = {
|
|
@@ -15,18 +14,13 @@ loaded_models = {}
|
|
| 15 |
|
| 16 |
def load_model(model_name):
|
| 17 |
if model_name not in loaded_models:
|
| 18 |
-
|
| 19 |
-
loaded_models[model_name] = pipeline("question-answering", model=models[model_name])
|
| 20 |
-
except PipelineException as e:
|
| 21 |
-
return str(e)
|
| 22 |
return loaded_models[model_name]
|
| 23 |
|
| 24 |
def answer_question(model_name, file, question):
|
| 25 |
model = load_model(model_name)
|
| 26 |
-
if isinstance(model, str):
|
| 27 |
-
return model, "", ""
|
| 28 |
|
| 29 |
-
context = file.read() if file else ""
|
| 30 |
result = model(question=question, context=context)
|
| 31 |
answer = result['answer']
|
| 32 |
score = result['score']
|
|
@@ -63,15 +57,19 @@ with gr.Blocks() as interface:
|
|
| 63 |
with gr.Row():
|
| 64 |
submit_button = gr.Button("Submit")
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
submit_button.click(
|
| 71 |
-
|
| 72 |
inputs=[model_dropdown, file_input, question_input],
|
| 73 |
-
outputs=[answer_output, score_output, explanation_output]
|
| 74 |
-
show_progress=progress_bar,
|
| 75 |
)
|
| 76 |
|
| 77 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
|
|
|
| 3 |
|
| 4 |
# Preload models
|
| 5 |
models = {
|
|
|
|
| 14 |
|
| 15 |
def load_model(model_name):
|
| 16 |
if model_name not in loaded_models:
|
| 17 |
+
loaded_models[model_name] = pipeline("question-answering", model=models[model_name])
|
|
|
|
|
|
|
|
|
|
| 18 |
return loaded_models[model_name]
|
| 19 |
|
| 20 |
def answer_question(model_name, file, question):
|
| 21 |
model = load_model(model_name)
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
context = file.read().decode('utf-8') if file else ""
|
| 24 |
result = model(question=question, context=context)
|
| 25 |
answer = result['answer']
|
| 26 |
score = result['score']
|
|
|
|
| 57 |
with gr.Row():
|
| 58 |
submit_button = gr.Button("Submit")
|
| 59 |
|
| 60 |
+
# Define a status area for progress
|
| 61 |
+
status = gr.Markdown(value="")
|
| 62 |
+
|
| 63 |
+
def on_submit(model_name, file, question):
|
| 64 |
+
status.update(value="Loading model...")
|
| 65 |
+
answer, score, explanation = answer_question(model_name, file, question)
|
| 66 |
+
status.update(value="Model loaded")
|
| 67 |
+
return answer, score, explanation
|
| 68 |
|
| 69 |
submit_button.click(
|
| 70 |
+
on_submit,
|
| 71 |
inputs=[model_dropdown, file_input, question_input],
|
| 72 |
+
outputs=[answer_output, score_output, explanation_output]
|
|
|
|
| 73 |
)
|
| 74 |
|
| 75 |
if __name__ == "__main__":
|