KevSun commited on
Commit
1c22cc5
verified
1 Parent(s): ff38d81

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -14
app.py CHANGED
@@ -58,9 +58,11 @@ def detect_language(text):
58
 
59
  @st.cache_data
60
  def tsne_visualization(embeddings, words):
61
- if len(words) < 30:
62
  return pd.DataFrame({'word': words})
63
- tsne = TSNE(n_components=2, random_state=42, perplexity=min(30, len(words) - 1))
 
 
64
  embeddings_2d = tsne.fit_transform(embeddings)
65
  df = pd.DataFrame(embeddings_2d, columns=['x', 'y'])
66
  df['word'] = words
@@ -96,18 +98,21 @@ if st.button("Analyze"):
96
  st.write("Word list (not enough words for t-SNE visualization):")
97
  st.write(", ".join(words))
98
 
99
- with st.spinner("Extracting topics..."):
100
- texts = [user_input, "Another text to improve topic modeling."]
101
- topic_distr, vectorizer = topic_modeling_agent.fit_transform(texts, lang)
102
- topics = topic_modeling_agent.get_topics(vectorizer)
103
- st.subheader("Topics Extracted:")
104
- for topic, words in topics.items():
105
- st.write(f"Topic {topic}: {', '.join(words)}")
106
-
107
- with st.spinner("Computing similarity..."):
108
- text2 = "Otro texto de ejemplo para comparaci贸n de similitud." if lang != 'en' else "Another example text for similarity comparison."
109
- similarity_score = similarity_agent.compute_similarity(user_input, text2)
110
- st.write(f"Similarity Score with example text: {similarity_score:.4f}")
 
 
 
111
 
112
  else:
113
  st.warning("Please enter some text to analyze.")
 
58
 
59
  @st.cache_data
60
  def tsne_visualization(embeddings, words):
61
+ if len(words) < 3: # Not enough words for t-SNE
62
  return pd.DataFrame({'word': words})
63
+
64
+ perplexity = min(30, len(words) - 1)
65
+ tsne = TSNE(n_components=2, random_state=42, perplexity=perplexity)
66
  embeddings_2d = tsne.fit_transform(embeddings)
67
  df = pd.DataFrame(embeddings_2d, columns=['x', 'y'])
68
  df['word'] = words
 
98
  st.write("Word list (not enough words for t-SNE visualization):")
99
  st.write(", ".join(words))
100
 
101
+ if len(words) > 1:
102
+ with st.spinner("Extracting topics..."):
103
+ texts = [user_input, "Another text to improve topic modeling."]
104
+ topic_distr, vectorizer = topic_modeling_agent.fit_transform(texts, lang)
105
+ topics = topic_modeling_agent.get_topics(vectorizer)
106
+ st.subheader("Topics Extracted:")
107
+ for topic, topic_words in topics.items():
108
+ st.write(f"Topic {topic}: {', '.join(topic_words)}")
109
+
110
+ with st.spinner("Computing similarity..."):
111
+ text2 = "Otro texto de ejemplo para comparaci贸n de similitud." if lang != 'en' else "Another example text for similarity comparison."
112
+ similarity_score = similarity_agent.compute_similarity(user_input, text2)
113
+ st.write(f"Similarity Score with example text: {similarity_score:.4f}")
114
+ else:
115
+ st.warning("Not enough words for topic modeling and similarity comparison.")
116
 
117
  else:
118
  st.warning("Please enter some text to analyze.")