Spaces:
Running
Running
Update pages/Comparision.py
Browse files- pages/Comparision.py +92 -49
pages/Comparision.py
CHANGED
@@ -5,62 +5,77 @@ from transformers import pipeline
|
|
5 |
from rake_nltk import Rake
|
6 |
from nltk.corpus import stopwords
|
7 |
from fuzzywuzzy import fuzz
|
|
|
|
|
|
|
8 |
|
9 |
-
#
|
10 |
-
|
11 |
-
nltk.download('stopwords', quiet=True)
|
12 |
|
13 |
-
|
|
|
14 |
|
15 |
-
# Define the options for the dropdown menu,
|
16 |
-
options = ['None','
|
17 |
|
18 |
# Create a dropdown menu to select options
|
19 |
selected_option = st.selectbox("Select a preset option", options)
|
20 |
|
21 |
# Define URLs for different options
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
28 |
|
29 |
# Function to fetch text content based on selected option
|
30 |
def fetch_text_content(selected_option):
|
31 |
-
if selected_option
|
32 |
-
return requests.get(url_option1).text
|
33 |
-
elif selected_option == 'Regret Letter':
|
34 |
-
return requests.get(url_option2).text
|
35 |
-
elif selected_option == 'Kindness Tale':
|
36 |
-
return requests.get(url_option3).text
|
37 |
-
elif selected_option == 'Lost Melody Tale':
|
38 |
-
return requests.get(url_option4).text
|
39 |
-
elif selected_option == 'Twitter Example 1':
|
40 |
-
return requests.get(url_option5).text
|
41 |
-
elif selected_option == 'Twitter Example 2':
|
42 |
-
return requests.get(url_option6).text
|
43 |
-
else:
|
44 |
-
return ""
|
45 |
|
46 |
# Fetch text content based on selected option
|
47 |
-
|
48 |
|
49 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
pipe_sent = pipeline('sentiment-analysis')
|
51 |
-
# Initialize pipeline for summarization
|
52 |
pipe_summ = pipeline("summarization", model="facebook/bart-large-cnn")
|
53 |
|
54 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
def extract_keywords(text):
|
56 |
r = Rake()
|
57 |
r.extract_keywords_from_text(text)
|
58 |
phrases_with_scores = r.get_ranked_phrases_with_scores()
|
59 |
stop_words = set(stopwords.words('english'))
|
60 |
-
keywords = []
|
61 |
-
for score, phrase in phrases_with_scores:
|
62 |
-
if phrase.lower() not in stop_words:
|
63 |
-
keywords.append((score, phrase))
|
64 |
keywords.sort(key=lambda x: x[0], reverse=True)
|
65 |
unique_keywords = []
|
66 |
seen_phrases = set()
|
@@ -75,28 +90,56 @@ def extract_keywords(text):
|
|
75 |
seen_phrases.add(phrase)
|
76 |
return unique_keywords[:10]
|
77 |
|
78 |
-
#
|
79 |
-
|
80 |
|
81 |
-
#
|
82 |
-
|
83 |
-
|
84 |
-
|
|
|
|
|
85 |
out_sentiment = pipe_sent(text)
|
86 |
sentiment_score = out_sentiment[0]['score']
|
87 |
sentiment_label = out_sentiment[0]['label']
|
88 |
sentiment_emoji = '😊' if sentiment_label == 'POSITIVE' else '😞'
|
89 |
sentiment_text = f"Sentiment Score: {sentiment_score}, Sentiment Label: {sentiment_label.capitalize()} {sentiment_emoji}"
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
|
|
94 |
out_summ = pipe_summ(text)
|
95 |
summarized_text = out_summ[0]['summary_text']
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
|
|
100 |
keywords = extract_keywords(text)
|
101 |
keyword_list = [keyword[1] for keyword in keywords]
|
102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
from rake_nltk import Rake
|
6 |
from nltk.corpus import stopwords
|
7 |
from fuzzywuzzy import fuzz
|
8 |
+
import openai
|
9 |
+
import os
|
10 |
+
from dotenv import load_dotenv
|
11 |
|
12 |
+
# Load environment variables for Llama 3
|
13 |
+
load_dotenv()
|
|
|
14 |
|
15 |
+
# Title of the app
|
16 |
+
st.title("Sentiment Analysis Comparison: Transformers vs Llama 3")
|
17 |
|
18 |
+
# Define the options for the dropdown menu, selecting a remote txt file already created to analyze the text
|
19 |
+
options = ['None', 'Appreciation Letter', 'Regret Letter', 'Kindness Tale', 'Lost Melody Tale', 'Twitter Example 1', 'Twitter Example 2']
|
20 |
|
21 |
# Create a dropdown menu to select options
|
22 |
selected_option = st.selectbox("Select a preset option", options)
|
23 |
|
24 |
# Define URLs for different options
|
25 |
+
urls = {
|
26 |
+
'Appreciation Letter': "https://raw.githubusercontent.com/peteciank/public_files/main/Transformers/Appreciation_Letter.txt",
|
27 |
+
'Regret Letter': "https://raw.githubusercontent.com/peteciank/public_files/main/Transformers/Regret_Letter.txt",
|
28 |
+
'Kindness Tale': "https://raw.githubusercontent.com/peteciank/public_files/main/Transformers/Kindness_Tale.txt",
|
29 |
+
'Lost Melody Tale': "https://raw.githubusercontent.com/peteciank/public_files/main/Transformers/Lost_Melody_Tale.txt",
|
30 |
+
'Twitter Example 1': "https://raw.githubusercontent.com/peteciank/public_files/main/Transformers/Twitter_Example_1.txt",
|
31 |
+
'Twitter Example 2': "https://raw.githubusercontent.com/peteciank/public_files/main/Transformers/Twitter_Example_2.txt"
|
32 |
+
}
|
33 |
|
34 |
# Function to fetch text content based on selected option
|
35 |
def fetch_text_content(selected_option):
|
36 |
+
return requests.get(urls[selected_option]).text if selected_option in urls else ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
# Fetch text content based on selected option
|
39 |
+
text = fetch_text_content(selected_option)
|
40 |
|
41 |
+
# Display text content in a text area
|
42 |
+
text = st.text_area('Enter the text to analyze', text)
|
43 |
+
|
44 |
+
# Download NLTK resources
|
45 |
+
nltk.download('punkt')
|
46 |
+
nltk.download('stopwords')
|
47 |
+
|
48 |
+
# Initialize sentiment, summarization, and keyword extraction pipelines for Transformers
|
49 |
pipe_sent = pipeline('sentiment-analysis')
|
|
|
50 |
pipe_summ = pipeline("summarization", model="facebook/bart-large-cnn")
|
51 |
|
52 |
+
# Llama 3 initialization
|
53 |
+
llama_api_key = os.getenv('HFSecret')
|
54 |
+
llama_base_url = "https://api-inference.huggingface.co/v1"
|
55 |
+
llama_repo_id = "meta-llama/Meta-Llama-3-8B-Instruct"
|
56 |
+
|
57 |
+
# Function to use Llama 3 for sentiment analysis, summarization, and keyword extraction
|
58 |
+
def analyze_with_llama(text):
|
59 |
+
headers = {
|
60 |
+
"Authorization": f"Bearer {llama_api_key}"
|
61 |
+
}
|
62 |
+
data = {
|
63 |
+
"inputs": text,
|
64 |
+
"parameters": {
|
65 |
+
"max_new_tokens": 200
|
66 |
+
}
|
67 |
+
}
|
68 |
+
# Perform the request
|
69 |
+
response = requests.post(f"{llama_base_url}/models/{llama_repo_id}", headers=headers, json=data)
|
70 |
+
return response.json()
|
71 |
+
|
72 |
+
# Function to extract keywords using RAKE and remove duplicates
|
73 |
def extract_keywords(text):
|
74 |
r = Rake()
|
75 |
r.extract_keywords_from_text(text)
|
76 |
phrases_with_scores = r.get_ranked_phrases_with_scores()
|
77 |
stop_words = set(stopwords.words('english'))
|
78 |
+
keywords = [(score, phrase) for score, phrase in phrases_with_scores if phrase.lower() not in stop_words]
|
|
|
|
|
|
|
79 |
keywords.sort(key=lambda x: x[0], reverse=True)
|
80 |
unique_keywords = []
|
81 |
seen_phrases = set()
|
|
|
90 |
seen_phrases.add(phrase)
|
91 |
return unique_keywords[:10]
|
92 |
|
93 |
+
# Create two columns
|
94 |
+
col1, col2 = st.columns(2)
|
95 |
|
96 |
+
# Transformer-based analysis in the first column
|
97 |
+
with col1:
|
98 |
+
st.header("Transformer-based Analysis")
|
99 |
+
if st.button("Analyze with Transformers"):
|
100 |
+
with st.spinner("Analyzing with Transformers..."):
|
101 |
+
# Sentiment analysis
|
102 |
out_sentiment = pipe_sent(text)
|
103 |
sentiment_score = out_sentiment[0]['score']
|
104 |
sentiment_label = out_sentiment[0]['label']
|
105 |
sentiment_emoji = '😊' if sentiment_label == 'POSITIVE' else '😞'
|
106 |
sentiment_text = f"Sentiment Score: {sentiment_score}, Sentiment Label: {sentiment_label.capitalize()} {sentiment_emoji}"
|
107 |
+
|
108 |
+
with st.expander("Sentiment Analysis (Transformers)"):
|
109 |
+
st.write(sentiment_text)
|
110 |
+
|
111 |
+
# Summarization
|
112 |
out_summ = pipe_summ(text)
|
113 |
summarized_text = out_summ[0]['summary_text']
|
114 |
+
|
115 |
+
with st.expander("Summarization (Transformers)"):
|
116 |
+
st.write(summarized_text)
|
117 |
+
|
118 |
+
# Keyword extraction
|
119 |
keywords = extract_keywords(text)
|
120 |
keyword_list = [keyword[1] for keyword in keywords]
|
121 |
+
|
122 |
+
with st.expander("Keywords (Transformers)"):
|
123 |
+
st.write(keyword_list)
|
124 |
+
|
125 |
+
# Llama 3-based analysis in the second column
|
126 |
+
with col2:
|
127 |
+
st.header("Llama 3-based Analysis")
|
128 |
+
if st.button("Analyze with Llama 3"):
|
129 |
+
with st.spinner("Analyzing with Llama 3..."):
|
130 |
+
llama_response = analyze_with_llama(text)
|
131 |
+
|
132 |
+
if llama_response:
|
133 |
+
# Assuming the response returns in the same format, adjust if needed
|
134 |
+
sentiment_text = llama_response.get('sentiment_analysis', 'No sentiment detected')
|
135 |
+
summarized_text = llama_response.get('summarization', 'No summary available')
|
136 |
+
keywords = llama_response.get('keywords', 'No keywords available')
|
137 |
+
|
138 |
+
with st.expander("Sentiment Analysis (Llama 3)"):
|
139 |
+
st.write(sentiment_text)
|
140 |
+
|
141 |
+
with st.expander("Summarization (Llama 3)"):
|
142 |
+
st.write(summarized_text)
|
143 |
+
|
144 |
+
with st.expander("Keywords (Llama 3)"):
|
145 |
+
st.write(keywords)
|