Kakaarot commited on
Commit
61d921e
·
verified ·
1 Parent(s): e622245

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -106
app.py CHANGED
@@ -1,106 +0,0 @@
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
-
30
- # Cache the model and tokenizer to avoid reloading on every run
31
- @st.cache_resource
32
- def load_model_and_tokenizer():
33
- model_name = "google/gemma-2b" # Replace with "gemma-7b" if desired
34
- tokenizer = AutoTokenizer.from_pretrained(model_name)
35
- model = AutoModelForCausalLM.from_pretrained(model_name)
36
- return tokenizer, model
37
-
38
- # Function to generate text with Gemma
39
- def generate_text(prompt, tone, max_length):
40
- tokenizer, model = load_model_and_tokenizer()
41
- # Adjust prompt based on tone
42
- tone_prompts = {
43
- "Funny": f"Generate a funny response to: {prompt}",
44
- "Serious": f"Provide a serious and thoughtful response to: {prompt}",
45
- "Poetic": f"Write a poetic response to: {prompt}"
46
- }
47
- input_text = tone_prompts.get(tone, prompt)
48
-
49
- inputs = tokenizer(input_text, return_tensors="pt")
50
- outputs = model.generate(
51
- inputs["input_ids"],
52
- max_length=max_length + len(input_text.split()), # Account for prompt length
53
- num_return_sequences=1,
54
- temperature=0.7, # Creativity level
55
- do_sample=True
56
- )
57
- return tokenizer.decode(outputs[0], skip_special_tokens=True)
58
-
59
- # Custom CSS for styling
60
- st.markdown("""
61
- <style>
62
- body {background-color: #f0f0f0;}
63
- .title {color: #2c3e50; font-size: 36px; font-weight: bold;}
64
- .instructions {color: #34495e; font-size: 18px;}
65
- .output-box {background-color: #ecf0f1; padding: 10px; border-radius: 5px;}
66
- </style>
67
- """, unsafe_allow_html=True)
68
-
69
- # App header with image
70
- st.image("https://unsplash.com/photos/8xznAGy4HcY/download?force=true&w=640", caption="AI in Action")
71
- st.markdown('<p class="title">Gemma Text Generator</p>', unsafe_allow_html=True)
72
-
73
- # Instructions and example
74
- st.markdown("""
75
- <p class="instructions">
76
- Enter a prompt below to generate text using the Gemma model from DeepMind. Customize the tone and length to see different outputs!<br>
77
- <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."
78
- </p>
79
- """, unsafe_allow_html=True)
80
-
81
- # User input section
82
- with st.form(key="input_form"):
83
- prompt = st.text_input("Enter a prompt", placeholder="e.g., 'The future of AI is'")
84
- tone = st.selectbox("Tone", ["Funny", "Serious", "Poetic"])
85
- max_length = st.slider("Word count", 20, 100, 50)
86
- submit_button = st.form_submit_button(label="Generate")
87
-
88
- # Generate and display output
89
- if submit_button:
90
- if not prompt:
91
- st.error("Please enter a prompt!")
92
- else:
93
- with st.spinner("Generating text..."):
94
- output = generate_text(prompt, tone, max_length)
95
- st.markdown(f'<div class="output-box">{output}</div>', unsafe_allow_html=True)
96
-
97
- # Generate and display word cloud
98
- wordcloud = WordCloud(width=400, height=200, background_color="white").generate(output)
99
- plt.figure(figsize=(8, 4))
100
- plt.imshow(wordcloud, interpolation="bilinear")
101
- plt.axis("off")
102
- st.pyplot(plt)
103
-
104
- # Footer
105
- st.markdown("---")
106
- st.write("Built with ❤️ by Saiyan Utkarsh Shukla for GSoC 2025 | Powered by Gemma and Hugging Face")