maria355 commited on
Commit
4e5890a
Β·
verified Β·
1 Parent(s): de2f39c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +128 -580
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
- from PIL import Image
8
- import requests
9
- import json
10
  from datetime import datetime
11
- import time
12
 
13
- # Import with error handling
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 Content Studio",
35
  page_icon="🎨",
36
- layout="wide",
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 'processing' not in st.session_state:
46
- st.session_state.processing = False
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
- def load_models():
55
- """Load models efficiently with progress tracking"""
56
-
57
- if st.session_state.models_loaded and st.session_state.whisper_model is not None:
58
- return True
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, # Use CPU
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 setup_gemini():
102
- """Setup Gemini API if available"""
103
- if not GENAI_AVAILABLE:
104
- return False
105
-
106
  try:
107
- api_key = os.getenv("GEMINI_API_KEY")
108
- if not api_key and hasattr(st, 'secrets'):
109
- api_key = st.secrets.get("GEMINI_API_KEY", "")
110
 
111
- if api_key:
112
- genai.configure(api_key=api_key)
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 generate_content_with_gemini(prompt):
162
- """Generate content using Gemini"""
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
- # Create structured content
206
- content = {
207
- "taglines": [
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
- # Format for display
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
- def generate_image_with_api(prompt):
235
- """Generate image using free API"""
236
- try:
237
- st.session_state.current_task = "Creating image with AI..."
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
- def format_content_display(content):
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
- def main():
293
- # Sidebar with tips and status
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
- # Main content
377
- st.title("🎨 VoiceCanvas - AI Content Studio")
378
- st.markdown("*Transform your ideas into comprehensive marketing content*")
379
-
380
- # Show model loading status
381
- if not st.session_state.models_loaded:
382
- st.warning("⚠️ AI models not loaded yet. Click 'Load AI Models' in the sidebar to enable speech recognition.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
383
 
384
- # Main input area
385
- st.header("πŸ’‘ Share Your Idea")
386
 
387
- # Dynamic tabs based on available features
388
- available_tabs = []
389
- if AUDIO_REC_AVAILABLE:
390
- available_tabs.append("πŸŽ™οΈ Record")
391
- available_tabs.extend(["πŸ“ Upload", "✍️ Type"])
392
 
393
- tabs = st.tabs(available_tabs)
394
- tab_index = 0
395
 
396
- # Recording tab (if available)
397
- if AUDIO_REC_AVAILABLE:
398
- with tabs[tab_index]:
399
- st.info("🎀 Click the microphone button to start recording")
400
-
401
- # Audio recorder
402
  wav_audio_data = st_audiorec()
403
 
404
  if wav_audio_data is not None:
405
- st.success("πŸŽ‰ Audio recorded successfully!")
406
  st.audio(wav_audio_data, format='audio/wav')
407
 
408
- col1, col2 = st.columns([1, 2])
409
- with col1:
410
- if st.button("πŸ”„ Transcribe Audio", key="transcribe_btn", type="primary"):
411
- if not st.session_state.models_loaded:
412
- st.error("Please load AI models first using the sidebar button.")
413
- else:
414
- st.session_state.processing = True
415
- st.rerun()
416
-
417
- with col2:
418
- if st.session_state.processing:
419
- st.info("πŸ”„ Processing your audio...")
420
- tab_index += 1
421
 
422
  # Upload tab
423
- with tabs[tab_index]:
424
- st.info("πŸ“ Upload an audio file containing your idea")
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("πŸ“„ File uploaded successfully!")
434
  st.audio(uploaded_file)
435
 
436
- col1, col2 = st.columns([1, 2])
437
- with col1:
438
- if st.button("πŸ”„ Process Audio", key="upload_transcribe", type="primary"):
439
- if not st.session_state.models_loaded:
440
- st.error("Please load AI models first using the sidebar button.")
441
- else:
442
- st.session_state.processing = True
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 tabs[tab_index]:
453
- st.info("✍️ Type or paste your product/service description")
454
-
455
  user_input = st.text_area(
456
- "Describe your idea:",
457
- placeholder="Example: A smart fitness tracker that monitors sleep patterns, heart rate, and stress levels. It provides personalized workout recommendations for busy professionals who want to maintain their health despite hectic schedules.",
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
- word_count = len(user_input.split())
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 transcription and editing
497
  if st.session_state.transcription:
498
  st.markdown("---")
499
- st.header("πŸ“ Review Your Input")
500
 
 
501
  edited_text = st.text_area(
502
- "Edit or refine your input:",
503
  value=st.session_state.transcription,
504
- height=120,
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 section
511
  st.markdown("---")
512
- col1, col2, col3 = st.columns([1, 2, 1])
513
-
514
- with col2:
515
- if st.button("πŸš€ Generate Marketing Content", type="primary", use_container_width=True):
516
- with st.spinner("✨ Creating comprehensive marketing content..."):
517
- if setup_gemini():
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
- # Display generated content
527
  if st.session_state.generated_content:
528
  st.markdown("---")
529
  st.header("✨ Your Marketing Content")
 
530
 
531
- # Text content
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.subheader("🎨 Visual Content")
538
-
539
- col1, col2 = st.columns([2, 1])
540
-
541
- with col1:
542
- if 'structured' in st.session_state.generated_content:
543
- # Show pre-made prompts
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
- # Footer
657
  st.markdown("---")
658
- col1, col2, col3 = st.columns([1, 2, 1])
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()