Added some of my comments, They are dope
Browse files
app.py
CHANGED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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.
|
| 13 |
+
# And whats awesome is Tokens will be generated following the configurations and requirements of model which we will be using.
|
| 14 |
+
|
| 15 |
+
# AutoModelForCausalLM is a powerful and convenient class serves as a high-level interface for loading pre-trained transformer models specifically designed for causal language modeling.
|
| 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
|
| 38 |
+
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 = {
|
| 51 |
+
"Funny": f"Generate a funny response to: {prompt}",
|
| 52 |
+
"Serious": f"Provide a serious and thoughtful response to: {prompt}",
|
| 53 |
+
"Poetic": f"Write a poetic response to: {prompt}"
|
| 54 |
+
}
|
| 55 |
+
input_text = tone_prompts.get(tone, prompt)
|
| 56 |
+
|
| 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)
|
| 66 |
+
|
| 67 |
+
# Custom CSS for styling
|
| 68 |
+
st.markdown("""
|
| 69 |
+
<style>
|
| 70 |
+
body {background-color: #f0f0f0;}
|
| 71 |
+
.title {color: #2c3e50; font-size: 36px; font-weight: bold;}
|
| 72 |
+
.instructions {color: #34495e; font-size: 18px;}
|
| 73 |
+
.output-box {background-color: #ecf0f1; padding: 10px; border-radius: 5px;}
|
| 74 |
+
</style>
|
| 75 |
+
""", unsafe_allow_html=True)
|
| 76 |
+
|
| 77 |
+
# App header with image
|
| 78 |
+
st.image("https://unsplash.com/photos/8xznAGy4HcY/download?force=true&w=640", caption="AI in Action")
|
| 79 |
+
st.markdown('<p class="title">Gemma Text Generator</p>', unsafe_allow_html=True)
|
| 80 |
+
|
| 81 |
+
# Instructions and example
|
| 82 |
+
st.markdown("""
|
| 83 |
+
<p class="instructions">
|
| 84 |
+
Enter a prompt below to generate text using the Gemma model from DeepMind. Customize the tone and length to see different outputs!<br>
|
| 85 |
+
<b>Example:</b> Prompt: "The cat sat on" | Tone: "Funny" | Length: 50 → "The cat sat on my homework and laughed as I cried over my grades."
|
| 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'")
|
| 92 |
+
tone = st.selectbox("Tone", ["Funny", "Serious", "Poetic"])
|
| 93 |
+
max_length = st.slider("Word count", 20, 100, 50)
|
| 94 |
+
submit_button = st.form_submit_button(label="Generate")
|
| 95 |
+
|
| 96 |
+
# Generate and display output
|
| 97 |
+
if submit_button:
|
| 98 |
+
if not prompt:
|
| 99 |
+
st.error("Please enter a prompt!")
|
| 100 |
+
else:
|
| 101 |
+
with st.spinner("Generating text..."):
|
| 102 |
+
output = generate_text(prompt, tone, max_length)
|
| 103 |
+
st.markdown(f'<div class="output-box">{output}</div>', unsafe_allow_html=True)
|
| 104 |
+
|
| 105 |
+
# Generate and display word cloud
|
| 106 |
+
wordcloud = WordCloud(width=400, height=200, background_color="white").generate(output)
|
| 107 |
+
plt.figure(figsize=(8, 4))
|
| 108 |
+
plt.imshow(wordcloud, interpolation="bilinear")
|
| 109 |
+
plt.axis("off")
|
| 110 |
+
st.pyplot(plt)
|
| 111 |
+
|
| 112 |
+
# Footer
|
| 113 |
+
st.markdown("---")
|
| 114 |
+
st.write("Built with ❤️ by Saiyan Utkarsh Shukla for GSoC 2025 | Powered by Gemma and Hugging Face")
|