Spaces:
Runtime error
Runtime error
Update app.py
Browse files252 unmodified lines2532542552562572582594 unmodified lines2642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123 unmodified lines316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352252 unmodified linestopic_model_params["nr_topics"] = "auto"topic_model = BERTopic(embedding_model=None, # Set to None since we're providing embeddings**topic_model_params)4 unmodified lines)topic_model.vectorizer_model = vectorizerprogress_placeholder = st.empty()progress_bar = progress_placeholder.progress(0)status_message = st.empty()for country, group in df.groupby('country'):gc.collect()torch.cuda.empty_cache() if torch.cuda.is_available() else Nonestatus_message.text(f"Processing poems for {country}...")texts = [clean_arabic_text(poem) for poem in group['poem'].dropna()]all_emotions = []embeddings_list = []total_texts = len(texts)for i, text in enumerate(texts):try:embedding = cache_embeddings(text, bert_tokenizer, bert_model)if embedding is not None and not np.isnan(embedding).any():# Ensure embedding is 2Dif len(embedding.shape) == 1:embedding = embedding.reshape(1, -1)embeddings_list.append(embedding)if i % max(1, total_texts // 100) == 0:progress = (i + 1) / total_texts * 0.4progress_bar.progress(progress)status_message.text(f"Generated embeddings for {i+1}/{total_texts} poems in {country}...")except Exception as e:st.warning(f"Error processing poem {i+1} in {country}: {str(e)}")continue# Process emotionsfor i, text in enumerate(texts):try:emotion = cache_emotion_classification(text, emotion_classifier)all_emotions.append(emotion)if i % max(1, total_texts // 100) == 0:progress = 0.4 + ((i + 1) / total_texts * 0.3)progress_bar.progress(progress)status_message.text(f"Classified emotions for {i+1}/{total_texts} poems in {country}...")except Exception as e:st.warning(f"Error classifying emotion for poem {i+1} in {country}: {str(e)}")continue3 unmodified linesst.warning(f"Not enough documents for {country} to generate meaningful topics (minimum {min_topic_size} required)")continueif embeddings_list:# Stack all embeddings into a single 2D arrayembeddings = np.vstack(embeddings_list)topics, probs = topic_model.fit_transform(texts, embeddings)topic_counts = Counter(topics)top_topics = format_topics(topic_model, topic_counts.most_common(top_n))top_emotions = format_emotions(Counter(all_emotions).most_common(top_n))summaries.append({'country': country,'total_poems': len(texts),'top_topics': top_topics,'top_emotions': top_emotions})progress_bar.progress(1.0, text="Processing complete!")else:st.warning(f"No valid embeddings generated for {country}")except Exception as e:st.warning(f"Could not generate topics for {country}: {str(e)}")continueprogress_placeholder.empty()status_message.empty()progress_placeholder = st.empty()progress_bar = progress_placeholder.progress(0)status_message = st.empty()return summaries, topic_modeltry:bert_tokenizer, bert_model, emotion_classifier = load_models()st.success("Models loaded successfully!")252 unmodified lines2532542552562572582594 unmodified lines2642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003 unmodified lines304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334252 unmodified linestopic_model_params["nr_topics"] = "auto"topic_model = BERTopic(embedding_model=None, # Changed from bert_model to None**topic_model_params)4 unmodified lines)topic_model.vectorizer_model = vectorizerfor country, group in df.groupby('country'):progress_text = f"Processing poems for {country}..."progress_bar = st.progress(0, text=progress_text)texts = [clean_arabic_text(poem) for poem in group['poem'].dropna()]all_emotions = []# Generate embeddingsembeddings = []for i, text in enumerate(texts):try:embedding = get_embedding_for_text(text, bert_tokenizer, bert_model)if embedding is not None and not np.isnan(embedding).any():embeddings.append(embedding)progress = (i + 1) / len(texts) * 0.4progress_bar.progress(progress, text=f"Generated embeddings for {i+1}/{len(texts)} poems...")except Exception as e:st.warning(f"Error processing poem {i+1} in {country}: {str(e)}")continue# Convert embeddings to numpy arrayembeddings = np.array(embeddings)# Process emotionsfor i, text in enumerate(texts):try:emotion = classify_emotion(text, emotion_classifier)all_emotions.append(emotion)progress = 0.4 + ((i + 1) / len(texts) * 0.3)progress_bar.progress(progress, text=f"Classified emotions for {i+1}/{len(texts)} poems...")except Exception as e:st.warning(f"Error classifying emotion for poem {i+1} in {country}: {str(e)}")continue3 unmodified linesst.warning(f"Not enough documents for {country} to generate meaningful topics (minimum {min_topic_size} required)")continue# Ensure texts and embeddings matchif len(embeddings) != len(texts):texts = texts[:len(embeddings)]# Fit and transform the topic modeltopics, probs = topic_model.fit_transform(texts, embeddings)topic_counts = Counter(topics)top_topics = format_topics(topic_model, topic_counts.most_common(top_n))top_emotions = format_emotions(Counter(all_emotions).most_common(top_n))summaries.append({'country': country,'total_poems': len(texts),'top_topics': top_topics,'top_emotions': top_emotions})progress_bar.progress(1.0, text="Processing complete!")except Exception as e:st.warning(f"Could not generate topics for {country}: {str(e)}")continuereturn summaries, topic_modeltry:bert_tokenizer, bert_model, emotion_classifier = load_models()st.success("Models loaded successfully!")
app.py+36-54