Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import tensorflow as tf
|
3 |
+
from transformers import TFAutoModel, AutoTokenizer
|
4 |
+
import os
|
5 |
+
import numpy as np
|
6 |
+
|
7 |
+
model_name = 'cardiffnlp/twitter-roberta-base-sentiment-latest'
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
9 |
+
model = tf.keras.models.load_model(
|
10 |
+
"/content/SpecX2/model.h5",
|
11 |
+
custom_objects={
|
12 |
+
'TFRobertaModel': TFAutoModel.from_pretrained(model_name)
|
13 |
+
}
|
14 |
+
)
|
15 |
+
|
16 |
+
labels = [
|
17 |
+
'Cardiologist',
|
18 |
+
'Dermatologist',
|
19 |
+
'ENT Specialist',
|
20 |
+
'Gastro-enterologist',
|
21 |
+
'General-Physicians',
|
22 |
+
'Neurologist/Gastro-enterologist',
|
23 |
+
'Ophthalmologist',
|
24 |
+
'Orthopedist',
|
25 |
+
'Psychiatrist',
|
26 |
+
'Respirologist',
|
27 |
+
'Rheumatologist',
|
28 |
+
'Rheumatologist/Gastro-enterologist',
|
29 |
+
'Rheumatologist/Orthopedist',
|
30 |
+
'Surgeon'
|
31 |
+
]
|
32 |
+
seq_len = 152
|
33 |
+
|
34 |
+
def prep_data(text):
|
35 |
+
tokens = tokenizer(
|
36 |
+
text, max_length=seq_len, truncation=True,
|
37 |
+
padding='max_length',
|
38 |
+
add_special_tokens=True,
|
39 |
+
return_tensors='tf'
|
40 |
+
)
|
41 |
+
return {
|
42 |
+
'input_ids': tokens['input_ids'],
|
43 |
+
'attention_mask': tokens['attention_mask']
|
44 |
+
}
|
45 |
+
|
46 |
+
def inference(text):
|
47 |
+
encoded_text = prep_data(text)
|
48 |
+
probs = model.predict_on_batch(encoded_text)
|
49 |
+
probabilities = {i:j for i,j in zip(labels, list(probs.flatten()))}
|
50 |
+
return probabilities
|
51 |
+
|
52 |
+
css = """
|
53 |
+
textarea {
|
54 |
+
background-color: #00000000;
|
55 |
+
border: 1px solid #6366f160;
|
56 |
+
color: #000000;
|
57 |
+
}
|
58 |
+
"""
|
59 |
+
with gr.Blocks(title="SpecX", css=css, theme=gr.themes.Soft()) as demo:
|
60 |
+
with gr.Row():
|
61 |
+
textmd = gr.Markdown('''
|
62 |
+
<div style="margin: 50px 0;"></div>
|
63 |
+
|
64 |
+
<h1 style="width:100%; text-align: center;">SpecX: Find the Right Specialist For Your Symptoms!</h1>
|
65 |
+
|
66 |
+
''')
|
67 |
+
with gr.Row():
|
68 |
+
with gr.Column(scale=1, min_width=600):
|
69 |
+
text_box = gr.Textbox(label="Explain your problem in one sentence.")
|
70 |
+
submit_btn = gr.Button("Submit", elem_id="warningk", variant='primary')
|
71 |
+
examples = gr.Examples(examples=[
|
72 |
+
"When I remember her I feel down",
|
73 |
+
"The area around my heart doesn't feel good.",
|
74 |
+
"I have a split on my thumb that will not heal."
|
75 |
+
], inputs=text_box)
|
76 |
+
label = gr.Label(num_top_classes=4, label="Recommended Specialist")
|
77 |
+
submit_btn.click(inference, inputs=text_box, outputs=label)
|
78 |
+
|
79 |
+
demo.launch()
|