Spaces:
Running
Running
Update pages/Comparision.py
Browse files- 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 |
-
|
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
|
13 |
load_dotenv()
|
14 |
|
15 |
-
#
|
16 |
-
|
|
|
17 |
|
18 |
-
#
|
19 |
-
|
|
|
20 |
|
21 |
-
#
|
22 |
-
|
23 |
|
24 |
-
#
|
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 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
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
|
58 |
def analyze_with_llama(text):
|
59 |
-
headers = {
|
60 |
-
"Authorization": f"Bearer {llama_api_key}"
|
61 |
-
}
|
62 |
data = {
|
63 |
"inputs": text,
|
64 |
-
"
|
65 |
-
"
|
|
|
66 |
}
|
67 |
}
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
#
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
#
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
if
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
|
|
|
|
|
|
|
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)
|