BeardedMonster commited on
Commit
9cee6a8
·
verified ·
1 Parent(s): 1cd1646

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -10
app.py CHANGED
@@ -1,14 +1,18 @@
 
1
  import streamlit as st
2
- from transformers import AutoTokenizer
3
  import time
4
  import asyncio
5
  import aiohttp
6
  import json
 
7
 
8
  repo_name = "BeardedMonster/SabiYarn-125M"
9
-
10
  tokenizer = AutoTokenizer.from_pretrained(repo_name, trust_remote_code=True)
11
 
 
 
12
  # Add sidebar with instructions
13
  st.sidebar.title("Instructions: How to use")
14
  st.sidebar.write("""
@@ -48,8 +52,8 @@ st.title("SabiYarn-125M: Generates text in multiple Nigerian languages.")
48
 
49
  st.write("**Supported Languages: English, Yoruba, Igbo, Hausa, Pidgin, Efik, Urhobo, Fulfulde, Fulah. \nResults might not be coherent for less represented languages (i.e Efik, \
50
  Urhobo, Fulfulde, Fulah).**")
51
- # st.write("**Model is running on CPU RAM. So, token generation might be slower (streaming not enabled).**")
52
- st.write("**Avg Response time: <3 seconds/ 50 tokens. Response time increase with input length**")
53
  st.write("-" * 50)
54
 
55
  async def generate_from_api(user_input, generation_config):
@@ -72,13 +76,13 @@ async def generate_from_api(user_input, generation_config):
72
  # Sample texts
73
  sample_texts = {
74
  "Tell me a story in pidgin": "Tell me a story in pidgin",
75
- "Oma Ede, Mi ji ogede...": "Oma Ede, Mi ji ogede mi a foroma orhorho edha meji ri eka. "
76
  "who are you?": "who are you?",
77
  "Translate 'how are you?' to Yoruba": "Translate 'how are you?' to Yoruba",
78
  "Classify the sentiment": "Anyi na-echefu oke ike.",
79
  "what is the topic of this text": "Africa Free Trade Zone: Kò sí ìdènà láti kó ọjà láti orílẹ̀èdè kan sí òmíràn",
80
  "diacritize this text: ": "E sun, Alaga, fun ise amalayi ti e n se ni Naijiria. E maa ba a lo, egbon!",
81
- "clean this text": "Di service say sheVbin doE tlykto find out how Zelensky wak^ g3 be before im go visit MykolailXwey flood fKg Junew Zele?sky confam s,y rem don inform a2 abouo di frrest, im sHy dR_head of SBU bin don update aI abou.Wdi "fiMht againsX traitorsfLZelevsky bin visi# Mykolaiv for June tk see di dam&ge wey di collapse of KakhoYka dam "ause Rgd den im v:sit again for July afta Russia1heavy bo+bing. Di 8ecurity sBrviceTs1y dem bin know/Mbout di plan bev9re di ;isit, snd dat onG make put vdditional security measurZs{",
82
  "headline of this text": '** Sylvain Itté French ambassador don comot Niger Republic **. Sylvain Itté, di French ambassador for Niger don comot Niamey and currently e dey for flight from Ndjamena to Paris. Sylvain Itté, di French ambassador for Niger don comot Niamey very early dis morning and currently e dey for flight from Ndjamena to Paris.\n\nDi military detain Bazoum and im family for di presidential palace. Niger na former French colony, and France still get 1,500 sojas for di African country.\n\n"France don decide to withdraw dia ambassador. In di next hours our ambassador and several diplomats go return to France," Oga Macron tok.\n\nE add say di military co-operation dey "over" and French troops go leave in "di months to come".\n\n"Dis Sunday we celebrate one new step towards di sovereignty of Niger," di junta tok, for one statement wey AFP news agency quote.\n\nDi decision by Paris dey come afta months of hostility and protest against di presence of French for di kontri, wit regular demonstrations for di capital Niamey.\n\nDi move don scata France operations against Islamist militants for di wider Sahel region and Paris influence for there. But oga Macron tok say "putschists no go hold France hostage,"'
83
  }
84
 
@@ -87,8 +91,8 @@ instruction_wrap = {
87
  "Translate 'how are you?' to Yoruba": "<prompt> Translate 'how are you?' to Yoruba <response>:",
88
  "who are you?": "<prompt> who are you? <response>:",
89
  "Classify the sentiment" : "<classify> Anyi na-echefu oke ike. <sentiment>",
90
- "clean this text": "<clean> <pcm>",
91
- "diacritize this text: ": "<diacritize> <yor>"
92
  "what is the topic of this text": "<classify> Africa Free Trade Zone: Kò sí ìdènà láti kó ọjà láti orílẹ̀èdè kan sí òmíràn <topic>",
93
  'headline of this text': '<title> ** Sylvain Itté French ambassador don comot Niger Republic **. Sylvain Itté, di French ambassador for Niger don comot Niamey and currently e dey for flight from Ndjamena to Paris. Sylvain Itté, di French ambassador for Niger don comot Niamey very early dis morning and currently e dey for flight from Ndjamena to Paris.\n\nDi military detain Bazoum and im family for di presidential palace. Niger na former French colony, and France still get 1,500 sojas for di African country.\n\n"France don decide to withdraw dia ambassador. In di next hours our ambassador and several diplomats go return to France," Oga Macron tok.\n\nE add say di military co-operation dey "over" and French troops go leave in "di months to come".\n\n"Dis Sunday we celebrate one new step towards di sovereignty of Niger," di junta tok, for one statement wey AFP news agency quote.\n\nDi decision by Paris dey come afta months of hostility and protest against di presence of French for di kontri, wit regular demonstrations for di capital Niamey.\n\nDi move don scata France operations against Islamist militants for di wider Sahel region and Paris influence for there. But oga Macron tok say "putschists no go hold France hostage," <headline>',
94
  }
@@ -145,7 +149,17 @@ if st.button("Generate"):
145
  wrapped_input = wrap_text(user_input)
146
  print("wrapped_input: ", wrapped_input)
147
  generation_config["max_new_tokens"]= min(max_new_tokens, 1024 - len(tokenizer.tokenize(wrapped_input)))
148
- generated_text = asyncio.run(generate_from_api(wrapped_input, generation_config))
 
 
 
 
 
 
 
 
 
 
149
  full_output = st.empty()
150
 
151
  start_time = time.time()
@@ -161,4 +175,4 @@ if st.button("Generate"):
161
  except Exception as e:
162
  st.error(f"Error during text generation: {e}")
163
  else:
164
- st.write("Please enter some text to generate.")
 
1
+
2
  import streamlit as st
3
+ from transformers import AutoTokenizer, AutoModelForCausalLM
4
  import time
5
  import asyncio
6
  import aiohttp
7
  import json
8
+ import torch
9
 
10
  repo_name = "BeardedMonster/SabiYarn-125M"
11
+ device = "cuda" if torch.cuda.is_available() else "cpu"
12
  tokenizer = AutoTokenizer.from_pretrained(repo_name, trust_remote_code=True)
13
 
14
+ model = AutoModelForCausalLM.from_pretrained(repo_name, trust_remote_code=True).to(device)
15
+
16
  # Add sidebar with instructions
17
  st.sidebar.title("Instructions: How to use")
18
  st.sidebar.write("""
 
52
 
53
  st.write("**Supported Languages: English, Yoruba, Igbo, Hausa, Pidgin, Efik, Urhobo, Fulfulde, Fulah. \nResults might not be coherent for less represented languages (i.e Efik, \
54
  Urhobo, Fulfulde, Fulah).**")
55
+ # st.write("****")
56
+ st.write("**For convenience, you can use chatgpt to copy text and evaluate model output.**")
57
  st.write("-" * 50)
58
 
59
  async def generate_from_api(user_input, generation_config):
 
76
  # Sample texts
77
  sample_texts = {
78
  "Tell me a story in pidgin": "Tell me a story in pidgin",
79
+ "Oma Ede, Mi ji ogede...": "Oma Ede, Mi ji ogede mi a foroma orhorho edha meji ri eka. ",
80
  "who are you?": "who are you?",
81
  "Translate 'how are you?' to Yoruba": "Translate 'how are you?' to Yoruba",
82
  "Classify the sentiment": "Anyi na-echefu oke ike.",
83
  "what is the topic of this text": "Africa Free Trade Zone: Kò sí ìdènà láti kó ọjà láti orílẹ̀èdè kan sí òmíràn",
84
  "diacritize this text: ": "E sun, Alaga, fun ise amalayi ti e n se ni Naijiria. E maa ba a lo, egbon!",
85
+ "clean this text": "Di service say sheVbin doE tlykto find out how Zelensky wak^ g3 be before im go visit MykolailXwey flood fKg Junew Zele?sky confam s,y rem don inform a2 abouo di frrest, im sHy dR_head of SBU bin don update aI abou.Wdi 'fiMht againsX traitorsfLZelevsky bin visi# Mykolaiv for June tk see di dam&ge wey di collapse of KakhoYka dam 'ause Rgd den im v:sit again for July afta Russia1heavy bo+bing. Di 8ecurity sBrviceTs1y dem bin know/Mbout di plan bev9re di ;isit, snd dat onG make put vdditional security measurZs{",
86
  "headline of this text": '** Sylvain Itté French ambassador don comot Niger Republic **. Sylvain Itté, di French ambassador for Niger don comot Niamey and currently e dey for flight from Ndjamena to Paris. Sylvain Itté, di French ambassador for Niger don comot Niamey very early dis morning and currently e dey for flight from Ndjamena to Paris.\n\nDi military detain Bazoum and im family for di presidential palace. Niger na former French colony, and France still get 1,500 sojas for di African country.\n\n"France don decide to withdraw dia ambassador. In di next hours our ambassador and several diplomats go return to France," Oga Macron tok.\n\nE add say di military co-operation dey "over" and French troops go leave in "di months to come".\n\n"Dis Sunday we celebrate one new step towards di sovereignty of Niger," di junta tok, for one statement wey AFP news agency quote.\n\nDi decision by Paris dey come afta months of hostility and protest against di presence of French for di kontri, wit regular demonstrations for di capital Niamey.\n\nDi move don scata France operations against Islamist militants for di wider Sahel region and Paris influence for there. But oga Macron tok say "putschists no go hold France hostage,"'
87
  }
88
 
 
91
  "Translate 'how are you?' to Yoruba": "<prompt> Translate 'how are you?' to Yoruba <response>:",
92
  "who are you?": "<prompt> who are you? <response>:",
93
  "Classify the sentiment" : "<classify> Anyi na-echefu oke ike. <sentiment>",
94
+ "clean this text": "<clean> Di service say sheVbin doE tlykto find out how Zelensky wak^ g3 be before im go visit MykolailXwey flood fKg Junew Zele?sky confam s,y rem don inform a2 abouo di frrest, im sHy dR_head of SBU bin don update aI abou.Wdi 'fiMht againsX traitorsfLZelevsky bin visi# Mykolaiv for June tk see di dam&ge wey di collapse of KakhoYka dam 'ause Rgd den im v:sit again for July afta Russia1heavy bo+bing. Di 8ecurity sBrviceTs1y dem bin know/Mbout di plan bev9re di ;isit, snd dat onG make put vdditional security measurZs{ <pcm>",
95
+ "diacritize this text: ": "<diacritize> E sun, Alaga, fun ise amalayi ti e n se ni Naijiria. E maa ba a lo, egbon! <yor>",
96
  "what is the topic of this text": "<classify> Africa Free Trade Zone: Kò sí ìdènà láti kó ọjà láti orílẹ̀èdè kan sí òmíràn <topic>",
97
  'headline of this text': '<title> ** Sylvain Itté French ambassador don comot Niger Republic **. Sylvain Itté, di French ambassador for Niger don comot Niamey and currently e dey for flight from Ndjamena to Paris. Sylvain Itté, di French ambassador for Niger don comot Niamey very early dis morning and currently e dey for flight from Ndjamena to Paris.\n\nDi military detain Bazoum and im family for di presidential palace. Niger na former French colony, and France still get 1,500 sojas for di African country.\n\n"France don decide to withdraw dia ambassador. In di next hours our ambassador and several diplomats go return to France," Oga Macron tok.\n\nE add say di military co-operation dey "over" and French troops go leave in "di months to come".\n\n"Dis Sunday we celebrate one new step towards di sovereignty of Niger," di junta tok, for one statement wey AFP news agency quote.\n\nDi decision by Paris dey come afta months of hostility and protest against di presence of French for di kontri, wit regular demonstrations for di capital Niamey.\n\nDi move don scata France operations against Islamist militants for di wider Sahel region and Paris influence for there. But oga Macron tok say "putschists no go hold France hostage," <headline>',
98
  }
 
149
  wrapped_input = wrap_text(user_input)
150
  print("wrapped_input: ", wrapped_input)
151
  generation_config["max_new_tokens"]= min(max_new_tokens, 1024 - len(tokenizer.tokenize(wrapped_input)))
152
+ try:
153
+ # Attempt the asynchronous API call
154
+ generation_config["max_new_tokens"] = min(max_new_tokens, 1024 - len(tokenizer.tokenize(wrapped_input)))
155
+ generated_text = asyncio.run(generate_from_api(wrapped_input, generation_config))
156
+ except Exception as e:
157
+ print(f"API call failed: {e}. Using local model for text generation.")
158
+ # Use the locally loaded model for text generation
159
+ input_ids = tokenizer(wrapped_input, return_tensors="pt")["input_ids"].to(device)
160
+ output = model.generate(input_ids, **generation_config)
161
+ generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
162
+
163
  full_output = st.empty()
164
 
165
  start_time = time.time()
 
175
  except Exception as e:
176
  st.error(f"Error during text generation: {e}")
177
  else:
178
+ st.write("Please enter some text to generate.")