Spaces:
Sleeping
Sleeping
| 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() | |