Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from openai import OpenAI
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from gtts import gTTS
|
| 5 |
+
import os
|
| 6 |
+
from io import BytesIO
|
| 7 |
+
from pydub import AudioSegment
|
| 8 |
+
|
| 9 |
+
# Initialize OpenAI client
|
| 10 |
+
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 11 |
+
|
| 12 |
+
st.set_page_config(page_title="Meme + Voice Notes App", layout="centered")
|
| 13 |
+
|
| 14 |
+
st.title("π Meme Generator + π Voice Notes Holder")
|
| 15 |
+
|
| 16 |
+
# ========== MEME GENERATOR ==========
|
| 17 |
+
st.header("π Meme Generator")
|
| 18 |
+
|
| 19 |
+
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
| 20 |
+
if uploaded_file:
|
| 21 |
+
img = Image.open(uploaded_file)
|
| 22 |
+
st.image(img, caption="Your Uploaded Image", use_column_width=True)
|
| 23 |
+
|
| 24 |
+
if st.button("Generate Meme"):
|
| 25 |
+
# Describe image + generate captions
|
| 26 |
+
prompt = f"Write 3 funny, Gen Z style meme captions for a photo like this. Keep it short & witty."
|
| 27 |
+
response = client.chat.completions.create(
|
| 28 |
+
model="gpt-4o-mini",
|
| 29 |
+
messages=[{"role": "user", "content": prompt}],
|
| 30 |
+
max_tokens=100
|
| 31 |
+
)
|
| 32 |
+
captions = response.choices[0].message.content.split("\n")
|
| 33 |
+
st.subheader("Pick Your Meme Caption")
|
| 34 |
+
for cap in captions:
|
| 35 |
+
if cap.strip():
|
| 36 |
+
st.text(cap.strip())
|
| 37 |
+
|
| 38 |
+
# ========== VOICE NOTES ==========
|
| 39 |
+
st.header("π Voice Notes Holder")
|
| 40 |
+
|
| 41 |
+
voice_file = st.file_uploader("Upload a voice note", type=["mp3", "wav", "m4a"])
|
| 42 |
+
if voice_file:
|
| 43 |
+
# Save uploaded voice file
|
| 44 |
+
audio_bytes = voice_file.read()
|
| 45 |
+
st.audio(audio_bytes, format="audio/mp3")
|
| 46 |
+
|
| 47 |
+
if st.button("Transcribe & Caption"):
|
| 48 |
+
# Transcribe audio (simplified: send raw text request)
|
| 49 |
+
transcript = client.audio.transcriptions.create(
|
| 50 |
+
model="whisper-1",
|
| 51 |
+
file=voice_file
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
st.write("π **Transcription:**")
|
| 55 |
+
st.write(transcript.text)
|
| 56 |
+
|
| 57 |
+
# Generate a funny caption for the note
|
| 58 |
+
caption_prompt = f"Make a short funny Gen Z style caption for this voice note text: {transcript.text}"
|
| 59 |
+
caption_response = client.chat.completions.create(
|
| 60 |
+
model="gpt-4o-mini",
|
| 61 |
+
messages=[{"role": "user", "content": caption_prompt}],
|
| 62 |
+
max_tokens=60
|
| 63 |
+
)
|
| 64 |
+
st.write("π **Meme Caption:**")
|
| 65 |
+
st.write(caption_response.choices[0].message.content)
|
| 66 |
+
|
| 67 |
+
# Optional: TTS the caption
|
| 68 |
+
tts = gTTS(text=caption_response.choices[0].message.content, lang="en")
|
| 69 |
+
tts_bytes = BytesIO()
|
| 70 |
+
tts.save("caption.mp3")
|
| 71 |
+
audio_file = open("caption.mp3", "rb")
|
| 72 |
+
audio_bytes = audio_file.read()
|
| 73 |
+
st.audio(audio_bytes, format="audio/mp3")
|
| 74 |
+
|
| 75 |
+
st.markdown("---")
|
| 76 |
+
st.info("π Tip: Add a Birthday Mode with custom memes + voice surprise for your sister!")
|