Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch # type: ignore
|
| 2 |
+
from transformers import pipeline # type: ignore
|
| 3 |
+
import gradio as gr # type: ignore
|
| 4 |
+
from dotenv import load_dotenv # type: ignore
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 8 |
+
|
| 9 |
+
load_dotenv()
|
| 10 |
+
|
| 11 |
+
task = 'text-classification'
|
| 12 |
+
model = 'SamLowe/roberta-base-go_emotions'
|
| 13 |
+
gitHubLink = 'https://github.com/pulkit-singhall'
|
| 14 |
+
linkedInLink = 'https://www.linkedin.com/in/pulkit-singhal-a8113822a/'
|
| 15 |
+
|
| 16 |
+
pipe = pipeline(task, model, device=device, framework='pt')
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def classify_text(text, top_results):
|
| 20 |
+
result = pipe(text, top_k = top_results) # array of dict
|
| 21 |
+
output = result[0]['label']
|
| 22 |
+
output = output[:1].upper() + output[1:]
|
| 23 |
+
|
| 24 |
+
for i in range(1,len(result)):
|
| 25 |
+
label = result[i]['label']
|
| 26 |
+
label = label[:1].upper() + label[1:]
|
| 27 |
+
output = '\n'.join([output, label])
|
| 28 |
+
return output
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
with gr.Blocks() as app:
|
| 32 |
+
gr.Markdown(value = 'Get emotions from any textual sentence...', height = 28)
|
| 33 |
+
with gr.Row():
|
| 34 |
+
with gr.Column():
|
| 35 |
+
sentence = gr.Textbox(label="English Sentence Here")
|
| 36 |
+
slider = gr.Slider(
|
| 37 |
+
label = 'Top Results you want', value = 1,
|
| 38 |
+
minimum = 1, maximum = 10, step = 1)
|
| 39 |
+
with gr.Row():
|
| 40 |
+
clear_btn = gr.ClearButton(components = [sentence], variant = 'secondary')
|
| 41 |
+
classify_btn = gr.Button(value="Submit", variant = 'primary')
|
| 42 |
+
with gr.Column():
|
| 43 |
+
result = gr.Textbox(label="Required Emotions")
|
| 44 |
+
|
| 45 |
+
classify_btn.click(classify_text, inputs=[sentence, slider], outputs=[result])
|
| 46 |
+
examples = gr.Examples(examples=["That movie was amazing but I did not like the actors.",
|
| 47 |
+
"Helen is a good swimmer.",
|
| 48 |
+
'What do you think about Elon Musk and his accomplishments?',
|
| 49 |
+
'Today was a horrible day'],
|
| 50 |
+
inputs=[sentence])
|
| 51 |
+
gr.Markdown(value = f'Check out my [GitHub]({gitHubLink}) and [LinkedIn]({linkedInLink})',
|
| 52 |
+
height = 28)
|
| 53 |
+
|
| 54 |
+
app.launch(share = True)
|