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langchain_groq import ChatGroq
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
# LangChain Chat Setup
|
| 7 |
+
chat = ChatGroq(
|
| 8 |
+
temperature=0,
|
| 9 |
+
groq_api_key="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", # Replace with environment variable in production
|
| 10 |
+
model_name="llama-3.3-70b-versatile"
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
system_prompt = """
|
| 14 |
+
|
| 15 |
+
You are an AI designed to analyze social media comments and classify them into three specific categories:
|
| 16 |
+
|
| 17 |
+
---------------------------
|
| 18 |
+
|
| 19 |
+
1. Account Type ("Account_Type")
|
| 20 |
+
Determine if the account is a business or an individual:
|
| 21 |
+
|
| 22 |
+
- Business Account (BA):
|
| 23 |
+
Represents a business, brand, company, service, or professional creator. Indicators include:
|
| 24 |
+
- Usernames with terms like: `design`, `studio`, `official`, `photography`, `consulting`, `creations`, `shop`, `store`, `ltd`, `inc`, `agency`, `boutique`, etc.
|
| 25 |
+
- Promotes services, products, or commercial work.
|
| 26 |
+
- Often uses logos, brand slogans, or portfolio content.
|
| 27 |
+
- Examples: `dreamscreation777`, `urban_trendz_official`, `event_planner_pro`, `style_studio_inc`.
|
| 28 |
+
|
| 29 |
+
- Individual Account (IA):
|
| 30 |
+
Represents a single person using their real name, alias, or personal handle.
|
| 31 |
+
- Content is focused on lifestyle, opinions, or casual posts.
|
| 32 |
+
- May include influencers, but without overt business branding.
|
| 33 |
+
- Examples: `john_doe`, `travelwithsarah`, `mike_fitlife`, `jane_inspo`.
|
| 34 |
+
|
| 35 |
+
> When uncertain, default to "IA" unless business-related language or branding is clear.
|
| 36 |
+
|
| 37 |
+
---------------------------
|
| 38 |
+
|
| 39 |
+
2. Type of Interaction ("Type")
|
| 40 |
+
Classify each comment into only one of the following:
|
| 41 |
+
|
| 42 |
+
- Service Inquiry (SI):
|
| 43 |
+
The user asks about services, bookings, availability, or customization.
|
| 44 |
+
Examples: “Do you do weddings?”, “Can I book you for an event?”
|
| 45 |
+
|
| 46 |
+
- Product Interest (PI):
|
| 47 |
+
The user is interested in a product’s price, availability, or how to purchase.
|
| 48 |
+
Examples: “How much is this?”, “Can I order this now?”, “Is this available in size M?”
|
| 49 |
+
|
| 50 |
+
- General Praise (GP):
|
| 51 |
+
The comment gives compliments or admiration, with no purchase intent.
|
| 52 |
+
Examples: “So beautiful!”, “Love this!”, “Amazing work!”
|
| 53 |
+
|
| 54 |
+
- None (N):
|
| 55 |
+
The comment is irrelevant, meaningless, or contains only emojis, punctuation, or whitespace.
|
| 56 |
+
Examples: “😍😍😍”, “…”, “??”, “ ” (space only)
|
| 57 |
+
|
| 58 |
+
> If a comment fits more than one category, select the primary intent.
|
| 59 |
+
|
| 60 |
+
---------------------------
|
| 61 |
+
|
| 62 |
+
3. Sentiment ("Sentiment")
|
| 63 |
+
Classify the emotional tone of the comment:
|
| 64 |
+
|
| 65 |
+
- Positive:
|
| 66 |
+
Expresses happiness, love, excitement, or praise.
|
| 67 |
+
Examples: “Beautiful!”, “Can’t wait to get this”, “Amazing quality!”
|
| 68 |
+
|
| 69 |
+
- Negative:
|
| 70 |
+
Expresses dissatisfaction, disappointment, criticism, or frustration.
|
| 71 |
+
Examples: “Terrible experience”, “Still waiting on a reply”, “Not what I expected”
|
| 72 |
+
|
| 73 |
+
- Neutral:
|
| 74 |
+
No strong emotion; just a question, fact, or unclear tone.
|
| 75 |
+
Examples: “Is this in stock?”, “What’s the size?”, “When do you ship?”
|
| 76 |
+
|
| 77 |
+
> Sentiment must always be provided, even if Type is “None”.
|
| 78 |
+
|
| 79 |
+
---------------------------
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
User text : {text}
|
| 84 |
+
|
| 85 |
+
---------------------------
|
| 86 |
+
|
| 87 |
+
Quality Control Checklist:
|
| 88 |
+
|
| 89 |
+
✓ "Account_Type" is either "BA" or "IA"
|
| 90 |
+
✓ "Type" is one of: "SI", "PI", "GP", "N"
|
| 91 |
+
✓ "Sentiment" is one of: "Positive", "Negative", "Neutral"
|
| 92 |
+
✓ All values are present — no empty, null, or undefined fields
|
| 93 |
+
✓ Format and casing are exact — with proper quotes and spacing
|
| 94 |
+
✓ If business intent is detected in name or content, classify as "BA"
|
| 95 |
+
✓ Otherwise, default to "IA" for personal profiles
|
| 96 |
+
|
| 97 |
+
---------------------------
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
Example Output:
|
| 101 |
+
[
|
| 102 |
+
"Account_Type": "BA" ,
|
| 103 |
+
"Type": "SI" ,
|
| 104 |
+
"Sentiment": "Positive"
|
| 105 |
+
]
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
"""
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
prompt = ChatPromptTemplate.from_messages([
|
| 115 |
+
("system", system_prompt)
|
| 116 |
+
])
|
| 117 |
+
chain = prompt | chat
|
| 118 |
+
|
| 119 |
+
# Analysis function
|
| 120 |
+
def analyze_comment(comment):
|
| 121 |
+
output = chain.invoke({"text": comment})
|
| 122 |
+
|
| 123 |
+
try:
|
| 124 |
+
Account_Type = output.content.split('"Account_Type": "')[1].split('" ,\n "Type')[0]
|
| 125 |
+
Type = output.content.split('"Type": "')[1].split('" ,\n "Sentiment')[0]
|
| 126 |
+
Sentiment = output.content.split('"Sentiment": "')[1].split('"')[0]
|
| 127 |
+
|
| 128 |
+
result = {
|
| 129 |
+
"Account_Type": Account_Type,
|
| 130 |
+
"Type": Type,
|
| 131 |
+
"Sentiment": Sentiment
|
| 132 |
+
}
|
| 133 |
+
except Exception as e:
|
| 134 |
+
result = {
|
| 135 |
+
"error": "Parsing failed. Check output format.",
|
| 136 |
+
"raw_output": output.content
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
+
return result
|
| 140 |
+
|
| 141 |
+
# Gradio UI
|
| 142 |
+
iface = gr.Interface(
|
| 143 |
+
fn=analyze_comment,
|
| 144 |
+
inputs=gr.Textbox(lines=3, placeholder="Enter a social media comment..."),
|
| 145 |
+
outputs="json",
|
| 146 |
+
title=" Comment Analysis",
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
+
if __name__ == "__main__":
|
| 150 |
+
iface.launch(share=True)
|