Spaces:
Sleeping
Sleeping
import gradio as gr | |
import pandas as pd | |
import yfinance as yf | |
from huggingface_hub import InferenceClient | |
# Initialize the Inference Client for the chatbot | |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta") | |
# Function for the chatbot response | |
def respond( | |
message, | |
history: list[tuple[str, str]], | |
system_message, | |
max_tokens, | |
temperature, | |
top_p, | |
): | |
messages = [{"role": "system", "content": system_message}] | |
for val in history: | |
if val[0]: | |
messages.append({"role": "user", "content": val[0]}) | |
if val[1]: | |
messages.append({"role": "assistant", "content": val[1]}) | |
messages.append({"role": "user", "content": message}) | |
response = "" | |
for message in client.chat_completion( | |
messages, | |
max_tokens=max_tokens, | |
stream=True, | |
temperature=temperature, | |
top_p=top_p, | |
): | |
token = message.choices[0].delta.content | |
response += token | |
yield response | |
# Function for the trading screener | |
def trading_screener(price_threshold, volume_threshold): | |
stocks = ["AAPL", "MSFT", "GOOGL", "AMZN", "TSLA"] | |
data = [] | |
for stock in stocks: | |
ticker = yf.Ticker(stock) | |
hist = ticker.history(period="1d") | |
current_price = hist['Close'].iloc[-1] | |
avg_volume = hist['Volume'].mean() | |
data.append({"Stock": stock, "Price": current_price, "Avg Volume": avg_volume}) | |
df = pd.DataFrame(data) | |
filtered_df = df[(df['Price'] > price_threshold) & (df['Avg Volume'] > volume_threshold)] | |
return filtered_df | |
# Create the Gradio interface | |
with gr.Blocks() as demo: | |
gr.Markdown("# Trading Screener and Chatbot") | |
# Trading Screener Section | |
with gr.Row(): | |
with gr.Column(): | |
gr.Markdown("## Trading Screener") | |
price_threshold = gr.Number(label="Price Threshold", value=100.0) | |
volume_threshold = gr.Number(label="Volume Threshold", value=1000000) | |
submit_btn = gr.Button("Run Screener") | |
output_df = gr.Dataframe(label="Filtered Stocks") | |
submit_btn.click(fn=trading_screener, inputs=[price_threshold, volume_threshold], outputs=output_df) | |
# Chatbot Section | |
gr.Markdown("## Chatbot") | |
chat_interface = gr.ChatInterface( | |
respond, | |
additional_inputs=[ | |
gr.Textbox(value="You are a friendly Chatbot.", label="System message"), | |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"), | |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"), | |
gr.Slider( | |
minimum=0.1, | |
maximum=1.0, | |
value=0.95, | |
step=0.05, | |
label="Top-p (nucleus sampling)", | |
), | |
], | |
) | |
# Launch the app | |
if __name__ == "__main__": | |
demo.launch() |