Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,34 +1,78 @@
|
|
1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import pandas as pd
|
3 |
import numpy as np
|
4 |
import matplotlib.pyplot as plt
|
5 |
import plotly.express as px
|
6 |
import plotly.graph_objects as go
|
7 |
from plotly.subplots import make_subplots
|
8 |
-
import io
|
9 |
-
import base64
|
10 |
-
import random
|
11 |
-
from datetime import datetime, timedelta
|
12 |
-
from collections import Counter
|
13 |
import gradio as gr
|
14 |
-
from
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
-
from transformers import pipeline
|
21 |
-
from keybert import KeyBERT
|
22 |
-
from youtube_comment_downloader import YoutubeCommentDownloader
|
23 |
-
from datetime import datetime, timedelta
|
24 |
-
import re
|
25 |
-
import pandas as pd
|
26 |
|
27 |
|
28 |
|
29 |
-
# Initialize models globally
|
30 |
-
classifier = pipeline("sentiment-analysis", model="cardiffnlp/twitter-roberta-base-sentiment-latest")
|
31 |
-
kw_model = KeyBERT()
|
32 |
|
33 |
# Label mapping - handling different model outputs
|
34 |
sentiment_map = {
|
@@ -38,8 +82,6 @@ sentiment_map = {
|
|
38 |
}
|
39 |
color_map = {"Positive": "#2E8B57", "Neutral": "#4682B4", "Negative": "#CD5C5C"}
|
40 |
|
41 |
-
|
42 |
-
|
43 |
# Default comments for when no file is uploaded
|
44 |
comments = [
|
45 |
"This new distance fare is really fair. I pay less for short trips!",
|
@@ -726,8 +768,6 @@ def export_data_to_csv(df_component):
|
|
726 |
|
727 |
|
728 |
def analyze_youtube_comments(video_url):
|
729 |
-
from youtube_comment_downloader import YoutubeCommentDownloader
|
730 |
-
import re
|
731 |
|
732 |
# Simple YouTube video URL validation
|
733 |
youtube_pattern = r"(https?://)?(www\.)?(youtube\.com/watch\?v=|youtu\.be/)[\w-]{11}"
|
@@ -999,7 +1039,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
999 |
|
1000 |
# Launch the app
|
1001 |
if __name__ == "__main__":
|
1002 |
-
demo.launch()
|
1003 |
|
1004 |
|
1005 |
|
|
|
1 |
+
|
2 |
+
|
3 |
+
# Standard Library Imports
|
4 |
+
import os
|
5 |
+
import io
|
6 |
+
import re
|
7 |
+
import base64
|
8 |
+
import random
|
9 |
+
from datetime import datetime, timedelta
|
10 |
+
from collections import Counter
|
11 |
+
from pathlib import Path # Needed for cache_dir
|
12 |
+
|
13 |
+
# Third-party Libraries
|
14 |
import pandas as pd
|
15 |
import numpy as np
|
16 |
import matplotlib.pyplot as plt
|
17 |
import plotly.express as px
|
18 |
import plotly.graph_objects as go
|
19 |
from plotly.subplots import make_subplots
|
|
|
|
|
|
|
|
|
|
|
20 |
import gradio as gr
|
21 |
+
from youtube_comment_downloader import YoutubeCommentDownloader
|
22 |
+
|
23 |
+
# Lazy import helper functions
|
24 |
+
def lazy_import_transformers():
|
25 |
+
try:
|
26 |
+
from transformers import pipeline
|
27 |
+
return pipeline
|
28 |
+
except ImportError:
|
29 |
+
print("transformers library not found.")
|
30 |
+
return None
|
31 |
|
32 |
+
def lazy_import_keybert():
|
33 |
+
try:
|
34 |
+
from keybert import KeyBERT
|
35 |
+
return KeyBERT
|
36 |
+
except ImportError:
|
37 |
+
print("keybert library not found.")
|
38 |
+
return None
|
39 |
+
|
40 |
+
# Initialize models with cache_dir for faster loading
|
41 |
+
def initialize_models():
|
42 |
+
classifier = None
|
43 |
+
kw_model = None
|
44 |
+
|
45 |
+
try:
|
46 |
+
pipeline = lazy_import_transformers()
|
47 |
+
if pipeline:
|
48 |
+
cache_dir = Path.home() / ".cache/huggingface"
|
49 |
+
cache_dir.mkdir(parents=True, exist_ok=True)
|
50 |
+
|
51 |
+
classifier = pipeline(
|
52 |
+
"sentiment-analysis",
|
53 |
+
model="cardiffnlp/twitter-roberta-base-sentiment-latest",
|
54 |
+
cache_dir=str(cache_dir)
|
55 |
+
)
|
56 |
+
|
57 |
+
KeyBERT = lazy_import_keybert()
|
58 |
+
if KeyBERT:
|
59 |
+
kw_model = KeyBERT()
|
60 |
+
|
61 |
+
except Exception as e:
|
62 |
+
print(f"Error initializing models: {e}")
|
63 |
+
|
64 |
+
return classifier, kw_model
|
65 |
+
|
66 |
+
# Set transformers logging to error only
|
67 |
+
import transformers
|
68 |
+
transformers.logging.set_verbosity_error()
|
69 |
+
|
70 |
+
# Load models
|
71 |
+
classifier, kw_model = initialize_models()
|
72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
|
74 |
|
75 |
|
|
|
|
|
|
|
76 |
|
77 |
# Label mapping - handling different model outputs
|
78 |
sentiment_map = {
|
|
|
82 |
}
|
83 |
color_map = {"Positive": "#2E8B57", "Neutral": "#4682B4", "Negative": "#CD5C5C"}
|
84 |
|
|
|
|
|
85 |
# Default comments for when no file is uploaded
|
86 |
comments = [
|
87 |
"This new distance fare is really fair. I pay less for short trips!",
|
|
|
768 |
|
769 |
|
770 |
def analyze_youtube_comments(video_url):
|
|
|
|
|
771 |
|
772 |
# Simple YouTube video URL validation
|
773 |
youtube_pattern = r"(https?://)?(www\.)?(youtube\.com/watch\?v=|youtu\.be/)[\w-]{11}"
|
|
|
1039 |
|
1040 |
# Launch the app
|
1041 |
if __name__ == "__main__":
|
1042 |
+
demo.launch(share=False)
|
1043 |
|
1044 |
|
1045 |
|