Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
from langchain_core.prompts import ChatPromptTemplate
|
4 |
+
from langchain_groq import ChatGroq
|
5 |
+
|
6 |
+
|
7 |
+
|
8 |
+
# ---------------------------
|
9 |
+
# LangChain Setup
|
10 |
+
# ---------------------------
|
11 |
+
|
12 |
+
llm = ChatGroq(
|
13 |
+
temperature=0,
|
14 |
+
groq_api_key=os.getenv('groq_api_key'),
|
15 |
+
model_name="llama-3.3-70b-versatile"
|
16 |
+
)
|
17 |
+
|
18 |
+
def niche_detection(text):
|
19 |
+
system_prompt = """
|
20 |
+
You are a social media niche expert.
|
21 |
+
Analyze the following Instagram post captions and hashtags to determine the niches this content belongs to.
|
22 |
+
|
23 |
+
User text : {text}
|
24 |
+
|
25 |
+
Output the result in the following format:
|
26 |
+
|
27 |
+
[
|
28 |
+
"niche": Fitness
|
29 |
+
]
|
30 |
+
"""
|
31 |
+
|
32 |
+
prompt = ChatPromptTemplate.from_messages([("system", system_prompt)])
|
33 |
+
chain = prompt | llm
|
34 |
+
output = chain.invoke({"text": text})
|
35 |
+
|
36 |
+
try:
|
37 |
+
return output.content.split('"niche": "')[1].split('"')[0]
|
38 |
+
except Exception:
|
39 |
+
return "Could not determine niche"
|
40 |
+
|
41 |
+
def content_styles(text):
|
42 |
+
system_prompt = """
|
43 |
+
You are an expert in analyzing social media content styles. Review the following Instagram captions and hashtags and classify the dominant content style used in each post. Choose from the following styles:
|
44 |
+
- Educational
|
45 |
+
- Motivational
|
46 |
+
- Meme/Relatable
|
47 |
+
- Personal Story
|
48 |
+
- Tips/Hacks
|
49 |
+
- Promotional
|
50 |
+
- Inspirational
|
51 |
+
- Opinion/Rant
|
52 |
+
|
53 |
+
User text : {text}
|
54 |
+
|
55 |
+
Output the result in the following format:
|
56 |
+
|
57 |
+
["content_styles": Educational]
|
58 |
+
"""
|
59 |
+
|
60 |
+
prompt = ChatPromptTemplate.from_messages([("system", system_prompt)])
|
61 |
+
chain = prompt | llm
|
62 |
+
output = chain.invoke({"text": text})
|
63 |
+
|
64 |
+
try:
|
65 |
+
return output.content.split('"content_styles": ')[1].split(']')[0].strip()
|
66 |
+
except Exception:
|
67 |
+
return "Could not determine content style"
|
68 |
+
|
69 |
+
# ---------------------------
|
70 |
+
# Gradio Interface
|
71 |
+
# ---------------------------
|
72 |
+
|
73 |
+
def analyze_post(text):
|
74 |
+
niche = niche_detection(text)
|
75 |
+
style = content_styles(text)
|
76 |
+
return {
|
77 |
+
"Niche": niche,
|
78 |
+
"Content Style": style
|
79 |
+
}
|
80 |
+
|
81 |
+
iface = gr.Interface(
|
82 |
+
fn=analyze_post,
|
83 |
+
inputs=gr.Textbox(lines=5, label="Instagram Post Text"),
|
84 |
+
outputs=[
|
85 |
+
gr.Textbox(label="Detected Niche"),
|
86 |
+
gr.Textbox(label="Detected Content Style")
|
87 |
+
],
|
88 |
+
title="Instagram Post Analyzer",
|
89 |
+
description="Enter an Instagram post (caption + hashtags) to detect its niche and content style."
|
90 |
+
)
|
91 |
+
|
92 |
+
if __name__ == "__main__":
|
93 |
+
iface.launch(share=True)
|