Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,19 +1,19 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
from transformers import pipeline
|
| 3 |
from diffusers import StableDiffusionPipeline
|
| 4 |
import torch
|
| 5 |
-
from
|
| 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
|
| 12 |
@st.cache_resource
|
| 13 |
def load_asr():
|
| 14 |
return pipeline("automatic-speech-recognition", model="openai/whisper-tiny")
|
| 15 |
|
| 16 |
-
# Load Stable Diffusion
|
| 17 |
@st.cache_resource
|
| 18 |
def load_sd():
|
| 19 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
@@ -25,20 +25,13 @@ def load_sd():
|
|
| 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 |
-
|
| 36 |
-
return image
|
| 37 |
|
| 38 |
-
# Tabs
|
| 39 |
tab1, tab2 = st.tabs(["π Text to Meme", "π€ Voice to Meme"])
|
| 40 |
|
| 41 |
-
#
|
| 42 |
with tab1:
|
| 43 |
text_input = st.text_area("Enter your meme idea")
|
| 44 |
if st.button("Generate Meme", key="text_meme"):
|
|
@@ -49,15 +42,19 @@ with tab1:
|
|
| 49 |
else:
|
| 50 |
st.warning("Please enter some text!")
|
| 51 |
|
| 52 |
-
# --
|
| 53 |
with tab2:
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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!")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from audiorecorder import audiorecorder
|
| 3 |
from transformers import pipeline
|
| 4 |
from diffusers import StableDiffusionPipeline
|
| 5 |
import torch
|
| 6 |
+
from tempfile import NamedTemporaryFile
|
| 7 |
|
|
|
|
| 8 |
st.set_page_config(page_title="AI Meme Generator", page_icon="π")
|
| 9 |
st.title("π AI Meme Generator (Voice + Text)")
|
| 10 |
|
| 11 |
+
# Load Whisper
|
| 12 |
@st.cache_resource
|
| 13 |
def load_asr():
|
| 14 |
return pipeline("automatic-speech-recognition", model="openai/whisper-tiny")
|
| 15 |
|
| 16 |
+
# Load Stable Diffusion
|
| 17 |
@st.cache_resource
|
| 18 |
def load_sd():
|
| 19 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
| 25 |
asr = load_asr()
|
| 26 |
sd_pipe = load_sd()
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
def generate_meme(prompt):
|
| 29 |
+
return sd_pipe(prompt).images[0]
|
|
|
|
| 30 |
|
| 31 |
+
# Tabs
|
| 32 |
tab1, tab2 = st.tabs(["π Text to Meme", "π€ Voice to Meme"])
|
| 33 |
|
| 34 |
+
# Text-to-Meme
|
| 35 |
with tab1:
|
| 36 |
text_input = st.text_area("Enter your meme idea")
|
| 37 |
if st.button("Generate Meme", key="text_meme"):
|
|
|
|
| 42 |
else:
|
| 43 |
st.warning("Please enter some text!")
|
| 44 |
|
| 45 |
+
# Voice-to-Meme (with mic recording)
|
| 46 |
with tab2:
|
| 47 |
+
st.write("π€ Record your voice below and create a meme!")
|
| 48 |
+
audio = audiorecorder("Click to record", "Click to stop recording")
|
| 49 |
+
|
| 50 |
+
if len(audio) > 0:
|
| 51 |
+
# Save temp audio file
|
| 52 |
+
with NamedTemporaryFile(suffix=".wav", delete=False) as f:
|
| 53 |
+
audio.export(f.name, format="wav")
|
| 54 |
+
text = asr(f.name)["text"]
|
| 55 |
+
|
| 56 |
+
st.success(f"Recognized Text: {text}")
|
| 57 |
+
if st.button("Generate Meme from Voice", key="voice_meme"):
|
| 58 |
with st.spinner("Transcribing and generating meme..."):
|
|
|
|
| 59 |
img = generate_meme(f"Meme style funny cartoon with text: {text}")
|
| 60 |
st.image(img, caption="Generated Meme")
|
|
|
|
|
|
|
|
|