Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
|
|
|
|
3 |
|
4 |
# Preload models
|
5 |
models = {
|
@@ -20,7 +22,20 @@ def load_model(model_name):
|
|
20 |
def answer_question(model_name, file, question):
|
21 |
model = load_model(model_name)
|
22 |
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
result = model(question=question, context=context)
|
25 |
answer = result['answer']
|
26 |
score = result['score']
|
@@ -35,7 +50,9 @@ with gr.Blocks() as interface:
|
|
35 |
gr.Markdown(
|
36 |
"""
|
37 |
# Question Answering System
|
38 |
-
Upload a document and ask questions to get answers based on the context.
|
|
|
|
|
39 |
""")
|
40 |
|
41 |
with gr.Row():
|
@@ -46,7 +63,7 @@ with gr.Blocks() as interface:
|
|
46 |
)
|
47 |
|
48 |
with gr.Row():
|
49 |
-
file_input = gr.File(label="Upload Document")
|
50 |
question_input = gr.Textbox(lines=2, placeholder="Enter your question here...", label="Question")
|
51 |
|
52 |
with gr.Row():
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
+
import PyPDF2
|
4 |
+
import markdown
|
5 |
|
6 |
# Preload models
|
7 |
models = {
|
|
|
22 |
def answer_question(model_name, file, question):
|
23 |
model = load_model(model_name)
|
24 |
|
25 |
+
if file is not None:
|
26 |
+
if file.type == "application/pdf":
|
27 |
+
pdf_reader = PyPDF2.PdfFileReader(file)
|
28 |
+
context = ""
|
29 |
+
for page_num in range(pdf_reader.numPages):
|
30 |
+
context += pdf_reader.getPage(page_num).extract_text()
|
31 |
+
elif file.name.endswith(".md"):
|
32 |
+
context = file.read().decode('utf-8')
|
33 |
+
context = markdown.markdown(context)
|
34 |
+
else:
|
35 |
+
context = file.read().decode('utf-8')
|
36 |
+
else:
|
37 |
+
context = ""
|
38 |
+
|
39 |
result = model(question=question, context=context)
|
40 |
answer = result['answer']
|
41 |
score = result['score']
|
|
|
50 |
gr.Markdown(
|
51 |
"""
|
52 |
# Question Answering System
|
53 |
+
Upload a document (text, PDF, or Markdown) and ask questions to get answers based on the context.
|
54 |
+
|
55 |
+
**Supported File Types**: `.txt`, `.pdf`, `.md`
|
56 |
""")
|
57 |
|
58 |
with gr.Row():
|
|
|
63 |
)
|
64 |
|
65 |
with gr.Row():
|
66 |
+
file_input = gr.File(label="Upload Document", file_types=["text", "pdf", "markdown"])
|
67 |
question_input = gr.Textbox(lines=2, placeholder="Enter your question here...", label="Question")
|
68 |
|
69 |
with gr.Row():
|