Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -12,8 +12,10 @@ from transformers import AutoModelForSequenceClassification, AutoTokenizer, Auto
|
|
12 |
from deap import base, creator, tools, algorithms
|
13 |
import nltk
|
14 |
from nltk.sentiment import SentimentIntensityAnalyzer
|
|
|
|
|
|
|
15 |
from textblob import TextBlob
|
16 |
-
import spacy
|
17 |
import matplotlib.pyplot as plt
|
18 |
import seaborn as sns
|
19 |
|
@@ -22,28 +24,9 @@ warnings.filterwarnings('ignore', category=FutureWarning, module='huggingface_hu
|
|
22 |
# Download necessary NLTK data
|
23 |
nltk.download('vader_lexicon', quiet=True)
|
24 |
nltk.download('punkt', quiet=True)
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
try:
|
30 |
-
import spacy
|
31 |
-
try:
|
32 |
-
nlp = spacy.load("en_core_web_sm")
|
33 |
-
except OSError:
|
34 |
-
print("Downloading spaCy model...")
|
35 |
-
download_spacy_model()
|
36 |
-
nlp = spacy.load("en_core_web_sm")
|
37 |
-
except ImportError:
|
38 |
-
print("Error: Unable to import spaCy. NLP features will be disabled.")
|
39 |
-
nlp = None
|
40 |
-
|
41 |
-
def extract_entities(text):
|
42 |
-
if nlp is None:
|
43 |
-
return []
|
44 |
-
doc = nlp(text)
|
45 |
-
entities = [(ent.text, ent.label_) for ent in doc.ents]
|
46 |
-
return entities
|
47 |
|
48 |
# Initialize Example Dataset (For Emotion Prediction)
|
49 |
data = {
|
@@ -239,8 +222,11 @@ def sentiment_analysis(text):
|
|
239 |
return sentiment_scores
|
240 |
|
241 |
def extract_entities(text):
|
242 |
-
|
243 |
-
entities = [
|
|
|
|
|
|
|
244 |
return entities
|
245 |
|
246 |
def analyze_text_complexity(text):
|
@@ -301,15 +287,15 @@ def interactive_interface(input_text):
|
|
301 |
def gradio_interface(input_text):
|
302 |
response = interactive_interface(input_text)
|
303 |
if isinstance(response, str):
|
304 |
-
return response
|
305 |
else:
|
306 |
return (
|
307 |
f"Predicted Emotion: {response['predicted_emotion']}\n"
|
308 |
f"Sentiment: {response['sentiment_scores']}\n"
|
309 |
f"Entities: {response['entities']}\n"
|
310 |
f"Text Complexity: {response['text_complexity']}\n"
|
311 |
-
f"Response: {response['response']}\n"
|
312 |
-
|
313 |
)
|
314 |
|
315 |
# Create Gradio interface
|
@@ -323,3 +309,4 @@ iface = gr.Interface(
|
|
323 |
|
324 |
if __name__ == "__main__":
|
325 |
iface.launch()
|
|
|
|
12 |
from deap import base, creator, tools, algorithms
|
13 |
import nltk
|
14 |
from nltk.sentiment import SentimentIntensityAnalyzer
|
15 |
+
from nltk.tokenize import word_tokenize
|
16 |
+
from nltk.tag import pos_tag
|
17 |
+
from nltk.chunk import ne_chunk
|
18 |
from textblob import TextBlob
|
|
|
19 |
import matplotlib.pyplot as plt
|
20 |
import seaborn as sns
|
21 |
|
|
|
24 |
# Download necessary NLTK data
|
25 |
nltk.download('vader_lexicon', quiet=True)
|
26 |
nltk.download('punkt', quiet=True)
|
27 |
+
nltk.download('averaged_perceptron_tagger', quiet=True)
|
28 |
+
nltk.download('maxent_ne_chunker', quiet=True)
|
29 |
+
nltk.download('words', quiet=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
# Initialize Example Dataset (For Emotion Prediction)
|
32 |
data = {
|
|
|
222 |
return sentiment_scores
|
223 |
|
224 |
def extract_entities(text):
|
225 |
+
chunked = ne_chunk(pos_tag(word_tokenize(text)))
|
226 |
+
entities = []
|
227 |
+
for chunk in chunked:
|
228 |
+
if hasattr(chunk, 'label'):
|
229 |
+
entities.append(((' '.join(c[0] for c in chunk)), chunk.label()))
|
230 |
return entities
|
231 |
|
232 |
def analyze_text_complexity(text):
|
|
|
287 |
def gradio_interface(input_text):
|
288 |
response = interactive_interface(input_text)
|
289 |
if isinstance(response, str):
|
290 |
+
return response, None
|
291 |
else:
|
292 |
return (
|
293 |
f"Predicted Emotion: {response['predicted_emotion']}\n"
|
294 |
f"Sentiment: {response['sentiment_scores']}\n"
|
295 |
f"Entities: {response['entities']}\n"
|
296 |
f"Text Complexity: {response['text_complexity']}\n"
|
297 |
+
f"Response: {response['response']}\n",
|
298 |
+
response['emotion_visualization']
|
299 |
)
|
300 |
|
301 |
# Create Gradio interface
|
|
|
309 |
|
310 |
if __name__ == "__main__":
|
311 |
iface.launch()
|
312 |
+
|