HamzaNaser's picture
Update app.py
a33eefb verified
import requests
import time
from PIL import Image
import gradio as gr
hold_time = time.time()
API_URL = "https://cm7kxsqi3sekfih7.us-east-1.aws.endpoints.huggingface.cloud"
headers = {
"Accept" : "application/json",
"Content-Type": "application/json"
}
def query(payload):
global hold_time
response = requests.post(API_URL, headers=headers, json=payload)
if response.status_code != 200:
print('Sleeping due to API error')
if (time.time() - hold_time) > 60:
hold_time = time.time()
return None
return response.json()
def run_model(Dialects):
global hold_time
output = query({
"inputs": Dialects,
"parameters": {}
})
if output:
hold_time = 1
return output[0]['generated_text']
else:
wait_time = int((hold_time - time.time()) + 35)
if wait_time >= 0:
return f'Model is being loaded, please try again in {wait_time} seconds.'
else:
return 'Taking longer than usual to load, please wait.'
response = requests.post(API_URL, headers=headers, json={
"inputs": 'احا',
"parameters": {}
})
if response.status_code != 200:
print('Sleeping due to Model is being loaded')
time.sleep(40)
examples_text = [
["ما ابغا أروح الإمتحان"],
["أييد أن انام ف لبيتنا"],
["Hello how are you today"],
["kef al7al"]
]
def mode_run(text):
result = run_model(text)
return result
link = '<a href="{}" target="_blank" style="cursor: pointer; font-size: 18px;">{}</a>'
with gr.Blocks(theme=gr.themes.GoogleFont('ali')) as demo:
gr.Markdown(
"""
## Dialects to MSA transformer
Start typing Non-Traditional Arabic to convert into Classical version.
"""
)
with gr.Row():
with gr.Column():
input = gr.Textbox(label='Dialects')
with gr.Column():
output = gr.Textbox(label='MSA')
with gr.Row():
button = gr.Button('Submit',variant='primary')
clear = gr.ClearButton(input)
examples = gr.Examples(examples_text,input,output,mode_run,cache_examples=True)
with gr.Row():
gr.Markdown(
"""
## Model Overview
This Model is optimized to convert written text in various non Standard Classical Arabic into Classic Arabic, the model was Fine-Tuned on 0.8M pairs of sentence generated by OpenAI API gpt-4o-mini Text Generation Model, beside being able to convert Dialects into Classical Arabic, the model can also be used in other NLP tasks such as Text Correction, Diacretization and Sentence Punctuation.
"""
)
with gr.Row():
gr.Markdown(
"""
## Dielcts the Model trained on
Below image shows an estimate of dialects the model trained on.
"""
)
with gr.Row():
with gr.Column(scale=3):
gr.Image(Image.open('Dialects by Region.png'),height=300,container=False)
with gr.Column(scale=3):
pass
with gr.Row():
with gr.Column(scale=1):
pass
with gr.Column(scale=2):
gr.HTML(
'<div style="text-align: center;">' +\
link.format('https://huggingface.co/HamzaNaser/Dialects-to-MSA-Transformer', 'Model Card') + ' -- '+link.format('https://www.linkedin.com/in/hamza-naser-0b4b90160/', 'LinkedIn') +\
'</div>'
)
with gr.Column(scale=1):
pass
input.submit(mode_run,input,output)
button.click(mode_run,input,output)
demo.launch()