Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,115 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from PIL import Image
|
| 3 |
import pytesseract
|
| 4 |
-
from numpy import nan as NaN # Parche para pandas-ta
|
| 5 |
-
import pandas_ta as ta
|
| 6 |
-
from textblob import TextBlob
|
| 7 |
import pandas as pd
|
| 8 |
import requests
|
|
|
|
| 9 |
from sklearn.linear_model import LinearRegression
|
| 10 |
from sklearn.preprocessing import PolynomialFeatures
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
#
|
| 16 |
def main():
|
| 17 |
-
st.
|
| 18 |
menu = [
|
| 19 |
"Chat", "Búsqueda Web", "Análisis de Imágenes",
|
| 20 |
"Análisis Técnico", "Análisis de Sentimiento", "Predicción de Precios"
|
|
@@ -24,27 +119,23 @@ def main():
|
|
| 24 |
if choice == "Chat":
|
| 25 |
chat_interface()
|
| 26 |
elif choice == "Búsqueda Web":
|
| 27 |
-
st.header("Búsqueda Web")
|
| 28 |
query = st.text_input("Ingrese su búsqueda:")
|
| 29 |
if query:
|
| 30 |
search_web(query)
|
| 31 |
elif choice == "Análisis de Imágenes":
|
| 32 |
-
st.header("Análisis de Imágenes")
|
| 33 |
uploaded_file = st.file_uploader("Suba una imagen", type=["png", "jpg", "jpeg"])
|
| 34 |
if uploaded_file:
|
| 35 |
analyze_image(uploaded_file)
|
| 36 |
elif choice == "Análisis Técnico":
|
| 37 |
-
st.header("Análisis Técnico de Criptomonedas")
|
| 38 |
df = fetch_crypto_data()
|
| 39 |
if df is not None:
|
| 40 |
analyze_crypto_data(df)
|
| 41 |
elif choice == "Análisis de Sentimiento":
|
| 42 |
-
st.header("Análisis de Sentimiento")
|
| 43 |
text = st.text_area("Ingrese el texto para analizar:")
|
| 44 |
if text:
|
| 45 |
-
analyze_sentiment(text)
|
|
|
|
| 46 |
elif choice == "Predicción de Precios":
|
| 47 |
-
st.header("Predicción de Precios de Criptomonedas")
|
| 48 |
df = fetch_crypto_data()
|
| 49 |
if df is not None:
|
| 50 |
predict_prices(df)
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from PIL import Image
|
| 3 |
import pytesseract
|
|
|
|
|
|
|
|
|
|
| 4 |
import pandas as pd
|
| 5 |
import requests
|
| 6 |
+
from textblob import TextBlob
|
| 7 |
from sklearn.linear_model import LinearRegression
|
| 8 |
from sklearn.preprocessing import PolynomialFeatures
|
| 9 |
+
import talib # Análisis técnico
|
| 10 |
+
|
| 11 |
+
# Función para búsqueda web
|
| 12 |
+
def search_web(query):
|
| 13 |
+
from googlesearch import search
|
| 14 |
+
st.subheader("Resultados de la Búsqueda Web")
|
| 15 |
+
results = []
|
| 16 |
+
for result in search(query, num_results=5):
|
| 17 |
+
results.append(result)
|
| 18 |
+
for idx, link in enumerate(results):
|
| 19 |
+
st.write(f"{idx + 1}. {link}")
|
| 20 |
+
|
| 21 |
+
# Función para análisis de imágenes
|
| 22 |
+
def analyze_image(uploaded_file):
|
| 23 |
+
st.subheader("Análisis de Imagen")
|
| 24 |
+
image = Image.open(uploaded_file)
|
| 25 |
+
st.image(image, caption="Imagen cargada", use_column_width=True)
|
| 26 |
+
text = pytesseract.image_to_string(image)
|
| 27 |
+
st.write("Texto extraído de la imagen:")
|
| 28 |
+
st.write(text)
|
| 29 |
+
sentiment = analyze_sentiment(text)
|
| 30 |
+
st.write(f"Análisis de sentimiento del texto extraído: {sentiment}")
|
| 31 |
+
|
| 32 |
+
# Función para análisis técnico
|
| 33 |
+
def analyze_crypto_data(df):
|
| 34 |
+
st.subheader("Análisis Técnico")
|
| 35 |
+
try:
|
| 36 |
+
# RSI
|
| 37 |
+
df['RSI'] = talib.RSI(df['close'], timeperiod=14)
|
| 38 |
+
|
| 39 |
+
# MACD
|
| 40 |
+
macd, macd_signal, macd_hist = talib.MACD(
|
| 41 |
+
df['close'], fastperiod=12, slowperiod=26, signalperiod=9
|
| 42 |
+
)
|
| 43 |
+
df['MACD'] = macd
|
| 44 |
+
df['MACD_signal'] = macd_signal
|
| 45 |
+
df['MACD_hist'] = macd_hist
|
| 46 |
+
|
| 47 |
+
# Bandas de Bollinger
|
| 48 |
+
upper, middle, lower = talib.BBANDS(
|
| 49 |
+
df['close'], timeperiod=20, nbdevup=2, nbdevdn=2, matype=0
|
| 50 |
+
)
|
| 51 |
+
df['BB_Upper'] = upper
|
| 52 |
+
df['BB_Mid'] = middle
|
| 53 |
+
df['BB_Lower'] = lower
|
| 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 |
+
return "Neutral"
|
| 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"
|
|
|
|
| 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)
|