Hwilner commited on
Commit
51ceaeb
1 Parent(s): 61d31b1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -0
app.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import gradio as gr
3
+ from transformers import pipeline
4
+
5
+ # Use a Hebrew question-answering model
6
+ model_name = "avichr/heBERT"
7
+
8
+ question_answer = pipeline("question-answering", model=model_name, tokenizer=model_name)
9
+
10
+ def read_file_content(file_obj):
11
+ """
12
+ Reads the content of a file object and returns it.
13
+ Parameters:
14
+ file_obj (file object): The file object to read from.
15
+ Returns:
16
+ str: The content of the file.
17
+ """
18
+ try:
19
+ with open(file_obj.name, 'r', encoding='utf-8') as file:
20
+ context = file.read()
21
+ return context
22
+ except Exception as e:
23
+ return f"An error occurred: {e}"
24
+
25
+ def get_answer(file, question):
26
+ context = read_file_content(file)
27
+ answer = question_answer(question=question, context=context)
28
+ return answer["answer"]
29
+
30
+ demo = gr.Interface(
31
+ fn=get_answer,
32
+ inputs=[gr.File(label="Upload your file"), gr.Textbox(label="Input your question", lines=1)],
33
+ outputs=[gr.Textbox(label="Answer text", lines=1)],
34
+ title="Document Q & A - Hebrew",
35
+ description="This application will be used to answer questions based on the context provided."
36
+ )
37
+
38
+ demo.launch()