Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import time
|
| 2 |
+
from transformers import AutoTokenizer, pipeline
|
| 3 |
+
from optimum.onnxruntime import ORTModelForSequenceClassification
|
| 4 |
+
import gradio as gr
|
| 5 |
+
# with gr.Blocks(css=".gradio-container {background-color: red}") as demo:# with gr.Blocks(css=".gradio-container {background: url('file=clouds.jpg')}") as demo:# css = """
|
| 6 |
+
|
| 7 |
+
# with gr.Blocks(css=css) as demo:# box1 = gr.Textbox(value="Good Job", elem_classes="feedback")# box2 = gr.Textbox(value="Failure", elem_id="warning", elem_classes="feedback")
|
| 8 |
+
model_id = "HassamAliCADI/SentimentOnx"
|
| 9 |
+
|
| 10 |
+
model = ORTModelForSequenceClassification.from_pretrained(model_id)
|
| 11 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 12 |
+
# with gr.Blocks(css=".gradio-container {background-color: red}") as demo:# with gr.Blocks(css=".gradio-container {background: url('file=clouds.jpg')}") as demo:# css = """
|
| 13 |
+
|
| 14 |
+
# with gr.Blocks(css=css) as demo:# box1 = gr.Textbox(value="Good Job", elem_classes="feedback")# box2 = gr.Textbox(value="Failure", elem_id="warning", elem_classes="feedback")
|
| 15 |
+
pipe = pipeline(task="text-classification", model=model, tokenizer=tokenizer)
|
| 16 |
+
|
| 17 |
+
def classify_text(text):
|
| 18 |
+
start_time = time.time()
|
| 19 |
+
results = pipe(text)
|
| 20 |
+
end_time = time.time()
|
| 21 |
+
|
| 22 |
+
# with gr.Blocks(css=".gradio-container {background-color: red}") as demo:# with gr.Blocks(css=".gradio-container {background: url('file=clouds.jpg')}") as demo:# css = """
|
| 23 |
+
|
| 24 |
+
# with gr.Blocks(css=css) as demo:# box1 = gr.Textbox(value="Good Job", elem_classes="feedback")# box2 = gr.Textbox(value="Failure", elem_id="warning", elem_classes="feedback")
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
# #warning {background-color: #FFCCCB}# .feedback textarea {font-size: 24px !important}# """
|
| 28 |
+
|
| 29 |
+
output = ""
|
| 30 |
+
for result in results:
|
| 31 |
+
output += f"Label: {result['label']}, Score: {result['score']:.4f}\n"
|
| 32 |
+
output += f"\nGeneration time: {end_time - start_time:.2f} seconds"
|
| 33 |
+
return output
|
| 34 |
+
|
| 35 |
+
gr.Interface(
|
| 36 |
+
|
| 37 |
+
# with gr.Blocks(css=".gradio-container {background-color: red}") as demo:# with gr.Blocks(css=".gradio-container {background: url('file=clouds.jpg')}") as demo:# css = """
|
| 38 |
+
|
| 39 |
+
# with gr.Blocks(css=css) as demo:# box1 = gr.Textbox(value="Good Job", elem_classes="feedback")# box2 = gr.Textbox(value="Failure", elem_id="warning", elem_classes="feedback")
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
# #warning {background-color: #FFCCCB}# .feedback textarea {font-size: 24px !important}# """
|
| 43 |
+
|
| 44 |
+
fn=classify_text,
|
| 45 |
+
title="Sentiment Classifier",
|
| 46 |
+
description="Enter text to classify sentiment",
|
| 47 |
+
inputs=gr.Textbox(
|
| 48 |
+
label="Input Text",
|
| 49 |
+
placeholder="Type something here..."
|
| 50 |
+
),
|
| 51 |
+
outputs=gr.Textbox(
|
| 52 |
+
label="Classification Results"
|
| 53 |
+
),
|
| 54 |
+
examples=[
|
| 55 |
+
["I am deeply disappointed in your bad performance in last league match loss, and quite disappointed, sad because of it."],
|
| 56 |
+
["I am very happy with your excellent performance!"]
|
| 57 |
+
]
|
| 58 |
+
).launch()
|