salomonsky commited on
Commit
d536a5f
·
1 Parent(s): 16bf3b3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -44
app.py CHANGED
@@ -1,47 +1,61 @@
1
  import streamlit as st
2
  from wordcloud import WordCloud
3
- import numpy as np
4
  import matplotlib.pyplot as plt
5
- from plotly.subplots import make_subplots
6
- import plotly.graph_objects as go
7
-
8
- st.title("Nube de palabras 3D")
9
-
10
- text = st.text_area("Introduce hasta 20 palabras separadas por espacio:", "Ejemplo:Python es genial.")
11
-
12
- words = text.strip().split()[:20]
13
- wordcloud = WordCloud(width=800, height=400, random_state=1).generate(" ".join(words))
14
-
15
- fig = make_subplots(rows=1, cols=1, specs=[[{"type": "scene"}]])
16
-
17
- x, y = np.random.rand(500), np.random.rand(500)
18
- colors = np.array([(i/len(words), i/len(words)/2, 1.) for i in range(len(words))])
19
-
20
- scatter = go.Scatter3d(
21
- x=x, y=y, z=np.zeros_like(x),
22
- mode='markers',
23
- marker={
24
- 'size': 12,
25
- 'color': colors,
26
- 'opacity': 0.9,
27
- 'line': {'width': 2, 'color': 'black'}
28
- },
29
- name="Palabras",
30
- text=words,
31
- hoverinfo='text'
32
- )
33
-
34
- fig.add_trace(scatter)
35
- fig.update_layout(
36
- scene=dict(
37
- xaxis_title='X-Axis',
38
- yaxis_title='Y-Axis',
39
- zaxis_title='Z-Axis',
40
- aspectmode='data'
41
- ),
42
- title="Nube de palabras 3D",
43
- width=700, height=600,
44
- showlegend=False
45
- )
46
-
47
- st.write(fig, plugins=["sankey3d"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  from wordcloud import WordCloud
 
3
  import matplotlib.pyplot as plt
4
+ from mpl_toolkits.mplot3d import Axes3D
5
+ from sklearn.feature_extraction.text import ENGLISH_STOP_WORDS
6
+ import networkx as nx
7
+ import numpy as np
8
+ from huggingface_hub import InferenceClient
9
+
10
+ client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
11
+
12
+ def format_prompt(message, history):
13
+ prompt = "<s>"
14
+ for user_prompt, bot_response in history:
15
+ prompt += f"[INST] {user_prompt} [/INST]"
16
+ prompt += f" {bot_response}</s> "
17
+ prompt += f"[INST] {message} [/INST]"
18
+ return prompt
19
+
20
+ def generate_wordcloud(text):
21
+ wordcloud = WordCloud(stopwords=ENGLISH_STOP_WORDS, background_color='white', width=800, height=400, max_words=20).generate(text)
22
+
23
+ fig = plt.figure(figsize=(10, 5))
24
+ ax = fig.add_subplot(111, projection='3d')
25
+
26
+ frequencies = wordcloud.words_
27
+
28
+ words = list(frequencies.keys())
29
+ sizes = list(frequencies.values())
30
+ colors = [hash(word) % 100 for word in words]
31
+
32
+ ax.scatter(words, sizes, colors, marker='o', s=sizes, depthshade=True)
33
+
34
+ ax.set_xlabel('Palabra')
35
+ ax.set_ylabel('Frecuencia')
36
+ ax.set_zlabel('Color')
37
+
38
+ ax.set_title('Nube de Palabras en 3D')
39
+
40
+ # Conectar palabras con líneas
41
+ G = nx.Graph()
42
+ for i, word in enumerate(words):
43
+ G.add_node(word)
44
+ for j in range(i + 1, len(words)):
45
+ G.add_edge(word, words[j])
46
+
47
+ pos = {word: (sizes[i], colors[i]) for i, word in enumerate(words)}
48
+ nx.draw(G, pos, ax=ax, with_labels=True, font_weight='bold', node_size=sizes, node_color=colors, font_size=8, edge_color='gray', alpha=0.5)
49
+
50
+ st.pyplot(fig)
51
+
52
+ def main():
53
+ st.title('Generador de Nube de Palabras en 3D')
54
+
55
+ input_text = st.text_area('Ingrese el texto:', height=200)
56
+
57
+ if st.button('Generar Nube de Palabras'):
58
+ generate_wordcloud(input_text)
59
+
60
+ if __name__ == '__main__':
61
+ main()