Spaces:
Runtime error
Runtime error
Update pages/Comparision.py
Browse files- pages/Comparision.py +79 -112
pages/Comparision.py
CHANGED
|
@@ -1,138 +1,105 @@
|
|
| 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
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# Load environment variables
|
| 11 |
load_dotenv()
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
|
| 17 |
-
# Initialize
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
# Define the Llama 3 model ID
|
| 22 |
-
|
| 23 |
|
| 24 |
-
# Function to fetch text content
|
| 25 |
def fetch_text_content(selected_option):
|
| 26 |
-
|
| 27 |
-
'
|
| 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(
|
| 35 |
-
|
| 36 |
-
# Function to
|
| 37 |
-
def
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
}
|
| 46 |
-
|
|
|
|
|
|
|
| 47 |
try:
|
| 48 |
-
response =
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 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
|
| 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 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
# Ensure that the score is properly handled as a float, or display the string as-is
|
| 110 |
-
def display_score(score):
|
| 111 |
-
try:
|
| 112 |
-
# Attempt to format as float if it's a valid number
|
| 113 |
-
return f"{float(score):.2f}"
|
| 114 |
-
except ValueError:
|
| 115 |
-
# If it's not a number, just return the score as is (probably a string error message)
|
| 116 |
-
return score
|
| 117 |
-
|
| 118 |
-
# Display results for Transformers-based analysis in the first column
|
| 119 |
-
with col1:
|
| 120 |
-
st.subheader("Transformers Analysis")
|
| 121 |
-
with st.expander("Sentiment Analysis - Transformers"):
|
| 122 |
-
sentiment_emoji = 'π' if sentiment_label_transformer == 'POSITIVE' else 'π'
|
| 123 |
-
st.write(f"Sentiment: {sentiment_label_transformer} ({sentiment_emoji})")
|
| 124 |
-
st.write(f"Score: {display_score(sentiment_score_transformer)}") # Use the display_score function
|
| 125 |
-
|
| 126 |
-
with st.expander("Summarization - Transformers"):
|
| 127 |
-
st.write(summary_transformer)
|
| 128 |
-
|
| 129 |
-
# Display results for Llama-based analysis in the second column
|
| 130 |
-
with col2:
|
| 131 |
-
st.subheader("Llama Analysis")
|
| 132 |
-
with st.expander("Sentiment Analysis - Llama"):
|
| 133 |
-
sentiment_emoji = 'π' if sentiment_label_llama == 'POSITIVE' else 'π'
|
| 134 |
-
st.write(f"Sentiment: {sentiment_label_llama} ({sentiment_emoji})")
|
| 135 |
-
st.write(f"Score: {display_score(sentiment_score_llama)}") # Use the display_score function
|
| 136 |
-
|
| 137 |
-
with st.expander("Summarization - Llama"):
|
| 138 |
-
st.write(summary_llama)
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
|
|
|
|
|
|
| 3 |
import os
|
|
|
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
+
from nltk.corpus import stopwords
|
| 6 |
+
from fuzzywuzzy import fuzz
|
| 7 |
+
from rake_nltk import Rake
|
| 8 |
+
import nltk
|
| 9 |
+
from openai import OpenAI
|
| 10 |
|
| 11 |
# Load environment variables
|
| 12 |
load_dotenv()
|
| 13 |
|
| 14 |
+
# Download NLTK resources
|
| 15 |
+
nltk.download('punkt')
|
| 16 |
+
nltk.download('stopwords')
|
| 17 |
|
| 18 |
+
# Initialize OpenAI client for Hugging Face Llama 3
|
| 19 |
+
client = OpenAI(
|
| 20 |
+
base_url="https://api-inference.huggingface.co/v1",
|
| 21 |
+
api_key=os.environ.get('HFSecret') # Replace with your token
|
| 22 |
+
)
|
| 23 |
|
| 24 |
+
# Define the Llama 3 model repo ID
|
| 25 |
+
repo_id = "meta-llama/Meta-Llama-3-8B-Instruct"
|
| 26 |
|
| 27 |
+
# Function to fetch text content based on selected option
|
| 28 |
def fetch_text_content(selected_option):
|
| 29 |
+
url_mapping = {
|
| 30 |
+
'Apprecitation Letter': "https://raw.githubusercontent.com/peteciank/public_files/main/Transformers/Appreciation_Letter.txt",
|
| 31 |
'Regret Letter': "https://raw.githubusercontent.com/peteciank/public_files/main/Transformers/Regret_Letter.txt",
|
| 32 |
'Kindness Tale': "https://raw.githubusercontent.com/peteciank/public_files/main/Transformers/Kindness_Tale.txt",
|
| 33 |
'Lost Melody Tale': "https://raw.githubusercontent.com/peteciank/public_files/main/Transformers/Lost_Melody_Tale.txt",
|
| 34 |
'Twitter Example 1': "https://raw.githubusercontent.com/peteciank/public_files/main/Transformers/Twitter_Example_1.txt",
|
| 35 |
'Twitter Example 2': "https://raw.githubusercontent.com/peteciank/public_files/main/Transformers/Twitter_Example_2.txt"
|
| 36 |
}
|
| 37 |
+
return requests.get(url_mapping.get(selected_option, "")).text
|
| 38 |
+
|
| 39 |
+
# Function to extract keywords
|
| 40 |
+
def extract_keywords(text):
|
| 41 |
+
r = Rake()
|
| 42 |
+
r.extract_keywords_from_text(text)
|
| 43 |
+
phrases_with_scores = r.get_ranked_phrases_with_scores()
|
| 44 |
+
stop_words = set(stopwords.words('english'))
|
| 45 |
+
keywords = [(score, phrase) for score, phrase in phrases_with_scores if phrase.lower() not in stop_words]
|
| 46 |
+
keywords.sort(key=lambda x: x[0], reverse=True)
|
| 47 |
+
|
| 48 |
+
unique_keywords = []
|
| 49 |
+
seen_phrases = set()
|
| 50 |
+
for score, phrase in keywords:
|
| 51 |
+
if phrase not in seen_phrases:
|
| 52 |
+
similar_phrases = [seen_phrase for seen_phrase in seen_phrases if fuzz.ratio(phrase, seen_phrase) > 70]
|
| 53 |
+
merged_phrase = max([phrase] + similar_phrases, key=len) if similar_phrases else phrase
|
| 54 |
+
unique_keywords.append((score, merged_phrase))
|
| 55 |
+
seen_phrases.add(phrase)
|
| 56 |
+
return unique_keywords[:10]
|
| 57 |
+
|
| 58 |
+
# Function to interact with Llama 3 for analysis
|
| 59 |
+
def llama3_analysis(text, task):
|
| 60 |
+
prompt_mapping = {
|
| 61 |
+
"sentiment": f"Analyze the sentiment of the following text: {text}",
|
| 62 |
+
"summarization": f"Summarize the following text: {text}"
|
| 63 |
}
|
| 64 |
+
|
| 65 |
+
prompt = prompt_mapping[task]
|
| 66 |
+
|
| 67 |
try:
|
| 68 |
+
response = client.completions.create(
|
| 69 |
+
model=repo_id,
|
| 70 |
+
prompt=prompt,
|
| 71 |
+
max_tokens=500,
|
| 72 |
+
temperature=0.5
|
| 73 |
+
)
|
| 74 |
+
return response.choices[0].text.strip()
|
| 75 |
+
except Exception as e:
|
| 76 |
+
return f"Error: {str(e)}"
|
| 77 |
+
|
| 78 |
+
# Streamlit App UI
|
| 79 |
+
st.title("Sentiment Analysis & Summarization with Llama 3")
|
| 80 |
+
|
| 81 |
+
# Dropdown menu to select the text source
|
| 82 |
+
options = ['None', 'Apprecitation Letter', 'Regret Letter', 'Kindness Tale', 'Lost Melody Tale', 'Twitter Example 1', 'Twitter Example 2']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
selected_option = st.selectbox("Select a preset option", options)
|
| 84 |
|
| 85 |
+
# Fetch the text based on selection
|
| 86 |
jd = fetch_text_content(selected_option)
|
| 87 |
+
|
| 88 |
+
# Text area for manual input or displaying fetched content
|
| 89 |
text = st.text_area('Enter the text to analyze', jd)
|
| 90 |
|
| 91 |
if st.button("Start Analysis"):
|
| 92 |
+
with st.spinner("Analyzing Sentiment..."):
|
| 93 |
+
sentiment_result = llama3_analysis(text, "sentiment")
|
| 94 |
+
with st.expander("Sentiment Analysis - β
Completed", expanded=False):
|
| 95 |
+
st.write(sentiment_result)
|
| 96 |
+
|
| 97 |
+
with st.spinner("Summarizing..."):
|
| 98 |
+
summary_result = llama3_analysis(text, "summarization")
|
| 99 |
+
with st.expander("Summarization - β
Completed", expanded=False):
|
| 100 |
+
st.write(summary_result)
|
| 101 |
+
|
| 102 |
+
with st.spinner("Extracting Keywords..."):
|
| 103 |
+
keywords = extract_keywords(text)
|
| 104 |
+
with st.expander("Keywords Extraction - β
Completed", expanded=False):
|
| 105 |
+
st.write([kw[1] for kw in keywords])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|