-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp2.py
83 lines (62 loc) · 2.65 KB
/
app2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# Version 2
import streamlit as st
from langchain_community.llms import CTransformers
from collections import defaultdict
import pandas as pd
# Define complaint categories
complaint_categories = [
'Product Quality Issues',
'Shipping and Delivery Problems',
'Customer Service Complaints',
'Payment and Billing Issues',
'Technical Issues',
'Product Availability',
'Marketing and Promotions',
'Privacy and Security Concerns',
'Return and Refund Process',
'Subscription Services'
]
# Create a prompt for classification
def create_prompt(text):
categories = ', '.join(complaint_categories)
prompt = f"You are an expert in classifying complaints. Classify the following complaint into one of these categories: Product Quality Issues,Shipping and Delivery Problems,Customer Service Complaints,Payment and Billing Issues,Technical Issues,Product Availability,Marketing and Promotions,Privacy and Security Concerns,Return and Refund Process,Subscription Services .\n\nComplaint Text: {text}\n\nPlease provide only the category name as your response:"
return prompt
# Classify the complaint using the model
def classify_complaint(text, model):
prompt = create_prompt(text)
result = model(prompt)
return result.strip()
# Load the model once and store in session state
def load_llm():
return CTransformers(
model="TheBloke/Llama-2-7B-Chat-GGML",
model_type="llama",
max_new_tokens=512,
temperature=0.5
)
# Streamlit app
st.title("Complaint Classification App")
# Load the model once and store it in session state
if 'model' not in st.session_state:
st.session_state.model = load_llm()
model = st.session_state.model
uploaded_file = st.file_uploader("Upload a complaints.txt file", type="txt")
if st.button("Analyze"):
if uploaded_file is not None:
complaints = uploaded_file.read().decode("utf-8").splitlines()
# Initialize category counts
category_counts = defaultdict(int)
for complaint in complaints:
category = classify_complaint(complaint, model)
if category in complaint_categories:
category_counts[category] += 1
# Display the results
st.subheader("Complaint Counts per Category:")
for category in complaint_categories:
st.write(f"{category}: {category_counts.get(category, 0)}")
# Convert dictionary to DataFrame
df = pd.DataFrame(list(category_counts.items()), columns=['Category', 'Count'])
# Create a Streamlit app
st.subheader("Complaints Category Bar Chart")
# Display the bar chart
st.bar_chart(df.set_index('Category'))