Update app.py
Browse files
app.py
CHANGED
@@ -1,16 +1,14 @@
|
|
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
|
5 |
-
import requests
|
6 |
import json
|
7 |
import nltk
|
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 |
-
import tensorflow as tf
|
13 |
-
from tensorflow import keras
|
14 |
import spacy
|
15 |
from bs4 import BeautifulSoup
|
16 |
|
@@ -18,7 +16,7 @@ nltk.download('punkt')
|
|
18 |
nltk.download('wordnet')
|
19 |
|
20 |
def download_spacy_model():
|
21 |
-
import spacy
|
22 |
try:
|
23 |
spacy.load("en_core_web_sm")
|
24 |
except OSError:
|
@@ -31,57 +29,31 @@ nlp = spacy.load("en_core_web_sm")
|
|
31 |
|
32 |
app = Flask(__name__)
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
with open('ai_chatbot_data.json', 'r') as file:
|
37 |
-
json_data = json.load(file)
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
template = "Message: {message}\n\nSentiment Analysis: {sentiment}\n\nConversation Now Between you and user: {history}\n\nDate and Time: {date_time}\n\nBitcoin Price: ${bitcoin_price}\n\nBitcoin history from 1-jan-2024 to today the tidy is date-open-high-low-close-adj close-volum: {database_tag}\n\nYour system: {json_data}.\n\nResponse:"
|
43 |
-
prompt = PromptTemplate(template=template, input_variables=["message", "sentiment", "history", "date_time", "bitcoin_price", "database_tag", "json_data"])
|
44 |
conversation_history = []
|
45 |
|
46 |
MAX_HISTORY_LENGTH = 55
|
47 |
|
48 |
-
url = "https://dooratre-info.hf.space/"
|
49 |
-
|
50 |
-
response = requests.get(url)
|
51 |
-
soup = BeautifulSoup(response.content, 'html.parser')
|
52 |
-
|
53 |
-
div_content = soup.find('div', {'id': '45'})
|
54 |
-
if div_content:
|
55 |
-
print(div_content)
|
56 |
-
else:
|
57 |
-
print("No div with id=45 found on the page.")
|
58 |
-
database_tag=div_content
|
59 |
-
|
60 |
def update_conversation_history(message):
|
61 |
if len(conversation_history) >= MAX_HISTORY_LENGTH:
|
62 |
conversation_history.pop(0)
|
63 |
conversation_history.append(message)
|
64 |
|
65 |
-
|
66 |
def get_bitcoin_price():
|
67 |
-
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
68 |
url = 'https://api.coindesk.com/v1/bpi/currentprice.json'
|
69 |
response = requests.get(url)
|
70 |
-
|
71 |
if response.status_code == 200:
|
72 |
data = response.json()
|
73 |
bitcoin_price = data['bpi']['USD']['rate']
|
|
|
74 |
return bitcoin_price, current_time
|
75 |
else:
|
76 |
-
return 'Error fetching data',
|
77 |
-
|
78 |
-
@app.route('/assets/<path:path>')
|
79 |
-
def send_static(path):
|
80 |
-
return send_from_directory('assets', path)
|
81 |
|
82 |
@app.route('/')
|
83 |
def index():
|
84 |
-
global conversation_history
|
85 |
return render_template('index.html', conversation=conversation_history)
|
86 |
|
87 |
@app.route('/submit', methods=['POST'])
|
@@ -92,39 +64,29 @@ def submit():
|
|
92 |
tokens = [token.text for token in doc]
|
93 |
|
94 |
sentiment = TextBlob(user_input).sentiment
|
95 |
-
|
96 |
-
# Add Spacy NLP processing here
|
97 |
-
|
98 |
ps = PorterStemmer()
|
99 |
stemmed_tokens = [ps.stem(token) for token in tokens]
|
100 |
|
101 |
lemmatizer = WordNetLemmatizer()
|
102 |
lemmatized_tokens = [lemmatizer.lemmatize(token) for token in tokens]
|
103 |
|
104 |
-
sentiment = TextBlob(user_input).sentiment
|
105 |
-
|
106 |
bitcoin_price, current_time = get_bitcoin_price()
|
107 |
|
108 |
conversation_history.append("User: " + user_input)
|
109 |
-
|
110 |
-
|
111 |
-
history_tokens = word_tokenize("<br>".join(conversation_history))
|
112 |
history_stemmed_tokens = [ps.stem(token) for token in history_tokens]
|
113 |
history_lemmatized_tokens = [lemmatizer.lemmatize(token) for token in history_tokens]
|
114 |
|
115 |
-
model_input = prompt.format(message=user_input, sentiment=sentiment, history="
|
116 |
-
|
117 |
-
response = llm(model_input, context="<br>".join(conversation_history))
|
118 |
-
|
119 |
-
bot_response = response.split('Response:')[1].strip()
|
120 |
-
bot_response = bot_response.strip().replace('\n', '<br>')
|
121 |
|
122 |
-
|
123 |
-
update_conversation_history("You " + bot_response)
|
124 |
|
125 |
-
|
|
|
126 |
|
127 |
-
return
|
128 |
|
129 |
@app.route('/clear_history')
|
130 |
def clear_history():
|
|
|
1 |
+
from flask import Flask, render_template, request, send_from_directory, jsonify
|
2 |
from datetime import datetime
|
3 |
+
import requests
|
4 |
from langchain_community.llms import HuggingFaceHub
|
5 |
from langchain.prompts import PromptTemplate
|
|
|
6 |
import json
|
7 |
import nltk
|
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 |
import spacy
|
13 |
from bs4 import BeautifulSoup
|
14 |
|
|
|
16 |
nltk.download('wordnet')
|
17 |
|
18 |
def download_spacy_model():
|
19 |
+
import spacy
|
20 |
try:
|
21 |
spacy.load("en_core_web_sm")
|
22 |
except OSError:
|
|
|
29 |
|
30 |
app = Flask(__name__)
|
31 |
|
32 |
+
template = "Message: {message}\n\nSentiment Analysis: {sentiment}\n\nConversation History: {history}\n\nDate and Time: {date_time}\n\nBitcoin Price: ${bitcoin_price}\n\nBitcoin Data: {database_tag}\n\nResponse: {response}"
|
33 |
+
prompt = PromptTemplate(template=template, input_variables=["message", "sentiment", "history", "date_time", "bitcoin_price", "database_tag", "response"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
conversation_history = []
|
35 |
|
36 |
MAX_HISTORY_LENGTH = 55
|
37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
def update_conversation_history(message):
|
39 |
if len(conversation_history) >= MAX_HISTORY_LENGTH:
|
40 |
conversation_history.pop(0)
|
41 |
conversation_history.append(message)
|
42 |
|
|
|
43 |
def get_bitcoin_price():
|
|
|
44 |
url = 'https://api.coindesk.com/v1/bpi/currentprice.json'
|
45 |
response = requests.get(url)
|
46 |
+
|
47 |
if response.status_code == 200:
|
48 |
data = response.json()
|
49 |
bitcoin_price = data['bpi']['USD']['rate']
|
50 |
+
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
51 |
return bitcoin_price, current_time
|
52 |
else:
|
53 |
+
return 'Error fetching data', None
|
|
|
|
|
|
|
|
|
54 |
|
55 |
@app.route('/')
|
56 |
def index():
|
|
|
57 |
return render_template('index.html', conversation=conversation_history)
|
58 |
|
59 |
@app.route('/submit', methods=['POST'])
|
|
|
64 |
tokens = [token.text for token in doc]
|
65 |
|
66 |
sentiment = TextBlob(user_input).sentiment
|
67 |
+
|
|
|
|
|
68 |
ps = PorterStemmer()
|
69 |
stemmed_tokens = [ps.stem(token) for token in tokens]
|
70 |
|
71 |
lemmatizer = WordNetLemmatizer()
|
72 |
lemmatized_tokens = [lemmatizer.lemmatize(token) for token in tokens]
|
73 |
|
|
|
|
|
74 |
bitcoin_price, current_time = get_bitcoin_price()
|
75 |
|
76 |
conversation_history.append("User: " + user_input)
|
77 |
+
|
78 |
+
history_tokens = word_tokenize(" ".join(conversation_history))
|
|
|
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, sentiment=sentiment, history=" ".join(conversation_history), database_tag="Placeholder", date_time=current_time, bitcoin_price=bitcoin_price, response="")
|
|
|
|
|
|
|
|
|
|
|
83 |
|
84 |
+
response = "Placeholder response" # Update with actual response generation logic
|
|
|
85 |
|
86 |
+
response_message = "Bot: " + response
|
87 |
+
update_conversation_history(response_message)
|
88 |
|
89 |
+
return jsonify({'response':response})
|
90 |
|
91 |
@app.route('/clear_history')
|
92 |
def clear_history():
|