Barto517 commited on
Commit
f3fb038
·
verified ·
1 Parent(s): 90c6175

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -25
app.py CHANGED
@@ -6,6 +6,7 @@ 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):
@@ -27,34 +28,51 @@ def analyze_image(uploaded_file):
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:
@@ -67,39 +85,50 @@ def fetch_crypto_data():
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)
 
6
  import pandas as pd
7
  import requests
8
  from sklearn.linear_model import LinearRegression
9
+ from sklearn.preprocessing import PolynomialFeatures
10
 
11
  # Helper Functions
12
  def search_web(query):
 
28
 
29
  def analyze_crypto_data(df):
30
  st.subheader("Análisis Técnico")
31
+ try:
32
+ df['RSI'] = ta.rsi(df['close'], length=14)
33
+ macd = ta.macd(df['close'])
34
+ if macd is not None:
35
+ df['MACD'], df['MACD_signal'], df['MACD_hist'] = (
36
+ macd['MACD_12_26_9'], macd['MACDs_12_26_9'], macd['MACDh_12_26_9']
37
+ )
38
+ bbands = ta.bbands(df['close'])
39
+ if bbands is not None:
40
+ df['BB_Lower'], df['BB_Mid'], df['BB_Upper'] = (
41
+ bbands['BBL_20_2.0'], bbands['BBM_20_2.0'], bbands['BBU_20_2.0']
42
+ )
43
+ st.write(df.tail(10))
44
+ except Exception as e:
45
+ st.error(f"Error en el análisis técnico: {e}")
46
 
47
  def analyze_sentiment(text):
48
+ st.subheader("Análisis de Sentimiento")
49
  analysis = TextBlob(text)
50
  sentiment = analysis.sentiment.polarity
51
  if sentiment > 0:
52
+ st.write("El sentimiento del texto es: **Positivo**")
53
  elif sentiment < 0:
54
+ st.write("El sentimiento del texto es: **Negativo**")
55
  else:
56
+ st.write("El sentimiento del texto es: **Neutral**")
57
 
58
  def predict_prices(df):
59
  st.subheader("Predicción de Precios")
60
+ try:
61
+ X = df.index.values.reshape(-1, 1)
62
+ y = df['close']
63
+ poly = PolynomialFeatures(degree=2)
64
+ X_poly = poly.fit_transform(X)
65
+ model = LinearRegression()
66
+ model.fit(X_poly, y)
67
+ future = pd.DataFrame(range(len(df), len(df) + 5), columns=['Index'])
68
+ future_poly = poly.transform(future)
69
+ predictions = model.predict(future_poly)
70
+ st.write("Predicciones de precios futuros:", predictions)
71
+ except Exception as e:
72
+ st.error(f"Error en la predicción de precios: {e}")
73
 
74
  def fetch_crypto_data():
75
+ st.subheader("Obtención de Datos de Criptomonedas")
76
  url = "https://api.coingecko.com/api/v3/coins/bitcoin/market_chart?vs_currency=usd&days=30&interval=daily"
77
  response = requests.get(url)
78
  if response.status_code == 200:
 
85
  return None
86
 
87
  def chat_interface():
88
+ st.title("Chat Cripto Analizador")
89
+ st.write("¡Pregúntame algo!")
90
+ user_input = st.text_input("Tu mensaje:", placeholder="Escribe aquí...")
91
  if user_input:
92
+ if "crypto" in user_input.lower():
93
+ st.write(f"Tú: {user_input}")
94
+ st.write("Bot: Estoy listo para analizar criptomonedas. Intenta seleccionar 'Análisis Técnico' desde la barra lateral.")
95
+ else:
96
+ st.write(f"Tú: {user_input}")
97
+ st.write("Bot: Aún estoy aprendiendo. Por ahora puedo analizar imágenes y criptomonedas.")
98
 
99
  # Main Application
100
  def main():
101
+ st.sidebar.title("Menú")
102
+ menu = [
103
+ "Chat", "Búsqueda Web", "Análisis de Imágenes",
104
+ "Análisis Técnico", "Análisis de Sentimiento", "Predicción de Precios"
105
+ ]
106
  choice = st.sidebar.selectbox("Seleccione una opción", menu)
107
 
108
  if choice == "Chat":
109
  chat_interface()
110
  elif choice == "Búsqueda Web":
111
+ st.header("Búsqueda Web")
112
  query = st.text_input("Ingrese su búsqueda:")
113
  if query:
114
  search_web(query)
115
  elif choice == "Análisis de Imágenes":
116
+ st.header("Análisis de Imágenes")
117
  uploaded_file = st.file_uploader("Suba una imagen", type=["png", "jpg", "jpeg"])
118
  if uploaded_file:
119
  analyze_image(uploaded_file)
120
  elif choice == "Análisis Técnico":
121
+ st.header("Análisis Técnico de Criptomonedas")
122
  df = fetch_crypto_data()
123
  if df is not None:
124
  analyze_crypto_data(df)
125
  elif choice == "Análisis de Sentimiento":
126
+ st.header("Análisis de Sentimiento")
127
  text = st.text_area("Ingrese el texto para analizar:")
128
  if text:
129
+ analyze_sentiment(text)
 
130
  elif choice == "Predicción de Precios":
131
+ st.header("Predicción de Precios de Criptomonedas")
132
  df = fetch_crypto_data()
133
  if df is not None:
134
  predict_prices(df)