File size: 1,604 Bytes
15297a2
 
 
 
 
 
 
 
86993cd
35f95cf
1e0ac48
 
86993cd
1e0ac48
15297a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ca3ebdb
 
6cdf5a7
5f3d851
15297a2
 
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
45
import torch
import gradio as gr

# Use a pipeline as a high-level helper
from transformers import pipeline

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

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

# question_answer = pipeline("question-answering", model="deepset/roberta-large-squad2")

# question_answer = pipeline("question-answering", model="google/flan-t5-large")


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




def read_file_content(file_path):
    """
    Reads the content of a file given its file path and returns it as a string.
    """
    try:
        with open(file_path, "r", encoding="utf-8") as file:
            return file.read()
    except Exception as e:
        return f"Error reading file: {e}"

def get_answer(file, question):
    context = read_file_content(file)  # 'file' is a path string, not a file object
    if context.startswith("Error"):
        return context  # Return error message if file reading fails
    answer = question_answer(question=question, context=context)
    return answer["answer"]

demo = gr.Interface(fn=get_answer,
                    inputs=[gr.File(label="Upload your file"),gr.Textbox(label="Ask any Question related to file",lines=1)],
                    outputs=[gr.Textbox(label="Answer",lines=2)],
                    title="@Naseem GenAI Project 2: Question Answering based on file provided",
                    description="THIS APPLICATION WILL PROVIDE ANSWER BASED ON FILE PROVIDED")
demo.launch()