Spaces:
Sleeping
Sleeping
A-Duss
commited on
Commit
·
2678b8c
1
Parent(s):
7c1e17d
Test multi lingual function
Browse files- app.py +54 -21
- requirements.txt +3 -1
app.py
CHANGED
|
@@ -1,7 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from shitsu import ShitsuScorer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
|
|
|
|
|
|
| 5 |
|
| 6 |
example_inputs = [
|
| 7 |
"The Beatles were a popular band in the 1960s. They released many hit songs.",
|
|
@@ -11,13 +46,14 @@ example_inputs = [
|
|
| 11 |
"Can you believe it's already September? This year is flying by!"
|
| 12 |
]
|
| 13 |
|
| 14 |
-
def get_score(user_text):
|
| 15 |
-
score =
|
| 16 |
formatted_score = f"{score:.4g}"
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
#language_options = ['am', 'ar', 'bg', 'bn', 'cs', 'da', 'de', 'el', 'en', 'es', 'fa', 'fi', 'fr', 'gu', 'ha', 'hi', 'hu', 'id', 'it', 'ja', 'jv', 'kn', 'ko', 'lt', 'mr', 'nl', 'no', 'yo', 'zh']
|
| 20 |
-
#https://huggingface.co/spaces/Dusduo/shitsu-text-scorer-demo/shitsu-logo.jpeg
|
| 21 |
css = '''
|
| 22 |
#gen_btn{height: 100%}
|
| 23 |
#title{text-align: center}
|
|
@@ -40,26 +76,32 @@ css = '''
|
|
| 40 |
justify-content: center;
|
| 41 |
}
|
| 42 |
'''
|
|
|
|
| 43 |
theme = gr.themes.Soft(
|
| 44 |
primary_hue="blue",
|
| 45 |
secondary_hue="sky",
|
| 46 |
)
|
| 47 |
|
| 48 |
-
#"https://huggingface.co/spaces/Dusduo/shitsu-text-scorer-demo/shitsu-logo.jpeg"
|
| 49 |
with gr.Blocks(theme=theme, css=css) as demo:
|
| 50 |
-
|
| 51 |
title = gr.HTML(
|
| 52 |
"""<h1><img src="https://huggingface.co/spaces/Dusduo/shitsu-text-scorer-demo/resolve/main/shitsu-logo.jpeg" alt="LightBlue"> Shitsu Text Scorer</h1>""",
|
| 53 |
elem_id="title",
|
| 54 |
)
|
| 55 |
gr.Markdown(
|
| 56 |
-
"""This is a demo of [Shitsu text scorer](https://huggingface.co/lightblue/shitsu_text_scorer) for
|
| 57 |
|
| 58 |
It outputs a score generally between 0 and 1 but can exceed both of these bounds as it is a regressor.
|
| 59 |
"""
|
| 60 |
)
|
| 61 |
with gr.Row():
|
| 62 |
user_text = gr.Textbox(label='Input text', placeholder='Type something here...')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
with gr.Column(scale=0):
|
| 64 |
submit_btn = gr.Button("Submit")
|
| 65 |
score = gr.HTML(
|
|
@@ -67,16 +109,9 @@ with gr.Blocks(theme=theme, css=css) as demo:
|
|
| 67 |
label="Output"
|
| 68 |
)
|
| 69 |
|
| 70 |
-
|
| 71 |
-
gr.Examples(examples=example_inputs, inputs=user_text)
|
| 72 |
|
| 73 |
-
|
| 74 |
-
# language_choice = gr.Dropdown(
|
| 75 |
-
# choices=language_options,
|
| 76 |
-
# label="Choose a language",
|
| 77 |
-
# info="Type to search",
|
| 78 |
-
# allow_custom_value=True, # Allows typing to search
|
| 79 |
-
# )
|
| 80 |
|
| 81 |
gr.Markdown(
|
| 82 |
"""
|
|
@@ -88,8 +123,6 @@ with gr.Blocks(theme=theme, css=css) as demo:
|
|
| 88 |
"""
|
| 89 |
)
|
| 90 |
|
| 91 |
-
|
| 92 |
-
user_text.submit(get_score, inputs=[user_text], outputs=[score])
|
| 93 |
-
submit_btn.click(get_score, inputs=[user_text], outputs=[score])
|
| 94 |
|
| 95 |
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from collections import OrderedDict
|
| 3 |
+
os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"
|
| 4 |
+
|
| 5 |
import gradio as gr
|
| 6 |
from shitsu import ShitsuScorer
|
| 7 |
+
from huggingface_hub import hf_hub_download
|
| 8 |
+
|
| 9 |
+
class OptimizedShitsuScorer:
|
| 10 |
+
def __init__(self, max_models=2):
|
| 11 |
+
self.scorers = OrderedDict()
|
| 12 |
+
self.max_models = max_models
|
| 13 |
+
self.current_language = None
|
| 14 |
+
|
| 15 |
+
def get_scorer(self, language):
|
| 16 |
+
if language in self.scorers:
|
| 17 |
+
# Move the accessed language to the end (most recently used)
|
| 18 |
+
self.scorers.move_to_end(language)
|
| 19 |
+
else:
|
| 20 |
+
# If we're at capacity, remove the least recently used model
|
| 21 |
+
if len(self.scorers) >= self.max_models:
|
| 22 |
+
self.scorers.popitem(last=False)
|
| 23 |
+
|
| 24 |
+
# Load the new model
|
| 25 |
+
self.scorers[language] = ShitsuScorer(language)
|
| 26 |
+
|
| 27 |
+
self.current_language = language
|
| 28 |
+
return self.scorers[language]
|
| 29 |
+
|
| 30 |
+
def score(self, text, language):
|
| 31 |
+
scorer = self.get_scorer(language)
|
| 32 |
+
return scorer.score([text])[0]
|
| 33 |
+
|
| 34 |
+
def get_loaded_languages(self):
|
| 35 |
+
return list(self.scorers.keys())
|
| 36 |
|
| 37 |
+
optimized_scorer = OptimizedShitsuScorer(max_models=2)
|
| 38 |
+
# Preload English model
|
| 39 |
+
optimized_scorer.get_scorer('en')
|
| 40 |
|
| 41 |
example_inputs = [
|
| 42 |
"The Beatles were a popular band in the 1960s. They released many hit songs.",
|
|
|
|
| 46 |
"Can you believe it's already September? This year is flying by!"
|
| 47 |
]
|
| 48 |
|
| 49 |
+
def get_score(user_text, language):
|
| 50 |
+
score = optimized_scorer.score(user_text, language)
|
| 51 |
formatted_score = f"{score:.4g}"
|
| 52 |
+
loaded_languages = optimized_scorer.get_loaded_languages()
|
| 53 |
+
return f'<div class="nice-box"> Score: {formatted_score}</div>', f"Currently loaded languages: {', '.join(loaded_languages)}"
|
| 54 |
+
|
| 55 |
+
language_options = ['am', 'ar', 'bg', 'bn', 'cs', 'da', 'de', 'el', 'en', 'es', 'fa', 'fi', 'fr', 'gu', 'ha', 'hi', 'hu', 'id', 'it', 'ja', 'jv', 'kn', 'ko', 'lt', 'mr', 'nl', 'no', 'yo', 'zh']
|
| 56 |
|
|
|
|
|
|
|
| 57 |
css = '''
|
| 58 |
#gen_btn{height: 100%}
|
| 59 |
#title{text-align: center}
|
|
|
|
| 76 |
justify-content: center;
|
| 77 |
}
|
| 78 |
'''
|
| 79 |
+
|
| 80 |
theme = gr.themes.Soft(
|
| 81 |
primary_hue="blue",
|
| 82 |
secondary_hue="sky",
|
| 83 |
)
|
| 84 |
|
|
|
|
| 85 |
with gr.Blocks(theme=theme, css=css) as demo:
|
|
|
|
| 86 |
title = gr.HTML(
|
| 87 |
"""<h1><img src="https://huggingface.co/spaces/Dusduo/shitsu-text-scorer-demo/resolve/main/shitsu-logo.jpeg" alt="LightBlue"> Shitsu Text Scorer</h1>""",
|
| 88 |
elem_id="title",
|
| 89 |
)
|
| 90 |
gr.Markdown(
|
| 91 |
+
"""This is a demo of [Shitsu text scorer](https://huggingface.co/lightblue/shitsu_text_scorer) for multiple languages, which scores text based on the amount of useful, textbook-like information in it.
|
| 92 |
|
| 93 |
It outputs a score generally between 0 and 1 but can exceed both of these bounds as it is a regressor.
|
| 94 |
"""
|
| 95 |
)
|
| 96 |
with gr.Row():
|
| 97 |
user_text = gr.Textbox(label='Input text', placeholder='Type something here...')
|
| 98 |
+
language_choice = gr.Dropdown(
|
| 99 |
+
choices=language_options,
|
| 100 |
+
label="Choose a language",
|
| 101 |
+
info="Type to search",
|
| 102 |
+
value="en",
|
| 103 |
+
allow_custom_value=True,
|
| 104 |
+
)
|
| 105 |
with gr.Column(scale=0):
|
| 106 |
submit_btn = gr.Button("Submit")
|
| 107 |
score = gr.HTML(
|
|
|
|
| 109 |
label="Output"
|
| 110 |
)
|
| 111 |
|
| 112 |
+
loaded_languages = gr.Markdown("Currently loaded languages: en")
|
|
|
|
| 113 |
|
| 114 |
+
gr.Examples(examples=example_inputs, inputs=user_text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
gr.Markdown(
|
| 117 |
"""
|
|
|
|
| 123 |
"""
|
| 124 |
)
|
| 125 |
|
| 126 |
+
submit_btn.click(get_score, inputs=[user_text, language_choice], outputs=[score, loaded_languages])
|
|
|
|
|
|
|
| 127 |
|
| 128 |
demo.launch()
|
requirements.txt
CHANGED
|
@@ -1 +1,3 @@
|
|
| 1 |
-
git+https://github.com/lightblue-tech/shitsu.git
|
|
|
|
|
|
|
|
|
| 1 |
+
git+https://github.com/lightblue-tech/shitsu.git
|
| 2 |
+
hf-transfer
|
| 3 |
+
huggingface_hub[hf_transfer]
|