import json
import random
from piper import Piper  # Ensure Piper is correctly installed

# Paths to the JSON files
customer_data_path = '/root/mm/android_final/automated_image_generator/customer_data.json'
prompts_path = '/root/mm/android_final/automated_image_generator/prompts.json'
voice_script_path = '/root/mm/android_final/automated_image_generator/plat_sub/voice/scripts/voice_script.txt'

# Function to load customer data from JSON file
def load_customer_data(path):
    with open(path, 'r') as file:
        return json.load(file)

# Function to create a unique image prompt
def create_image_prompt(customer_data):
    business_info = customer_data['business_info']
    preferences = customer_data['preferences']
    
    # Randomly select elements from customer data
    selected_keywords = random.sample(business_info['keywords_info']['keywords'], 2)
    selected_style = random.choice(preferences['styles'])
    selected_color = random.choice(preferences['color_schemes'])
    selected_mood = random.choice(preferences['moods'])
    
    # Build the prompt
    prompt_text = (
        f"Create an image for {business_info['business_name']}, "
        f"a {business_info['business_type']} specializing in {', '.join(selected_keywords)}. "
        f"The style is {selected_style}, with a {selected_color} color scheme, "
        f"and a {selected_mood} mood."
    )
    
    return prompt_text

# Function to save prompts to JSON file
def save_prompts_to_json(prompt_text, path):
    prompts_data = {
        "prompts": [
            {
                "prompt_text": prompt_text,
                "image_settings": {
                    "width": 1024,
                    "height": 1024,
                    "format": "png"
                }
            }
        ]
    }
    
    with open(path, 'w') as file:
        json.dump(prompts_data, file, indent=4)

# Function to create voice script using Piper
def create_voice_script(prompt_text, path):
    # Creating a voice script based on the prompt
    voice_script = (
        f"This image represents {prompt_text}. "
        "It captures the essence of what makes your business unique and appealing to your target audience."
        "Experience the beauty of lawn care with our expert services that enhance your home's curb appeal."
    )
    
    # Save the voice script to a text file
    with open(path, 'w') as file:
        file.write(voice_script)
    
    # Use Piper to generate audio from the voice script
    piper = Piper()
    audio_output_path = path.replace('.txt', '.mp3')  # Save as MP3
    piper.generate_audio(voice_script, audio_output_path)

# Main function
if __name__ == "__main__":
    # Load customer data
    customer_data = load_customer_data(customer_data_path)
    
    # Create image prompt
    image_prompt = create_image_prompt(customer_data)
    
    # Save the prompt to JSON
    save_prompts_to_json(image_prompt, prompts_path)
    
    # Create voice script and generate audio
    create_voice_script(image_prompt, voice_script_path)

    print("Image prompt and voice script created successfully.")
id_final/automated_image_generator root# python3 pipervoice.py

Traceback (most recent call last):
  File "/root/mm/android_final/automated_image_generator/pipervoice.py", line 3, in <module>
    from piper import Piper  # Ensure Piper is correctly installed
    ^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/mm/android_final/my_venv/lib/python3.12/site-packages/piper/__init__.py", line 1, in <module>
    from .voice import PiperVoice
  File "/root/mm/android_final/my_venv/lib/python3.12/site-packages/piper/voice.py", line 10, in <module>
    from piper_phonemize import phonemize_codepoints, phonemize_espeak, tashkeel_run
  File "/root/mm/android_final/my_venv/lib/python3.12/site-packages/piper_phonemize/__init__.py", line 6, in <module>
    from piper_phonemize_cpp import (
ImportError: /root/mm/android_final/my_venv/lib/python3.12/site-packages/piper_phonemize_cpp.cpython-312-aarch64-linux-gnu.so: undefined symbol: espeak_TextToPhonemesWithTerminator