Spaces:
Sleeping
Sleeping
peterciank
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,115 +1,89 @@
|
|
1 |
import streamlit as st
|
|
|
|
|
2 |
import requests
|
3 |
-
import
|
4 |
-
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 |
-
#
|
12 |
-
|
|
|
|
|
|
|
13 |
|
14 |
-
#
|
15 |
-
|
16 |
-
|
17 |
-
# Define URLs for different options
|
18 |
-
url_option1 = "https://raw.githubusercontent.com/peteciank/public_files/main/Transformers/Appreciation_Letter.txt"
|
19 |
-
url_option2 = "https://raw.githubusercontent.com/peteciank/public_files/main/Transformers/Regret_Letter.txt"
|
20 |
-
url_option3 = "https://raw.githubusercontent.com/peteciank/public_files/main/Transformers/Kindness_Tale.txt"
|
21 |
-
url_option4 = "https://raw.githubusercontent.com/peteciank/public_files/main/Transformers/Lost_Melody_Tale.txt"
|
22 |
-
url_option5 = "https://raw.githubusercontent.com/peteciank/public_files/main/Transformers/Twitter_Example_1.txt"
|
23 |
-
url_option6 = "https://raw.githubusercontent.com/peteciank/public_files/main/Transformers/Twitter_Example_2.txt"
|
24 |
|
25 |
-
#
|
26 |
-
|
27 |
-
if selected_option == 'Apprecitation Letter':
|
28 |
-
return requests.get(url_option1).text
|
29 |
-
elif selected_option == 'Regret Letter':
|
30 |
-
return requests.get(url_option2).text
|
31 |
-
elif selected_option == 'Kindness Tale':
|
32 |
-
return requests.get(url_option3).text
|
33 |
-
elif selected_option == 'Lost Melody Tale':
|
34 |
-
return requests.get(url_option4).text
|
35 |
-
elif selected_option == 'Twitter Example 1':
|
36 |
-
return requests.get(url_option5).text
|
37 |
-
elif selected_option == 'Twitter Example 2':
|
38 |
-
return requests.get(url_option6).text
|
39 |
-
else:
|
40 |
-
return ""
|
41 |
-
|
42 |
-
# Fetch text content based on selected option
|
43 |
-
jd = fetch_text_content(selected_option)
|
44 |
|
45 |
-
#
|
46 |
-
|
47 |
|
|
|
|
|
48 |
|
49 |
-
#
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
-
#
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
|
|
57 |
|
58 |
-
#
|
59 |
-
|
60 |
-
r = Rake()
|
61 |
-
r.extract_keywords_from_text(text)
|
62 |
-
# Get all phrases scored
|
63 |
-
phrases_with_scores = r.get_ranked_phrases_with_scores()
|
64 |
-
# Filter out stopwords
|
65 |
-
stop_words = set(stopwords.words('english'))
|
66 |
-
keywords = []
|
67 |
-
for score, phrase in phrases_with_scores:
|
68 |
-
# Check if the phrase is not a stopword and add to the list
|
69 |
-
if phrase.lower() not in stop_words:
|
70 |
-
keywords.append((score, phrase))
|
71 |
-
# Sort keywords by score in descending order
|
72 |
-
keywords.sort(key=lambda x: x[0], reverse=True)
|
73 |
-
# Remove duplicates and merge similar keywords
|
74 |
-
unique_keywords = []
|
75 |
-
seen_phrases = set()
|
76 |
-
for score, phrase in keywords:
|
77 |
-
if phrase not in seen_phrases:
|
78 |
-
# Check if the phrase is similar to any of the seen phrases
|
79 |
-
similar_phrases = [seen_phrase for seen_phrase in seen_phrases if fuzz.ratio(phrase, seen_phrase) > 70]
|
80 |
-
if similar_phrases:
|
81 |
-
# If similar phrases are found, merge them into one phrase
|
82 |
-
merged_phrase = max([phrase] + similar_phrases, key=len)
|
83 |
-
unique_keywords.append((score, merged_phrase))
|
84 |
-
else:
|
85 |
-
unique_keywords.append((score, phrase))
|
86 |
-
seen_phrases.add(phrase)
|
87 |
-
return unique_keywords[:10] # Return only the first 10 keywords
|
88 |
|
|
|
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 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
sentiment_emoji = '😊' if sentiment_label == 'POSITIVE' else '😞'
|
100 |
-
sentiment_text = f"Sentiment Score: {sentiment_score}, Sentiment Label: {sentiment_label.capitalize()} {sentiment_emoji}"
|
101 |
-
st.write(sentiment_text)
|
102 |
|
103 |
-
with st.spinner("Summarizing
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
|
|
109 |
|
110 |
-
with st.spinner("Extracting Keywords"):
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from openai import OpenAI
|
3 |
+
import os
|
4 |
import requests
|
5 |
+
from dotenv import load_dotenv
|
|
|
|
|
|
|
|
|
6 |
|
7 |
+
# Load environment variables
|
8 |
+
load_dotenv()
|
9 |
|
10 |
+
# Initialize the client with HuggingFace
|
11 |
+
client = OpenAI(
|
12 |
+
base_url="https://api-inference.huggingface.co/v1",
|
13 |
+
api_key=os.environ.get('HFSecret') # Replace with your HuggingFace token
|
14 |
+
)
|
15 |
|
16 |
+
# Define the Llama 3 8B model
|
17 |
+
repo_id = "meta-llama/Meta-Llama-3-8B-Instruct"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
+
# Title of the App
|
20 |
+
st.title("Text Analysis with Llama 3: Sentiment, Summarization, and Keyword Extraction")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
+
# Dropdown options to choose a text file
|
23 |
+
options = ['None', 'Appreciation Letter', 'Regret Letter', 'Kindness Tale', 'Lost Melody Tale', 'Twitter Example 1', 'Twitter Example 2']
|
24 |
|
25 |
+
# Create a dropdown menu to select options
|
26 |
+
selected_option = st.selectbox("Select a preset option", options)
|
27 |
|
28 |
+
# Define URLs for different text options
|
29 |
+
url_dict = {
|
30 |
+
'Appreciation 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 |
|
38 |
+
# Function to fetch text content
|
39 |
+
def fetch_text_content(option):
|
40 |
+
if option in url_dict:
|
41 |
+
response = requests.get(url_dict[option])
|
42 |
+
return response.text if response.status_code == 200 else "Error fetching the text"
|
43 |
+
return ""
|
44 |
|
45 |
+
# Fetch the selected text
|
46 |
+
jd = fetch_text_content(selected_option)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
+
# Display fetched text
|
49 |
text = st.text_area('Enter the text to analyze', jd)
|
50 |
|
51 |
+
# Function to call Llama 3 for analysis
|
52 |
+
def call_llama_analysis(task, text):
|
53 |
+
prompt = f"Perform {task} on the following text:\n\n{text}"
|
54 |
+
|
55 |
+
# Call Llama 3 for the task
|
56 |
+
response = client.completions.create(
|
57 |
+
model=repo_id,
|
58 |
+
prompt=prompt,
|
59 |
+
max_tokens=3000,
|
60 |
+
temperature=0.5
|
61 |
+
)
|
62 |
+
|
63 |
+
return response['choices'][0]['text']
|
64 |
+
|
65 |
+
# Start analysis on button click
|
66 |
if st.button("Start Analysis"):
|
67 |
+
with st.spinner("Analyzing Sentiment..."):
|
68 |
+
try:
|
69 |
+
sentiment_result = call_llama_analysis("sentiment analysis", text)
|
70 |
+
with st.expander("Sentiment Analysis - ✅ Completed", expanded=True):
|
71 |
+
st.write(sentiment_result)
|
72 |
+
except Exception as e:
|
73 |
+
st.error(f"Error in Sentiment Analysis: {str(e)}")
|
|
|
|
|
|
|
74 |
|
75 |
+
with st.spinner("Summarizing..."):
|
76 |
+
try:
|
77 |
+
summary_result = call_llama_analysis("summarization", text)
|
78 |
+
with st.expander("Summarization - ✅ Completed", expanded=True):
|
79 |
+
st.write(summary_result)
|
80 |
+
except Exception as e:
|
81 |
+
st.error(f"Error in Summarization: {str(e)}")
|
82 |
|
83 |
+
with st.spinner("Extracting Keywords..."):
|
84 |
+
try:
|
85 |
+
keywords_result = call_llama_analysis("keyword extraction", text)
|
86 |
+
with st.expander("Keywords Extraction - ✅ Completed", expanded=True):
|
87 |
+
st.write(keywords_result)
|
88 |
+
except Exception as e:
|
89 |
+
st.error(f"Error in Keyword Extraction: {str(e)}")
|