Alpha108 commited on
Commit
31eeafc
Β·
verified Β·
1 Parent(s): 9d328c5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -22
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 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"
@@ -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
- 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"):
@@ -49,15 +42,19 @@ with tab1:
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!")
 
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")