Kakaarot commited on
Commit
51c00b5
·
verified ·
1 Parent(s): fa24e24

Added Temperature, Top-p (Nucleus Sampling), Repetition Penalty with one click example and basic user guide

Browse files
Files changed (1) hide show
  1. app.py +47 -8
app.py CHANGED
@@ -1,12 +1,14 @@
1
  # Hey its your saiyan Utkarsh Shukla. I gonna write my custom comments after each line of code. So even a beginner (previous me) can read and get whats going on.
2
  # Bear with me, I got your Back buddy
3
 
 
4
  import streamlit as st
5
  # The saviour web app creator, easy peasy web app creation by few lines of codes.
6
- # No HTML, CSS, or JS needed!"
 
7
 
8
  from transformers import AutoTokenizer, AutoModelForCausalLM
9
- # transformers here is just library which gives us access to Transformer architecture based pretrained models for natural language processing and other tasks. For now, Think of Tranformer Architecture, as mystery Architecture which makes models way cooler.
10
 
11
 
12
  # AutoTokenizer helps in Text input -> Sentences -> Words -> Even subwords like ['un', 'break', 'able'] -> Integer IDs that model expects.
@@ -16,22 +18,27 @@ from transformers import AutoTokenizer, AutoModelForCausalLM
16
  # The cooler part of these Auto* classes are you don't need to know exact class name of model(like GPT2LMHeadModel, CTRLLMHeadModel, ReformerLMHeadModel, etc.).
17
  # Meaning The AutoModelForCausalLM automatically determine the correct model architecture based on the pretrained_model_name_or_path you provide. AutoModelForCausalLM infers this from the configuration files associated with the pre-trained model.
18
 
 
19
  from wordcloud import WordCloud
20
  # This will help us in knowing which words have large frequency. It creates a visual representation of words used, know as Word Cloud.
21
  # More the frequency + More the importance -> Word will appear larger in Word Cloud.
22
  # Mostly it avoids our stop words like it, is, are etc
23
-
24
 
25
  import matplotlib.pyplot as plt
26
  # This guy helps us to plot. So wait till you see it.
 
 
27
 
28
  import torch
29
- # This import makes a library pyTorch available in our python code.
 
30
  # Think of it as a toolkit which can do maths very very efficiently is being available for our code now.
31
 
 
32
  @st.cache_resource
33
  # A decorator in python is a way to enhance a function or a class. As they are followed by @ symbol
34
- # The function above whome they are specified the decorator code is executed both before and after of function code on function call.
35
  # Now here @st.cache_resource decorator is used before loading AutoTokenizer and AutoModelForCasualLM from gemm-2b.
36
  # Cache the model and tokenizer to avoid reloading on every run
37
  # So first run will load and save resources to global cache, and as user interact and causes rerun of load_model_and_tokenizer(), instead of loading again it will directly use cached resources from memory
@@ -39,12 +46,15 @@ def load_model_and_tokenizer():
39
  model_name = "google/gemma-2b" # using gemma-2b for prototype for my GSOC Proposal. Wish me luck.
40
  tokenizer = AutoTokenizer.from_pretrained(model_name)
41
  # Responsible for automatically downloading and loading the tokenizer configuration and vocabulary associated with the specified pre-trained model.
 
42
  model = AutoModelForCausalLM.from_pretrained(model_name)
43
  # As we discussed, this class is designed for loading pre-trained language models for causal (next-token prediction) tasks.
 
44
  return tokenizer, model
45
 
 
46
  # Function to generate text with Gemma
47
- def generate_text(prompt, tone, max_length):
48
  tokenizer, model = load_model_and_tokenizer()
49
  # Adjust prompt based on tone
50
  tone_prompts = {
@@ -57,9 +67,11 @@ def generate_text(prompt, tone, max_length):
57
  inputs = tokenizer(input_text, return_tensors="pt")
58
  outputs = model.generate(
59
  inputs["input_ids"],
60
- max_length=max_length + len(input_text.split()), # Account for prompt length
 
 
 
61
  num_return_sequences=1,
62
- temperature=0.7, # Creativity level
63
  do_sample=True
64
  )
65
  return tokenizer.decode(outputs[0], skip_special_tokens=True)
@@ -86,6 +98,33 @@ st.markdown("""
86
  </p>
87
  """, unsafe_allow_html=True)
88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
  # User input section
90
  with st.form(key="input_form"):
91
  prompt = st.text_input("Enter a prompt", placeholder="e.g., 'The future of AI is'")
 
1
  # Hey its your saiyan Utkarsh Shukla. I gonna write my custom comments after each line of code. So even a beginner (previous me) can read and get whats going on.
2
  # Bear with me, I got your Back buddy
3
 
4
+
5
  import streamlit as st
6
  # The saviour web app creator, easy peasy web app creation by few lines of codes.
7
+ # No HTML, CSS, or JS needed!
8
+
9
 
10
  from transformers import AutoTokenizer, AutoModelForCausalLM
11
+ # transformers here is just library which gives us access to Transformer architecture based pretrained models for natural language processing and other tasks. For now, Think of Transformer Architecture, as mystery Architecture which makes models way cooler.
12
 
13
 
14
  # AutoTokenizer helps in Text input -> Sentences -> Words -> Even subwords like ['un', 'break', 'able'] -> Integer IDs that model expects.
 
18
  # The cooler part of these Auto* classes are you don't need to know exact class name of model(like GPT2LMHeadModel, CTRLLMHeadModel, ReformerLMHeadModel, etc.).
19
  # Meaning The AutoModelForCausalLM automatically determine the correct model architecture based on the pretrained_model_name_or_path you provide. AutoModelForCausalLM infers this from the configuration files associated with the pre-trained model.
20
 
21
+
22
  from wordcloud import WordCloud
23
  # This will help us in knowing which words have large frequency. It creates a visual representation of words used, know as Word Cloud.
24
  # More the frequency + More the importance -> Word will appear larger in Word Cloud.
25
  # Mostly it avoids our stop words like it, is, are etc
26
+ # More frequency = more importance → bigger word in the cloud.
27
 
28
  import matplotlib.pyplot as plt
29
  # This guy helps us to plot. So wait till you see it.
30
+ # We’ll use it to show our Word Cloud in style.
31
+
32
 
33
  import torch
34
+ # This import makes a library pyTorch available in our python code.
35
+ # This makes the PyTorch library available — a powerful math engine and deep learning framework our model runs on.
36
  # Think of it as a toolkit which can do maths very very efficiently is being available for our code now.
37
 
38
+
39
  @st.cache_resource
40
  # A decorator in python is a way to enhance a function or a class. As they are followed by @ symbol
41
+ # The function above whome they are specified, the decorator code is executed both before and after of function code, on function call.
42
  # Now here @st.cache_resource decorator is used before loading AutoTokenizer and AutoModelForCasualLM from gemm-2b.
43
  # Cache the model and tokenizer to avoid reloading on every run
44
  # So first run will load and save resources to global cache, and as user interact and causes rerun of load_model_and_tokenizer(), instead of loading again it will directly use cached resources from memory
 
46
  model_name = "google/gemma-2b" # using gemma-2b for prototype for my GSOC Proposal. Wish me luck.
47
  tokenizer = AutoTokenizer.from_pretrained(model_name)
48
  # Responsible for automatically downloading and loading the tokenizer configuration and vocabulary associated with the specified pre-trained model.
49
+ # Downloads and loads the tokenizer config and vocab for the given model
50
  model = AutoModelForCausalLM.from_pretrained(model_name)
51
  # As we discussed, this class is designed for loading pre-trained language models for causal (next-token prediction) tasks.
52
+ # Loads the actual model used for causal (next-word) prediction tasks
53
  return tokenizer, model
54
 
55
+
56
  # Function to generate text with Gemma
57
+ def generate_text(prompt, tone, max_length, temperature=0.5, top_p=0.8, repetition_penalty=1.0):
58
  tokenizer, model = load_model_and_tokenizer()
59
  # Adjust prompt based on tone
60
  tone_prompts = {
 
67
  inputs = tokenizer(input_text, return_tensors="pt")
68
  outputs = model.generate(
69
  inputs["input_ids"],
70
+ max_length=max_length + len(input_text.split()),
71
+ temperature=temperature,
72
+ top_p=top_p,
73
+ repetition_penalty=repetition_penalty,
74
  num_return_sequences=1,
 
75
  do_sample=True
76
  )
77
  return tokenizer.decode(outputs[0], skip_special_tokens=True)
 
98
  </p>
99
  """, unsafe_allow_html=True)
100
 
101
+
102
+ # Beginner friendly explanation block
103
+ with st.expander("\U0001F9E0 How does this work? Click to peek inside."):
104
+ st.markdown("""
105
+ - This app uses **Gemma-2B**, a language model from Google DeepMind.
106
+ - You give it a prompt, and it predicts the next words one-by-one (aka causal language modeling).
107
+ - The **tone** you choose adds flavor to the prompt before it hits the model.
108
+ - Parameters like **temperature** control how wild or safe the answers are.
109
+ - The output is visualized in a **Word Cloud** so you can see which words stand out!
110
+ """)
111
+
112
+ # One-click examples
113
+ example_clicked = None
114
+ col1, col2 = st.columns(2)
115
+ with col1:
116
+ if st.button("Try Funny Cat Story"):
117
+ prompt = "The cat hacked my WiFi"
118
+ tone = "Funny"
119
+ example_clicked = True
120
+ with col2:
121
+ if st.button("Try Poetic Goodbye"):
122
+ prompt = "As the sun set on our final day"
123
+ tone = "Poetic"
124
+ example_clicked = True
125
+
126
+
127
+
128
  # User input section
129
  with st.form(key="input_form"):
130
  prompt = st.text_input("Enter a prompt", placeholder="e.g., 'The future of AI is'")