BeardedMonster commited on
Commit
863cc12
1 Parent(s): 1197a30

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +126 -0
app.py ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig
3
+ import time
4
+ import requests
5
+ import json
6
+
7
+ repo_name = "BeardedMonster/SabiYarn-125M"
8
+
9
+ tokenizer = AutoTokenizer.from_pretrained(repo_name, trust_remote_code=True)
10
+
11
+ # Add sidebar with instructions
12
+ st.sidebar.title("Instructions: How to use")
13
+ st.sidebar.write("""
14
+ 1.These are the tags for languages: **<yor>, <ibo>, <hau>, <efi>, <urh>, <fuv>, <eng>, <pcm>**\n
15
+ 2. You can either input any string in the specified language:\n
16
+ - how are you?, Abin mamaki ne aikin da\n
17
+ Or you can prompt the model with questions or instructions using defined tags:\n
18
+ - **Instruction following:**
19
+ <prompt> who are you? <response>:, <prompt> speak yoruba <response>:\n
20
+ - **sentiment classification:**
21
+ <classify> I don't like apples <sentiment>\n
22
+ - **Topic classification:**
23
+ <classify> TEXT HERE <topic>\n
24
+ - **Title Generation:**
25
+ <title> TEXT HERE <headline>\n
26
+ - **Diacritize tex:t**
27
+ <diacritize> TEXT HERE <yor>, <diacritize> TEXT HERE <hau>\n
28
+ - **Clean text:**
29
+ <clean> TEXT HERE <hau>\n
30
+ **Note: Model's performance vary with prompts due to model size and training data distribution.**\n
31
+ 3. Lastly, you can play with some of the generation parameters below to improve performance.
32
+ """)
33
+
34
+ # Define generation configuration
35
+ max_length = st.sidebar.slider("Max Length", min_value=10, max_value=500, value=100)
36
+ num_beams = st.sidebar.slider("Number of Beams", min_value=1, max_value=10, value=5)
37
+ temperature = st.sidebar.slider("Temperature", min_value=0.1, max_value=2.0, value=0.9)
38
+ top_k = st.sidebar.slider("Top-K", min_value=1, max_value=100, value=50)
39
+ top_p = st.sidebar.slider("Top-P", min_value=0.1, max_value=1.0, value=0.95)
40
+ repetition_penalty = st.sidebar.slider("Repetition Penalty", min_value=1.0, max_value=10.0, value=2.0)
41
+ length_penalty = st.sidebar.slider("Length Penalty", min_value=0.1, max_value=10.0, value=1.7)
42
+ # early_stopping = st.sidebar.selectbox("Early Stopping", [True, False], index=0)
43
+
44
+ generation_config = {
45
+ "max_length": max_length,
46
+ "num_beams": num_beams,
47
+ "do_sample": True,
48
+ "temperature": temperature,
49
+ "top_k": top_k,
50
+ "top_p": top_p,
51
+ "repetition_penalty": repetition_penalty,
52
+ "length_penalty": length_penalty,
53
+ "early_stopping": True
54
+ }
55
+
56
+ # Streamlit app
57
+ st.title("SabiYarn-125M: Generates text in multiple Nigerian languages.")
58
+
59
+ 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, \
60
+ Urhobo, Fulfulde, Fulah).**")
61
+ st.write("**Model is running on CPU RAM. So, token generation might be slower (streaming not enabled).**")
62
+ st.write("**Avg Response time: 15 secs/ 50 tokens. Response time increase with input length**")
63
+ st.write("-" * 50)
64
+
65
+ def generate_from_api(user_input, generation_config):
66
+ url = " https://pauljeffrey--sabiyarn-fastapi-app.modal.run/predict"
67
+
68
+ payload = json.dumps({
69
+ "prompt": user_input,
70
+ "config": generation_config
71
+ })
72
+ headers = {
73
+ 'Content-Type': 'application/json'
74
+ }
75
+
76
+ response = requests.request("POST", url, headers=headers, data=payload)
77
+ return response.json()["generated_text"]
78
+
79
+ # Sample texts
80
+ sample_texts = {
81
+ "Tell me a story in pidgin": "<prompt> Tell me a story in pidgin <response>:",
82
+ "Translate 'how are you?' to Yoruba": "<prompt> Translate 'how are you?' to Yoruba <response>:",
83
+ "Classify the sentiment of 'I don't like apples'": "<classify> I don't like apples <sentiment>",
84
+ "Generate a title for 'This is a sample text'": "<title> This is a sample text <headline>"
85
+ }
86
+
87
+ # Task options
88
+ task_options = {
89
+ "Instruction following": "<prompt> {} <response>:",
90
+ "Sentiment classification": "<classify> {} <sentiment>",
91
+ "Topic classification": "<classify> {} <topic>",
92
+ "Title Generation": "<title> {} <headline>",
93
+ "Diacritize text": "<diacritize> {} <yor>",
94
+ "Clean text": "<clean> {} <hau>"
95
+ }
96
+
97
+ # Dropdown for sample text
98
+ sample_text = st.selectbox("Select a sample text to test the model:", list(sample_texts.keys()))
99
+
100
+ # Dropdown for tasks
101
+ task = st.selectbox("Select a task for the model:", list(task_options.keys()))
102
+
103
+ # Text input
104
+ user_input = st.text_area("Enter text below **(please, first read the instructions on how to use in the side bar)**: ", sample_texts[sample_text])
105
+
106
+ if st.button("Generate"):
107
+ if user_input:
108
+ try:
109
+ st.write("**Generated Text Below:**")
110
+ wrapped_input = task_options[task].format(user_input)
111
+ generated_text = generate_from_api(wrapped_input, generation_config)
112
+ full_output = st.empty()
113
+
114
+ start_time = time.time()
115
+ output = ""
116
+ for next_token in tokenizer.tokenize(generated_text):
117
+ output += tokenizer.convert_tokens_to_string([next_token])
118
+ full_output.text(output)
119
+ time.sleep(0.1)
120
+ end_time = time.time()
121
+ time_diff = end_time - start_time
122
+ st.write("Time taken: ", time_diff , "seconds.")
123
+ except Exception as e:
124
+ st.error(f"Error during text generation: {e}")
125
+ else:
126
+ st.write("Please enter some text to generate.")