Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from diffusers import StableDiffusionPipeline
|
| 4 |
+
import torch
|
| 5 |
+
from PIL import Image
|
| 6 |
+
|
| 7 |
+
# Title
|
| 8 |
+
st.set_page_config(page_title="AI Meme Generator", page_icon="🎭")
|
| 9 |
+
st.title("🎭 AI Meme Generator (Voice + Text)")
|
| 10 |
+
|
| 11 |
+
# Load Whisper (speech-to-text)
|
| 12 |
+
@st.cache_resource
|
| 13 |
+
def load_asr():
|
| 14 |
+
return pipeline("automatic-speech-recognition", model="openai/whisper-tiny")
|
| 15 |
+
|
| 16 |
+
# Load Stable Diffusion (text-to-image)
|
| 17 |
+
@st.cache_resource
|
| 18 |
+
def load_sd():
|
| 19 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 20 |
+
return StableDiffusionPipeline.from_pretrained(
|
| 21 |
+
"runwayml/stable-diffusion-v1-5",
|
| 22 |
+
torch_dtype=torch.float16 if device == "cuda" else torch.float32
|
| 23 |
+
).to(device)
|
| 24 |
+
|
| 25 |
+
asr = load_asr()
|
| 26 |
+
sd_pipe = load_sd()
|
| 27 |
+
|
| 28 |
+
# Function: Voice -> Text
|
| 29 |
+
def transcribe(audio_file):
|
| 30 |
+
result = asr(audio_file)
|
| 31 |
+
return result["text"]
|
| 32 |
+
|
| 33 |
+
# Function: Text -> Meme
|
| 34 |
+
def generate_meme(prompt):
|
| 35 |
+
image = sd_pipe(prompt).images[0]
|
| 36 |
+
return image
|
| 37 |
+
|
| 38 |
+
# Tabs for input type
|
| 39 |
+
tab1, tab2 = st.tabs(["📝 Text to Meme", "🎤 Voice to Meme"])
|
| 40 |
+
|
| 41 |
+
# ---------------- Text to Meme ----------------
|
| 42 |
+
with tab1:
|
| 43 |
+
text_input = st.text_area("Enter your meme idea")
|
| 44 |
+
if st.button("Generate Meme", key="text_meme"):
|
| 45 |
+
if text_input.strip():
|
| 46 |
+
with st.spinner("Generating meme..."):
|
| 47 |
+
img = generate_meme(f"Meme style funny cartoon with text: {text_input}")
|
| 48 |
+
st.image(img, caption="Generated Meme")
|
| 49 |
+
else:
|
| 50 |
+
st.warning("Please enter some text!")
|
| 51 |
+
|
| 52 |
+
# ---------------- Voice to Meme ----------------
|
| 53 |
+
with tab2:
|
| 54 |
+
audio_file = st.file_uploader("Upload your voice recording (mp3/wav)", type=["mp3", "wav"])
|
| 55 |
+
if st.button("Generate Meme from Voice", key="voice_meme"):
|
| 56 |
+
if audio_file is not None:
|
| 57 |
+
with st.spinner("Transcribing and generating meme..."):
|
| 58 |
+
text = transcribe(audio_file)
|
| 59 |
+
img = generate_meme(f"Meme style funny cartoon with text: {text}")
|
| 60 |
+
st.image(img, caption="Generated Meme")
|
| 61 |
+
st.success(f"Recognized Text: {text}")
|
| 62 |
+
else:
|
| 63 |
+
st.warning("Please upload a voice file!")
|