import torch from transformers import FinBERTTokenizer, FinBERTForSequenceClassification import yfinance as yf import gradio as gr # Load the FinBERT tokenizer and model tokenizer = FinBERTTokenizer.from_pretrained('yiyanghkust/finbert-tone', model_name_or_path='yiyanghkust/finbert-tone') model = FinBERTForSequenceClassification.from_pretrained('yiyanghkust/finbert-tone', model_name_or_path='yiyanghkust/finbert-tone') def predict_stock_price(day, month, year, input_text): # Get the stock symbol from the input text stock_symbol = extract_stock_symbol(input_text) # Format the date for yfinance start_date = f"{year}-{month:02d}-{day:02d}" end_date = f"{year}-{month:02d}-{day:02d}" # Get the historical stock data using yfinance stock_data = yf.download(stock_symbol, start=start_date, end=end_date) # Perform any necessary data preprocessing # Tokenize the input text with appropriate keyword arguments inputs = tokenizer(input_text, return_tensors="pt") # Forward pass through the model outputs = model(**inputs) # Get the predicted stock price predicted_price = outputs.logits.item() return predicted_price def extract_stock_symbol(input_text): # Implement the logic to extract the stock symbol from the input text # For example, you can use regular expressions or a custom parsing algorithm # For simplicity, let's assume the stock symbol is the first word in the input text return input_text.split()[0] # Define the Gradio interface inputs = [ gr.inputs.Number(label="Day", min_value=1, max_value=31), gr.inputs.Number(label="Month", min_value=1, max_value=12), gr.inputs.Number(label="Year", min_value=2000, max_value=2022), gr.inputs.Textbox(label="Input Text") ] output = gr.outputs.Textbox(label="Predicted Stock Price") gr.Interface(fn=predict_stock_price, inputs=inputs, outputs=output).launch()