Dooratre commited on
Commit
65d7ee4
·
verified ·
1 Parent(s): 73a8035

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -24
app.py CHANGED
@@ -1,4 +1,4 @@
1
- from flask import Flask, render_template, request, send_from_directory, url_for
2
  from datetime import datetime
3
  from langchain_community.llms import HuggingFaceHub
4
  from langchain.prompts import PromptTemplate
@@ -9,6 +9,8 @@ from textblob import TextBlob
9
  from nltk.tokenize import word_tokenize
10
  from nltk.stem import PorterStemmer
11
  from nltk.stem import WordNetLemmatizer
 
 
12
 
13
  nltk.download('punkt')
14
  nltk.download('wordnet')
@@ -27,6 +29,13 @@ template = "Message: {message}\n\nConversation History: {history}\n\nDate and Ti
27
  prompt = PromptTemplate(template=template, input_variables=["message","history", "date_time", "bitcoin_price", "database_tag", "json_data"])
28
  conversation_history = []
29
 
 
 
 
 
 
 
 
30
  def get_bitcoin_price():
31
  current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
32
  url = 'https://api.coindesk.com/v1/bpi/currentprice.json'
@@ -70,36 +79,20 @@ def submit():
70
  history_stemmed_tokens = [ps.stem(token) for token in history_tokens]
71
  history_lemmatized_tokens = [lemmatizer.lemmatize(token) for token in history_tokens]
72
 
73
-
74
  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)
75
- response = llm(model_input)
 
76
 
77
  bot_response = response.split('Response:')[1].strip()
78
  bot_response = bot_response.strip().replace('\n', '<br>')
79
- conversation_history.append("Bot: " + bot_response)
 
 
80
 
81
  conversation_html = '<br>'.join(conversation_history)
82
 
83
  return bot_response
84
- ##############################################################################################
85
- @app.route('/add_data', methods=['GET', 'POST'])
86
- def add_data():
87
- if request.method == 'POST':
88
- date = request.form['date']
89
- open_price = request.form['open_price']
90
- high_price = request.form['high_price']
91
- low_price = request.form['low_price']
92
- close_price = request.form['close_price']
93
- adj_close = request.form['adj_close']
94
- volume = request.form['volume']
95
-
96
- new_data = [date, open_price, high_price, low_price, close_price, adj_close, volume]
97
-
98
- with open('info.txt', 'a') as file:
99
- file.write('\t'.join(new_data) + '\n')
100
-
101
- return render_template('admin.html')
102
- ################################################################################################################################
103
  @app.route('/clear_history')
104
  def clear_history():
105
  global conversation_history
@@ -142,4 +135,5 @@ if __name__ == "__main__":
142
  "online_learning": True,
143
  }
144
  )
145
- app.run(host="0.0.0.0", port=7860)
 
 
1
+ from flask import Flask, render_template, request, send_from_directory
2
  from datetime import datetime
3
  from langchain_community.llms import HuggingFaceHub
4
  from langchain.prompts import PromptTemplate
 
9
  from nltk.tokenize import word_tokenize
10
  from nltk.stem import PorterStemmer
11
  from nltk.stem import WordNetLemmatizer
12
+ import tensorflow as tf
13
+ from tensorflow import keras
14
 
15
  nltk.download('punkt')
16
  nltk.download('wordnet')
 
29
  prompt = PromptTemplate(template=template, input_variables=["message","history", "date_time", "bitcoin_price", "database_tag", "json_data"])
30
  conversation_history = []
31
 
32
+ MAX_HISTORY_LENGTH = 55 # Adjust the maximum history length as needed
33
+
34
+ def update_conversation_history(message):
35
+ if len(conversation_history) >= MAX_HISTORY_LENGTH:
36
+ conversation_history.pop(0) # Remove the oldest message
37
+ conversation_history.append(message)
38
+
39
  def get_bitcoin_price():
40
  current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
41
  url = 'https://api.coindesk.com/v1/bpi/currentprice.json'
 
79
  history_stemmed_tokens = [ps.stem(token) for token in history_tokens]
80
  history_lemmatized_tokens = [lemmatizer.lemmatize(token) for token in history_tokens]
81
 
 
82
  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)
83
+
84
+ response = llm(model_input, context="<br>".join(conversation_history))
85
 
86
  bot_response = response.split('Response:')[1].strip()
87
  bot_response = bot_response.strip().replace('\n', '<br>')
88
+
89
+ # Update the conversation history with bot's response
90
+ update_conversation_history("You " + bot_response)
91
 
92
  conversation_html = '<br>'.join(conversation_history)
93
 
94
  return bot_response
95
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
  @app.route('/clear_history')
97
  def clear_history():
98
  global conversation_history
 
135
  "online_learning": True,
136
  }
137
  )
138
+
139
+ app.run(debug=True)