app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import requests
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import plotly.graph_objs as go
|
| 6 |
+
from transformers import pipeline
|
| 7 |
+
|
| 8 |
+
# Load ChatGPT model (adjust to use a model supported in Hugging Face Spaces)
|
| 9 |
+
chatgpt = pipeline("text-generation", model="gpt2") # Change "gpt2" to "chatgpt" if available
|
| 10 |
+
|
| 11 |
+
# Function to fetch and process data from GPT model
|
| 12 |
+
def fetch_and_process_data(prompt):
|
| 13 |
+
response = chatgpt(prompt, max_length=200, do_sample=True)[0]['generated_text']
|
| 14 |
+
return response
|
| 15 |
+
|
| 16 |
+
# Function to fetch historical price data from CoinGecko
|
| 17 |
+
def fetch_historical_data(coin_id, from_timestamp, to_timestamp):
|
| 18 |
+
url = f"https://api.coingecko.com/api/v3/coins/{coin_id}/market_chart/range?vs_currency=usd&from={from_timestamp}&to={to_timestamp}"
|
| 19 |
+
response = requests.get(url)
|
| 20 |
+
if response.status_code == 200:
|
| 21 |
+
data = response.json()
|
| 22 |
+
prices = data['prices']
|
| 23 |
+
return prices
|
| 24 |
+
else:
|
| 25 |
+
return f"Error fetching historical data for {coin_id}"
|
| 26 |
+
|
| 27 |
+
# Function to convert dates to timestamps
|
| 28 |
+
def date_to_timestamp(date_str):
|
| 29 |
+
return int(pd.Timestamp(date_str).timestamp())
|
| 30 |
+
|
| 31 |
+
# Function to plot historical prices using Plotly
|
| 32 |
+
def plot_historical_prices(coin_name, from_date, to_date):
|
| 33 |
+
from_timestamp = date_to_timestamp(from_date)
|
| 34 |
+
to_timestamp = date_to_timestamp(to_date)
|
| 35 |
+
|
| 36 |
+
prices = fetch_historical_data(coin_name, from_timestamp, to_timestamp)
|
| 37 |
+
|
| 38 |
+
if isinstance(prices, str): # In case of error
|
| 39 |
+
return prices
|
| 40 |
+
|
| 41 |
+
df = pd.DataFrame(prices, columns=['timestamp', 'price'])
|
| 42 |
+
df['date'] = pd.to_datetime(df['timestamp'], unit='ms')
|
| 43 |
+
|
| 44 |
+
fig = go.Figure()
|
| 45 |
+
fig.add_trace(go.Scatter(x=df['date'], y=df['price'], mode='lines', name=coin_name))
|
| 46 |
+
fig.update_layout(title=f'{coin_name.capitalize()} Prices from {from_date} to {to_date}', xaxis_title='Date', yaxis_title='Price (USD)')
|
| 47 |
+
return fig
|
| 48 |
+
|
| 49 |
+
# Top 100 Cryptocurrencies (by CoinGecko IDs)
|
| 50 |
+
top_100_cryptos = [
|
| 51 |
+
'bitcoin', 'ethereum', 'binancecoin', 'ripple', 'solana', 'cardano', 'dogecoin', 'polygon', 'polkadot', 'tron',
|
| 52 |
+
# Add more top coins as necessary
|
| 53 |
+
]
|
| 54 |
+
|
| 55 |
+
# Function to display both ChatGPT response and price chart
|
| 56 |
+
def combined_analysis(prompt, coin_name, from_date, to_date):
|
| 57 |
+
# Fetch ChatGPT response
|
| 58 |
+
chatgpt_response = fetch_and_process_data(prompt)
|
| 59 |
+
|
| 60 |
+
# Fetch and plot historical price data
|
| 61 |
+
price_chart = plot_historical_prices(coin_name, from_date, to_date)
|
| 62 |
+
|
| 63 |
+
return chatgpt_response, price_chart
|
| 64 |
+
|
| 65 |
+
# Create Gradio Interface
|
| 66 |
+
interface = gr.Interface(
|
| 67 |
+
fn=combined_analysis,
|
| 68 |
+
inputs=[
|
| 69 |
+
gr.Textbox(label="Enter a prompt for ChatGPT"),
|
| 70 |
+
gr.Dropdown(choices=top_100_cryptos, label="Select Cryptocurrency"),
|
| 71 |
+
gr.Textbox(value="2024-01-01", label="From Date (YYYY-MM-DD)"),
|
| 72 |
+
gr.Textbox(value="2025-12-31", label="To Date (YYYY-MM-DD)")
|
| 73 |
+
],
|
| 74 |
+
outputs=[
|
| 75 |
+
gr.Textbox(label="ChatGPT Response"),
|
| 76 |
+
gr.Plot(label="Cryptocurrency Price Chart")
|
| 77 |
+
],
|
| 78 |
+
title="ChatGPT and Cryptocurrency Analysis",
|
| 79 |
+
description="This tool provides real-time cryptocurrency analysis and allows you to interact with ChatGPT for insights."
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
# Launch Gradio app
|
| 83 |
+
interface.launch(server_name="0.0.0.0")
|