Spaces:
Runtime error
Runtime error
app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 2 |
+
import requests
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
# Load the model and tokenizer from Hugging Face
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained("LaierTwoLabsInc/Satoshi-7B")
|
| 7 |
+
model = AutoModelForCausalLM.from_pretrained("LaierTwoLabsInc/Satoshi-7B")
|
| 8 |
+
|
| 9 |
+
# Function to fetch BTC price from CoinGecko API
|
| 10 |
+
def fetch_btc_price():
|
| 11 |
+
url = "https://api.coingecko.com/api/v3/simple/price"
|
| 12 |
+
params = {'ids': 'bitcoin', 'vs_currencies': 'usd'}
|
| 13 |
+
response = requests.get(url, params=params)
|
| 14 |
+
if response.status_code == 200:
|
| 15 |
+
data = response.json()
|
| 16 |
+
return data['bitcoin']['usd']
|
| 17 |
+
return None
|
| 18 |
+
|
| 19 |
+
# Function to generate a response based on the prompt
|
| 20 |
+
def generate_custom_response(prompt):
|
| 21 |
+
# Encode the input prompt
|
| 22 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 23 |
+
|
| 24 |
+
# Generate a response from the model
|
| 25 |
+
outputs = model.generate(inputs['input_ids'], max_length=200, num_return_sequences=1)
|
| 26 |
+
|
| 27 |
+
# Decode the generated response
|
| 28 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 29 |
+
return response
|
| 30 |
+
|
| 31 |
+
# Function to fetch BTC price and generate analysis based on the prompt
|
| 32 |
+
def btc_analysis(prompt):
|
| 33 |
+
btc_price = fetch_btc_price()
|
| 34 |
+
if btc_price:
|
| 35 |
+
full_prompt = f"Bitcoin's current price is ${btc_price}. {prompt}"
|
| 36 |
+
ai_response = generate_custom_response(full_prompt)
|
| 37 |
+
return ai_response
|
| 38 |
+
else:
|
| 39 |
+
return "Error fetching Bitcoin price."
|
| 40 |
+
|
| 41 |
+
# Gradio Interface for BTC analysis
|
| 42 |
+
interface = gr.Interface(
|
| 43 |
+
fn=btc_analysis,
|
| 44 |
+
inputs=gr.Textbox(value="What does this price mean for investors and the market?", label="Prompt"),
|
| 45 |
+
outputs="text",
|
| 46 |
+
title="Bitcoin Price Analysis",
|
| 47 |
+
description="Fetch Bitcoin's current price and get analysis based on the provided prompt using Hugging Face's model."
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
# Launch the Gradio app
|
| 51 |
+
interface.launch()
|