basharat8763 commited on
Commit
19d6563
·
verified ·
1 Parent(s): 1fe3658

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -0
app.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import gradio as gr
3
+
4
+ # Use a pipeline as a high-level helper
5
+ from transformers import pipeline
6
+
7
+ # model_path = "../Models/models--deepset--roberta-base-squad2/snapshots/adc3b06f79f797d1c575d5479d6f5efe54a9e3b4"
8
+
9
+ question_answer = pipeline("question-answering", model="deepset/roberta-base-squad2")
10
+
11
+ # question_answer = pipeline("question-answering", model=model_path)
12
+
13
+
14
+ def read_file_content(file_obj):
15
+ """
16
+ Reads the content of a file object and returns it.
17
+ Parameters:
18
+ file_obj (file object): The file object to read from.
19
+ Returns:
20
+ str: The content of the file.
21
+ """
22
+ try:
23
+ with open(file_obj.name, 'r', encoding='utf-8') as file:
24
+ context = file.read()
25
+ return context
26
+ except Exception as e:
27
+ return f"An error occurred: {e}"
28
+
29
+
30
+ def get_answer(file, question):
31
+ context = read_file_content(file)
32
+ answer = question_answer(question=question, context=context)
33
+ return answer["answer"]
34
+
35
+
36
+ demo = gr.Interface(
37
+ fn=get_answer,
38
+ inputs=[gr.File(label="Upload your file"), gr.Textbox(label="Input your question", lines=1)],
39
+ outputs=[gr.Textbox(label="Answer text", lines=1)],
40
+ title="Project 04: Document QnA",
41
+ description="As understood from the title, if not already, this application will provide answer to your question "
42
+ "based on the context provided"
43
+ )
44
+
45
+ demo.launch()