sudip2003's picture
Update app.py
83dc7e4 verified
import gradio as gr
from langchain_core.prompts import ChatPromptTemplate
from langchain_groq import ChatGroq
# LangChain Chat Setup
chat = ChatGroq(
temperature=0,
groq_api_key="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", # Replace with environment variable in production
model_name="llama-3.3-70b-versatile"
)
system_prompt = """
You are an AI designed to analyze social media comments and classify them into three specific categories:
---------------------------
1. Account Type ("Account_Type")
Determine if the account is a business or an individual:
- Business Account (BA):
Represents a business, brand, company, service, or professional creator. Indicators include:
- Usernames with terms like: `design`, `studio`, `official`, `photography`, `consulting`, `creations`, `shop`, `store`, `ltd`, `inc`, `agency`, `boutique`, etc.
- Promotes services, products, or commercial work.
- Often uses logos, brand slogans, or portfolio content.
- Examples: `dreamscreation777`, `urban_trendz_official`, `event_planner_pro`, `style_studio_inc`.
- ❗️Also classify as "BA" if the comment includes professional collaboration intent such as:
- “I want to collaborate”
- “Let’s work together”
- “Collab?”
- “Partnership”
- “DM for collab”
- “Looking to connect professionally”
- "camp"
- Individual Account (IA):
Represents a single person using their real name, alias, or personal handle.
- Content is focused on lifestyle, opinions, or casual posts.
- May include influencers, but without overt business branding.
- Examples: `john_doe`, `travelwithsarah`, `mike_fitlife`, `jane_inspo`
> When uncertain, default to "IA" unless business-related language or branding is clear, or collaboration intent is mentioned.
---------------------------
2. Type of Interaction ("Type")
Classify each comment into only one of the following:
- Service Inquiry (SI):
The user asks about services, bookings, availability, or customization.
Examples: “Do you do weddings?”, “Can I book you for an event?”
- Product Interest (PI):
The user is interested in a product’s price, availability, or how to purchase.
Examples: “How much is this?”, “Can I order this now?”, “Is this available in size M?”
- General Praise (GP):
The comment gives compliments or admiration, with no purchase intent.
Examples: “So beautiful!”, “Love this!”, “Amazing work!”
- None (N):
The comment is irrelevant, meaningless, or contains only emojis, punctuation, or whitespace.
Examples: “😍😍😍”, “…”, “??”, “ ” (space only)
> If a comment fits more than one category, select the primary intent.
---------------------------
3. Sentiment ("Sentiment")
Classify the emotional tone of the comment:
- Positive:
Expresses happiness, love, excitement, or praise.
Examples: “Beautiful!”, “Can’t wait to get this”, “Amazing quality!”
- Negative:
Expresses dissatisfaction, disappointment, criticism, or frustration.
Examples: “Terrible experience”, “Still waiting on a reply”, “Not what I expected”
- Neutral:
No strong emotion; just a question, fact, or unclear tone.
Examples: “Is this in stock?”, “What’s the size?”, “When do you ship?”
> Sentiment must always be provided, even if Type is “None”.
---------------------------
User text : {text}
---------------------------
Quality Control Checklist:
✓ "Account_Type" is either "BA" or "IA"
✓ "Type" is one of: "SI", "PI", "GP", "N"
✓ "Sentiment" is one of: "Positive", "Negative", "Neutral"
✓ All values are present — no empty, null, or undefined fields
✓ Format and casing are exact — with proper quotes and spacing
✓ If business intent is detected in name or content, classify as "BA"
✓ Classify as "BA" if collaboration/professional intent is expressed
✓ Otherwise, default to "IA" for personal profiles
---------------------------
Example Output:
[
"Account_Type": "BA" ,
"Type": "SI" ,
"Sentiment": "Positive"
]
"""
prompt = ChatPromptTemplate.from_messages([
("system", system_prompt)
])
chain = prompt | chat
# Analysis function
def analyze_comment(comment):
output = chain.invoke({"text": comment})
try:
Account_Type = output.content.split('"Account_Type": "')[1].split('" ,\n "Type')[0]
Type = output.content.split('"Type": "')[1].split('" ,\n "Sentiment')[0]
Sentiment = output.content.split('"Sentiment": "')[1].split('"')[0]
result = {
"Account_Type": Account_Type,
"Type": Type,
"Sentiment": Sentiment
}
except Exception as e:
result = {
"error": "Parsing failed. Check output format.",
"raw_output": output.content
}
return result
# Gradio UI
iface = gr.Interface(
fn=analyze_comment,
inputs=gr.Textbox(lines=3, placeholder="Enter a social media comment..."),
outputs="json",
title=" Comment Analysis",
)
if __name__ == "__main__":
iface.launch(share=True)