Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -14,16 +14,27 @@ from transformers import AutoModelForSequenceClassification, AutoTokenizer, Auto
|
|
14 |
|
15 |
warnings.filterwarnings('ignore', category=FutureWarning)
|
16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
# Download necessary NLTK data
|
18 |
nltk.download('vader_lexicon', quiet=True)
|
19 |
|
20 |
# ---------------------------
|
21 |
# Backend Support for GGUF Models
|
22 |
# ---------------------------
|
23 |
-
# Attempt to import a llama_cpp binding if available.
|
24 |
-
# Otherwise, fallback to the Hugging Face transformers interface.
|
25 |
try:
|
26 |
-
#
|
27 |
from llama_cpp import Llama
|
28 |
BACKEND = "llama_cpp"
|
29 |
except ImportError:
|
@@ -34,14 +45,14 @@ except ImportError:
|
|
34 |
# ---------------------------
|
35 |
class EmotionalAnalyzer:
|
36 |
def __init__(self):
|
37 |
-
# Load a pre-trained emotion classifier
|
38 |
self.emotion_model = AutoModelForSequenceClassification.from_pretrained(
|
39 |
"bhadresh-savani/distilbert-base-uncased-emotion"
|
40 |
)
|
41 |
self.emotion_tokenizer = AutoTokenizer.from_pretrained(
|
42 |
"bhadresh-savani/distilbert-base-uncased-emotion"
|
43 |
)
|
44 |
-
# Define labels
|
45 |
self.emotion_labels = ["sadness", "joy", "love", "anger", "fear", "surprise"]
|
46 |
self.sia = SentimentIntensityAnalyzer()
|
47 |
|
@@ -57,7 +68,6 @@ class EmotionalAnalyzer:
|
|
57 |
return self.sia.polarity_scores(text)
|
58 |
|
59 |
def detailed_emotional_analysis(self, text):
|
60 |
-
"""Combine VADER and TextBlob analysis for richer emotional insight."""
|
61 |
vader_scores = self.sentiment_analysis(text)
|
62 |
blob = TextBlob(text)
|
63 |
textblob_analysis = {
|
@@ -74,7 +84,6 @@ class EmotionalAnalyzer:
|
|
74 |
}
|
75 |
|
76 |
def visualize_emotions(self, emotions_dict):
|
77 |
-
"""Plot a bar chart of the current emotional state."""
|
78 |
emotions_df = pd.DataFrame(list(emotions_dict.items()), columns=['Emotion', 'Percentage'])
|
79 |
plt.figure(figsize=(8, 4))
|
80 |
sns.barplot(x='Emotion', y='Percentage', data=emotions_df)
|
@@ -92,18 +101,19 @@ class LLMResponder:
|
|
92 |
def __init__(self, model_name="SicariusSicariiStuff/Impish_LLAMA_3B_GGUF"):
|
93 |
self.model_name = model_name
|
94 |
if BACKEND == "llama_cpp":
|
95 |
-
# Initialize using llama_cpp backend
|
96 |
-
|
|
|
97 |
self.backend = "llama_cpp"
|
98 |
else:
|
99 |
-
# Load
|
100 |
self.llm_tokenizer = AutoTokenizer.from_pretrained(model_name)
|
101 |
self.llm_model = AutoModelForCausalLM.from_pretrained(model_name)
|
102 |
self.backend = "transformers"
|
103 |
|
104 |
def generate_response(self, prompt):
|
105 |
if self.backend == "llama_cpp":
|
106 |
-
# Use llama_cpp inference (
|
107 |
result = self.llm(prompt=prompt, max_tokens=256, temperature=0.95, top_p=0.95)
|
108 |
response = result.get("response", "")
|
109 |
else:
|
@@ -124,14 +134,12 @@ class LLMResponder:
|
|
124 |
# Main Interactive Interface Function
|
125 |
# ---------------------------
|
126 |
def interactive_interface(input_text):
|
127 |
-
# Initialize modules
|
128 |
emotion_analyzer = EmotionalAnalyzer()
|
129 |
llm_responder = LLMResponder()
|
130 |
|
131 |
-
# Perform detailed emotional analysis
|
132 |
emotional_data = emotion_analyzer.detailed_emotional_analysis(input_text)
|
133 |
-
#
|
134 |
-
# In a real-world scenario, this could be updated based on conversation history.
|
135 |
current_emotions = {
|
136 |
'joy': random.randint(10, 30),
|
137 |
'sadness': random.randint(5, 20),
|
@@ -142,15 +150,15 @@ def interactive_interface(input_text):
|
|
142 |
}
|
143 |
emotion_image = emotion_analyzer.visualize_emotions(current_emotions)
|
144 |
|
145 |
-
# Create a prompt
|
146 |
-
prompt = (
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
|
|
151 |
llm_response = llm_responder.generate_response(prompt)
|
152 |
|
153 |
-
# Organize the result into a dictionary
|
154 |
result = {
|
155 |
'detailed_emotional_analysis': emotional_data,
|
156 |
'llm_response': llm_response,
|
@@ -182,4 +190,3 @@ iface = gr.Interface(
|
|
182 |
|
183 |
if __name__ == "__main__":
|
184 |
iface.launch(share=True)
|
185 |
-
|
|
|
14 |
|
15 |
warnings.filterwarnings('ignore', category=FutureWarning)
|
16 |
|
17 |
+
# --- Monkey Patch for Gradio Client JSON Schema Bug ---
|
18 |
+
import gradio_client.utils as client_utils
|
19 |
+
|
20 |
+
original_get_type = client_utils.get_type
|
21 |
+
|
22 |
+
def patched_get_type(schema):
|
23 |
+
if not isinstance(schema, dict):
|
24 |
+
return type(schema).__name__
|
25 |
+
return original_get_type(schema)
|
26 |
+
|
27 |
+
client_utils.get_type = patched_get_type
|
28 |
+
# --- End of Monkey Patch ---
|
29 |
+
|
30 |
# Download necessary NLTK data
|
31 |
nltk.download('vader_lexicon', quiet=True)
|
32 |
|
33 |
# ---------------------------
|
34 |
# Backend Support for GGUF Models
|
35 |
# ---------------------------
|
|
|
|
|
36 |
try:
|
37 |
+
# Attempt to import a hypothetical llama_cpp binding for GGUF models.
|
38 |
from llama_cpp import Llama
|
39 |
BACKEND = "llama_cpp"
|
40 |
except ImportError:
|
|
|
45 |
# ---------------------------
|
46 |
class EmotionalAnalyzer:
|
47 |
def __init__(self):
|
48 |
+
# Load a pre-trained emotion classifier and tokenizer
|
49 |
self.emotion_model = AutoModelForSequenceClassification.from_pretrained(
|
50 |
"bhadresh-savani/distilbert-base-uncased-emotion"
|
51 |
)
|
52 |
self.emotion_tokenizer = AutoTokenizer.from_pretrained(
|
53 |
"bhadresh-savani/distilbert-base-uncased-emotion"
|
54 |
)
|
55 |
+
# Define the emotion labels as per the model card
|
56 |
self.emotion_labels = ["sadness", "joy", "love", "anger", "fear", "surprise"]
|
57 |
self.sia = SentimentIntensityAnalyzer()
|
58 |
|
|
|
68 |
return self.sia.polarity_scores(text)
|
69 |
|
70 |
def detailed_emotional_analysis(self, text):
|
|
|
71 |
vader_scores = self.sentiment_analysis(text)
|
72 |
blob = TextBlob(text)
|
73 |
textblob_analysis = {
|
|
|
84 |
}
|
85 |
|
86 |
def visualize_emotions(self, emotions_dict):
|
|
|
87 |
emotions_df = pd.DataFrame(list(emotions_dict.items()), columns=['Emotion', 'Percentage'])
|
88 |
plt.figure(figsize=(8, 4))
|
89 |
sns.barplot(x='Emotion', y='Percentage', data=emotions_df)
|
|
|
101 |
def __init__(self, model_name="SicariusSicariiStuff/Impish_LLAMA_3B_GGUF"):
|
102 |
self.model_name = model_name
|
103 |
if BACKEND == "llama_cpp":
|
104 |
+
# Initialize using the llama_cpp backend.
|
105 |
+
# Replace "path/to/your/gguf/file.gguf" with the actual path to your GGUF file.
|
106 |
+
self.llm = Llama(model_path="path/to/your/gguf/file.gguf", n_ctx=1024)
|
107 |
self.backend = "llama_cpp"
|
108 |
else:
|
109 |
+
# Load model via Hugging Face transformers.
|
110 |
self.llm_tokenizer = AutoTokenizer.from_pretrained(model_name)
|
111 |
self.llm_model = AutoModelForCausalLM.from_pretrained(model_name)
|
112 |
self.backend = "transformers"
|
113 |
|
114 |
def generate_response(self, prompt):
|
115 |
if self.backend == "llama_cpp":
|
116 |
+
# Use llama_cpp inference (adjust parameters as needed)
|
117 |
result = self.llm(prompt=prompt, max_tokens=256, temperature=0.95, top_p=0.95)
|
118 |
response = result.get("response", "")
|
119 |
else:
|
|
|
134 |
# Main Interactive Interface Function
|
135 |
# ---------------------------
|
136 |
def interactive_interface(input_text):
|
|
|
137 |
emotion_analyzer = EmotionalAnalyzer()
|
138 |
llm_responder = LLMResponder()
|
139 |
|
140 |
+
# Perform detailed emotional analysis on the input
|
141 |
emotional_data = emotion_analyzer.detailed_emotional_analysis(input_text)
|
142 |
+
# Simulate dynamic emotional state (could be updated based on conversation history)
|
|
|
143 |
current_emotions = {
|
144 |
'joy': random.randint(10, 30),
|
145 |
'sadness': random.randint(5, 20),
|
|
|
150 |
}
|
151 |
emotion_image = emotion_analyzer.visualize_emotions(current_emotions)
|
152 |
|
153 |
+
# Create a prompt combining the input and detected emotion data
|
154 |
+
prompt = (
|
155 |
+
f"Input: {input_text}\n"
|
156 |
+
f"Detected Emotion: {emotional_data['predicted_emotion']}\n"
|
157 |
+
f"VADER Scores: {emotional_data['vader']}\n"
|
158 |
+
"Provide a thoughtful, emotionally aware response that reflects the above data:"
|
159 |
+
)
|
160 |
llm_response = llm_responder.generate_response(prompt)
|
161 |
|
|
|
162 |
result = {
|
163 |
'detailed_emotional_analysis': emotional_data,
|
164 |
'llm_response': llm_response,
|
|
|
190 |
|
191 |
if __name__ == "__main__":
|
192 |
iface.launch(share=True)
|
|