Spaces:
Sleeping
Sleeping
Commit ·
d536a5f
1
Parent(s): 16bf3b3
Update app.py
Browse files
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
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|