import streamlit as st from openai import OpenAI import os import re # Initialize OpenAI client client = OpenAI(api_key=os.getenv("OPENAI_API_KEY")) def search_location_info(location, script_content): """ Uses AI to search for location-specific information relevant to the script content. """ try: search_prompt = f""" You are a location research expert. Search for current, relevant information about {location} that would be useful for customizing the following video script content. Script content preview: {script_content[:500]}... Please search for and provide: 1. Key local businesses, landmarks, or services mentioned in or relevant to this script 2. Local regulations, practices, or customs that might be relevant 3. Popular local areas, neighborhoods, or districts 4. Local events, seasons, or cultural aspects that could be incorporated 5. Demographics and target audience characteristics for this location Focus on information that would help make this script feel authentic and locally relevant. """ response = client.chat.completions.create( model="gpt-4", messages=[ {"role": "system", "content": "You are a local market research expert with access to current information. Use web search capabilities to find relevant, current information about locations."}, {"role": "user", "content": search_prompt} ] ) return response.choices[0].message.content except Exception as e: return f"Unable to gather location-specific information: {str(e)}" def customize_script_for_location(original_script, location, research_info): """ Customizes the video script for the specific location using research information. """ try: customization_prompt = f""" You are a expert video script writer specializing in local content adaptation. Original Script: {original_script} Target Location: {location} Research Information: {research_info} Your task is to customize this script to be specific and relevant to {location}. Guidelines: 1. **Subtle Integration**: Use the location name SPARINGLY (1-3 times maximum) and only where it feels natural 2. **Strategic Local Context**: Add 1-2 authentic local details that enhance the message without overwhelming it 3. **Maintain Original Flow**: Keep the exact same structure, length, and tone of the original script 4. **Quality Over Quantity**: Better to make fewer, higher-impact local references than to force many 5. **Natural Feel**: The script should feel like the original with just enough local flavor to be relevant SPECIFIC RULES: - Use the location name maximum 2-3 times in the entire script - Add only 1-2 specific local references (businesses, landmarks, regulations, or practices) - Keep the same conversational tone and length as the original - Don't add unnecessary local adjectives or descriptors - Focus on enhancing key points rather than adding location references everywhere If the script topic isn't naturally location-based: - Add the location name once in the opening or closing - Include ONE relevant local reference if it naturally fits - Don't force location into every sentence If the script IS location-based (real estate, local services, etc.): - Replace 1-2 generic references with specific local ones - Mention local market conditions or regulations only if directly relevant - Use local area names sparingly and only where they add real value Return only the customized script, maintaining the same format and structure as the original. """ response = client.chat.completions.create( model="gpt-4", messages=[ {"role": "system", "content": "You are an expert video script writer who specializes in creating location-specific content that feels authentic and locally relevant."}, {"role": "user", "content": customization_prompt} ] ) return response.choices[0].message.content except Exception as e: return f"Error customizing script: {str(e)}" def generate_social_media_content(customized_script, location, original_script): """ Generates optimized social media content including title, description, and hashtags. """ try: # Extract key themes from the script for better social media content script_preview = customized_script[:800] if len(customized_script) > 800 else customized_script social_media_prompt = f""" Based on this location-customized video script for {location}, create optimized social media content: Script Preview: {script_preview} Create: 1. **Video Title** (60 characters max, engaging and location-specific) 2. **Description** (150-300 words, engaging copy that includes the location and key script themes) 3. **Hashtags** (15-20 relevant hashtags including location-based and topic-based tags) Guidelines: - Make the title compelling and click-worthy with the location mentioned ONCE - Write the description to hook viewers, mention location 1-2 times maximum - Don't over-emphasize location in the description - focus on value and engagement - Include location hashtags (city, state, region) - about 3-5 location tags - Add topic-specific hashtags related to the script content - about 8-10 topic tags - Include 2-3 popular general hashtags that could boost reach - Mix broad and niche hashtags for optimal discoverability - Keep total hashtags to 15-20 maximum Format as: **TITLE:** [Title here] **DESCRIPTION:** [Description here] **HASHTAGS:** [All hashtags here] """ response = client.chat.completions.create( model="gpt-4", messages=[ {"role": "system", "content": "You are a social media marketing expert who creates viral, location-specific content that maximizes local engagement and discoverability."}, {"role": "user", "content": social_media_prompt} ] ) return response.choices[0].message.content except Exception as e: return f"Error generating social media content: {str(e)}" # Define initial system message for context initial_system_message = { "role": "system", "content": """You are a expert content localization specialist with deep knowledge of local markets, cultural nuances, and location-based marketing strategies. You excel at making any content feel authentically local while maintaining its original purpose and effectiveness.""" } # Streamlit setup st.set_page_config(layout="wide") # Initialize session state if "customized_script" not in st.session_state: st.session_state["customized_script"] = None if "social_content" not in st.session_state: st.session_state["social_content"] = None if "show_notice" not in st.session_state: st.session_state["show_notice"] = False # Centered title st.markdown("