ADE_detection / app.py
Patridev's picture
add app
4c8fe65
import gradio as gr
from models import classifier, extractor
import api.get_tweets as get_tweets
nl = '\n'
tab = '\t'
def get_analysis(text='', mode=False, n_tweets=3):
list_anns = []
if mode == 'Search in Twitter':
for word in get_tweets.use(text, n_tweets):
list_anns.append((word, process_text(word)))
else:
list_anns.append((text, process_text(text)))
return generate_string(list_anns)
def process_text(text):
detection, pred_class = classifier.use(text)
pred_ext = None
if detection:
pred_ext = extractor.use(text)
return pred_class, pred_ext
def generate_string(anns):
result = f'Nº{tab}Text{tab}Result{nl}'
for idx, ann in enumerate(anns):
result += f'{nl}{idx+1}:{tab}"{ann[0]}"{tab}{ann[1][0]}'
if ann[1][1]:
result += f'{nl}{ann[1][1]}'
return result
iface = gr.Interface(fn=get_analysis,
inputs=[gr.inputs.Textbox(lines=2, label="Text or words for search"),
gr.inputs.Radio(choices=['Text', 'Search in Twitter'], label="Mode"),
gr.inputs.Slider(0, 5, label='Nº tweets per word', step=1)],
outputs=gr.inputs.Textbox(lines=7, label="Result"),
title='Adverse Drug Effect Detection',
description='Tell us your symptoms as if you were talking to a friend or search tweets with '
'specific words '
'in Twitter and we will give you a '
'positive or negative result and the keywords we have detected.',
article='*An adverse drug effect (ADE) is when someone is harmed by a medicine.',
theme="grass",
allow_flagging=False,
allow_screenshot=False,
)
iface.launch()