peterciank commited on
Commit
b64289a
·
verified ·
1 Parent(s): b876064

Update pages/Comparision.py

Browse files
Files changed (1) hide show
  1. pages/Comparision.py +108 -127
pages/Comparision.py CHANGED
@@ -1,145 +1,126 @@
1
  import streamlit as st
2
  import requests
3
- import nltk
4
  from transformers import pipeline
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()
82
- for score, phrase in keywords:
83
- if phrase not in seen_phrases:
84
- similar_phrases = [seen_phrase for seen_phrase in seen_phrases if fuzz.ratio(phrase, seen_phrase) > 70]
85
- if similar_phrases:
86
- merged_phrase = max([phrase] + similar_phrases, key=len)
87
- unique_keywords.append((score, merged_phrase))
88
- else:
89
- unique_keywords.append((score, phrase))
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)
 
 
 
 
1
  import streamlit as st
2
  import requests
 
3
  from transformers import pipeline
4
+ import concurrent.futures
 
 
 
5
  import os
6
+ import json
7
  from dotenv import load_dotenv
8
+ from requests.exceptions import JSONDecodeError
9
 
10
+ # Load environment variables
11
  load_dotenv()
12
 
13
+ # Initialize Hugging Face API for Llama 3
14
+ HF_API_URL = "https://api-inference.huggingface.co/v1"
15
+ HF_API_KEY = os.getenv('HFSecret')
16
 
17
+ # Initialize pipelines for Transformers
18
+ pipe_sent_transformers = pipeline('sentiment-analysis')
19
+ pipe_summ_transformers = pipeline("summarization", model="facebook/bart-large-cnn")
20
 
21
+ # Define the Llama 3 model ID
22
+ LLAMA_MODEL_ID = "meta-llama/Meta-Llama-3-8B-Instruct"
23
 
24
+ # Function to fetch text content from Transformers app
 
 
 
 
 
 
 
 
 
 
25
  def fetch_text_content(selected_option):
26
+ options_urls = {
27
+ 'Appreciation Letter': "https://raw.githubusercontent.com/peteciank/public_files/main/Transformers/Appreciation_Letter.txt",
28
+ 'Regret Letter': "https://raw.githubusercontent.com/peteciank/public_files/main/Transformers/Regret_Letter.txt",
29
+ 'Kindness Tale': "https://raw.githubusercontent.com/peteciank/public_files/main/Transformers/Kindness_Tale.txt",
30
+ 'Lost Melody Tale': "https://raw.githubusercontent.com/peteciank/public_files/main/Transformers/Lost_Melody_Tale.txt",
31
+ 'Twitter Example 1': "https://raw.githubusercontent.com/peteciank/public_files/main/Transformers/Twitter_Example_1.txt",
32
+ 'Twitter Example 2': "https://raw.githubusercontent.com/peteciank/public_files/main/Transformers/Twitter_Example_2.txt"
33
+ }
34
+ return requests.get(options_urls[selected_option]).text if selected_option in options_urls else ""
 
 
 
 
 
 
 
 
 
 
 
35
 
36
+ # Function to analyze sentiment using Llama
37
  def analyze_with_llama(text):
38
+ headers = {"Authorization": f"Bearer {HF_API_KEY}"}
 
 
39
  data = {
40
  "inputs": text,
41
+ "options": {
42
+ "use_cache": False,
43
+ "wait_for_model": True
44
  }
45
  }
46
+
47
+ try:
48
+ response = requests.post(f"{HF_API_URL}/models/{LLAMA_MODEL_ID}", headers=headers, json=data)
49
+ response.raise_for_status()
50
+ return response.json() # Ensure valid JSON
51
+ except (requests.RequestException, json.JSONDecodeError):
52
+ return {"error": "Error occurred while processing Llama model response."}
53
+
54
+ # Function to run Transformer-based analysis
55
+ def transformer_analysis(text):
56
+ # Sentiment analysis
57
+ sentiment_result = pipe_sent_transformers(text)
58
+ sentiment_score = sentiment_result[0]['score']
59
+ sentiment_label = sentiment_result[0]['label']
60
+
61
+ # Summarization
62
+ summary_result = pipe_summ_transformers(text)
63
+ summary = summary_result[0]['summary_text']
64
+
65
+ return sentiment_score, sentiment_label, summary
66
+
67
+ # Function to run Llama-based analysis
68
+ def llama_analysis(text):
69
+ llama_response = analyze_with_llama(text)
70
+
71
+ if "error" in llama_response:
72
+ return "Error", "Error", "Error"
73
+
74
+ # Extract sentiment and summary if valid JSON
75
+ sentiment_label = llama_response.get('sentiment', 'UNKNOWN')
76
+ sentiment_score = llama_response.get('sentiment_score', 0.0)
77
+ summary = llama_response.get('summary', 'No summary available.')
78
+
79
+ return sentiment_score, sentiment_label, summary
80
+
81
+ # Streamlit app layout with two columns
82
+ st.title("Parallel Sentiment Analysis with Transformers and Llama")
83
+
84
+ # Select text to analyze from dropdown
85
+ options = ['None', 'Appreciation Letter', 'Regret Letter', 'Kindness Tale', 'Lost Melody Tale', 'Twitter Example 1', 'Twitter Example 2']
86
+ selected_option = st.selectbox("Select a preset option", options)
87
+
88
+ # Fetch text content for analysis
89
+ jd = fetch_text_content(selected_option)
90
+ text = st.text_area('Enter the text to analyze', jd)
91
+
92
+ if st.button("Start Analysis"):
93
+ # Set up the two columns for parallel analysis
94
+ col1, col2 = st.columns(2)
95
+
96
+ with st.spinner("Running sentiment analysis..."):
97
+ with concurrent.futures.ThreadPoolExecutor() as executor:
98
+ # Execute analyses in parallel
99
+ future_transformer = executor.submit(transformer_analysis, text)
100
+ future_llama = executor.submit(llama_analysis, text)
101
+
102
+ # Retrieve results from both transformers and Llama
103
+ sentiment_score_transformer, sentiment_label_transformer, summary_transformer = future_transformer.result()
104
+ sentiment_score_llama, sentiment_label_llama, summary_llama = future_llama.result()
105
+
106
+ # Display results for Transformers-based analysis in the first column
107
+ with col1:
108
+ st.subheader("Transformers Analysis")
109
+ with st.expander("Sentiment Analysis - Transformers"):
110
+ sentiment_emoji = '😊' if sentiment_label_transformer == 'POSITIVE' else '😞'
111
+ st.write(f"Sentiment: {sentiment_label_transformer} ({sentiment_emoji})")
112
+ st.write(f"Score: {sentiment_score_transformer:.2f}")
113
+
114
+ with st.expander("Summarization - Transformers"):
115
+ st.write(summary_transformer)
116
+
117
+ # Display results for Llama-based analysis in the second column
118
+ with col2:
119
+ st.subheader("Llama Analysis")
120
+ with st.expander("Sentiment Analysis - Llama"):
121
+ sentiment_emoji = '😊' if sentiment_label_llama == 'POSITIVE' else '😞'
122
+ st.write(f"Sentiment: {sentiment_label_llama} ({sentiment_emoji})")
123
+ st.write(f"Score: {sentiment_score_llama:.2f}")
124
+
125
+ with st.expander("Summarization - Llama"):
126
+ st.write(summary_llama)