File size: 1,521 Bytes
9c1f8f7
 
 
 
 
 
 
98b2f71
9c1f8f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# Use a pipeline as a high-level helper
from transformers import pipeline
import torch
import gradio as gr

question_answering  = pipeline("question-answering", model="deepset/roberta-base-squad2")

# model_path = "../Models/models--deepset--roberta-base-squad2/snapshots/cbf50ba81465d4d8676b8bab348e31835147541b"

# question_answering  = pipeline("question-answering", model=model_path)

def read_file(file_obj):
    """
    Reads the contents of a given file object.
    
    Args:
        file_obj (file): The file object to be read.
    
    Returns:
        str: The contents of the file.
    """
    try:
        with open(file_obj.name, 'r', encoding='utf-8') as file:
            context = file.read()
        return context
    except Exception as e:
        return f"Error: Unable to read the file. {e}"

def get_answer(file_obj, question):
    context = read_file(file_obj=file_obj)
    answer = question_answering(question=question, context=context)
    return answer["answer"]

gr.close_all()

demo = gr.Interface(fn=get_answer,
                    inputs=[gr.File(label="Upload your file" ), gr.Textbox(label="Input your question here...", lines=2 )],
                    outputs=[gr.Textbox(label="Answer Text", lines=2)],
                    title="@IT AI Enthusiast (https://www.youtube.com/@itaienthusiast/) - Project 5: DocuQ&A",
                    description="This application will be used to Ask Questions Based On The Context Given To It",
                    concurrency_limit=16)
demo.launch()