Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,28 +1,15 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import torch
|
| 3 |
-
import numpy as np
|
| 4 |
-
import io
|
| 5 |
-
import os
|
| 6 |
import tempfile
|
| 7 |
-
|
| 8 |
-
import requests
|
| 9 |
-
import json
|
| 10 |
from datetime import datetime
|
| 11 |
-
import time
|
| 12 |
|
| 13 |
-
#
|
| 14 |
try:
|
| 15 |
from transformers import pipeline
|
| 16 |
TRANSFORMERS_AVAILABLE = True
|
| 17 |
except ImportError:
|
| 18 |
TRANSFORMERS_AVAILABLE = False
|
| 19 |
|
| 20 |
-
try:
|
| 21 |
-
import google.generativeai as genai
|
| 22 |
-
GENAI_AVAILABLE = True
|
| 23 |
-
except ImportError:
|
| 24 |
-
GENAI_AVAILABLE = False
|
| 25 |
-
|
| 26 |
try:
|
| 27 |
from st_audiorec import st_audiorec
|
| 28 |
AUDIO_REC_AVAILABLE = True
|
|
@@ -31,634 +18,195 @@ except ImportError:
|
|
| 31 |
|
| 32 |
# Configure page
|
| 33 |
st.set_page_config(
|
| 34 |
-
page_title="VoiceCanvas - AI
|
| 35 |
page_icon="π¨",
|
| 36 |
-
layout="
|
| 37 |
-
initial_sidebar_state="expanded"
|
| 38 |
)
|
| 39 |
|
| 40 |
-
# Initialize session state
|
| 41 |
-
if 'generated_content' not in st.session_state:
|
| 42 |
-
st.session_state.generated_content = {}
|
| 43 |
if 'transcription' not in st.session_state:
|
| 44 |
st.session_state.transcription = ""
|
| 45 |
-
if '
|
| 46 |
-
st.session_state.
|
| 47 |
-
if 'current_task' not in st.session_state:
|
| 48 |
-
st.session_state.current_task = ""
|
| 49 |
-
if 'models_loaded' not in st.session_state:
|
| 50 |
-
st.session_state.models_loaded = False
|
| 51 |
if 'whisper_model' not in st.session_state:
|
| 52 |
st.session_state.whisper_model = None
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
if
|
| 58 |
-
return
|
| 59 |
-
|
| 60 |
-
if not TRANSFORMERS_AVAILABLE:
|
| 61 |
-
st.error("β Transformers library not available. Please install: pip install transformers")
|
| 62 |
-
return False
|
| 63 |
-
|
| 64 |
-
progress_bar = st.progress(0)
|
| 65 |
-
status_text = st.empty()
|
| 66 |
-
|
| 67 |
-
try:
|
| 68 |
-
# Load Whisper model
|
| 69 |
-
status_text.text("Loading speech recognition model...")
|
| 70 |
-
progress_bar.progress(25)
|
| 71 |
-
|
| 72 |
-
# Use session state to store the model
|
| 73 |
-
st.session_state.whisper_model = pipeline(
|
| 74 |
"automatic-speech-recognition",
|
| 75 |
model="openai/whisper-tiny",
|
| 76 |
-
device=-1
|
| 77 |
-
torch_dtype=torch.float32,
|
| 78 |
-
return_timestamps=False
|
| 79 |
)
|
| 80 |
-
|
| 81 |
-
progress_bar.progress(75)
|
| 82 |
-
status_text.text("Models loaded successfully!")
|
| 83 |
-
progress_bar.progress(100)
|
| 84 |
-
|
| 85 |
-
st.session_state.models_loaded = True
|
| 86 |
-
|
| 87 |
-
# Clear progress indicators after a moment
|
| 88 |
-
time.sleep(1)
|
| 89 |
-
progress_bar.empty()
|
| 90 |
-
status_text.empty()
|
| 91 |
-
|
| 92 |
-
return True
|
| 93 |
-
|
| 94 |
-
except Exception as e:
|
| 95 |
-
st.error(f"β Error loading models: {str(e)}")
|
| 96 |
-
st.error("Try installing additional dependencies: pip install librosa soundfile")
|
| 97 |
-
progress_bar.empty()
|
| 98 |
-
status_text.empty()
|
| 99 |
-
return False
|
| 100 |
|
| 101 |
-
def
|
| 102 |
-
"""
|
| 103 |
-
if not GENAI_AVAILABLE:
|
| 104 |
-
return False
|
| 105 |
-
|
| 106 |
try:
|
| 107 |
-
|
| 108 |
-
if
|
| 109 |
-
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
return True
|
| 114 |
-
return False
|
| 115 |
except Exception as e:
|
| 116 |
-
return False
|
| 117 |
-
|
| 118 |
-
def transcribe_audio_simple(audio_file):
|
| 119 |
-
"""Simple audio transcription with progress tracking"""
|
| 120 |
-
try:
|
| 121 |
-
# Check if model is loaded
|
| 122 |
-
if st.session_state.whisper_model is None:
|
| 123 |
-
st.error("β Speech recognition model not loaded. Please try loading models first.")
|
| 124 |
-
return "Error: Speech recognition model not available"
|
| 125 |
-
|
| 126 |
-
st.session_state.current_task = "Converting speech to text..."
|
| 127 |
-
|
| 128 |
-
# Handle different input types
|
| 129 |
-
if isinstance(audio_file, str):
|
| 130 |
-
# File path
|
| 131 |
-
audio_input = audio_file
|
| 132 |
-
else:
|
| 133 |
-
# File-like object
|
| 134 |
-
audio_input = audio_file
|
| 135 |
-
|
| 136 |
-
# Transcribe using pipeline
|
| 137 |
-
result = st.session_state.whisper_model(audio_input)
|
| 138 |
-
|
| 139 |
-
st.session_state.current_task = ""
|
| 140 |
-
|
| 141 |
-
# Handle different result formats
|
| 142 |
-
if isinstance(result, dict) and "text" in result:
|
| 143 |
-
return result["text"].strip()
|
| 144 |
-
elif isinstance(result, str):
|
| 145 |
-
return result.strip()
|
| 146 |
-
else:
|
| 147 |
-
return str(result).strip()
|
| 148 |
-
|
| 149 |
-
except Exception as e:
|
| 150 |
-
st.session_state.current_task = ""
|
| 151 |
-
error_msg = f"Transcription error: {str(e)}"
|
| 152 |
-
st.error(error_msg)
|
| 153 |
-
|
| 154 |
-
# Provide troubleshooting suggestions
|
| 155 |
-
if "librosa" in str(e).lower() or "soundfile" in str(e).lower():
|
| 156 |
-
st.error("π§ Missing audio processing libraries. Install with:")
|
| 157 |
-
st.code("pip install librosa soundfile")
|
| 158 |
-
|
| 159 |
return f"Error: {str(e)}"
|
| 160 |
|
| 161 |
-
def
|
| 162 |
-
"""Generate content
|
| 163 |
-
if not GENAI_AVAILABLE:
|
| 164 |
-
return generate_content_offline(prompt)
|
| 165 |
-
|
| 166 |
-
try:
|
| 167 |
-
st.session_state.current_task = "Generating enhanced content with Gemini AI..."
|
| 168 |
-
|
| 169 |
-
model = genai.GenerativeModel('gemini-pro')
|
| 170 |
-
response = model.generate_content(f"""
|
| 171 |
-
Based on this input: "{prompt}"
|
| 172 |
-
|
| 173 |
-
Create comprehensive marketing content with:
|
| 174 |
-
|
| 175 |
-
## Marketing Taglines
|
| 176 |
-
Generate 3 catchy, memorable taglines (max 12 words each)
|
| 177 |
-
|
| 178 |
-
## Social Media Posts
|
| 179 |
-
Create 3 engaging social media posts (max 280 characters each)
|
| 180 |
-
|
| 181 |
-
## Product Description
|
| 182 |
-
Write 1 compelling product description (100-150 words)
|
| 183 |
-
|
| 184 |
-
## Image Generation Prompts
|
| 185 |
-
Provide 3 detailed prompts for AI image generation
|
| 186 |
-
|
| 187 |
-
## Call-to-Action Ideas
|
| 188 |
-
Suggest 3 effective call-to-action phrases
|
| 189 |
-
|
| 190 |
-
Format with clear markdown headers and numbered lists.
|
| 191 |
-
""")
|
| 192 |
-
|
| 193 |
-
st.session_state.current_task = ""
|
| 194 |
-
return response.text
|
| 195 |
-
|
| 196 |
-
except Exception as e:
|
| 197 |
-
st.warning(f"Gemini error: {e}. Using offline generation.")
|
| 198 |
-
st.session_state.current_task = ""
|
| 199 |
-
return generate_content_offline(prompt)
|
| 200 |
-
|
| 201 |
-
def generate_content_offline(prompt):
|
| 202 |
-
"""Generate content using offline methods"""
|
| 203 |
-
st.session_state.current_task = "Generating content with offline templates..."
|
| 204 |
|
| 205 |
-
#
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
f"Experience {prompt} like never before",
|
| 209 |
-
f"Transform your world with {prompt}",
|
| 210 |
-
f"Discover the power of {prompt}"
|
| 211 |
-
],
|
| 212 |
-
"social_posts": [
|
| 213 |
-
f"π Ready to explore {prompt}? Join thousands who've already discovered the difference! #Innovation",
|
| 214 |
-
f"π« {prompt} is changing the game! Don't miss out on this incredible opportunity. #GameChanger",
|
| 215 |
-
f"π The future of {prompt} is here! Experience what everyone's talking about. #FutureTech"
|
| 216 |
-
],
|
| 217 |
-
"description": f"Discover the revolutionary world of {prompt}. Our innovative approach combines cutting-edge technology with user-friendly design to deliver an unmatched experience. Perfect for both beginners and experts, this solution transforms how you interact with {prompt}. Join thousands of satisfied users today!",
|
| 218 |
-
"image_prompts": [
|
| 219 |
-
f"Professional product photo of {prompt}, clean white background, studio lighting",
|
| 220 |
-
f"Modern minimalist illustration of {prompt}, flat design, vibrant colors",
|
| 221 |
-
f"Futuristic concept art of {prompt}, digital art, high quality, detailed"
|
| 222 |
-
]
|
| 223 |
-
}
|
| 224 |
|
| 225 |
-
#
|
| 226 |
-
formatted = format_content_display(content)
|
| 227 |
-
|
| 228 |
-
# Store both versions
|
| 229 |
-
st.session_state.generated_content['structured'] = content
|
| 230 |
-
st.session_state.current_task = ""
|
| 231 |
-
|
| 232 |
-
return formatted
|
| 233 |
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
api_url = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-2-1"
|
| 240 |
-
headers = {"Authorization": f"Bearer {os.getenv('HF_TOKEN', '')}"}
|
| 241 |
-
|
| 242 |
-
if not os.getenv('HF_TOKEN'):
|
| 243 |
-
st.warning("Add HF_TOKEN environment variable for image generation")
|
| 244 |
-
st.session_state.current_task = ""
|
| 245 |
-
return None
|
| 246 |
-
|
| 247 |
-
response = requests.post(api_url, headers=headers, json={"inputs": prompt}, timeout=60)
|
| 248 |
-
|
| 249 |
-
if response.status_code == 200:
|
| 250 |
-
image = Image.open(io.BytesIO(response.content))
|
| 251 |
-
st.session_state.current_task = ""
|
| 252 |
-
return image
|
| 253 |
-
else:
|
| 254 |
-
st.warning(f"Image API returned status {response.status_code}")
|
| 255 |
-
st.session_state.current_task = ""
|
| 256 |
-
return None
|
| 257 |
-
|
| 258 |
-
except Exception as e:
|
| 259 |
-
st.error(f"Image generation error: {e}")
|
| 260 |
-
st.session_state.current_task = ""
|
| 261 |
-
return None
|
| 262 |
|
| 263 |
-
|
| 264 |
-
"""Format content for nice display"""
|
| 265 |
-
if isinstance(content, dict):
|
| 266 |
-
formatted = ""
|
| 267 |
-
|
| 268 |
-
if "taglines" in content:
|
| 269 |
-
formatted += "## π·οΈ Marketing Taglines\n"
|
| 270 |
-
for i, tagline in enumerate(content["taglines"], 1):
|
| 271 |
-
formatted += f"{i}. **{tagline}**\n"
|
| 272 |
-
formatted += "\n"
|
| 273 |
-
|
| 274 |
-
if "social_posts" in content:
|
| 275 |
-
formatted += "## π± Social Media Posts\n"
|
| 276 |
-
for i, post in enumerate(content["social_posts"], 1):
|
| 277 |
-
formatted += f"**Post {i}:**\n{post}\n\n"
|
| 278 |
-
|
| 279 |
-
if "description" in content:
|
| 280 |
-
formatted += "## π Product Description\n"
|
| 281 |
-
formatted += f"{content['description']}\n\n"
|
| 282 |
-
|
| 283 |
-
if "image_prompts" in content:
|
| 284 |
-
formatted += "## π¨ Image Generation Prompts\n"
|
| 285 |
-
for i, prompt in enumerate(content["image_prompts"], 1):
|
| 286 |
-
formatted += f"{i}. {prompt}\n"
|
| 287 |
-
|
| 288 |
-
return formatted
|
| 289 |
-
|
| 290 |
-
return str(content)
|
| 291 |
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
with st.sidebar:
|
| 295 |
-
st.header("π¨ VoiceCanvas")
|
| 296 |
-
st.markdown("*AI Content Studio*")
|
| 297 |
-
|
| 298 |
-
# Load models button
|
| 299 |
-
if not st.session_state.models_loaded:
|
| 300 |
-
if st.button("π Load AI Models", type="primary", use_container_width=True):
|
| 301 |
-
load_models()
|
| 302 |
-
|
| 303 |
-
# Status section
|
| 304 |
-
st.subheader("π System Status")
|
| 305 |
-
|
| 306 |
-
gemini_available = setup_gemini()
|
| 307 |
-
|
| 308 |
-
col1, col2 = st.columns(2)
|
| 309 |
-
with col1:
|
| 310 |
-
st.metric("Mode", "Enhanced" if gemini_available else "Basic")
|
| 311 |
-
with col2:
|
| 312 |
-
st.metric("Status", "Ready" if not st.session_state.processing else "Working")
|
| 313 |
-
|
| 314 |
-
# Component status
|
| 315 |
-
st.write("π€ **Components:**")
|
| 316 |
-
st.write(f"β’ Speech Recognition: {'β
' if st.session_state.models_loaded else 'β'}")
|
| 317 |
-
st.write(f"β’ Audio Recording: {'β
' if AUDIO_REC_AVAILABLE else 'β'}")
|
| 318 |
-
st.write(f"β’ Enhanced AI: {'β
' if gemini_available else 'β'}")
|
| 319 |
-
|
| 320 |
-
# Current task indicator
|
| 321 |
-
if st.session_state.current_task:
|
| 322 |
-
st.info(f"π {st.session_state.current_task}")
|
| 323 |
-
|
| 324 |
-
st.markdown("---")
|
| 325 |
-
|
| 326 |
-
# Tips and help
|
| 327 |
-
st.subheader("π‘ How to Use")
|
| 328 |
-
|
| 329 |
-
with st.expander("π Quick Start", expanded=True):
|
| 330 |
-
st.markdown("""
|
| 331 |
-
1. **Load Models**: Click "Load AI Models" button first
|
| 332 |
-
2. **Input**: Use voice, upload audio, or type text
|
| 333 |
-
3. **Edit**: Review and refine your input
|
| 334 |
-
4. **Generate**: Create marketing content
|
| 335 |
-
5. **Export**: Download your materials
|
| 336 |
-
""")
|
| 337 |
-
|
| 338 |
-
with st.expander("π― Best Practices"):
|
| 339 |
-
st.markdown("""
|
| 340 |
-
**For Voice/Audio:**
|
| 341 |
-
- Speak clearly at normal pace
|
| 342 |
-
- Use quiet environment
|
| 343 |
-
- Describe your product/service
|
| 344 |
-
- Mention target audience
|
| 345 |
-
|
| 346 |
-
**For Text:**
|
| 347 |
-
- Be specific about features
|
| 348 |
-
- Include benefits and use cases
|
| 349 |
-
- Mention what makes it unique
|
| 350 |
-
- Use 50+ words for detail
|
| 351 |
-
""")
|
| 352 |
-
|
| 353 |
-
with st.expander("βοΈ Setup (Optional)"):
|
| 354 |
-
st.markdown("""
|
| 355 |
-
**Enhanced Features:**
|
| 356 |
-
|
| 357 |
-
Add environment variables:
|
| 358 |
-
- `GEMINI_API_KEY`: Advanced text generation
|
| 359 |
-
- `HF_TOKEN`: AI image generation
|
| 360 |
-
|
| 361 |
-
**Get API Keys:**
|
| 362 |
-
- [Google AI Studio](https://makersuite.google.com/app/apikey) (Free)
|
| 363 |
-
- [Hugging Face](https://huggingface.co/settings/tokens) (Free)
|
| 364 |
-
""")
|
| 365 |
-
|
| 366 |
-
with st.expander("π οΈ Troubleshooting"):
|
| 367 |
-
st.markdown("""
|
| 368 |
-
**Common Issues:**
|
| 369 |
-
- "Speech recognition not available" β Click "Load AI Models"
|
| 370 |
-
- Audio processing errors β Install: `pip install librosa soundfile`
|
| 371 |
-
- Slow processing β Models loading for first time
|
| 372 |
-
- No image generation β Add HF_TOKEN
|
| 373 |
-
- Basic content only β Add GEMINI_API_KEY
|
| 374 |
-
""")
|
| 375 |
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 383 |
|
| 384 |
-
|
| 385 |
-
st.header("π‘ Share Your Idea")
|
| 386 |
|
| 387 |
-
#
|
| 388 |
-
|
| 389 |
-
if AUDIO_REC_AVAILABLE:
|
| 390 |
-
available_tabs.append("ποΈ Record")
|
| 391 |
-
available_tabs.extend(["π Upload", "βοΈ Type"])
|
| 392 |
|
| 393 |
-
|
| 394 |
-
|
| 395 |
|
| 396 |
-
# Recording tab
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
st.info("π€
|
| 400 |
-
|
| 401 |
-
# Audio recorder
|
| 402 |
wav_audio_data = st_audiorec()
|
| 403 |
|
| 404 |
if wav_audio_data is not None:
|
| 405 |
-
st.success("
|
| 406 |
st.audio(wav_audio_data, format='audio/wav')
|
| 407 |
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
st.
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
tab_index += 1
|
| 421 |
|
| 422 |
# Upload tab
|
| 423 |
-
with
|
| 424 |
-
st.info("π Upload
|
| 425 |
-
|
| 426 |
-
uploaded_file = st.file_uploader(
|
| 427 |
-
"Choose audio file",
|
| 428 |
-
type=['wav', 'mp3', 'm4a'],
|
| 429 |
-
help="Supported: WAV, MP3, M4A β’ Max 10MB β’ Best: 30 seconds or less"
|
| 430 |
-
)
|
| 431 |
|
| 432 |
if uploaded_file:
|
| 433 |
-
st.success("
|
| 434 |
st.audio(uploaded_file)
|
| 435 |
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
st.rerun()
|
| 444 |
-
|
| 445 |
-
with col2:
|
| 446 |
-
if st.session_state.processing:
|
| 447 |
-
st.info("π Converting speech to text...")
|
| 448 |
-
|
| 449 |
-
tab_index += 1
|
| 450 |
|
| 451 |
# Text tab
|
| 452 |
-
with
|
| 453 |
-
st.info("βοΈ Type or paste your product/service description")
|
| 454 |
-
|
| 455 |
user_input = st.text_area(
|
| 456 |
-
"Describe your
|
| 457 |
-
placeholder="
|
| 458 |
-
height=150
|
| 459 |
-
help="Be detailed! Include features, benefits, and target audience for best results."
|
| 460 |
)
|
| 461 |
|
| 462 |
if user_input:
|
| 463 |
st.session_state.transcription = user_input
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
if word_count < 10:
|
| 467 |
-
st.warning("π‘ Add more details for better results (at least 10 words)")
|
| 468 |
-
elif word_count < 30:
|
| 469 |
-
st.info("π Good start! Add more features/benefits for richer content")
|
| 470 |
-
else:
|
| 471 |
-
st.success(f"β
Great detail! ({word_count} words)")
|
| 472 |
-
|
| 473 |
-
# Process audio transcription
|
| 474 |
-
if st.session_state.processing:
|
| 475 |
-
if AUDIO_REC_AVAILABLE and 'wav_audio_data' in locals() and wav_audio_data is not None:
|
| 476 |
-
# Process recorded audio
|
| 477 |
-
with st.spinner("π― Converting your speech to text..."):
|
| 478 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
|
| 479 |
-
tmp_file.write(wav_audio_data)
|
| 480 |
-
transcription = transcribe_audio_simple(tmp_file.name)
|
| 481 |
-
st.session_state.transcription = transcription
|
| 482 |
-
os.unlink(tmp_file.name)
|
| 483 |
-
|
| 484 |
-
st.session_state.processing = False
|
| 485 |
-
st.rerun()
|
| 486 |
-
|
| 487 |
-
elif 'uploaded_file' in locals() and uploaded_file is not None:
|
| 488 |
-
# Process uploaded file
|
| 489 |
-
with st.spinner("π― Processing your audio file..."):
|
| 490 |
-
transcription = transcribe_audio_simple(uploaded_file)
|
| 491 |
-
st.session_state.transcription = transcription
|
| 492 |
-
|
| 493 |
-
st.session_state.processing = False
|
| 494 |
-
st.rerun()
|
| 495 |
|
| 496 |
-
# Show
|
| 497 |
if st.session_state.transcription:
|
| 498 |
st.markdown("---")
|
| 499 |
-
st.header("π
|
| 500 |
|
|
|
|
| 501 |
edited_text = st.text_area(
|
| 502 |
-
"Edit
|
| 503 |
value=st.session_state.transcription,
|
| 504 |
-
height=
|
| 505 |
-
key="edit_transcription",
|
| 506 |
-
help="Make any corrections or add more details"
|
| 507 |
)
|
| 508 |
st.session_state.transcription = edited_text
|
| 509 |
|
| 510 |
-
# Generate content
|
| 511 |
st.markdown("---")
|
| 512 |
-
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
|
| 516 |
-
|
| 517 |
-
|
| 518 |
-
content_text = generate_content_with_gemini(st.session_state.transcription)
|
| 519 |
-
st.session_state.generated_content['text'] = content_text
|
| 520 |
-
else:
|
| 521 |
-
content_text = generate_content_offline(st.session_state.transcription)
|
| 522 |
-
st.session_state.generated_content['text'] = content_text
|
| 523 |
-
st.success("β
Content generated successfully!")
|
| 524 |
-
st.rerun()
|
| 525 |
|
| 526 |
-
#
|
| 527 |
if st.session_state.generated_content:
|
| 528 |
st.markdown("---")
|
| 529 |
st.header("β¨ Your Marketing Content")
|
|
|
|
| 530 |
|
| 531 |
-
#
|
| 532 |
-
if 'text' in st.session_state.generated_content:
|
| 533 |
-
st.markdown(st.session_state.generated_content['text'])
|
| 534 |
-
|
| 535 |
-
# Image generation section
|
| 536 |
st.markdown("---")
|
| 537 |
-
st.
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
prompts = st.session_state.generated_content['structured'].get('image_prompts', [])
|
| 545 |
-
if prompts:
|
| 546 |
-
selected_prompt = st.selectbox(
|
| 547 |
-
"Choose image style:",
|
| 548 |
-
prompts,
|
| 549 |
-
help="Select from AI-generated image prompts"
|
| 550 |
-
)
|
| 551 |
-
else:
|
| 552 |
-
selected_prompt = st.text_input(
|
| 553 |
-
"Describe the image you want:",
|
| 554 |
-
placeholder="Professional product photo with clean white background",
|
| 555 |
-
help="Be specific about style, colors, composition"
|
| 556 |
-
)
|
| 557 |
-
else:
|
| 558 |
-
# Custom prompt input
|
| 559 |
-
selected_prompt = st.text_input(
|
| 560 |
-
"Describe the image you want:",
|
| 561 |
-
placeholder="Professional product photo with clean white background",
|
| 562 |
-
help="Be specific about style, colors, composition"
|
| 563 |
-
)
|
| 564 |
-
|
| 565 |
-
with col2:
|
| 566 |
-
st.write("") # Spacing
|
| 567 |
-
st.write("") # Spacing
|
| 568 |
-
|
| 569 |
-
if st.button("πΌοΈ Generate Image", use_container_width=True):
|
| 570 |
-
if selected_prompt:
|
| 571 |
-
img = generate_image_with_api(selected_prompt)
|
| 572 |
-
if img:
|
| 573 |
-
st.session_state.generated_content['image'] = img
|
| 574 |
-
st.success("π¨ Image created!")
|
| 575 |
-
st.rerun()
|
| 576 |
-
else:
|
| 577 |
-
st.error("Image generation failed. Check HF_TOKEN.")
|
| 578 |
-
else:
|
| 579 |
-
st.warning("Please enter/select an image description")
|
| 580 |
-
|
| 581 |
-
# Display generated image
|
| 582 |
-
if 'image' in st.session_state.generated_content:
|
| 583 |
-
st.image(
|
| 584 |
-
st.session_state.generated_content['image'],
|
| 585 |
-
caption="AI Generated Image",
|
| 586 |
-
use_column_width=True
|
| 587 |
-
)
|
| 588 |
-
|
| 589 |
-
# Export section
|
| 590 |
-
st.markdown("---")
|
| 591 |
-
st.header("π₯ Export Your Content")
|
| 592 |
-
|
| 593 |
-
col1, col2, col3 = st.columns(3)
|
| 594 |
-
|
| 595 |
-
with col1:
|
| 596 |
-
# Text export
|
| 597 |
-
if 'text' in st.session_state.generated_content:
|
| 598 |
-
content_export = f"""VOICECANVAS MARKETING CONTENT
|
| 599 |
-
Generated: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
|
| 600 |
-
Source: {st.session_state.transcription[:100]}...
|
| 601 |
-
|
| 602 |
-
{st.session_state.generated_content['text']}
|
| 603 |
-
|
| 604 |
-
---
|
| 605 |
-
Created with VoiceCanvas AI Content Studio
|
| 606 |
-
"""
|
| 607 |
-
|
| 608 |
-
st.download_button(
|
| 609 |
-
"π Download Text",
|
| 610 |
-
content_export,
|
| 611 |
-
file_name=f"marketing_content_{datetime.now().strftime('%Y%m%d_%H%M%S')}.txt",
|
| 612 |
-
mime="text/plain",
|
| 613 |
-
use_container_width=True,
|
| 614 |
-
help="Download complete text content"
|
| 615 |
-
)
|
| 616 |
-
|
| 617 |
-
with col2:
|
| 618 |
-
# JSON export
|
| 619 |
-
if 'structured' in st.session_state.generated_content:
|
| 620 |
-
json_data = {
|
| 621 |
-
"metadata": {
|
| 622 |
-
"timestamp": datetime.now().isoformat(),
|
| 623 |
-
"generator": "VoiceCanvas AI Studio",
|
| 624 |
-
"mode": "Enhanced" if setup_gemini() else "Basic"
|
| 625 |
-
},
|
| 626 |
-
"input": st.session_state.transcription,
|
| 627 |
-
"content": st.session_state.generated_content['structured']
|
| 628 |
-
}
|
| 629 |
-
|
| 630 |
-
st.download_button(
|
| 631 |
-
"π Download Data",
|
| 632 |
-
json.dumps(json_data, indent=2),
|
| 633 |
-
file_name=f"content_data_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json",
|
| 634 |
-
mime="application/json",
|
| 635 |
-
use_container_width=True,
|
| 636 |
-
help="Download structured data (JSON)"
|
| 637 |
-
)
|
| 638 |
-
|
| 639 |
-
with col3:
|
| 640 |
-
# Image export
|
| 641 |
-
if 'image' in st.session_state.generated_content:
|
| 642 |
-
img_buffer = io.BytesIO()
|
| 643 |
-
st.session_state.generated_content['image'].save(img_buffer, format="PNG")
|
| 644 |
-
|
| 645 |
-
st.download_button(
|
| 646 |
-
"πΌοΈ Download Image",
|
| 647 |
-
img_buffer.getvalue(),
|
| 648 |
-
file_name=f"ai_image_{datetime.now().strftime('%Y%m%d_%H%M%S')}.png",
|
| 649 |
-
mime="image/png",
|
| 650 |
-
use_container_width=True,
|
| 651 |
-
help="Download generated image"
|
| 652 |
-
)
|
| 653 |
-
else:
|
| 654 |
-
st.info("Generate an image first", icon="βΉοΈ")
|
| 655 |
|
| 656 |
-
#
|
| 657 |
st.markdown("---")
|
| 658 |
-
|
| 659 |
-
with col2:
|
| 660 |
-
st.markdown("π¨ **VoiceCanvas AI Content Studio**")
|
| 661 |
-
st.caption("Transform ideas into marketing magic β’ Built with Streamlit")
|
| 662 |
|
| 663 |
if __name__ == "__main__":
|
| 664 |
main()
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import tempfile
|
| 3 |
+
import os
|
|
|
|
|
|
|
| 4 |
from datetime import datetime
|
|
|
|
| 5 |
|
| 6 |
+
# Simple imports only
|
| 7 |
try:
|
| 8 |
from transformers import pipeline
|
| 9 |
TRANSFORMERS_AVAILABLE = True
|
| 10 |
except ImportError:
|
| 11 |
TRANSFORMERS_AVAILABLE = False
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
try:
|
| 14 |
from st_audiorec import st_audiorec
|
| 15 |
AUDIO_REC_AVAILABLE = True
|
|
|
|
| 18 |
|
| 19 |
# Configure page
|
| 20 |
st.set_page_config(
|
| 21 |
+
page_title="VoiceCanvas - Simple AI Studio",
|
| 22 |
page_icon="π¨",
|
| 23 |
+
layout="centered"
|
|
|
|
| 24 |
)
|
| 25 |
|
| 26 |
+
# Initialize session state - SIMPLIFIED
|
|
|
|
|
|
|
| 27 |
if 'transcription' not in st.session_state:
|
| 28 |
st.session_state.transcription = ""
|
| 29 |
+
if 'generated_content' not in st.session_state:
|
| 30 |
+
st.session_state.generated_content = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
if 'whisper_model' not in st.session_state:
|
| 32 |
st.session_state.whisper_model = None
|
| 33 |
|
| 34 |
+
@st.cache_resource
|
| 35 |
+
def load_whisper_model():
|
| 36 |
+
"""Load Whisper model once and cache it"""
|
| 37 |
+
if TRANSFORMERS_AVAILABLE:
|
| 38 |
+
return pipeline(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
"automatic-speech-recognition",
|
| 40 |
model="openai/whisper-tiny",
|
| 41 |
+
device=-1
|
|
|
|
|
|
|
| 42 |
)
|
| 43 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
def transcribe_audio(audio_file):
|
| 46 |
+
"""Simple audio transcription"""
|
|
|
|
|
|
|
|
|
|
| 47 |
try:
|
| 48 |
+
model = load_whisper_model()
|
| 49 |
+
if model is None:
|
| 50 |
+
return "Error: Speech recognition not available"
|
| 51 |
|
| 52 |
+
result = model(audio_file)
|
| 53 |
+
return result["text"].strip()
|
|
|
|
|
|
|
| 54 |
except Exception as e:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
return f"Error: {str(e)}"
|
| 56 |
|
| 57 |
+
def generate_simple_content(prompt):
|
| 58 |
+
"""Generate simple marketing content without external APIs"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
+
# Extract key words from prompt
|
| 61 |
+
words = prompt.lower().split()
|
| 62 |
+
key_features = [word for word in words if len(word) > 4][:3]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
+
content = f"""# π― Marketing Content for: {prompt[:50]}...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
+
## π·οΈ Taglines
|
| 67 |
+
1. **Experience {key_features[0] if key_features else 'innovation'} like never before**
|
| 68 |
+
2. **Transform your world with our solution**
|
| 69 |
+
3. **Discover the power of smart technology**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
+
## π± Social Media Posts
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
+
**Post 1:**
|
| 74 |
+
π Ready to experience something amazing? Our innovative solution is changing lives every day! #Innovation #Technology
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
+
**Post 2:**
|
| 77 |
+
π« Join thousands who've already discovered the difference. Don't miss out on this incredible opportunity! #GameChanger
|
| 78 |
+
|
| 79 |
+
**Post 3:**
|
| 80 |
+
π The future is here! Experience what everyone's talking about and transform your daily routine. #Future
|
| 81 |
+
|
| 82 |
+
## π Product Description
|
| 83 |
+
{prompt}
|
| 84 |
+
|
| 85 |
+
Our innovative approach combines cutting-edge technology with user-friendly design. Perfect for both beginners and experts, this solution delivers results that exceed expectations.
|
| 86 |
+
|
| 87 |
+
## π― Call-to-Action Ideas
|
| 88 |
+
1. **Get Started Today!**
|
| 89 |
+
2. **Transform Your Experience Now**
|
| 90 |
+
3. **Join the Revolution**
|
| 91 |
+
|
| 92 |
+
---
|
| 93 |
+
*Generated by VoiceCanvas AI Studio*
|
| 94 |
+
"""
|
| 95 |
+
return content
|
| 96 |
+
|
| 97 |
+
def main():
|
| 98 |
+
# Header
|
| 99 |
+
st.title("π¨ VoiceCanvas - Simple AI Studio")
|
| 100 |
+
st.markdown("*Transform your ideas into marketing content quickly*")
|
| 101 |
+
|
| 102 |
+
# Simple status
|
| 103 |
+
col1, col2 = st.columns(2)
|
| 104 |
+
with col1:
|
| 105 |
+
st.metric("Speech Recognition", "β
Ready" if TRANSFORMERS_AVAILABLE else "β Not Available")
|
| 106 |
+
with col2:
|
| 107 |
+
st.metric("Audio Recording", "β
Ready" if AUDIO_REC_AVAILABLE else "β Not Available")
|
| 108 |
|
| 109 |
+
st.markdown("---")
|
|
|
|
| 110 |
|
| 111 |
+
# Input Section
|
| 112 |
+
st.header("π‘ Your Idea")
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
+
# Simple tabs
|
| 115 |
+
tab1, tab2, tab3 = st.tabs(["ποΈ Record" if AUDIO_REC_AVAILABLE else "β Record", "π Upload", "βοΈ Type"])
|
| 116 |
|
| 117 |
+
# Recording tab
|
| 118 |
+
with tab1:
|
| 119 |
+
if AUDIO_REC_AVAILABLE:
|
| 120 |
+
st.info("π€ Record your idea")
|
|
|
|
|
|
|
| 121 |
wav_audio_data = st_audiorec()
|
| 122 |
|
| 123 |
if wav_audio_data is not None:
|
| 124 |
+
st.success("β
Audio recorded!")
|
| 125 |
st.audio(wav_audio_data, format='audio/wav')
|
| 126 |
|
| 127 |
+
# Single button with immediate processing
|
| 128 |
+
if st.button("π Convert to Text", key="record_btn", type="primary"):
|
| 129 |
+
with st.spinner("Converting speech to text..."):
|
| 130 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
|
| 131 |
+
tmp_file.write(wav_audio_data)
|
| 132 |
+
transcription = transcribe_audio(tmp_file.name)
|
| 133 |
+
os.unlink(tmp_file.name)
|
| 134 |
+
st.session_state.transcription = transcription
|
| 135 |
+
st.success("β
Done!")
|
| 136 |
+
st.rerun()
|
| 137 |
+
else:
|
| 138 |
+
st.warning("Audio recording not available")
|
|
|
|
| 139 |
|
| 140 |
# Upload tab
|
| 141 |
+
with tab2:
|
| 142 |
+
st.info("π Upload audio file")
|
| 143 |
+
uploaded_file = st.file_uploader("Choose file", type=['wav', 'mp3', 'm4a'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
if uploaded_file:
|
| 146 |
+
st.success("β
File uploaded!")
|
| 147 |
st.audio(uploaded_file)
|
| 148 |
|
| 149 |
+
# Single button with immediate processing
|
| 150 |
+
if st.button("π Convert to Text", key="upload_btn", type="primary"):
|
| 151 |
+
with st.spinner("Converting speech to text..."):
|
| 152 |
+
transcription = transcribe_audio(uploaded_file)
|
| 153 |
+
st.session_state.transcription = transcription
|
| 154 |
+
st.success("β
Done!")
|
| 155 |
+
st.rerun()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
|
| 157 |
# Text tab
|
| 158 |
+
with tab3:
|
|
|
|
|
|
|
| 159 |
user_input = st.text_area(
|
| 160 |
+
"Describe your product/service:",
|
| 161 |
+
placeholder="A smart fitness tracker that helps busy professionals stay healthy...",
|
| 162 |
+
height=150
|
|
|
|
| 163 |
)
|
| 164 |
|
| 165 |
if user_input:
|
| 166 |
st.session_state.transcription = user_input
|
| 167 |
+
st.success(f"β
{len(user_input.split())} words entered")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
|
| 169 |
+
# Show current input
|
| 170 |
if st.session_state.transcription:
|
| 171 |
st.markdown("---")
|
| 172 |
+
st.header("π Your Input")
|
| 173 |
|
| 174 |
+
# Editable text
|
| 175 |
edited_text = st.text_area(
|
| 176 |
+
"Edit if needed:",
|
| 177 |
value=st.session_state.transcription,
|
| 178 |
+
height=100
|
|
|
|
|
|
|
| 179 |
)
|
| 180 |
st.session_state.transcription = edited_text
|
| 181 |
|
| 182 |
+
# Generate content button
|
| 183 |
st.markdown("---")
|
| 184 |
+
if st.button("π Generate Marketing Content", type="primary", use_container_width=True):
|
| 185 |
+
with st.spinner("β¨ Creating marketing content..."):
|
| 186 |
+
content = generate_simple_content(st.session_state.transcription)
|
| 187 |
+
st.session_state.generated_content = content
|
| 188 |
+
st.success("β
Content generated!")
|
| 189 |
+
st.rerun()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
|
| 191 |
+
# Show generated content
|
| 192 |
if st.session_state.generated_content:
|
| 193 |
st.markdown("---")
|
| 194 |
st.header("β¨ Your Marketing Content")
|
| 195 |
+
st.markdown(st.session_state.generated_content)
|
| 196 |
|
| 197 |
+
# Simple download
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
st.markdown("---")
|
| 199 |
+
st.download_button(
|
| 200 |
+
"π₯ Download Content",
|
| 201 |
+
st.session_state.generated_content,
|
| 202 |
+
file_name=f"marketing_content_{datetime.now().strftime('%Y%m%d_%H%M%S')}.md",
|
| 203 |
+
mime="text/markdown",
|
| 204 |
+
use_container_width=True
|
| 205 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
|
| 207 |
+
# Simple footer
|
| 208 |
st.markdown("---")
|
| 209 |
+
st.caption("π¨ VoiceCanvas - Simple & Fast")
|
|
|
|
|
|
|
|
|
|
| 210 |
|
| 211 |
if __name__ == "__main__":
|
| 212 |
main()
|