Update app.py
Browse files
app.py
CHANGED
@@ -1,81 +1,108 @@
|
|
1 |
import streamlit as st
|
2 |
-
import requests
|
3 |
-
from transformers import pipeline
|
4 |
from PIL import Image
|
5 |
-
import
|
|
|
|
|
6 |
import pandas as pd
|
7 |
-
import
|
|
|
8 |
|
9 |
-
#
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
-
|
13 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
-
|
23 |
-
st.
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
if
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
st.write(
|
50 |
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
if crypto:
|
58 |
-
st.subheader(f"Análisis Técnico para {crypto.upper()}")
|
59 |
-
|
60 |
-
# Llamada a la API de Coinalyze
|
61 |
-
endpoint = f"{COINALYZE_URL}/cryptocurrencies/{crypto}/historical"
|
62 |
-
headers = {"Authorization": f"Bearer {COINALYZE_API_KEY}"}
|
63 |
-
|
64 |
-
response = requests.get(endpoint, headers=headers)
|
65 |
-
if response.status_code == 200:
|
66 |
-
data = response.json()
|
67 |
-
df = pd.DataFrame(data["prices"], columns=["timestamp", "price"])
|
68 |
-
|
69 |
-
# Convertir timestamp a fecha
|
70 |
-
df["timestamp"] = pd.to_datetime(df["timestamp"], unit="ms")
|
71 |
-
|
72 |
-
# Graficar precios
|
73 |
-
fig = px.line(df, x="timestamp", y="price", title=f"Precio de {crypto.upper()} a lo largo del tiempo")
|
74 |
-
st.plotly_chart(fig)
|
75 |
-
else:
|
76 |
-
st.error("No se pudieron obtener los datos. Verifica el símbolo o intenta más tarde.")
|
77 |
|
78 |
-
|
79 |
-
|
80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
|
|
|
|
|
|
1 |
import streamlit as st
|
|
|
|
|
2 |
from PIL import Image
|
3 |
+
import pytesseract
|
4 |
+
import pandas_ta as ta
|
5 |
+
from textblob import TextBlob
|
6 |
import pandas as pd
|
7 |
+
import requests
|
8 |
+
from sklearn.linear_model import LinearRegression
|
9 |
|
10 |
+
# Helper Functions
|
11 |
+
def search_web(query):
|
12 |
+
from googlesearch import search
|
13 |
+
st.subheader("Resultados de la Búsqueda Web")
|
14 |
+
results = []
|
15 |
+
for result in search(query, num_results=5):
|
16 |
+
results.append(result)
|
17 |
+
for idx, link in enumerate(results):
|
18 |
+
st.write(f"{idx + 1}. {link}")
|
19 |
|
20 |
+
def analyze_image(uploaded_file):
|
21 |
+
st.subheader("Análisis de Imagen")
|
22 |
+
image = Image.open(uploaded_file)
|
23 |
+
st.image(image, caption="Imagen cargada", use_column_width=True)
|
24 |
+
text = pytesseract.image_to_string(image)
|
25 |
+
st.write("Texto extraído de la imagen:")
|
26 |
+
st.write(text)
|
27 |
|
28 |
+
def analyze_crypto_data(df):
|
29 |
+
st.subheader("Análisis Técnico")
|
30 |
+
df['RSI'] = ta.rsi(df['close'], length=14)
|
31 |
+
macd = ta.macd(df['close'])
|
32 |
+
df['MACD'], df['MACD_signal'], df['MACD_hist'] = macd['MACD_12_26_9'], macd['MACDs_12_26_9'], macd['MACDh_12_26_9']
|
33 |
+
bbands = ta.bbands(df['close'])
|
34 |
+
df['BB_Lower'], df['BB_Mid'], df['BB_Upper'] = bbands['BBL_20_2.0'], bbands['BBM_20_2.0'], bbands['BBU_20_2.0']
|
35 |
+
st.write(df.tail(10))
|
36 |
|
37 |
+
def analyze_sentiment(text):
|
38 |
+
analysis = TextBlob(text)
|
39 |
+
sentiment = analysis.sentiment.polarity
|
40 |
+
if sentiment > 0:
|
41 |
+
return "Positivo"
|
42 |
+
elif sentiment < 0:
|
43 |
+
return "Negativo"
|
44 |
+
else:
|
45 |
+
return "Neutral"
|
46 |
|
47 |
+
def predict_prices(df):
|
48 |
+
st.subheader("Predicción de Precios")
|
49 |
+
X = df.index.values.reshape(-1, 1)
|
50 |
+
y = df['close']
|
51 |
+
model = LinearRegression()
|
52 |
+
model.fit(X, y)
|
53 |
+
future = pd.DataFrame({"Index": range(len(df), len(df) + 5)})
|
54 |
+
predictions = model.predict(future)
|
55 |
+
st.write("Predicciones de precios futuros:", predictions)
|
56 |
|
57 |
+
def fetch_crypto_data():
|
58 |
+
url = "https://api.coingecko.com/api/v3/coins/bitcoin/market_chart?vs_currency=usd&days=30&interval=daily"
|
59 |
+
response = requests.get(url)
|
60 |
+
if response.status_code == 200:
|
61 |
+
data = response.json()
|
62 |
+
prices = [item[1] for item in data['prices']]
|
63 |
+
df = pd.DataFrame(prices, columns=['close'])
|
64 |
+
return df
|
65 |
+
else:
|
66 |
+
st.error("Error al obtener datos de criptomonedas.")
|
67 |
+
return None
|
68 |
+
|
69 |
+
def chat_interface():
|
70 |
+
st.header("Chat Interactivo")
|
71 |
+
user_input = st.text_input("Escribe tu mensaje aquí:")
|
72 |
+
if user_input:
|
73 |
+
st.write(f"Tú: {user_input}")
|
74 |
+
# Aquí agregarías lógica para procesar la entrada y responder
|
75 |
+
st.write("Chatbot: Lo siento, estoy aprendiendo a responder.")
|
76 |
|
77 |
+
# Main Application
|
78 |
+
def main():
|
79 |
+
st.title("Aplicación de Criptomonedas")
|
80 |
+
menu = ["Chat", "Búsqueda Web", "Análisis de Imágenes", "Análisis Técnico", "Análisis de Sentimiento", "Predicción de Precios"]
|
81 |
+
choice = st.sidebar.selectbox("Seleccione una opción", menu)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
|
83 |
+
if choice == "Chat":
|
84 |
+
chat_interface()
|
85 |
+
elif choice == "Búsqueda Web":
|
86 |
+
query = st.text_input("Ingrese su búsqueda:")
|
87 |
+
if query:
|
88 |
+
search_web(query)
|
89 |
+
elif choice == "Análisis de Imágenes":
|
90 |
+
uploaded_file = st.file_uploader("Suba una imagen", type=["png", "jpg", "jpeg"])
|
91 |
+
if uploaded_file:
|
92 |
+
analyze_image(uploaded_file)
|
93 |
+
elif choice == "Análisis Técnico":
|
94 |
+
df = fetch_crypto_data()
|
95 |
+
if df is not None:
|
96 |
+
analyze_crypto_data(df)
|
97 |
+
elif choice == "Análisis de Sentimiento":
|
98 |
+
text = st.text_area("Ingrese el texto para analizar:")
|
99 |
+
if text:
|
100 |
+
sentiment = analyze_sentiment(text)
|
101 |
+
st.write(f"El sentimiento del texto es: {sentiment}")
|
102 |
+
elif choice == "Predicción de Precios":
|
103 |
+
df = fetch_crypto_data()
|
104 |
+
if df is not None:
|
105 |
+
predict_prices(df)
|
106 |
|
107 |
+
if __name__ == "__main__":
|
108 |
+
main()
|