Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,40 +4,46 @@ import chardet
|
|
| 4 |
|
| 5 |
# Initialize the question-answering pipeline
|
| 6 |
#qa_pipeline = pipeline("question-answering",model="deepset/roberta-base-squad2")
|
| 7 |
-
qa_pipeline = pipeline("question-answering",model="distilbert-base-cased-distilled-squad")
|
|
|
|
| 8 |
def answer_question(context, question):
|
| 9 |
result = qa_pipeline(question=question, context=context)
|
| 10 |
return result['answer']
|
| 11 |
|
| 12 |
-
|
| 13 |
def process(context_file, question):
|
| 14 |
# Read the context from the uploaded file
|
| 15 |
-
|
| 16 |
-
#with open(context_file.name, 'r') as file:
|
| 17 |
-
#context = file.read()
|
| 18 |
with open(context_file.name, 'rb') as file:
|
| 19 |
raw_data = file.read()
|
| 20 |
result = chardet.detect(raw_data)
|
| 21 |
encoding = result['encoding']
|
| 22 |
-
|
| 23 |
# Fallback to a default encoding if detection fails
|
| 24 |
if encoding is None:
|
| 25 |
-
encoding = 'utf-8' #
|
| 26 |
-
|
| 27 |
context = raw_data.decode(encoding, errors='replace') # Replace errors with a placeholder
|
| 28 |
-
|
| 29 |
|
| 30 |
answer = answer_question(context, question)
|
| 31 |
return answer
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
# Gradio interface
|
| 35 |
demo = gr.Interface(
|
| 36 |
fn=process,
|
| 37 |
-
inputs=[
|
|
|
|
|
|
|
|
|
|
| 38 |
outputs=[gr.Textbox(label="Answer")],
|
| 39 |
title="Question Answering",
|
| 40 |
-
description="Upload a file with context and ask a question. The answer will be displayed."
|
|
|
|
| 41 |
)
|
| 42 |
|
| 43 |
if __name__ == "__main__":
|
|
|
|
| 4 |
|
| 5 |
# Initialize the question-answering pipeline
|
| 6 |
#qa_pipeline = pipeline("question-answering",model="deepset/roberta-base-squad2")
|
| 7 |
+
qa_pipeline = pipeline("question-answering", model="distilbert-base-cased-distilled-squad")
|
| 8 |
+
|
| 9 |
def answer_question(context, question):
|
| 10 |
result = qa_pipeline(question=question, context=context)
|
| 11 |
return result['answer']
|
| 12 |
|
|
|
|
| 13 |
def process(context_file, question):
|
| 14 |
# Read the context from the uploaded file
|
|
|
|
|
|
|
|
|
|
| 15 |
with open(context_file.name, 'rb') as file:
|
| 16 |
raw_data = file.read()
|
| 17 |
result = chardet.detect(raw_data)
|
| 18 |
encoding = result['encoding']
|
| 19 |
+
|
| 20 |
# Fallback to a default encoding if detection fails
|
| 21 |
if encoding is None:
|
| 22 |
+
encoding = 'utf-8' # Default encoding
|
| 23 |
+
|
| 24 |
context = raw_data.decode(encoding, errors='replace') # Replace errors with a placeholder
|
|
|
|
| 25 |
|
| 26 |
answer = answer_question(context, question)
|
| 27 |
return answer
|
| 28 |
|
| 29 |
+
# Example context file content
|
| 30 |
+
example_context = """Saudi Arabia, officially known as the Kingdom of Saudi Arabia (KSA), is a country in Western Asia. Riyadh is its capital and largest city. The country is known for its vast deserts and rich cultural heritage."""
|
| 31 |
+
|
| 32 |
+
# Save example context to a file
|
| 33 |
+
with open("example_context.txt", "w", encoding="utf-8") as f:
|
| 34 |
+
f.write(example_context)
|
| 35 |
|
| 36 |
# Gradio interface
|
| 37 |
demo = gr.Interface(
|
| 38 |
fn=process,
|
| 39 |
+
inputs=[
|
| 40 |
+
gr.File(label="Upload Context File", file_types=[".txt"], file_path="example_context.txt"),
|
| 41 |
+
gr.Textbox(label="Question", value="What is the capital city of Saudia Arabia?")
|
| 42 |
+
],
|
| 43 |
outputs=[gr.Textbox(label="Answer")],
|
| 44 |
title="Question Answering",
|
| 45 |
+
description="Upload a file with context and ask a question. The answer will be displayed.",
|
| 46 |
+
examples=[["example_context.txt", "What is the capital city of Saudia Arabia?"]]
|
| 47 |
)
|
| 48 |
|
| 49 |
if __name__ == "__main__":
|