Spaces:
Running
Running
Upload 3 files
Browse files- app.py +123 -0
- requirements.txt +3 -0
- style.css +74 -0
app.py
ADDED
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from huggingface_hub import InferenceClient
|
3 |
+
from gradio_client import Client
|
4 |
+
import re
|
5 |
+
|
6 |
+
# Set the page config
|
7 |
+
st.set_page_config(layout="wide")
|
8 |
+
|
9 |
+
# Load custom CSS
|
10 |
+
with open('style.css') as f:
|
11 |
+
st.markdown(f'<style>{f.read()}</style>', unsafe_allow_html=True)
|
12 |
+
|
13 |
+
# Initialize the HuggingFace Inference Client
|
14 |
+
text_client = InferenceClient(model="mistralai/Mistral-7B-Instruct-v0.1")
|
15 |
+
image_client = Client("phenixrhyder/nsfw-waifu-gradio")
|
16 |
+
|
17 |
+
def format_prompt_for_description(caption_text):
|
18 |
+
prompt = f"Generate a funny and relatable meme caption for Pepe the Frog: {caption_text}"
|
19 |
+
return prompt
|
20 |
+
|
21 |
+
def format_prompt_for_image(caption_text):
|
22 |
+
prompt = f"Generate an image prompt for a Pepe the Frog meme with the following caption: {caption_text}"
|
23 |
+
return prompt
|
24 |
+
|
25 |
+
def clean_generated_text(text):
|
26 |
+
# Remove any unwanted trailing tags or characters like </s>
|
27 |
+
clean_text = re.sub(r'</s>$', '', text).strip()
|
28 |
+
return clean_text
|
29 |
+
|
30 |
+
def generate_text(prompt, temperature=0.9, max_new_tokens=512, top_p=0.95, repetition_penalty=1.0):
|
31 |
+
temperature = max(temperature, 1e-2)
|
32 |
+
generate_kwargs = dict(
|
33 |
+
temperature=temperature,
|
34 |
+
max_new_tokens=max_new_tokens,
|
35 |
+
top_p=top_p,
|
36 |
+
repetition_penalty=repetition_penalty,
|
37 |
+
do_sample=True,
|
38 |
+
seed=42,
|
39 |
+
)
|
40 |
+
try:
|
41 |
+
stream = text_client.text_generation(prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
42 |
+
output = ""
|
43 |
+
for response in stream:
|
44 |
+
output += response.token.text
|
45 |
+
return clean_generated_text(output)
|
46 |
+
except Exception as e:
|
47 |
+
st.error(f"Error generating text: {e}")
|
48 |
+
return ""
|
49 |
+
|
50 |
+
# Updated part for the new API
|
51 |
+
def generate_image(prompt):
|
52 |
+
try:
|
53 |
+
result = image_client.predict(
|
54 |
+
param_0=prompt,
|
55 |
+
api_name="/predict"
|
56 |
+
)
|
57 |
+
# Process and display the result
|
58 |
+
if result:
|
59 |
+
return [result] # Assuming the API returns a single image path as a result
|
60 |
+
else:
|
61 |
+
st.error("Unexpected result format from the Gradio API.")
|
62 |
+
return None
|
63 |
+
except Exception as e:
|
64 |
+
st.error(f"Error generating image: {e}")
|
65 |
+
st.write("Full error details:", e)
|
66 |
+
return None
|
67 |
+
|
68 |
+
def main():
|
69 |
+
st.title("Pepe Meme Generator")
|
70 |
+
|
71 |
+
# User inputs
|
72 |
+
col1, col2 = st.columns(2)
|
73 |
+
with col1:
|
74 |
+
caption_text = st.text_input("Enter a caption or meme idea for Pepe")
|
75 |
+
|
76 |
+
# Advanced settings
|
77 |
+
with st.expander("Advanced Settings"):
|
78 |
+
temperature = st.slider("Temperature", 0.0, 1.0, 0.9, step=0.05)
|
79 |
+
max_new_tokens = st.slider("Max new tokens", 0, 8192, 512, step=64)
|
80 |
+
top_p = st.slider("Top-p (nucleus sampling)", 0.0, 1.0, 0.95, step=0.05)
|
81 |
+
repetition_penalty = st.slider("Repetition penalty", 1.0, 2.0, 1.0, step=0.05)
|
82 |
+
|
83 |
+
# Initialize session state for generated text and image prompt
|
84 |
+
if "meme_caption" not in st.session_state:
|
85 |
+
st.session_state.meme_caption = ""
|
86 |
+
if "image_prompt" not in st.session_state:
|
87 |
+
st.session_state.image_prompt = ""
|
88 |
+
if "image_paths" not in st.session_state:
|
89 |
+
st.session_state.image_paths = []
|
90 |
+
|
91 |
+
# Generate button
|
92 |
+
if st.button("Generate Pepe Meme"):
|
93 |
+
with st.spinner("Generating Pepe meme..."):
|
94 |
+
description_prompt = format_prompt_for_description(caption_text)
|
95 |
+
image_prompt = format_prompt_for_image(caption_text)
|
96 |
+
|
97 |
+
# Generate meme caption
|
98 |
+
st.session_state.meme_caption = generate_text(description_prompt, temperature, max_new_tokens, top_p, repetition_penalty)
|
99 |
+
|
100 |
+
# Generate image prompt
|
101 |
+
st.session_state.image_prompt = generate_text(image_prompt, temperature, max_new_tokens, top_p, repetition_penalty)
|
102 |
+
|
103 |
+
# Generate image from image prompt
|
104 |
+
st.session_state.image_paths = generate_image(st.session_state.image_prompt)
|
105 |
+
|
106 |
+
st.success("Pepe meme generated!")
|
107 |
+
|
108 |
+
with col2:
|
109 |
+
# Display the generated meme caption and image prompt
|
110 |
+
if st.session_state.meme_caption:
|
111 |
+
st.subheader("Generated Meme Caption")
|
112 |
+
st.write(st.session_state.meme_caption)
|
113 |
+
if st.session_state.image_prompt:
|
114 |
+
st.subheader("Image Prompt")
|
115 |
+
st.write(st.session_state.image_prompt)
|
116 |
+
if st.session_state.image_paths:
|
117 |
+
st.subheader("Generated Image")
|
118 |
+
for image_path in st.session_state.image_paths:
|
119 |
+
st.image(image_path, caption="Generated Pepe Meme Image")
|
120 |
+
|
121 |
+
if __name__ == "__main__":
|
122 |
+
main()
|
123 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
streamlit==1.19.0
|
2 |
+
transformers==4.34.0
|
3 |
+
torch==2.0.1
|
style.css
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
body {
|
2 |
+
font-family: 'Comic Sans MS', 'Arial', sans-serif;
|
3 |
+
background-color: #f0f8ff;
|
4 |
+
background-image: url('https://i.imgur.com/avF4QAU.png'); /* Replace with a URL to a Pepe background image */
|
5 |
+
background-size: cover;
|
6 |
+
background-repeat: no-repeat;
|
7 |
+
background-attachment: fixed;
|
8 |
+
}
|
9 |
+
|
10 |
+
h1, h2, h3 {
|
11 |
+
color: #32CD32;
|
12 |
+
text-shadow: 2px 2px 4px #000000;
|
13 |
+
}
|
14 |
+
|
15 |
+
.stButton button {
|
16 |
+
background-color: #32CD32;
|
17 |
+
color: white;
|
18 |
+
border: none;
|
19 |
+
padding: 10px 20px;
|
20 |
+
text-align: center;
|
21 |
+
text-decoration: none;
|
22 |
+
display: inline-block;
|
23 |
+
font-size: 16px;
|
24 |
+
margin: 4px 2px;
|
25 |
+
cursor: pointer;
|
26 |
+
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);
|
27 |
+
border-radius: 10px;
|
28 |
+
}
|
29 |
+
|
30 |
+
.stTextInput input {
|
31 |
+
border-radius: 10px;
|
32 |
+
padding: 10px;
|
33 |
+
border: 1px solid #ccc;
|
34 |
+
font-size: 16px;
|
35 |
+
box-shadow: inset 0 1px 3px rgba(0,0,0,0.12), inset 0 1px 2px rgba(0,0,0,0.24);
|
36 |
+
}
|
37 |
+
|
38 |
+
.stSelectbox select {
|
39 |
+
border-radius: 10px;
|
40 |
+
padding: 10px;
|
41 |
+
border: 1px solid #ccc;
|
42 |
+
font-size: 16px;
|
43 |
+
box-shadow: inset 0 1px 3px rgba(0,0,0,0.12), inset 0 1px 2px rgba(0,0,0,0.24);
|
44 |
+
}
|
45 |
+
|
46 |
+
.stSlider .stSliderLabel {
|
47 |
+
color: #32CD32;
|
48 |
+
}
|
49 |
+
|
50 |
+
.stSlider .stSliderValue {
|
51 |
+
color: #FF4500;
|
52 |
+
}
|
53 |
+
|
54 |
+
.stMarkdown {
|
55 |
+
color: #32CD32;
|
56 |
+
font-family: 'Comic Sans MS', 'Arial', sans-serif;
|
57 |
+
text-shadow: 1px 1px 2px #000000;
|
58 |
+
}
|
59 |
+
|
60 |
+
.stTextArea textarea {
|
61 |
+
border-radius: 10px;
|
62 |
+
padding: 10px;
|
63 |
+
border: 1px solid #ccc;
|
64 |
+
font-size: 16px;
|
65 |
+
box-shadow: inset 0 1px 3px rgba(0,0,0,0.12), inset 0 1px 2px rgba(0,0,0,0.24);
|
66 |
+
}
|
67 |
+
|
68 |
+
.stExpander .stExpanderContent {
|
69 |
+
background-color: rgba(50, 205, 50, 0.1);
|
70 |
+
border: 1px solid #32CD32;
|
71 |
+
border-radius: 10px;
|
72 |
+
padding: 10px;
|
73 |
+
}
|
74 |
+
|