File size: 1,910 Bytes
69edce9
 
 
d536a5f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from wordcloud import WordCloud
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from sklearn.feature_extraction.text import ENGLISH_STOP_WORDS
import networkx as nx
import numpy as np
from huggingface_hub import InferenceClient

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_wordcloud(text):
    wordcloud = WordCloud(stopwords=ENGLISH_STOP_WORDS, background_color='white', width=800, height=400, max_words=20).generate(text)

    fig = plt.figure(figsize=(10, 5))
    ax = fig.add_subplot(111, projection='3d')

    frequencies = wordcloud.words_

    words = list(frequencies.keys())
    sizes = list(frequencies.values())
    colors = [hash(word) % 100 for word in words]

    ax.scatter(words, sizes, colors, marker='o', s=sizes, depthshade=True)

    ax.set_xlabel('Palabra')
    ax.set_ylabel('Frecuencia')
    ax.set_zlabel('Color')

    ax.set_title('Nube de Palabras en 3D')

    # Conectar palabras con líneas
    G = nx.Graph()
    for i, word in enumerate(words):
        G.add_node(word)
        for j in range(i + 1, len(words)):
            G.add_edge(word, words[j])

    pos = {word: (sizes[i], colors[i]) for i, word in enumerate(words)}
    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)

    st.pyplot(fig)

def main():
    st.title('Generador de Nube de Palabras en 3D')

    input_text = st.text_area('Ingrese el texto:', height=200)

    if st.button('Generar Nube de Palabras'):
        generate_wordcloud(input_text)

if __name__ == '__main__':
    main()