Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,5 @@
|
|
1 |
import gradio as gr
|
2 |
from PIL import Image
|
3 |
-
import os
|
4 |
import pytesseract
|
5 |
import torch
|
6 |
import numpy as np
|
@@ -8,7 +7,6 @@ import nltk
|
|
8 |
nltk.download('stopwords')
|
9 |
nltk.download('punkt')
|
10 |
from nltk.corpus import stopwords
|
11 |
-
from nltk.tokenize import word_tokenize, sent_tokenize
|
12 |
from nltk.cluster.util import cosine_distance
|
13 |
import networkx as nx
|
14 |
from transformers import pipeline
|
@@ -28,14 +26,11 @@ def read(filepath):
|
|
28 |
def clean_text(text):
|
29 |
article = text.split(".")
|
30 |
article=[sentence for sentence in article if sentence!=""]
|
31 |
-
# print(article)
|
32 |
|
33 |
sentences = []
|
34 |
|
35 |
for sentence in article:
|
36 |
-
#print(sentence)
|
37 |
sentence=sentence.replace(",", " , ").replace("'", " ' ").split(" ")
|
38 |
-
#sentence=sentence.replace("[^a-zA-Z]", " ").split(" ")
|
39 |
sentence=[word for word in sentence if word!=""]
|
40 |
sentences.append(sentence)
|
41 |
|
@@ -139,8 +134,8 @@ def important_sentences(filepath, no_of_sentences=5):
|
|
139 |
def summarize(filepath):
|
140 |
extractedInformation=read(filepath)
|
141 |
extractedInformation=' '.join(extractedInformation.split('\n'))
|
142 |
-
|
143 |
-
return (gr.Textbox.update(
|
144 |
|
145 |
def Question_Answer(filepath,question,mod):
|
146 |
extractedInformation=read(filepath)
|
@@ -152,9 +147,6 @@ def Question_Answer(filepath,question,mod):
|
|
152 |
obj=question_answerer(question=question, context=extractedInformation)
|
153 |
return obj['answer']
|
154 |
|
155 |
-
def show_fn():
|
156 |
-
return (gr.Textbox.update(visible=True),gr.Button.update(visible=True),gr.Textbox.update(""))
|
157 |
-
|
158 |
def show_fn():
|
159 |
return (gr.Textbox.update(visible=True),gr.Button.update(visible=True),gr.Dropdown.update(visible=True),gr.Textbox.update(""))
|
160 |
def dummy_fn(x):
|
@@ -166,18 +158,18 @@ with gr.Blocks() as demo:
|
|
166 |
img=gr.components.Image(type="filepath", label="Input Image")
|
167 |
|
168 |
with gr.Row():
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
|
173 |
mode=gr.Dropdown(["Roberta","DistilBert"],label="Model",info="Choose a model",visible=False)
|
174 |
ques_box = gr.Textbox(label="Question",info="Enter a Question",interactive=True,visible=False)
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
gr.Markdown("## Image Examples")
|
182 |
with gr.Row():
|
183 |
gr.Examples(
|
|
|
1 |
import gradio as gr
|
2 |
from PIL import Image
|
|
|
3 |
import pytesseract
|
4 |
import torch
|
5 |
import numpy as np
|
|
|
7 |
nltk.download('stopwords')
|
8 |
nltk.download('punkt')
|
9 |
from nltk.corpus import stopwords
|
|
|
10 |
from nltk.cluster.util import cosine_distance
|
11 |
import networkx as nx
|
12 |
from transformers import pipeline
|
|
|
26 |
def clean_text(text):
|
27 |
article = text.split(".")
|
28 |
article=[sentence for sentence in article if sentence!=""]
|
|
|
29 |
|
30 |
sentences = []
|
31 |
|
32 |
for sentence in article:
|
|
|
33 |
sentence=sentence.replace(",", " , ").replace("'", " ' ").split(" ")
|
|
|
34 |
sentence=[word for word in sentence if word!=""]
|
35 |
sentences.append(sentence)
|
36 |
|
|
|
134 |
def summarize(filepath):
|
135 |
extractedInformation=read(filepath)
|
136 |
extractedInformation=' '.join(extractedInformation.split('\n'))
|
137 |
+
abstractive_summary = summarizer(extractedInformation, max_length=int(len(extractedInformation)/6), min_length=int(len(extractedInformation)/10), do_sample=False)
|
138 |
+
return (gr.Textbox.update(abstractive_summary[0]["summary_text"]),gr.Button.update(visible=False),gr.Textbox.update(visible=False),gr.Dropdown.update(visible=False))
|
139 |
|
140 |
def Question_Answer(filepath,question,mod):
|
141 |
extractedInformation=read(filepath)
|
|
|
147 |
obj=question_answerer(question=question, context=extractedInformation)
|
148 |
return obj['answer']
|
149 |
|
|
|
|
|
|
|
150 |
def show_fn():
|
151 |
return (gr.Textbox.update(visible=True),gr.Button.update(visible=True),gr.Dropdown.update(visible=True),gr.Textbox.update(""))
|
152 |
def dummy_fn(x):
|
|
|
158 |
img=gr.components.Image(type="filepath", label="Input Image")
|
159 |
|
160 |
with gr.Row():
|
161 |
+
summary_btn = gr.Button(value="Summary")
|
162 |
+
sentence_btn = gr.Button(value="Important Sentences")
|
163 |
+
quesAndAns_btn = gr.Button(value="Question and Answers")
|
164 |
|
165 |
mode=gr.Dropdown(["Roberta","DistilBert"],label="Model",info="Choose a model",visible=False)
|
166 |
ques_box = gr.Textbox(label="Question",info="Enter a Question",interactive=True,visible=False)
|
167 |
+
submit_btn= gr.Button(value="Submit",visible=False)
|
168 |
+
out_box=gr.Textbox(label="Generated Text")
|
169 |
+
summary_btn.click(fn=summarize,inputs=[img],outputs=[out_box,submit_btn,ques_box,mode])
|
170 |
+
sentence_btn.click(fn=important_sentences,inputs=[img],outputs=[out_box,submit_btn,ques_box,mode])
|
171 |
+
quesAndAns_btn.click(fn=show_fn,outputs=[submit_btn,ques_box,mode,out_box])
|
172 |
+
submit_btn.click(fn=Question_Answer,inputs=[img,ques_box,mode],outputs=[out_box])
|
173 |
gr.Markdown("## Image Examples")
|
174 |
with gr.Row():
|
175 |
gr.Examples(
|