File size: 6,961 Bytes
7efeab0 06350c8 7efeab0 169a56e 06350c8 7efeab0 169a56e 7efeab0 169a56e 7efeab0 169a56e 7efeab0 |
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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
import pandas as pd
from bertopic import BERTopic
from huggingface_hub import InferenceClient
from bertopic.vectorizers import ClassTfidfTransformer
from sentence_transformers import SentenceTransformer
from sklearn import preprocessing
from sklearn.preprocessing import LabelEncoder
from tempfile import NamedTemporaryFile
import matplotlib.pyplot as plt
import plotly.express as px
import subprocess
from wordcloud import WordCloud
def process_file_bm25(file,mode,min_cluster_size,top_n_words,ngram):
if file.name.endswith('.csv'):
df = pd.read_csv(file)
elif file.name.endswith('.xls') or file.name.endswith('.xlsx'):
df = pd.read_excel(file)
else:
raise ValueError("Unsupported file format. Please provide a CSV or Excel file.")
# Ensure that the 'products' column is present in the dataframe
if 'products' not in df.columns.str.lower():
raise ValueError("The input file must have a column named 'products'.")
# Convert the 'products' column to a list
sentences_list = df['products'].tolist()
print(len(sentences_list))
ctfidf_model = ClassTfidfTransformer(bm25_weighting=True,reduce_frequent_words=True)
if mode=="Automated clustering":
topic_model = BERTopic(ctfidf_model=ctfidf_model,n_gram_range =(1,ngram),top_n_words=top_n_words)
else:
topic_model = BERTopic(ctfidf_model=ctfidf_model,n_gram_range =(1,ngram),top_n_words=top_n_words,min_topic_size=min_cluster_size)
# Perform topic modeling
topics, probabilities = topic_model.fit_transform(sentences_list)
# Visualize all graphs
topics_info=topic_model.get_topic_info()
df_topics_bm25= topics_info
#print(topics)
try:
barchart = topic_model.visualize_barchart(top_n_topics=10)
except:
barchart='Error message'
try:
topics_plot = topic_model.visualize_topics()
except:
topics_plot = ' Error message'
heatmap = topic_model.visualize_heatmap()
hierarchy = topic_model.visualize_hierarchy()
df['topic_number'] = topics
# Encode the topic numbers to make them categorical
label_encoder = LabelEncoder()
df['topic_number_encoded'] = label_encoder.fit_transform(df['topic_number'])
temp_file = NamedTemporaryFile(delete=False, suffix=".xlsx")
df.to_excel(temp_file.name, index=False)
df_bm25=df
#print(df)
return df,temp_file.name,topics_info ,barchart,topics_plot, heatmap, hierarchy
def process_file_bert(file,mode,min_cluster_size,top_n_words,ngram):
# Read the Excel sheet or CSV file
if file.name.endswith('.csv'):
df = pd.read_csv(file)
elif file.name.endswith('.xls') or file.name.endswith('.xlsx'):
df = pd.read_excel(file)
else:
raise ValueError("Unsupported file format. Please provide a CSV or Excel file.")
# Ensure that the 'products' column is present in the dataframe
if 'products' not in df.columns.str.lower():
raise ValueError("The input file must have a column named 'products'.")
# Convert the 'products' column to a list
sentences_list = df['products'].tolist()
print(len(sentences_list))
representation_model = KeyBERTInspired()
if mode=="Automated clustering":
# Fine-tune your topic representations
topic_model = BERTopic(representation_model=representation_model,n_gram_range =(1,ngram),top_n_words=top_n_words)
else:
topic_model = BERTopic(representation_model=representation_model,n_gram_range =(1,ngram),top_n_words=top_n_words,min_topic_size=min_cluster_size)
topics, probabilities = topic_model.fit_transform(sentences_list)
# Visualize all graphs
topics_info=topic_model.get_topic_info()
state.df_topics_bert= topics_info
#print(topics)
try:
barchart = topic_model.visualize_barchart(top_n_topics=10)
except:
barchart='Error message'
try:
topics_plot = topic_model.visualize_topics()
except:
topics_plot = ' Error message'
heatmap = topic_model.visualize_heatmap()
hierarchy = topic_model.visualize_hierarchy()
df['topic_number'] = topics
# Encode the topic numbers to make them categorical
label_encoder = LabelEncoder()
df['topic_number_encoded'] = label_encoder.fit_transform(df['topic_number'])
temp_file = NamedTemporaryFile(delete=False, suffix=".xlsx")
df.to_excel(temp_file.name, index=False)
state.df_bert=df
return df, topics_info ,barchart,topics_plot, heatmap, hierarchy
client = InferenceClient(
"mistralai/Mixtral-8x7B-Instruct-v0.1"
)
def format_prompt(message, history):
prompt = "<s>"
for user_prompt, bot_response in history:
prompt += f"[INST] {user_prompt} [/INST]"
prompt += f" {bot_response}</s> "
prompt += f"[INST] {message} [/INST]"
return prompt
def generate(
prompt, history, system_prompt, temperature=0.9, max_new_tokens=4096, top_p=0.95, repetition_penalty=1.0,
):
temperature = float(temperature)
if temperature < 1e-2:
temperature = 1e-2
top_p = float(top_p)
generate_kwargs = dict(
temperature=temperature,
max_new_tokens=max_new_tokens,
top_p=top_p,
repetition_penalty=repetition_penalty,
do_sample=True,
seed=42,
)
formatted_prompt = format_prompt(f"{system_prompt}, {prompt}", history)
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
output = ""
for response in stream:
output += response.token.text
yield output
return output
# Define the function to generate the plot based on user inputs
def generate_plot(topic, x_axis_index, y_axis_index, chart_type, agg_func):
x_axis = df.columns[1:][x_axis_index]
y_axis = df.columns[1:][y_axis_index]
print(x_axis,y_axis)
filtered_df = df[df['Topic Number'] == topic]
if chart_type == "scatter":
fig = px.scatter(filtered_df, x=x_axis, y=y_axis)
elif chart_type == "bar":
print('Bar chart selected')
if agg_func == "count_distinct":
fig = px.bar(filtered_df, x=x_axis, y=y_axis, color=y_axis, barmode='group')
else:
fig = px.bar(filtered_df, x=x_axis, y=y_axis, color=y_axis)
elif chart_type == "line":
fig = px.line(filtered_df, x=x_axis, y=y_axis)
elif chart_type == "box":
fig = px.box(filtered_df, x=x_axis, y=y_axis)
elif chart_type == "wordcloud":
text = ' '.join(filtered_df[y_axis].astype(str))
wordcloud = WordCloud(width=800, height=400, random_state=21, max_font_size=110).generate(text)
plt.figure(figsize=(10, 7))
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis('off')
plt.show()
return None
elif chart_type == "pie":
fig = px.pie(filtered_df, names=x_axis, values=y_axis)
print('Pie chart selected')
return fig
|