Spaces:
Running
Running
Create Check 3.py
Browse files- pages/Check 3.py +91 -0
pages/Check 3.py
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import requests
|
3 |
+
import os
|
4 |
+
import time
|
5 |
+
|
6 |
+
# Define the endpoint and API key
|
7 |
+
api_url = "https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-8B-Instruct"
|
8 |
+
api_key = os.getenv('HFSecret')
|
9 |
+
|
10 |
+
headers = {
|
11 |
+
"Authorization": f"Bearer {api_key}"
|
12 |
+
}
|
13 |
+
|
14 |
+
# API call function
|
15 |
+
def call_huggingface_api(prompt):
|
16 |
+
data = {"inputs": prompt, "parameters": {"max_length": 500, "temperature": 0.5}}
|
17 |
+
response = requests.post(api_url, headers=headers, json=data)
|
18 |
+
|
19 |
+
if response.status_code != 200:
|
20 |
+
st.error(f"Error: {response.status_code} - {response.text}")
|
21 |
+
return None
|
22 |
+
|
23 |
+
return response.json()
|
24 |
+
|
25 |
+
# Function to load text from a URL
|
26 |
+
def load_text_from_url(url):
|
27 |
+
response = requests.get(url)
|
28 |
+
return response.text if response.status_code == 200 else ""
|
29 |
+
|
30 |
+
# Preset sample text options
|
31 |
+
options = ['None', 'Appreciation Letter', 'Regret Letter', 'Kindness Tale', 'Lost Melody Tale', 'Twitter Example 1', 'Twitter Example 2']
|
32 |
+
url_dict = {
|
33 |
+
'Appreciation Letter': "https://raw.githubusercontent.com/peteciank/public_files/main/Transformers/Appreciation_Letter.txt",
|
34 |
+
'Regret Letter': "https://raw.githubusercontent.com/peteciank/public_files/main/Transformers/Regret_Letter.txt",
|
35 |
+
'Kindness Tale': "https://raw.githubusercontent.com/peteciank/public_files/main/Transformers/Kindness_Tale.txt",
|
36 |
+
'Lost Melody Tale': "https://raw.githubusercontent.com/peteciank/public_files/main/Transformers/Lost_Melody_Tale.txt",
|
37 |
+
'Twitter Example 1': "https://raw.githubusercontent.com/peteciank/public_files/main/Transformers/Twitter_Example_1.txt",
|
38 |
+
'Twitter Example 2': "https://raw.githubusercontent.com/peteciank/public_files/main/Transformers/Twitter_Example_2.txt"
|
39 |
+
}
|
40 |
+
|
41 |
+
# Streamlit layout
|
42 |
+
st.title("Sentiment Analysis, Summarization, and Keyword Extraction")
|
43 |
+
|
44 |
+
# Dropdown to select a text file
|
45 |
+
selected_option = st.selectbox("Select a preset option", options)
|
46 |
+
|
47 |
+
# Initialize text_input
|
48 |
+
text_input = ""
|
49 |
+
|
50 |
+
# Load text based on dropdown selection
|
51 |
+
if selected_option != 'None':
|
52 |
+
with st.spinner("Loading text..."):
|
53 |
+
text_input = load_text_from_url(url_dict[selected_option])
|
54 |
+
time.sleep(1) # Simulate loading time
|
55 |
+
st.success("Text loaded!")
|
56 |
+
else:
|
57 |
+
text_input = st.text_area("Or enter your own text for analysis")
|
58 |
+
|
59 |
+
if st.button("Analyze"):
|
60 |
+
if text_input:
|
61 |
+
with st.spinner('Processing...'):
|
62 |
+
# Sentiment Analysis
|
63 |
+
sentiment_prompt = f"Perform sentiment analysis on the following text: {text_input}"
|
64 |
+
sentiment_result = call_huggingface_api(sentiment_prompt)
|
65 |
+
|
66 |
+
# Summarization
|
67 |
+
summarization_prompt = f"Summarize the following text: {text_input}"
|
68 |
+
summarization_result = call_huggingface_api(summarization_prompt)
|
69 |
+
|
70 |
+
# Keyword Extraction
|
71 |
+
keyword_prompt = f"Extract important keywords from the following text: {text_input}"
|
72 |
+
keyword_result = call_huggingface_api(keyword_prompt)
|
73 |
+
|
74 |
+
time.sleep(1) # Simulate a small delay
|
75 |
+
st.success('Analysis completed!')
|
76 |
+
|
77 |
+
# Display Results in Collapsible Expanders
|
78 |
+
if sentiment_result:
|
79 |
+
with st.expander("Sentiment Analysis (Conclusion)"):
|
80 |
+
st.write("Conclusion: Positive :) or Negative :( ")
|
81 |
+
st.write(sentiment_result[0]['generated_text'])
|
82 |
+
|
83 |
+
if summarization_result:
|
84 |
+
with st.expander("Summarization"):
|
85 |
+
st.write(summarization_result[0]['generated_text'])
|
86 |
+
|
87 |
+
if keyword_result:
|
88 |
+
with st.expander("Keyword Extraction"):
|
89 |
+
st.write(keyword_result[0]['generated_text'].split(',')) # Display keywords as list
|
90 |
+
else:
|
91 |
+
st.warning("Please enter some text for analysis.")
|