Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -24,17 +24,6 @@ tokenizer, model = load_model()
|
|
24 |
|
25 |
# Add sidebar with instructions
|
26 |
st.sidebar.title("Instructions: How to use")
|
27 |
-
# st.sidebar.write("""
|
28 |
-
# 1. Write something in the text area (a prompt or random text) or use the dropdown menu to select predefined sample text.
|
29 |
-
# 2. Select a task from the **task dropdown menu** below only if you are providing your own text. **This is very important as it ensures the model responds accordingly.**
|
30 |
-
# 3. If you are providing your own text, please do not select any predefined sample text from the dropdown menu.
|
31 |
-
# 3. If a dropdown menu pops up for a nigerian language, **select the nigerian language (it represents a base language for diacritization and text cleaning tasks & target language for translation task).**
|
32 |
-
# 4. Then, click the Generate button.\n
|
33 |
-
# 5. For Translation tasks, setting english as the target language yields the best result (english as base language performs the worse).
|
34 |
-
# **Note: Model's overall performance vary (hallucinates) due to model size and training data distribution. Performance may worsen with other task outside text generation and translation.
|
35 |
-
# For other tasks, we suggest you try them several times due to the generator's sampling parameter settings.**\n
|
36 |
-
# 6. Lastly, you can play with some of the generation parameters below to improve performance.
|
37 |
-
# """)
|
38 |
|
39 |
st.sidebar.write("""
|
40 |
1. **Write Text or Select Sample:**
|
@@ -55,14 +44,19 @@ st.sidebar.write("""
|
|
55 |
|
56 |
6. **Translation Tips:**
|
57 |
- English as the target language gives the best results.
|
|
|
58 |
|
59 |
7. **Performance Note:**
|
60 |
- The model's performance varies due to its size and training data. It performs best on text generation and translation.
|
61 |
-
- For other tasks, try multiple times
|
62 |
-
- It's best to read/translate the model's output completely first. Model can sometimes fail to stop generation after providing correct answers.
|
63 |
-
|
64 |
-
8. **
|
65 |
-
-
|
|
|
|
|
|
|
|
|
66 |
""")
|
67 |
|
68 |
max_length = 100
|
@@ -102,8 +96,9 @@ st.title("SabiYarn-125M : Generates text in multiple Nigerian languages.")
|
|
102 |
|
103 |
st.write("**Supported Languages: English, Yoruba, Igbo, Hausa, Pidgin, Efik, Urhobo, Fulfulde, Fulah. \nResults may not be coherent for less represented languages (i.e Efik, \
|
104 |
Urhobo, Fulfulde, Fulah).**")
|
105 |
-
st.write("**It
|
106 |
-
st.write("**
|
|
|
107 |
st.write("-" * 50)
|
108 |
|
109 |
|
@@ -155,14 +150,21 @@ async def generate_from_api(user_input, generation_config):
|
|
155 |
# Sample texts
|
156 |
sample_texts = {
|
157 |
"select":"",
|
|
|
|
|
|
|
|
|
|
|
158 |
"Hausa: Afirka tana da al'adu...": "Afirka tana da al'adu da harsuna masu yawa. Tana da albarkatu da wuraren yawon shakatawa masu ban mamaki.",
|
159 |
"Yoruba: Ìmọ̀ sáyẹ́nsì àti...": "Ìmọ̀ sáyẹ́nsì àti tẹ̀knọ́lójì ń ṣe émi lóore tó níye lori ní Áfíríkà. Ó ń fún àwọn ènìyàn ní ànfààní láti dá irọyin àti kí wọ́n lè ṣe àwọn nǹkan tuntun.",
|
160 |
"Efik: Oma Ede, Mi ji ogede...": "Oma Ede, Mi ji ogede mi a foroma orhorho edha meji ri eka. ",
|
161 |
"Igbo: N'ala Igbo ...": "N'ala Igbo, ọtụtụ ndị mmadụ kwenyere na e nwere mmiri ara na elu-ilu",
|
162 |
"urhobo: Eshare nana ri...":"Eshare nana ri vwo ẹguọnọ rẹ iyono rẹ Aristotle vẹ Plato na",
|
163 |
"Efik: Ke eyo ...":"Ke eyo Jesus ye mme mbet esie, etop emi ama ada ifụre ọsọk mme Jew oro esịt okobụn̄ọde ke ntak idiọkido ke Israel, oro ẹkenyụn̄ ẹdude ke mfụhọ ke itie-ufụn mme nsunsu ido edinam Ido Ukpono Mme Jew eke akpa isua ikie.",
|
164 |
-
"
|
165 |
-
"
|
|
|
|
|
166 |
"Translate 'how are you?' to Yoruba": "how are you?",
|
167 |
"Translate to pidgin": "Spain won the 2024 europa football cup. it was a tough one because they had to play very strong opponents in the quarter-finals, semi-finals and finals.",
|
168 |
"Translate 'Often, all Yoruba children...' to Yoruba": "Often, all Yoruba children take pride in speaking the Yoruba language.",
|
@@ -175,12 +177,20 @@ sample_texts = {
|
|
175 |
|
176 |
instruction_wrap = {
|
177 |
# "Translate 'Often, all Yoruba children...' to Yoruba":"<translate> Often, all Yoruba children take pride in speaking the Yoruba language. <yor>",
|
|
|
|
|
|
|
|
|
|
|
178 |
"Tell me a story in pidgin": "<prompt> Tell me a story in pidgin <response>:",
|
179 |
"Spain won the 2024 europa football cup. it was a tough one because they had to play very strong opponents in the quarter-finals, semi-finals and finals.": "<translate> Spain won the 2024 europa football cup. it was a tough one because they had to play very strong opponents in the quarter-finals, semi-finals and finals. <pcm>",
|
180 |
"how are you?": "<translate> how are you? <yor>:",
|
181 |
"Often, all Yoruba children take pride in speaking the Yoruba language.": "<translate> Often, all Yoruba children take pride in speaking the Yoruba language. <yor>",
|
182 |
"who are you?": "<prompt> who are you? <response>:",
|
|
|
183 |
"Speak Yoruba": "<prompt> Speak Yoruba <response>:",
|
|
|
|
|
184 |
"Anyi na-echefu oke ike." : "<classify> Anyi na-echefu oke ike. <sentiment>",
|
185 |
"Abin mamaki ne aikin da shugabaZn HNajeriya ybake yi. kCiF 39gaba Tda haRkGa sir!": "<clean> Abin mamaki ne aikin da shugabaZn HNajeriya ybake yi. kCiF 39gaba Tda haRkGa sir! <pcm>",
|
186 |
"E sun, Alaga, fun ise amalayi ti e n se ni Naijiria. E maa ba a lo, egbon!": "<diacritize> E sun, Alaga, fun ise amalayi ti e n se ni Naijiria. E maa ba a lo, egbon! <yor>",
|
@@ -209,9 +219,11 @@ task_options = {
|
|
209 |
"Translation": "<translate> {} ",
|
210 |
"Sentiment Classification": "<classify> {} <sentiment>:",
|
211 |
"Topic Classification": "<classify> {} <topic>",
|
212 |
-
"Instruction Following" : "<prompt> {} <response>:",
|
213 |
"Headline Generation": "<title> {} <headline>",
|
214 |
"Text Diacritization": "<diacritize> {} ",
|
|
|
|
|
215 |
"Text Cleaning": "<clean> {} "
|
216 |
}
|
217 |
|
@@ -223,9 +235,9 @@ language_options = {
|
|
223 |
"Ibo": "<ibo>",
|
224 |
"Pidgin": "<pcm>",
|
225 |
"English": "<eng>",
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
}
|
230 |
|
231 |
|
@@ -250,17 +262,15 @@ def wrap_text(text, task_value):
|
|
250 |
|
251 |
# Text input
|
252 |
user_input = st.text_area("Enter text below **(PLEASE, FIRST READ ALL INSTRUCTIONS IN THE SIDEBAR CAREFULLY FOR THE BEST EXPERIENCE)**: ", sample_texts.get(sample_text, sample_text))
|
253 |
-
if task != "select":
|
254 |
-
user_input = user_input + " "
|
255 |
|
256 |
-
|
|
|
|
|
257 |
print("Final user input: ", user_input)
|
258 |
|
259 |
if st.button("Generate"):
|
260 |
if user_input:
|
261 |
-
with st.spinner("Please wait..."):
|
262 |
-
# try:
|
263 |
-
# st.write("**Generated Text Below:**")
|
264 |
wrapped_input = wrap_text(user_input, task_value)
|
265 |
print("wrapped_input: ", wrapped_input)
|
266 |
generation_config["max_new_tokens"]= min(max_new_tokens, 1024 - len(tokenizer.tokenize(wrapped_input)))
|
@@ -281,7 +291,7 @@ if st.button("Generate"):
|
|
281 |
generated_text = generated_text.strip("\n")
|
282 |
print("Generated text: ", generated_text)
|
283 |
|
284 |
-
if task == "Sentiment Classification"
|
285 |
if generated_text.strip().lower().startswith("negative"):
|
286 |
generated_text = "Negative"
|
287 |
elif generated_text.strip().lower().startswith("positive"):
|
@@ -289,7 +299,7 @@ if st.button("Generate"):
|
|
289 |
elif generated_text.strip().lower().startswith("neutral"):
|
290 |
generated_text = "Neutral"
|
291 |
|
292 |
-
elif task == "Topic Classification"
|
293 |
generated_text = generated_text[:15]
|
294 |
print("split", generated_text.split(" ")[0], re.split(r"\.|\n|\*\*|\*", generated_text)[0], generated_text.split(" "))
|
295 |
generated_text = re.split(r"\.|\n|\*\*|\*", generated_text)[0]
|
@@ -297,7 +307,7 @@ if st.button("Generate"):
|
|
297 |
generated_text = asyncio.run(assign_topic(generated_text))
|
298 |
|
299 |
elif task == "Translation":
|
300 |
-
n_sentences = len(
|
301 |
generated_text = ".".join(re.split(r"\.|\n", generated_text)[:n_sentences])
|
302 |
|
303 |
full_output = st.empty()
|
@@ -311,7 +321,6 @@ if st.button("Generate"):
|
|
311 |
end_time = time.time()
|
312 |
time_diff = end_time - start_time
|
313 |
st.write("Time taken: ", time_diff , "seconds.")
|
314 |
-
|
315 |
-
# st.error(f"Error during text generation: {e}")
|
316 |
else:
|
317 |
st.write("Please enter some text to generate.")
|
|
|
24 |
|
25 |
# Add sidebar with instructions
|
26 |
st.sidebar.title("Instructions: How to use")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
st.sidebar.write("""
|
29 |
1. **Write Text or Select Sample:**
|
|
|
44 |
|
45 |
6. **Translation Tips:**
|
46 |
- English as the target language gives the best results.
|
47 |
+
- You can also test inter-language translation i.e yoruba to igbo
|
48 |
|
49 |
7. **Performance Note:**
|
50 |
- The model's performance varies due to its size and training data. It performs best on text generation and translation.
|
51 |
+
- For other tasks, try multiple times if model's output is not optimal (This is due to the generator's sampling parameter settings).
|
52 |
+
- It's best to read/understand/translate the model's output completely first. Model can sometimes fail to stop generation after providing correct answers.
|
53 |
+
|
54 |
+
8. **Other Tips:**
|
55 |
+
- Use simple instructions for instruction following.
|
56 |
+
- For question answering and generation, follow the structure in the corresponding sample text.
|
57 |
+
|
58 |
+
9. **Adjust Parameters:**
|
59 |
+
- Experiment with the generation parameters below to improve performance. However, default values are sufficient.
|
60 |
""")
|
61 |
|
62 |
max_length = 100
|
|
|
96 |
|
97 |
st.write("**Supported Languages: English, Yoruba, Igbo, Hausa, Pidgin, Efik, Urhobo, Fulfulde, Fulah. \nResults may not be coherent for less represented languages (i.e Efik, \
|
98 |
Urhobo, Fulfulde, Fulah).**")
|
99 |
+
st.write("**It takes a while (~25s) to return an output on the first 'generate' click. Avg response time: 1-2s
|
100 |
+
st.write("**Model outputs 50 tokens as default. Adjust in the side bar.**")
|
101 |
+
st.write("**For convenience, you can use chatgpt to provide input text and translate/evaluate model output.**")
|
102 |
st.write("-" * 50)
|
103 |
|
104 |
|
|
|
150 |
# Sample texts
|
151 |
sample_texts = {
|
152 |
"select":"",
|
153 |
+
"Me ya nuna?": "Me ya nuna?",
|
154 |
+
"Wetin dem dey call you?": "Wetin dem dey call you?",
|
155 |
+
"M nwere ike ịma onye ị bụ? Gịnị bụ njirimara gị?": "M nwere ike ịma onye ị bụ? Gịnị bụ njirimara gị?",
|
156 |
+
"Bawo ni, kini...": "Bawo ni, kini nkan ti o nilo lati maa mo bayi?",
|
157 |
+
"What are you called?": "What are you called?",
|
158 |
"Hausa: Afirka tana da al'adu...": "Afirka tana da al'adu da harsuna masu yawa. Tana da albarkatu da wuraren yawon shakatawa masu ban mamaki.",
|
159 |
"Yoruba: Ìmọ̀ sáyẹ́nsì àti...": "Ìmọ̀ sáyẹ́nsì àti tẹ̀knọ́lójì ń ṣe émi lóore tó níye lori ní Áfíríkà. Ó ń fún àwọn ènìyàn ní ànfààní láti dá irọyin àti kí wọ́n lè ṣe àwọn nǹkan tuntun.",
|
160 |
"Efik: Oma Ede, Mi ji ogede...": "Oma Ede, Mi ji ogede mi a foroma orhorho edha meji ri eka. ",
|
161 |
"Igbo: N'ala Igbo ...": "N'ala Igbo, ọtụtụ ndị mmadụ kwenyere na e nwere mmiri ara na elu-ilu",
|
162 |
"urhobo: Eshare nana ri...":"Eshare nana ri vwo ẹguọnọ rẹ iyono rẹ Aristotle vẹ Plato na",
|
163 |
"Efik: Ke eyo ...":"Ke eyo Jesus ye mme mbet esie, etop emi ama ada ifụre ọsọk mme Jew oro esịt okobụn̄ọde ke ntak idiọkido ke Israel, oro ẹkenyụn̄ ẹdude ke mfụhọ ke itie-ufụn mme nsunsu ido edinam Ido Ukpono Mme Jew eke akpa isua ikie.",
|
164 |
+
"Question Generation: Afghanistan ...": "Afghanistan has around 150 radio stations and over 50 television stations, which includes the state-owned RTA TV and various private channels such as TOLO and Shamshad TV. The first Afghan newspaper was published in 1906 and there are hundreds of print outlets today. By the 1920s, Radio Kabul was broadcasting local radio services. Television programs began airing in the early 1970s. Voice of America, BBC, and Radio Free Europe/Radio Liberty (RFE/RL) broadcast in both of Afghanistan's official languages.\n Considering this context, what question would you ask?",
|
165 |
+
"Instruct: Please narrate a story..": "Please narrate a short story in yoruba",
|
166 |
+
"Question-Answering: Kai found one ...": "Kai found one for sale online but it was too much money for her. Keeping the provided context in mind, please answer the subsequent question: What does Kai need to do before this? A. cheaper B. Open up her laptop C. save money",
|
167 |
+
|
168 |
"Translate 'how are you?' to Yoruba": "how are you?",
|
169 |
"Translate to pidgin": "Spain won the 2024 europa football cup. it was a tough one because they had to play very strong opponents in the quarter-finals, semi-finals and finals.",
|
170 |
"Translate 'Often, all Yoruba children...' to Yoruba": "Often, all Yoruba children take pride in speaking the Yoruba language.",
|
|
|
177 |
|
178 |
instruction_wrap = {
|
179 |
# "Translate 'Often, all Yoruba children...' to Yoruba":"<translate> Often, all Yoruba children take pride in speaking the Yoruba language. <yor>",
|
180 |
+
"Me ya nuna?":"<prompt> Me ya nuna? <response>:",
|
181 |
+
"Wetin dem dey call you?":"<prompt> Wetin dem dey call you? <response>:",
|
182 |
+
"M nwere ike ịma onye ị bụ? Gịnị bụ njirimara gị?":"<prompt> M nwere ike ịma onye ị bụ? Gịnị bụ njirimara gị? <response>:",
|
183 |
+
"What are you called?":"<prompt> What are you called? <response>:",
|
184 |
+
"Bawo ni, kini nkan ti o nilo lati maa mo bayi?":"<prompt> Bawo ni, kini nkan ti o nilo lati maa mo bayi? <response>:",
|
185 |
"Tell me a story in pidgin": "<prompt> Tell me a story in pidgin <response>:",
|
186 |
"Spain won the 2024 europa football cup. it was a tough one because they had to play very strong opponents in the quarter-finals, semi-finals and finals.": "<translate> Spain won the 2024 europa football cup. it was a tough one because they had to play very strong opponents in the quarter-finals, semi-finals and finals. <pcm>",
|
187 |
"how are you?": "<translate> how are you? <yor>:",
|
188 |
"Often, all Yoruba children take pride in speaking the Yoruba language.": "<translate> Often, all Yoruba children take pride in speaking the Yoruba language. <yor>",
|
189 |
"who are you?": "<prompt> who are you? <response>:",
|
190 |
+
"Kai found one for sale online but it was too much money for her. Keeping the provided context in mind, please answer the subsequent question: What does Kai need to do before this? A. cheaper B. Open up her laptop C. save money":"<prompt> Kai found one for sale online but it was too much money for her. Keeping the provided context in mind, please answer the subsequent question: What does Kai need to do before this? A. cheaper B. Open up her laptop C. save money <response>:",
|
191 |
"Speak Yoruba": "<prompt> Speak Yoruba <response>:",
|
192 |
+
"Please narrate a short story in yoruba":"<prompt> Please narrate a short story in yoruba <response>:",
|
193 |
+
"Afghanistan has around 150 radio stations and over 50 television stations, which includes the state-owned RTA TV and various private channels such as TOLO and Shamshad TV. The first Afghan newspaper was published in 1906 and there are hundreds of print outlets today. By the 1920s, Radio Kabul was broadcasting local radio services. Television programs began airing in the early 1970s. Voice of America, BBC, and Radio Free Europe/Radio Liberty (RFE/RL) broadcast in both of Afghanistan's official languages.\n Considering this context, what question would you ask?":"<prompt> Afghanistan has around 150 radio stations and over 50 television stations, which includes the state-owned RTA TV and various private channels such as TOLO and Shamshad TV. The first Afghan newspaper was published in 1906 and there are hundreds of print outlets today. By the 1920s, Radio Kabul was broadcasting local radio services. Television programs began airing in the early 1970s. Voice of America, BBC, and Radio Free Europe/Radio Liberty (RFE/RL) broadcast in both of Afghanistan's official languages.\n Considering this context, what question would you ask? <response>:"
|
194 |
"Anyi na-echefu oke ike." : "<classify> Anyi na-echefu oke ike. <sentiment>",
|
195 |
"Abin mamaki ne aikin da shugabaZn HNajeriya ybake yi. kCiF 39gaba Tda haRkGa sir!": "<clean> Abin mamaki ne aikin da shugabaZn HNajeriya ybake yi. kCiF 39gaba Tda haRkGa sir! <pcm>",
|
196 |
"E sun, Alaga, fun ise amalayi ti e n se ni Naijiria. E maa ba a lo, egbon!": "<diacritize> E sun, Alaga, fun ise amalayi ti e n se ni Naijiria. E maa ba a lo, egbon! <yor>",
|
|
|
219 |
"Translation": "<translate> {} ",
|
220 |
"Sentiment Classification": "<classify> {} <sentiment>:",
|
221 |
"Topic Classification": "<classify> {} <topic>",
|
222 |
+
"Simple Instruction Following" : "<prompt> {} <response>:",
|
223 |
"Headline Generation": "<title> {} <headline>",
|
224 |
"Text Diacritization": "<diacritize> {} ",
|
225 |
+
"Question Generation": "<prompt> {} <response>:",
|
226 |
+
"Question-Answering" : "<prompt> {} <response>:",
|
227 |
"Text Cleaning": "<clean> {} "
|
228 |
}
|
229 |
|
|
|
235 |
"Ibo": "<ibo>",
|
236 |
"Pidgin": "<pcm>",
|
237 |
"English": "<eng>",
|
238 |
+
"Efik": "<efi>",
|
239 |
+
"Urhobo": "<urh>",
|
240 |
+
"Fulah": "<ful>"
|
241 |
}
|
242 |
|
243 |
|
|
|
262 |
|
263 |
# Text input
|
264 |
user_input = st.text_area("Enter text below **(PLEASE, FIRST READ ALL INSTRUCTIONS IN THE SIDEBAR CAREFULLY FOR THE BEST EXPERIENCE)**: ", sample_texts.get(sample_text, sample_text))
|
|
|
|
|
265 |
|
266 |
+
if task == "select":
|
267 |
+
user_input = instruction_wrap.get(user_input, user_input)
|
268 |
+
|
269 |
print("Final user input: ", user_input)
|
270 |
|
271 |
if st.button("Generate"):
|
272 |
if user_input:
|
273 |
+
with st.spinner("Please wait..."):")
|
|
|
|
|
274 |
wrapped_input = wrap_text(user_input, task_value)
|
275 |
print("wrapped_input: ", wrapped_input)
|
276 |
generation_config["max_new_tokens"]= min(max_new_tokens, 1024 - len(tokenizer.tokenize(wrapped_input)))
|
|
|
291 |
generated_text = generated_text.strip("\n")
|
292 |
print("Generated text: ", generated_text)
|
293 |
|
294 |
+
if task == "Sentiment Classification" :
|
295 |
if generated_text.strip().lower().startswith("negative"):
|
296 |
generated_text = "Negative"
|
297 |
elif generated_text.strip().lower().startswith("positive"):
|
|
|
299 |
elif generated_text.strip().lower().startswith("neutral"):
|
300 |
generated_text = "Neutral"
|
301 |
|
302 |
+
elif task == "Topic Classification":
|
303 |
generated_text = generated_text[:15]
|
304 |
print("split", generated_text.split(" ")[0], re.split(r"\.|\n|\*\*|\*", generated_text)[0], generated_text.split(" "))
|
305 |
generated_text = re.split(r"\.|\n|\*\*|\*", generated_text)[0]
|
|
|
307 |
generated_text = asyncio.run(assign_topic(generated_text))
|
308 |
|
309 |
elif task == "Translation":
|
310 |
+
n_sentences = len(re.split(r"\.|\n", user_input))
|
311 |
generated_text = ".".join(re.split(r"\.|\n", generated_text)[:n_sentences])
|
312 |
|
313 |
full_output = st.empty()
|
|
|
321 |
end_time = time.time()
|
322 |
time_diff = end_time - start_time
|
323 |
st.write("Time taken: ", time_diff , "seconds.")
|
324 |
+
|
|
|
325 |
else:
|
326 |
st.write("Please enter some text to generate.")
|