Update app.py
Browse files
app.py
CHANGED
|
@@ -38,19 +38,31 @@ model = ORTModelForSequenceClassification.from_pretrained(model_id)
|
|
| 38 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 39 |
|
| 40 |
# Set top_k=3 to get the top 3 results
|
| 41 |
-
|
|
|
|
| 42 |
|
| 43 |
def classify_text(text):
|
| 44 |
-
|
| 45 |
-
output = ""
|
| 46 |
|
| 47 |
-
#
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
return output
|
| 53 |
|
|
|
|
| 54 |
gr.Interface(
|
| 55 |
fn=classify_text,
|
| 56 |
title="Sentiment Classifier",
|
|
@@ -66,4 +78,4 @@ gr.Interface(
|
|
| 66 |
["I am deeply disappointed in your bad performance in last league match loss, and quite disappointed, sad because of it."],
|
| 67 |
["I am very happy with your excellent performance!"]
|
| 68 |
]
|
| 69 |
-
).launch()
|
|
|
|
| 38 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 39 |
|
| 40 |
# Set top_k=3 to get the top 3 results
|
| 41 |
+
# Define the pipeline
|
| 42 |
+
pipe = pipeline(task="text-classification", model=model, tokenizer=tokenizer)
|
| 43 |
|
| 44 |
def classify_text(text):
|
| 45 |
+
start_time = time.time()
|
|
|
|
| 46 |
|
| 47 |
+
# Get results with all scores returned
|
| 48 |
+
results = pipe(text, return_all_scores=True)
|
| 49 |
+
|
| 50 |
+
end_time = time.time()
|
| 51 |
|
| 52 |
+
output = f"Sentence: {text}\n"
|
| 53 |
+
|
| 54 |
+
# Sort results by score in descending order
|
| 55 |
+
sorted_results = sorted(results[0], key=lambda x: x['score'], reverse=True)
|
| 56 |
+
|
| 57 |
+
# Print the top 3 highest-scoring labels and scores
|
| 58 |
+
for i, result in enumerate(sorted_results[:3]): # Limiting to the top 3 results
|
| 59 |
+
output += f"Label {i+1}: {result['label']}, Score: {result['score']:.4f}\n"
|
| 60 |
+
|
| 61 |
+
output += f"Generation time: {end_time - start_time:.2f} seconds\n"
|
| 62 |
+
|
| 63 |
return output
|
| 64 |
|
| 65 |
+
# Gradio Interface
|
| 66 |
gr.Interface(
|
| 67 |
fn=classify_text,
|
| 68 |
title="Sentiment Classifier",
|
|
|
|
| 78 |
["I am deeply disappointed in your bad performance in last league match loss, and quite disappointed, sad because of it."],
|
| 79 |
["I am very happy with your excellent performance!"]
|
| 80 |
]
|
| 81 |
+
).launch()
|