Spaces:
Sleeping
Sleeping
File size: 5,159 Bytes
5033639 83dc7e4 5033639 83dc7e4 5033639 83dc7e4 5033639 83dc7e4 5033639 83dc7e4 5033639 |
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 119 120 121 122 123 124 125 126 127 128 129 130 |
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)
|