File size: 3,372 Bytes
e17f092 4d40826 e17f092 7008cc0 e17f092 7008cc0 e17f092 7008cc0 e17f092 7008cc0 e17f092 de969bb e17f092 7008cc0 e17f092 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
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)
|