Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -130,41 +130,78 @@ def important_sentences(filepath, no_of_sentences=5):
|
|
| 130 |
text=""
|
| 131 |
for index,sent in enumerate(extractive_summary.split(".")):
|
| 132 |
if sent!='':text+=str(index+1)+". "+str(sent).strip()+".\n\n"
|
| 133 |
-
return (gr.Textbox.update(text),gr.Button.update(visible=False),gr.Textbox.update(visible=False))
|
| 134 |
|
| 135 |
def summarize(filepath):
|
| 136 |
extractedInformation=read(filepath)
|
| 137 |
extractedInformation=' '.join(extractedInformation.split('\n'))
|
| 138 |
output = summarizer(extractedInformation, max_length=int(len(extractedInformation)/6), min_length=int(len(extractedInformation)/10), do_sample=False)
|
| 139 |
-
return (gr.Textbox.update(output[0]["summary_text"]),gr.Button.update(visible=False),gr.Textbox.update(visible=False))
|
| 140 |
|
| 141 |
-
def Question_Answer(filepath,question):
|
| 142 |
extractedInformation=read(filepath)
|
| 143 |
extractedInformation=' '.join(extractedInformation.split('\n'))
|
| 144 |
-
|
|
|
|
|
|
|
|
|
|
| 145 |
obj=question_answerer(question=question, context=extractedInformation)
|
| 146 |
return obj['answer']
|
| 147 |
|
| 148 |
-
|
| 149 |
def show_fn():
|
| 150 |
return (gr.Textbox.update(visible=True),gr.Button.update(visible=True),gr.Textbox.update(""))
|
| 151 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
with gr.Blocks() as demo:
|
| 153 |
gr.Markdown("# **PicSum**")
|
| 154 |
gr.Markdown("Gradio demo for PicSum project. You can give an image as input and select any of the three buttons. It generates summary, important sentences and answers questions related to context.")
|
| 155 |
img=gr.components.Image(type="filepath", label="Input Image")
|
| 156 |
-
|
| 157 |
with gr.Row():
|
| 158 |
summary = gr.Button(value="Summary")
|
| 159 |
sentence = gr.Button(value="Important Sentences")
|
| 160 |
quesAndAns = gr.Button(value="Question and Answers")
|
| 161 |
|
| 162 |
-
|
|
|
|
| 163 |
submit= gr.Button(value="Submit",visible=False)
|
| 164 |
out=gr.Textbox(label="Generated Text")
|
| 165 |
-
summary.click(fn=summarize,inputs=[img],outputs=[out,submit,ques_box])
|
| 166 |
-
sentence.click(fn=important_sentences,inputs=[img],outputs=[out,submit,ques_box])
|
| 167 |
-
quesAndAns.click(fn=show_fn,outputs=[submit,ques_box,out])
|
| 168 |
-
submit.click(fn=Question_Answer,inputs=[img,ques_box],outputs=[out])
|
| 169 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
demo.launch(debug=True)
|
|
|
|
| 130 |
text=""
|
| 131 |
for index,sent in enumerate(extractive_summary.split(".")):
|
| 132 |
if sent!='':text+=str(index+1)+". "+str(sent).strip()+".\n\n"
|
| 133 |
+
return (gr.Textbox.update(text),gr.Button.update(visible=False),gr.Textbox.update(visible=False),gr.Dropdown.update(visible=False))
|
| 134 |
|
| 135 |
def summarize(filepath):
|
| 136 |
extractedInformation=read(filepath)
|
| 137 |
extractedInformation=' '.join(extractedInformation.split('\n'))
|
| 138 |
output = summarizer(extractedInformation, max_length=int(len(extractedInformation)/6), min_length=int(len(extractedInformation)/10), do_sample=False)
|
| 139 |
+
return (gr.Textbox.update(output[0]["summary_text"]),gr.Button.update(visible=False),gr.Textbox.update(visible=False),gr.Dropdown.update(visible=False))
|
| 140 |
|
| 141 |
+
def Question_Answer(filepath,question,mod):
|
| 142 |
extractedInformation=read(filepath)
|
| 143 |
extractedInformation=' '.join(extractedInformation.split('\n'))
|
| 144 |
+
if mod=="Roberta":
|
| 145 |
+
question_answerer = pipeline("question-answering", model="SMD00/QA_model-roberta")
|
| 146 |
+
else :
|
| 147 |
+
question_answerer = pipeline("question-answering", model="SMD00/QA_model-distilbert")
|
| 148 |
obj=question_answerer(question=question, context=extractedInformation)
|
| 149 |
return obj['answer']
|
| 150 |
|
|
|
|
| 151 |
def show_fn():
|
| 152 |
return (gr.Textbox.update(visible=True),gr.Button.update(visible=True),gr.Textbox.update(""))
|
| 153 |
|
| 154 |
+
def show_fn():
|
| 155 |
+
return (gr.Textbox.update(visible=True),gr.Button.update(visible=True),gr.Dropdown.update(visible=True),gr.Textbox.update(""))
|
| 156 |
+
def dummy_fn(x):
|
| 157 |
+
return x
|
| 158 |
+
|
| 159 |
with gr.Blocks() as demo:
|
| 160 |
gr.Markdown("# **PicSum**")
|
| 161 |
gr.Markdown("Gradio demo for PicSum project. You can give an image as input and select any of the three buttons. It generates summary, important sentences and answers questions related to context.")
|
| 162 |
img=gr.components.Image(type="filepath", label="Input Image")
|
| 163 |
+
|
| 164 |
with gr.Row():
|
| 165 |
summary = gr.Button(value="Summary")
|
| 166 |
sentence = gr.Button(value="Important Sentences")
|
| 167 |
quesAndAns = gr.Button(value="Question and Answers")
|
| 168 |
|
| 169 |
+
mode=gr.Dropdown(["Roberta","DistilBert"],label="Model",info="Choose a model",visible=False)
|
| 170 |
+
ques_box = gr.Textbox(label="Question",info="Enter a Question",interactive=True,visible=False)
|
| 171 |
submit= gr.Button(value="Submit",visible=False)
|
| 172 |
out=gr.Textbox(label="Generated Text")
|
| 173 |
+
summary.click(fn=summarize,inputs=[img],outputs=[out,submit,ques_box,mode])
|
| 174 |
+
sentence.click(fn=important_sentences,inputs=[img],outputs=[out,submit,ques_box,mode])
|
| 175 |
+
quesAndAns.click(fn=show_fn,outputs=[submit,ques_box,mode,out])
|
| 176 |
+
submit.click(fn=Question_Answer,inputs=[img,ques_box,mode],outputs=[out])
|
| 177 |
+
gr.Markdown("## Image Examples")
|
| 178 |
+
with gr.Row():
|
| 179 |
+
gr.Examples(
|
| 180 |
+
examples=[ "a.png"],
|
| 181 |
+
inputs=img,
|
| 182 |
+
outputs=img,
|
| 183 |
+
fn=dummy_fn,
|
| 184 |
+
cache_examples=True,
|
| 185 |
+
)
|
| 186 |
+
gr.Examples(
|
| 187 |
+
examples=[ "b.png"],
|
| 188 |
+
inputs=img,
|
| 189 |
+
outputs=img,
|
| 190 |
+
fn=dummy_fn,
|
| 191 |
+
cache_examples=True,
|
| 192 |
+
)
|
| 193 |
+
gr.Examples(
|
| 194 |
+
examples=[ "c.png"],
|
| 195 |
+
inputs=img,
|
| 196 |
+
outputs=img,
|
| 197 |
+
fn=dummy_fn,
|
| 198 |
+
cache_examples=True,
|
| 199 |
+
)
|
| 200 |
+
gr.Examples(
|
| 201 |
+
examples=[ "d.png"],
|
| 202 |
+
inputs=img,
|
| 203 |
+
outputs=img,
|
| 204 |
+
fn=dummy_fn,
|
| 205 |
+
cache_examples=True,
|
| 206 |
+
)
|
| 207 |
demo.launch(debug=True)
|