Spaces:
Sleeping
Sleeping
Initial commit for Hugging Face Spaces
Browse files- app.py +216 -0
- requirements.txt +7 -0
app.py
ADDED
@@ -0,0 +1,216 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
+
import gradio as gr
|
4 |
+
import re
|
5 |
+
|
6 |
+
# Set page title and description
|
7 |
+
title = "π Pickup Line Generator"
|
8 |
+
description = """
|
9 |
+
<div style="text-align: center; max-width: 650px; margin: 0 auto;">
|
10 |
+
<div>
|
11 |
+
<p>Generate fun, clever, or cringey pickup lines using SmolLM-135M! Select a vibe and click generate to get started! π</p>
|
12 |
+
</div>
|
13 |
+
</div>
|
14 |
+
"""
|
15 |
+
|
16 |
+
# Load model and tokenizer
|
17 |
+
print("Loading SmolLM-135M model...")
|
18 |
+
MODEL_NAME = "HuggingFaceTB/SmolLM-135M"
|
19 |
+
|
20 |
+
# Check for CUDA availability
|
21 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
22 |
+
print(f"Using device: {device}")
|
23 |
+
|
24 |
+
# Load the model and tokenizer
|
25 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
26 |
+
# Set pad_token to eos_token to handle padding
|
27 |
+
tokenizer.pad_token = tokenizer.eos_token
|
28 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME).to(device)
|
29 |
+
|
30 |
+
print(f"Model loaded successfully! Memory footprint: {model.get_memory_footprint() / 1e6:.2f} MB")
|
31 |
+
|
32 |
+
def get_vibe_guidance(vibe):
|
33 |
+
"""Get specific guidance for each vibe with examples"""
|
34 |
+
vibe_patterns = {
|
35 |
+
"romantic": """Generate a romantic and sweet pickup line that's genuine and heartfelt.
|
36 |
+
Example:
|
37 |
+
Input: Generate a romantic pickup line
|
38 |
+
Output: Are you a magician? Because whenever I look at you, everyone else disappears. β€οΈ
|
39 |
+
|
40 |
+
Now generate a romantic pickup line: """,
|
41 |
+
|
42 |
+
"cheesy": """Generate a super cheesy and over-the-top pickup line.
|
43 |
+
Example:
|
44 |
+
Input: Generate a cheesy pickup line
|
45 |
+
Output: Are you a parking ticket? Because you've got FINE written all over you! π
|
46 |
+
|
47 |
+
Now generate a cheesy pickup line: """,
|
48 |
+
|
49 |
+
"nerdy": """Generate a nerdy, science-themed pickup line.
|
50 |
+
Example:
|
51 |
+
Input: Generate a nerdy pickup line
|
52 |
+
Output: Are you made of copper and tellurium? Because you're Cu-Te! π¬
|
53 |
+
|
54 |
+
Now generate a nerdy pickup line: """,
|
55 |
+
|
56 |
+
"cringe": """Generate the most cringey and over-the-top pickup line imaginable.
|
57 |
+
Example:
|
58 |
+
Input: Generate a cringe pickup line
|
59 |
+
Output: Are you a dictionary? Because you're adding meaning to my life! π
|
60 |
+
|
61 |
+
Now generate a cringe pickup line: """,
|
62 |
+
|
63 |
+
"flirty": """Generate a bold and flirty pickup line.
|
64 |
+
Example:
|
65 |
+
Input: Generate a flirty pickup line
|
66 |
+
Output: Is your name Google? Because you've got everything I've been searching for! π
|
67 |
+
|
68 |
+
Now generate a flirty pickup line: """
|
69 |
+
}
|
70 |
+
return vibe_patterns.get(vibe, "Generate a pickup line with a ")
|
71 |
+
|
72 |
+
def generate_pickup_line(vibe):
|
73 |
+
"""Generate a pickup line based on the selected vibe"""
|
74 |
+
# Get the vibe guidance
|
75 |
+
vibe_guide = get_vibe_guidance(vibe)
|
76 |
+
|
77 |
+
# Create the prompt
|
78 |
+
prompt = f"""Instructions: Generate a pickup line with a {vibe} vibe.
|
79 |
+
{vibe_guide}"""
|
80 |
+
|
81 |
+
# Prepare inputs with explicit attention mask
|
82 |
+
encoded_input = tokenizer.encode_plus(
|
83 |
+
prompt,
|
84 |
+
return_tensors="pt",
|
85 |
+
padding=True,
|
86 |
+
return_attention_mask=True
|
87 |
+
)
|
88 |
+
input_ids = encoded_input["input_ids"].to(device)
|
89 |
+
attention_mask = encoded_input["attention_mask"].to(device)
|
90 |
+
|
91 |
+
# Generate multiple responses and pick the best one
|
92 |
+
num_tries = 3
|
93 |
+
best_response = None
|
94 |
+
|
95 |
+
for _ in range(num_tries):
|
96 |
+
with torch.no_grad():
|
97 |
+
outputs = model.generate(
|
98 |
+
input_ids,
|
99 |
+
attention_mask=attention_mask,
|
100 |
+
max_new_tokens=100,
|
101 |
+
do_sample=True,
|
102 |
+
temperature=0.8,
|
103 |
+
top_p=0.92,
|
104 |
+
top_k=50,
|
105 |
+
pad_token_id=tokenizer.eos_token_id,
|
106 |
+
eos_token_id=tokenizer.eos_token_id,
|
107 |
+
)
|
108 |
+
|
109 |
+
# Get the full generated text
|
110 |
+
full_response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
111 |
+
|
112 |
+
# Extract just the pickup line
|
113 |
+
if full_response.startswith(prompt):
|
114 |
+
response = full_response[len(prompt):].strip()
|
115 |
+
else:
|
116 |
+
response = full_response.replace(prompt, "").strip()
|
117 |
+
|
118 |
+
# Clean up the response
|
119 |
+
for marker in ["Instructions:", "Generate a pickup line:", "\n"]:
|
120 |
+
if marker in response:
|
121 |
+
response = response.split(marker, 1)[0].strip()
|
122 |
+
|
123 |
+
# Add appropriate emoji based on vibe
|
124 |
+
if vibe == "romantic":
|
125 |
+
response += " β€οΈ"
|
126 |
+
elif vibe == "cheesy":
|
127 |
+
response += " π"
|
128 |
+
elif vibe == "nerdy":
|
129 |
+
response += " π¬"
|
130 |
+
elif vibe == "cringe":
|
131 |
+
response += " π"
|
132 |
+
elif vibe == "flirty":
|
133 |
+
response += " π"
|
134 |
+
|
135 |
+
best_response = response
|
136 |
+
break
|
137 |
+
|
138 |
+
return best_response
|
139 |
+
|
140 |
+
# Create custom CSS
|
141 |
+
custom_css = """
|
142 |
+
.gradio-container {
|
143 |
+
background-color: #fef6f9 !important;
|
144 |
+
}
|
145 |
+
.title {
|
146 |
+
font-family: 'Lobster', cursive !important;
|
147 |
+
color: #ff69b4 !important;
|
148 |
+
}
|
149 |
+
.button {
|
150 |
+
background: linear-gradient(45deg, #ff69b4, #ff1493) !important;
|
151 |
+
color: white !important;
|
152 |
+
border: none !important;
|
153 |
+
transition: all 0.3s ease !important;
|
154 |
+
}
|
155 |
+
.button:hover {
|
156 |
+
transform: translateY(-2px);
|
157 |
+
box-shadow: 0 4px 8px rgba(255, 105, 180, 0.3);
|
158 |
+
}
|
159 |
+
"""
|
160 |
+
|
161 |
+
# Create the Gradio interface
|
162 |
+
with gr.Blocks(theme="soft", css=custom_css) as demo:
|
163 |
+
gr.Markdown(f"# {title}")
|
164 |
+
gr.Markdown(description)
|
165 |
+
|
166 |
+
with gr.Row():
|
167 |
+
with gr.Column():
|
168 |
+
vibe_dropdown = gr.Dropdown(
|
169 |
+
choices=[
|
170 |
+
"romantic",
|
171 |
+
"cheesy",
|
172 |
+
"nerdy",
|
173 |
+
"cringe",
|
174 |
+
"flirty"
|
175 |
+
],
|
176 |
+
label="Pick a vibe",
|
177 |
+
value="romantic"
|
178 |
+
)
|
179 |
+
generate_btn = gr.Button("Generate Line", elem_classes="button")
|
180 |
+
|
181 |
+
with gr.Column():
|
182 |
+
output = gr.Textbox(
|
183 |
+
label="Your pickup line",
|
184 |
+
lines=3,
|
185 |
+
interactive=False
|
186 |
+
)
|
187 |
+
copy_btn = gr.Button("π Copy to Clipboard", elem_classes="button")
|
188 |
+
|
189 |
+
# Example inputs
|
190 |
+
gr.Examples(
|
191 |
+
examples=[
|
192 |
+
["romantic"],
|
193 |
+
["cheesy"],
|
194 |
+
["nerdy"],
|
195 |
+
["cringe"],
|
196 |
+
["flirty"]
|
197 |
+
],
|
198 |
+
inputs=[vibe_dropdown]
|
199 |
+
)
|
200 |
+
|
201 |
+
generate_btn.click(
|
202 |
+
fn=generate_pickup_line,
|
203 |
+
inputs=[vibe_dropdown],
|
204 |
+
outputs=output
|
205 |
+
)
|
206 |
+
|
207 |
+
# Footer
|
208 |
+
gr.Markdown("""
|
209 |
+
<div style="text-align: center; margin-top: 20px; color: #666;">
|
210 |
+
Built by Nath with SmolLM π₯
|
211 |
+
</div>
|
212 |
+
""")
|
213 |
+
|
214 |
+
# Launch the app
|
215 |
+
if __name__ == "__main__":
|
216 |
+
demo.launch(share=True) # Set share=False if you don't want to create a public link
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio>=4.0.0
|
2 |
+
torch>=2.0.0
|
3 |
+
transformers>=4.30.0
|
4 |
+
huggingface-hub>=0.19.0
|
5 |
+
numpy>=1.24.0
|
6 |
+
tqdm>=4.65.0
|
7 |
+
typing-extensions>=4.5.0
|