Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
| 2 |
+
import evaluate
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import os
|
| 6 |
+
from datasets import load_dataset, Dataset, concatenate_datasets
|
| 7 |
+
import datetime
|
| 8 |
+
|
| 9 |
+
# Load metrics
|
| 10 |
+
bleu = evaluate.load("bleu")
|
| 11 |
+
meteor = evaluate.load("meteor")
|
| 12 |
+
chrf = evaluate.load("chrf")
|
| 13 |
+
|
| 14 |
+
# Leaderboard file
|
| 15 |
+
|
| 16 |
+
scoreDatasetName = "NAMAA-Space/egyptian_leaderboard_scores"
|
| 17 |
+
requestsDatasetName = "NAMAA-Space/egyptian_leaderboard_requests"
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def show_leaderboard():
|
| 22 |
+
leaderboard = load_dataset(scoreDatasetName, split="train").to_pandas()
|
| 23 |
+
## only leave unique with hightst bleu value
|
| 24 |
+
leaderboard = leaderboard.groupby("model_name").agg({"bleu": "max", "meteor": "max", "chrf": "max"}).reset_index()
|
| 25 |
+
|
| 26 |
+
return leaderboard.sort_values(by="bleu", ascending=False)
|
| 27 |
+
|
| 28 |
+
def submit_to_leaderboard(model_name, model_type, revision, model_prompt):
|
| 29 |
+
|
| 30 |
+
requestsDataset = load_dataset(requestsDatasetName, split="train")
|
| 31 |
+
|
| 32 |
+
found = requestsDataset.filter(lambda x: x["model_name"] == model_name)
|
| 33 |
+
|
| 34 |
+
if len(found) > 0:
|
| 35 |
+
return "Model already submitted to the leaderboard"
|
| 36 |
+
|
| 37 |
+
newRequest = Dataset.from_list([{
|
| 38 |
+
"model_name": model_name,
|
| 39 |
+
"submission_time": pd.Timestamp.now(),
|
| 40 |
+
"model_type": model_type,
|
| 41 |
+
"status": "pending",
|
| 42 |
+
"revision": revision,
|
| 43 |
+
"prompt": model_prompt
|
| 44 |
+
}])
|
| 45 |
+
|
| 46 |
+
finalRequestsDataset = concatenate_datasets([ requestsDataset , newRequest])
|
| 47 |
+
|
| 48 |
+
finalRequestsDataset.push_to_hub(requestsDatasetName, private=True)
|
| 49 |
+
|
| 50 |
+
return "Model submitted to the leaderboard"
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
# Gradio Interface
|
| 54 |
+
with gr.Blocks() as demo:
|
| 55 |
+
gr.Markdown("# English-to-Egyptian Arabic Translation Leaderboard")
|
| 56 |
+
|
| 57 |
+
with gr.Row():
|
| 58 |
+
with gr.Column():
|
| 59 |
+
model_input = gr.Textbox(label="Model Name", placeholder="Enter the model's Hugging Face name...")
|
| 60 |
+
## model type translation or text-generation
|
| 61 |
+
model_type = gr.Radio(choices=["translation", "text-generation"], label="Model Type")
|
| 62 |
+
|
| 63 |
+
revision = gr.Textbox(label="Revision", placeholder="Enter the model's revision...")
|
| 64 |
+
|
| 65 |
+
model_prompt = gr.Textbox(label="Model Prompt", placeholder="Enter the model's prompt if it's a text-generation model as prefix...")
|
| 66 |
+
|
| 67 |
+
submit_btn = gr.Button("Submit")
|
| 68 |
+
|
| 69 |
+
message = gr.Textbox(label="Message")
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
# leaderboard_btn = gr.Button("Show Leaderboard")/
|
| 73 |
+
leaderboard_output = gr.DataFrame(label="Leaderboard", value=show_leaderboard())
|
| 74 |
+
|
| 75 |
+
submit_btn.click(
|
| 76 |
+
submit_to_leaderboard,
|
| 77 |
+
inputs=[model_input, model_type, revision, model_prompt],
|
| 78 |
+
outputs=[message]
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
demo.launch()
|
| 83 |
+
|