Spaces:
Sleeping
Sleeping
Create pages/Comparision.py
Browse files- pages/Comparision.py +114 -0
pages/Comparision.py
ADDED
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
from openai import OpenAI
|
9 |
+
import os
|
10 |
+
from dotenv import load_dotenv
|
11 |
+
|
12 |
+
# Load environment variables for OpenAI
|
13 |
+
load_dotenv()
|
14 |
+
|
15 |
+
# Initialize OpenAI client for Llama 3 model
|
16 |
+
client = OpenAI(
|
17 |
+
base_url="https://api-inference.huggingface.co/v1",
|
18 |
+
api_key=os.environ.get('HFSecret') # Replace with your token
|
19 |
+
)
|
20 |
+
repo_id = "meta-llama/Meta-Llama-3-8B-Instruct"
|
21 |
+
|
22 |
+
st.title("Parallel Sentiment Analysis: Transformers vs. Llama 3")
|
23 |
+
|
24 |
+
# Define the options for the dropdown menu, selecting a remote txt file already created to analyze the text
|
25 |
+
options = ['None', 'Apprecitation Letter', 'Regret Letter', 'Kindness Tale', 'Lost Melody Tale', 'Twitter Example 1', 'Twitter Example 2']
|
26 |
+
|
27 |
+
# Create a dropdown menu to select options
|
28 |
+
selected_option = st.selectbox("Select a preset option", options)
|
29 |
+
|
30 |
+
# Define URLs for different options
|
31 |
+
urls = {
|
32 |
+
"Apprecitation Letter": "https://raw.githubusercontent.com/peteciank/public_files/main/Transformers/Appreciation_Letter.txt",
|
33 |
+
"Regret Letter": "https://raw.githubusercontent.com/peteciank/public_files/main/Transformers/Regret_Letter.txt",
|
34 |
+
"Kindness Tale": "https://raw.githubusercontent.com/peteciank/public_files/main/Transformers/Kindness_Tale.txt",
|
35 |
+
"Lost Melody Tale": "https://raw.githubusercontent.com/peteciank/public_files/main/Transformers/Lost_Melody_Tale.txt",
|
36 |
+
"Twitter Example 1": "https://raw.githubusercontent.com/peteciank/public_files/main/Transformers/Twitter_Example_1.txt",
|
37 |
+
"Twitter Example 2": "https://raw.githubusercontent.com/peteciank/public_files/main/Transformers/Twitter_Example_2.txt"
|
38 |
+
}
|
39 |
+
|
40 |
+
# Function to fetch text content based on selected option
|
41 |
+
def fetch_text_content(selected_option):
|
42 |
+
return requests.get(urls.get(selected_option, "")).text if selected_option in urls else ""
|
43 |
+
|
44 |
+
# Fetch text content
|
45 |
+
jd = fetch_text_content(selected_option)
|
46 |
+
|
47 |
+
# Download NLTK resources
|
48 |
+
nltk.download('punkt')
|
49 |
+
nltk.download('stopwords')
|
50 |
+
|
51 |
+
# Initialize transformer sentiment analysis pipeline
|
52 |
+
pipe_sent = pipeline('sentiment-analysis')
|
53 |
+
|
54 |
+
# Function to extract keywords
|
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()
|
67 |
+
for score, phrase in keywords:
|
68 |
+
if phrase not in seen_phrases:
|
69 |
+
similar_phrases = [seen_phrase for seen_phrase in seen_phrases if fuzz.ratio(phrase, seen_phrase) > 70]
|
70 |
+
if similar_phrases:
|
71 |
+
merged_phrase = max([phrase] + similar_phrases, key=len)
|
72 |
+
unique_keywords.append((score, merged_phrase))
|
73 |
+
else:
|
74 |
+
unique_keywords.append((score, phrase))
|
75 |
+
seen_phrases.add(phrase)
|
76 |
+
return unique_keywords[:10]
|
77 |
+
|
78 |
+
# Text input
|
79 |
+
text = st.text_area('Enter the text to analyze', jd)
|
80 |
+
|
81 |
+
if st.button("Start Analysis"):
|
82 |
+
col1, col2 = st.columns(2)
|
83 |
+
|
84 |
+
# Transformers (Column 1)
|
85 |
+
with col1:
|
86 |
+
st.header("Transformers Model")
|
87 |
+
with st.spinner("Analyzing with Transformers..."):
|
88 |
+
out_sentiment = pipe_sent(text)
|
89 |
+
sentiment_score = out_sentiment[0]['score']
|
90 |
+
sentiment_label = out_sentiment[0]['label']
|
91 |
+
sentiment_emoji = '😊' if sentiment_label == 'POSITIVE' else '😞'
|
92 |
+
st.write(f"Sentiment Score: {sentiment_score}, Sentiment Label: {sentiment_label.capitalize()} {sentiment_emoji}")
|
93 |
+
|
94 |
+
st.subheader("Keywords")
|
95 |
+
keywords = extract_keywords(text)
|
96 |
+
st.write([keyword[1] for keyword in keywords])
|
97 |
+
|
98 |
+
# Llama 3 Model (Column 2)
|
99 |
+
with col2:
|
100 |
+
st.header("Llama 3 Model")
|
101 |
+
with st.spinner("Analyzing with Llama 3..."):
|
102 |
+
try:
|
103 |
+
stream = client.chat.completions.create(
|
104 |
+
model=repo_id,
|
105 |
+
messages=[{"role": "user", "content": text}],
|
106 |
+
temperature=0.5,
|
107 |
+
stream=True,
|
108 |
+
max_tokens=3000
|
109 |
+
)
|
110 |
+
response = ''.join([chunk['choices'][0]['text'] for chunk in stream])
|
111 |
+
st.write(response)
|
112 |
+
except Exception as e:
|
113 |
+
st.error("Error occurred while fetching response from Llama 3")
|
114 |
+
|