Spaces:
Building
Building
hatimanees
commited on
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# app.py (Streamlit frontend)
|
2 |
+
import streamlit as st
|
3 |
+
import requests
|
4 |
+
from PyPDF2 import PdfReader
|
5 |
+
import io
|
6 |
+
|
7 |
+
API_URL = "http://localhost:5000/upload"
|
8 |
+
|
9 |
+
|
10 |
+
def main():
|
11 |
+
st.title("Sentiment Analysis on Call Transcripts")
|
12 |
+
|
13 |
+
uploaded_file = st.file_uploader("Upload your call transcript", type=["txt", "pdf"])
|
14 |
+
|
15 |
+
if uploaded_file:
|
16 |
+
try:
|
17 |
+
# Process the uploaded file
|
18 |
+
if uploaded_file.name.endswith(".txt"):
|
19 |
+
transcript = uploaded_file.read().decode('utf-8')
|
20 |
+
elif uploaded_file.name.endswith(".pdf"):
|
21 |
+
reader = PdfReader(uploaded_file)
|
22 |
+
transcript = ""
|
23 |
+
for page in reader.pages:
|
24 |
+
transcript += page.extract_text()
|
25 |
+
|
26 |
+
# Display the extracted text
|
27 |
+
st.text_area("Uploaded Transcript", transcript, height=300)
|
28 |
+
|
29 |
+
# Send the transcript for sentiment analysis
|
30 |
+
if st.button("Analyze Sentiment"):
|
31 |
+
with st.spinner("Analyzing sentiment..."):
|
32 |
+
try:
|
33 |
+
# Send the transcript directly as form data
|
34 |
+
response = requests.post(
|
35 |
+
API_URL,
|
36 |
+
data={'transcript': transcript},
|
37 |
+
timeout=10
|
38 |
+
)
|
39 |
+
|
40 |
+
if response.status_code == 200:
|
41 |
+
sentiment = response.json().get('sentiment', [])
|
42 |
+
st.success("Analysis complete!")
|
43 |
+
|
44 |
+
# Create a nice display for results
|
45 |
+
st.subheader("Sentiment Results")
|
46 |
+
for result in sentiment:
|
47 |
+
score = result['score']
|
48 |
+
label = result['label']
|
49 |
+
|
50 |
+
# Create a progress bar for visualization
|
51 |
+
st.write(f"{label}:")
|
52 |
+
st.progress(score)
|
53 |
+
st.write(f"Score: {score:.2f}")
|
54 |
+
|
55 |
+
# Add interpretation
|
56 |
+
if label == 'Overall Sentiment':
|
57 |
+
if score > 0.6:
|
58 |
+
st.info("π This text is predominantly positive")
|
59 |
+
elif score < 0.4:
|
60 |
+
st.info("π This text is predominantly negative")
|
61 |
+
else:
|
62 |
+
st.info("π This text is relatively neutral")
|
63 |
+
elif label == 'Confidence':
|
64 |
+
if score > 0.8:
|
65 |
+
st.info("β¨ High confidence in this analysis")
|
66 |
+
elif score < 0.5:
|
67 |
+
st.warning("β οΈ Take this analysis with a grain of salt")
|
68 |
+
else:
|
69 |
+
st.error(f"Error: {response.json().get('error', 'Unknown error')}")
|
70 |
+
|
71 |
+
except requests.exceptions.ConnectionError:
|
72 |
+
st.error("Could not connect to the server. Please make sure the Flask backend is running.")
|
73 |
+
except requests.exceptions.Timeout:
|
74 |
+
st.error("Request timed out. Please try again.")
|
75 |
+
except Exception as e:
|
76 |
+
st.error(f"An error occurred: {str(e)}")
|
77 |
+
|
78 |
+
except Exception as e:
|
79 |
+
st.error(f"Error processing file: {str(e)}")
|
80 |
+
|
81 |
+
|
82 |
+
if __name__ == "__main__":
|
83 |
+
main()
|
84 |
+
|
85 |
+
|
86 |
+
|
87 |
+
|