Update app.py
Browse files
app.py
CHANGED
|
@@ -1,145 +1,58 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
|
| 3 |
-
import pytesseract
|
| 4 |
import pandas as pd
|
| 5 |
-
import
|
| 6 |
-
from
|
| 7 |
-
from sklearn.
|
| 8 |
-
from
|
| 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 |
-
st.write("Últimos 10 datos de análisis técnico:")
|
| 56 |
-
st.write(df.tail(10))
|
| 57 |
-
except Exception as e:
|
| 58 |
-
st.error(f"Error en el análisis técnico: {e}")
|
| 59 |
-
|
| 60 |
-
# Función para análisis de sentimiento
|
| 61 |
-
def analyze_sentiment(text):
|
| 62 |
-
analysis = TextBlob(text)
|
| 63 |
-
sentiment = analysis.sentiment.polarity
|
| 64 |
-
if sentiment > 0:
|
| 65 |
-
return "Positivo"
|
| 66 |
-
elif sentiment < 0:
|
| 67 |
-
return "Negativo"
|
| 68 |
else:
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
# Función para predicción de precios
|
| 72 |
-
def predict_prices(df):
|
| 73 |
-
st.subheader("Predicción de Precios")
|
| 74 |
-
try:
|
| 75 |
-
X = df.index.values.reshape(-1, 1)
|
| 76 |
-
y = df['close']
|
| 77 |
-
model = LinearRegression()
|
| 78 |
-
model.fit(X, y)
|
| 79 |
-
|
| 80 |
-
future = pd.DataFrame({"Index": range(len(df), len(df) + 5)})
|
| 81 |
-
predictions = model.predict(future['Index'].values.reshape(-1, 1))
|
| 82 |
-
st.write("Predicciones de precios futuros:")
|
| 83 |
-
for i, pred in enumerate(predictions):
|
| 84 |
-
st.write(f"Día {len(df) + i + 1}: {pred:.2f} USD")
|
| 85 |
-
except Exception as e:
|
| 86 |
-
st.error(f"Error al realizar la predicción: {e}")
|
| 87 |
-
|
| 88 |
-
# Función para obtener datos de criptomonedas
|
| 89 |
-
def fetch_crypto_data():
|
| 90 |
-
url = "https://api.coingecko.com/api/v3/coins/bitcoin/market_chart?vs_currency=usd&days=30&interval=daily"
|
| 91 |
-
response = requests.get(url)
|
| 92 |
-
if response.status_code == 200:
|
| 93 |
-
data = response.json()
|
| 94 |
-
prices = [item[1] for item in data['prices']]
|
| 95 |
-
df = pd.DataFrame(prices, columns=['close'])
|
| 96 |
-
return df
|
| 97 |
-
else:
|
| 98 |
-
st.error("Error al obtener datos de criptomonedas.")
|
| 99 |
-
return None
|
| 100 |
-
|
| 101 |
-
# Interfaz del chat
|
| 102 |
-
def chat_interface():
|
| 103 |
-
st.header("Chat Interactivo")
|
| 104 |
-
user_input = st.text_input("Escribe tu mensaje aquí:")
|
| 105 |
-
if user_input:
|
| 106 |
-
st.write(f"Tú: {user_input}")
|
| 107 |
-
response = f"No estoy entrenado como ChatGPT, pero aquí estoy para ayudarte. Tú dijiste: {user_input}"
|
| 108 |
-
st.write(f"Chatbot: {response}")
|
| 109 |
-
|
| 110 |
-
# Función principal de la aplicación
|
| 111 |
-
def main():
|
| 112 |
-
st.title("Aplicación de Criptomonedas")
|
| 113 |
-
menu = [
|
| 114 |
-
"Chat", "Búsqueda Web", "Análisis de Imágenes",
|
| 115 |
-
"Análisis Técnico", "Análisis de Sentimiento", "Predicción de Precios"
|
| 116 |
-
]
|
| 117 |
-
choice = st.sidebar.selectbox("Seleccione una opción", menu)
|
| 118 |
-
|
| 119 |
-
if choice == "Chat":
|
| 120 |
-
chat_interface()
|
| 121 |
-
elif choice == "Búsqueda Web":
|
| 122 |
-
query = st.text_input("Ingrese su búsqueda:")
|
| 123 |
-
if query:
|
| 124 |
-
search_web(query)
|
| 125 |
-
elif choice == "Análisis de Imágenes":
|
| 126 |
-
uploaded_file = st.file_uploader("Suba una imagen", type=["png", "jpg", "jpeg"])
|
| 127 |
-
if uploaded_file:
|
| 128 |
-
analyze_image(uploaded_file)
|
| 129 |
-
elif choice == "Análisis Técnico":
|
| 130 |
-
df = fetch_crypto_data()
|
| 131 |
-
if df is not None:
|
| 132 |
-
analyze_crypto_data(df)
|
| 133 |
-
elif choice == "Análisis de Sentimiento":
|
| 134 |
-
text = st.text_area("Ingrese el texto para analizar:")
|
| 135 |
-
if text:
|
| 136 |
-
sentiment = analyze_sentiment(text)
|
| 137 |
-
st.write(f"El sentimiento del texto es: {sentiment}")
|
| 138 |
-
elif choice == "Predicción de Precios":
|
| 139 |
-
df = fetch_crypto_data()
|
| 140 |
-
if df is not None:
|
| 141 |
-
predict_prices(df)
|
| 142 |
|
| 143 |
-
|
| 144 |
-
|
| 145 |
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import numpy as np
|
|
|
|
| 3 |
import pandas as pd
|
| 4 |
+
from sklearn.ensemble import RandomForestClassifier
|
| 5 |
+
from sklearn.model_selection import train_test_split
|
| 6 |
+
from sklearn.metrics import accuracy_score
|
| 7 |
+
from transformers import pipeline
|
| 8 |
+
|
| 9 |
+
# Título de la aplicación
|
| 10 |
+
st.title("Aplicación Demo con Hugging Face")
|
| 11 |
+
|
| 12 |
+
# Introducción
|
| 13 |
+
st.write("""
|
| 14 |
+
## ¿Qué hace esta aplicación?
|
| 15 |
+
Esta aplicación muestra ejemplos básicos usando:
|
| 16 |
+
- Clasificación de datos usando Scikit-learn.
|
| 17 |
+
- Modelos de texto de Hugging Face con Transformers.
|
| 18 |
+
""")
|
| 19 |
+
|
| 20 |
+
# Clasificación de datos con Scikit-learn
|
| 21 |
+
st.header("Ejemplo: Clasificación de Datos")
|
| 22 |
+
data = pd.DataFrame({
|
| 23 |
+
'Feature 1': np.random.rand(100),
|
| 24 |
+
'Feature 2': np.random.rand(100),
|
| 25 |
+
'Label': np.random.choice([0, 1], size=100)
|
| 26 |
+
})
|
| 27 |
+
|
| 28 |
+
st.write("### Dataset")
|
| 29 |
+
st.write(data)
|
| 30 |
+
|
| 31 |
+
X = data[['Feature 1', 'Feature 2']]
|
| 32 |
+
y = data['Label']
|
| 33 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
|
| 34 |
+
model = RandomForestClassifier()
|
| 35 |
+
model.fit(X_train, y_train)
|
| 36 |
+
predictions = model.predict(X_test)
|
| 37 |
+
accuracy = accuracy_score(y_test, predictions)
|
| 38 |
+
|
| 39 |
+
st.write("### Precisión del Modelo:")
|
| 40 |
+
st.write(f"{accuracy:.2f}")
|
| 41 |
+
|
| 42 |
+
# Uso de un pipeline de Hugging Face
|
| 43 |
+
st.header("Ejemplo: Uso de Hugging Face Transformers")
|
| 44 |
+
st.write("Genera texto a partir de una entrada")
|
| 45 |
+
|
| 46 |
+
# Cargar un modelo de Hugging Face (distilgpt2 para generación de texto)
|
| 47 |
+
generator = pipeline("text-generation", model="distilgpt2")
|
| 48 |
+
input_text = st.text_input("Escribe un texto:")
|
| 49 |
+
if st.button("Generar Texto"):
|
| 50 |
+
if input_text:
|
| 51 |
+
result = generator(input_text, max_length=50, num_return_sequences=1)
|
| 52 |
+
st.write(result[0]['generated_text'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
else:
|
| 54 |
+
st.write("Por favor, escribe un texto para generar.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
+
# Final de la aplicación
|
| 57 |
+
st.write("Desarrollado por [TU VIEJA](https://huggingface.co/) 👨💻")
|
| 58 |
|