|
from flask import Flask, render_template, request, send_from_directory, url_for |
|
from datetime import datetime |
|
from langchain_community.llms import HuggingFaceHub |
|
from langchain.prompts import PromptTemplate |
|
import requests |
|
import json |
|
import nltk |
|
from textblob import TextBlob |
|
from nltk.tokenize import word_tokenize |
|
from nltk.stem import PorterStemmer |
|
from nltk.stem import WordNetLemmatizer |
|
|
|
app = Flask(__name__) |
|
|
|
|
|
with open('ai_chatbot_data.json', 'r') as file: |
|
json_data = json.load(file) |
|
|
|
with open('info.txt', 'r') as file: |
|
database_content = file.read() |
|
database_tag = database_content |
|
|
|
template = "Message: {message}\n\nConversation History: {history}\n\nDate and Time: {date_time}\n\nBitcoin Price: ${bitcoin_price}\n\nBitcoin history from 1-jan-2024 to today: {database_tag}\n\nYour system: {json_data}.\n\nResponse:" |
|
prompt = PromptTemplate(template=template, input_variables=["message","history", "date_time", "bitcoin_price", "database_tag", "json_data"]) |
|
conversation_history = [] |
|
|
|
def get_bitcoin_price(): |
|
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S") |
|
url = 'https://api.coindesk.com/v1/bpi/currentprice.json' |
|
response = requests.get(url) |
|
|
|
if response.status_code == 200: |
|
data = response.json() |
|
bitcoin_price = data['bpi']['USD']['rate'] |
|
return bitcoin_price, current_time |
|
else: |
|
return 'Error fetching data', current_time |
|
|
|
@app.route('/assets/<path:path>') |
|
def send_static(path): |
|
return send_from_directory('assets', path) |
|
|
|
@app.route('/') |
|
def index(): |
|
global conversation_history |
|
return render_template('index.html', conversation=conversation_history) |
|
|
|
@app.route('/submit', methods=['POST']) |
|
def submit(): |
|
user_input = request.json.get('user_input') |
|
|
|
tokens = word_tokenize(user_input) |
|
ps = PorterStemmer() |
|
stemmed_tokens = [ps.stem(token) for token in tokens] |
|
|
|
lemmatizer = WordNetLemmatizer() |
|
lemmatized_tokens = [lemmatizer.lemmatize(token) for token in tokens] |
|
|
|
sentiment = TextBlob(user_input).sentiment |
|
|
|
bitcoin_price, current_time = get_bitcoin_price() |
|
|
|
conversation_history.append("User: " + user_input) |
|
|
|
|
|
history_tokens = word_tokenize("<br>".join(conversation_history)) |
|
history_stemmed_tokens = [ps.stem(token) for token in history_tokens] |
|
history_lemmatized_tokens = [lemmatizer.lemmatize(token) for token in history_tokens] |
|
|
|
|
|
model_input = prompt.format(message=user_input, history="<br>".join(conversation_history), database_tag=database_content, date_time=current_time, bitcoin_price=bitcoin_price, json_data=json_data,history_tokens=history_tokens,history_stemmed_tokens=history_stemmed_tokens,history_lemmatized_tokens=history_lemmatized_tokens) |
|
response = llm(model_input) |
|
|
|
bot_response = response.split('Response:')[1].strip() |
|
bot_response = bot_response.strip().replace('\n', '<br>') |
|
conversation_history.append("Bot: " + bot_response) |
|
|
|
conversation_html = '<br>'.join(conversation_history) |
|
|
|
return bot_response |
|
|
|
@app.route('/add_data', methods=['GET', 'POST']) |
|
def add_data(): |
|
if request.method == 'POST': |
|
date = request.form['date'] |
|
open_price = request.form['open_price'] |
|
high_price = request.form['high_price'] |
|
low_price = request.form['low_price'] |
|
close_price = request.form['close_price'] |
|
adj_close = request.form['adj_close'] |
|
volume = request.form['volume'] |
|
|
|
new_data = [date, open_price, high_price, low_price, close_price, adj_close, volume] |
|
|
|
with open('info.txt', 'a') as file: |
|
file.write('\t'.join(new_data) + '\n') |
|
|
|
return render_template('admin.html') |
|
|
|
@app.route('/clear_history') |
|
def clear_history(): |
|
global conversation_history |
|
conversation_history = [] |
|
return 'Conversation history cleared' |
|
|
|
with open('i.txt', 'r') as file: |
|
data = file.read() |
|
|
|
if __name__ == "__main__": |
|
repo_id = "mistralai/Mixtral-8x7B-Instruct-v0.1" |
|
huggingfacehub_api_token = "hf" + data |
|
|
|
llm = HuggingFaceHub(huggingfacehub_api_token=huggingfacehub_api_token, |
|
repo_id=repo_id, |
|
model_kwargs={ |
|
"temperature": 0.5, |
|
"max_new_tokens": 512, |
|
"top_p": 0.3, |
|
"repetition_penalty": 1.2, |
|
"num_beams": 3, |
|
"length_penalty": 1.5, |
|
"no_repeat_ngram_size": 2, |
|
"early_stopping": True, |
|
"num_return_sequences": 1, |
|
"use_cache": True, |
|
"task": "predictions", |
|
"data_source": "financial_markets", |
|
"historical_data_fetch": True, |
|
"real-time_data_integration": True, |
|
"feature_engineering": ["technical_indicators", "sentiment_analysis", "volume_analysis"], |
|
"machine_learning_models": ["LSTM", "Random Forest", "ARIMA", "Gradient Boosting"], |
|
"prediction_horizon": "short-term", |
|
"evaluation_metrics": ["accuracy", "MSE", "MAE", "RMSE"], |
|
"model_fine-tuning": True, |
|
"interpretability_explanation": True, |
|
"ensemble_methods": ["voting", "stacking"], |
|
"hyperparameter_optimization": True, |
|
"cross-validation": True, |
|
"online_learning": True, |
|
} |
|
) |
|
app.run(host="0.0.0.0", port=7860) |
|
|