Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 5 |
+
from sklearn.ensemble import RandomForestClassifier
|
| 6 |
+
from sklearn.utils import Bunch
|
| 7 |
+
|
| 8 |
+
# 1. SETUP & DATA LOADING (Based on your notebook logic)
|
| 9 |
+
def load_message_data():
|
| 10 |
+
# In a real Space, you'd upload your 'messages_emojis.csv' to the repo
|
| 11 |
+
try:
|
| 12 |
+
df = pd.read_csv('messages_emojis.csv').dropna(subset=['content'])
|
| 13 |
+
except:
|
| 14 |
+
# Fallback dummy data for demonstration if file is missing
|
| 15 |
+
data = {
|
| 16 |
+
'content': ["I love this!", "That is so scary", "Hahaha so funny", "I agree", "I am sad"],
|
| 17 |
+
'emoji': ["โค๏ธ", "๐ฎ", "๐", "๐", "๐ข"]
|
| 18 |
+
}
|
| 19 |
+
df = pd.DataFrame(data)
|
| 20 |
+
|
| 21 |
+
sent_map = {'โค๏ธ':'Pos', '๐':'Pos', '๐':'Pos', '๐ฏ':'Pos', '๐ข':'Neg', '๐ญ':'Neg', '๐ฎ':'Neu'}
|
| 22 |
+
intent_map = {'โค๏ธ':'Emotion', '๐':'Agreement', '๐':'Emotion', '๐ฎ':'Surprise'}
|
| 23 |
+
|
| 24 |
+
get_sent = lambda x: sent_map.get(x, 'Neutral')
|
| 25 |
+
get_intent = lambda x: intent_map.get(x, 'Other')
|
| 26 |
+
|
| 27 |
+
return Bunch(
|
| 28 |
+
data=df['content'].values,
|
| 29 |
+
target_emoji=df['emoji'].values,
|
| 30 |
+
target_sentiment=df['emoji'].apply(get_sent).values,
|
| 31 |
+
target_intent=df['emoji'].apply(get_intent).values
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
# 2. MODEL TRAINING
|
| 35 |
+
dataset = load_message_data()
|
| 36 |
+
tfidf = TfidfVectorizer(max_features=1000, stop_words='english')
|
| 37 |
+
X = tfidf.fit_transform(dataset.data)
|
| 38 |
+
|
| 39 |
+
clf_emoji = RandomForestClassifier().fit(X, dataset.target_emoji)
|
| 40 |
+
clf_sent = RandomForestClassifier().fit(X, dataset.target_sentiment)
|
| 41 |
+
clf_intent = RandomForestClassifier().fit(X, dataset.target_intent)
|
| 42 |
+
|
| 43 |
+
# 3. CHAT LOGIC
|
| 44 |
+
def predict_all(text):
|
| 45 |
+
vec = tfidf.transform([text])
|
| 46 |
+
emoji = clf_emoji.predict(vec)[0]
|
| 47 |
+
sentiment = clf_sent.predict(vec)[0]
|
| 48 |
+
intent = clf_intent.predict(vec)[0]
|
| 49 |
+
return emoji, sentiment, intent
|
| 50 |
+
|
| 51 |
+
def chat_response(message, history):
|
| 52 |
+
emoji, sentiment, intent = predict_all(message)
|
| 53 |
+
|
| 54 |
+
# The models "responding" as people in a group chat
|
| 55 |
+
responses = [
|
| 56 |
+
{"role": "assistant", "content": f"I react to that with: {emoji}", "metadata": {"title": "EmojiBot"}},
|
| 57 |
+
{"role": "assistant", "content": f"I sense a {sentiment} vibe from you.", "metadata": {"title": "SentiBot"}},
|
| 58 |
+
{"role": "assistant", "content": f"Your intent seems to be {intent}.", "metadata": {"title": "IntentBot"}}
|
| 59 |
+
]
|
| 60 |
+
|
| 61 |
+
# We yield them one by one to simulate typing
|
| 62 |
+
full_history = []
|
| 63 |
+
for resp in responses:
|
| 64 |
+
full_history.append(resp)
|
| 65 |
+
|
| 66 |
+
return f"**Group Chat Analysis:**\n\n**EmojiBot**: {emoji}\n\n**SentiBot**: I'm feeling a {sentiment} energy!\n\n**IntentBot**: Noted! Category: {intent}."
|
| 67 |
+
|
| 68 |
+
# 4. UI CUSTOMIZATION (Messenger Style)
|
| 69 |
+
# Dicebear Avatars
|
| 70 |
+
user_avatar = "https://api.dicebear.com/7.x/avataaars/svg?seed=User"
|
| 71 |
+
bot_avatar = "https://api.dicebear.com/7.x/bottts/svg?seed=ModelGroup"
|
| 72 |
+
|
| 73 |
+
demo = gr.ChatInterface(
|
| 74 |
+
fn=chat_response,
|
| 75 |
+
title="Model Group Chat",
|
| 76 |
+
description="Type a message to see how the models react and respond!",
|
| 77 |
+
examples=["I'm so happy today!", "This is very confusing", "I totally agree with you"],
|
| 78 |
+
theme="soft",
|
| 79 |
+
avatar_images=[user_avatar, bot_avatar]
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
if __name__ == "__main__":
|
| 83 |
+
demo.launch()
|