|
import streamlit as st |
|
from PIL import Image |
|
import pytesseract |
|
import pandas_ta as ta |
|
from textblob import TextBlob |
|
import pandas as pd |
|
import requests |
|
from sklearn.linear_model import LinearRegression |
|
from sklearn.preprocessing import PolynomialFeatures |
|
|
|
|
|
def search_web(query): |
|
from googlesearch import search |
|
st.subheader("Resultados de la Búsqueda Web") |
|
results = [] |
|
for result in search(query, num_results=5): |
|
results.append(result) |
|
for idx, link in enumerate(results): |
|
st.write(f"{idx + 1}. {link}") |
|
|
|
def analyze_image(uploaded_file): |
|
st.subheader("Análisis de Imagen") |
|
image = Image.open(uploaded_file) |
|
st.image(image, caption="Imagen cargada", use_column_width=True) |
|
text = pytesseract.image_to_string(image) |
|
st.write("Texto extraído de la imagen:") |
|
st.write(text) |
|
|
|
def analyze_crypto_data(df): |
|
st.subheader("Análisis Técnico") |
|
try: |
|
df['RSI'] = ta.rsi(df['close'], length=14) |
|
macd = ta.macd(df['close']) |
|
if macd is not None: |
|
df['MACD'], df['MACD_signal'], df['MACD_hist'] = ( |
|
macd['MACD_12_26_9'], macd['MACDs_12_26_9'], macd['MACDh_12_26_9'] |
|
) |
|
bbands = ta.bbands(df['close']) |
|
if bbands is not None: |
|
df['BB_Lower'], df['BB_Mid'], df['BB_Upper'] = ( |
|
bbands['BBL_20_2.0'], bbands['BBM_20_2.0'], bbands['BBU_20_2.0'] |
|
) |
|
st.write(df.tail(10)) |
|
except Exception as e: |
|
st.error(f"Error en el análisis técnico: {e}") |
|
|
|
def analyze_sentiment(text): |
|
st.subheader("Análisis de Sentimiento") |
|
analysis = TextBlob(text) |
|
sentiment = analysis.sentiment.polarity |
|
if sentiment > 0: |
|
st.write("El sentimiento del texto es: **Positivo**") |
|
elif sentiment < 0: |
|
st.write("El sentimiento del texto es: **Negativo**") |
|
else: |
|
st.write("El sentimiento del texto es: **Neutral**") |
|
|
|
def predict_prices(df): |
|
st.subheader("Predicción de Precios") |
|
try: |
|
X = df.index.values.reshape(-1, 1) |
|
y = df['close'] |
|
poly = PolynomialFeatures(degree=2) |
|
X_poly = poly.fit_transform(X) |
|
model = LinearRegression() |
|
model.fit(X_poly, y) |
|
future = pd.DataFrame(range(len(df), len(df) + 5), columns=['Index']) |
|
future_poly = poly.transform(future) |
|
predictions = model.predict(future_poly) |
|
st.write("Predicciones de precios futuros:", predictions) |
|
except Exception as e: |
|
st.error(f"Error en la predicción de precios: {e}") |
|
|
|
def fetch_crypto_data(): |
|
st.subheader("Obtención de Datos de Criptomonedas") |
|
url = "https://api.coingecko.com/api/v3/coins/bitcoin/market_chart?vs_currency=usd&days=30&interval=daily" |
|
response = requests.get(url) |
|
if response.status_code == 200: |
|
data = response.json() |
|
prices = [item[1] for item in data['prices']] |
|
df = pd.DataFrame(prices, columns=['close']) |
|
return df |
|
else: |
|
st.error("Error al obtener datos de criptomonedas.") |
|
return None |
|
|
|
def chat_interface(): |
|
st.title("Chat Cripto Analizador") |
|
st.write("¡Pregúntame algo!") |
|
user_input = st.text_input("Tu mensaje:", placeholder="Escribe aquí...") |
|
if user_input: |
|
if "crypto" in user_input.lower(): |
|
st.write(f"Tú: {user_input}") |
|
st.write("Bot: Estoy listo para analizar criptomonedas. Intenta seleccionar 'Análisis Técnico' desde la barra lateral.") |
|
else: |
|
st.write(f"Tú: {user_input}") |
|
st.write("Bot: Aún estoy aprendiendo. Por ahora puedo analizar imágenes y criptomonedas.") |
|
|
|
|
|
def main(): |
|
st.sidebar.title("Menú") |
|
menu = [ |
|
"Chat", "Búsqueda Web", "Análisis de Imágenes", |
|
"Análisis Técnico", "Análisis de Sentimiento", "Predicción de Precios" |
|
] |
|
choice = st.sidebar.selectbox("Seleccione una opción", menu) |
|
|
|
if choice == "Chat": |
|
chat_interface() |
|
elif choice == "Búsqueda Web": |
|
st.header("Búsqueda Web") |
|
query = st.text_input("Ingrese su búsqueda:") |
|
if query: |
|
search_web(query) |
|
elif choice == "Análisis de Imágenes": |
|
st.header("Análisis de Imágenes") |
|
uploaded_file = st.file_uploader("Suba una imagen", type=["png", "jpg", "jpeg"]) |
|
if uploaded_file: |
|
analyze_image(uploaded_file) |
|
elif choice == "Análisis Técnico": |
|
st.header("Análisis Técnico de Criptomonedas") |
|
df = fetch_crypto_data() |
|
if df is not None: |
|
analyze_crypto_data(df) |
|
elif choice == "Análisis de Sentimiento": |
|
st.header("Análisis de Sentimiento") |
|
text = st.text_area("Ingrese el texto para analizar:") |
|
if text: |
|
analyze_sentiment(text) |
|
elif choice == "Predicción de Precios": |
|
st.header("Predicción de Precios de Criptomonedas") |
|
df = fetch_crypto_data() |
|
if df is not None: |
|
predict_prices(df) |
|
|
|
if __name__ == "__main__": |
|
main() |
|
|
|
|