import gradio as gr import os from langchain_core.prompts import ChatPromptTemplate from langchain_groq import ChatGroq # --------------------------- # LangChain Setup # --------------------------- llm = ChatGroq( temperature=0, groq_api_key=os.getenv('GROQ_API_KEY'), model_name="llama-3.3-70b-versatile" ) def niche_detection(text): system_prompt = """ You are a highly skilled social media strategist and niche classification expert. Your task is to analyze the following Instagram caption and hashtags to identify the **main niche** the content belongs to. Examples of possible niches include: - Fitness - Nutrition - Mental Health - Personal Finance - Entrepreneurship - Fashion - Tech/Gadgets - Parenting - Travel - Beauty/Skincare - Relationships - Digital Marketing - Motivation - Self-improvement - Gaming - Food/Cooking - Home Decor - Photography - Music - Art/Design Analyze the content carefully and respond with ONLY the niche name (e.g., "Fitness" or "Travel"). ** Don't retunr - " Could not determine niche " User text : {text} Output the result in the following format: [ "niche": Fitness ] """ prompt = ChatPromptTemplate.from_messages([("system", system_prompt)]) chain = prompt | llm output = chain.invoke({"text": text}) return output.content def content_styles(text): system_prompt = """ You are an expert in analyzing social media content styles. Review the following Instagram caption and hashtags and classify the dominant content style. Choose from these content styles: - Educational (teaching something new, how-to content) - Motivational (encouraging action, inspiring change) - Meme/Relatable (funny, relatable content) - Personal Story (sharing personal experiences) - Tips/Hacks (quick tips, life hacks) - Promotional (selling products/services) - Inspirational (uplifting quotes, positive messages) - Opinion/Rant (personal opinions, controversial takes) - Behind-the-Scenes (showing process, daily life) - Question/Poll (asking for engagement) Analyze the content and respond with ONLY the content style name (e.g., "Educational" or "Motivational"). User text : {text} Output the result in the following format: ["content_styles": Educational] """ prompt = ChatPromptTemplate.from_messages([("system", system_prompt)]) chain = prompt | llm output = chain.invoke({"text": text}) return output.content # --------------------------- # Gradio Interface # --------------------------- def analyze_post(text): niche = niche_detection(text) style = content_styles(text) return niche, style iface = gr.Interface( fn=analyze_post, inputs=gr.Textbox(lines=5, label="Instagram Post Text"), outputs=[ gr.Textbox(label="Detected Niche"), gr.Textbox(label="Detected Content Style") ], description="Enter an Instagram post (caption + hashtags) to detect its niche and content style." ) if __name__ == "__main__": iface.launch(share=True)