Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from langchain_community.llms import HuggingFaceTextGenInference
|
| 3 |
import os
|
|
|
|
| 4 |
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
|
| 5 |
from langchain.schema import StrOutputParser
|
| 6 |
# from datetime import datetime
|
|
@@ -21,7 +22,7 @@ os.environ['LANGCHAIN_TRACING_V2'] = "true"
|
|
| 21 |
|
| 22 |
|
| 23 |
API_TOKEN = os.getenv('HF_INFER_API')
|
| 24 |
-
POSTGRE_URL = os.environ['POSTGRE_URL']
|
| 25 |
|
| 26 |
@st.cache_resource
|
| 27 |
def get_llm_chain():
|
|
@@ -33,29 +34,29 @@ def get_llm_chain():
|
|
| 33 |
)
|
| 34 |
|
| 35 |
|
| 36 |
-
@st.cache_resource
|
| 37 |
-
def get_db_connection(conn_url, password=None):
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
|
| 52 |
# @st.cache_resource
|
| 53 |
# def get_memory():
|
| 54 |
# return PostgresChatMessageHistory(connection_string=POSTGRE_URL, session_id=str(datetime.timestamp(datetime.now())))
|
| 55 |
|
| 56 |
|
| 57 |
-
if 'conn' not in st.session_state:
|
| 58 |
-
|
| 59 |
|
| 60 |
# if 'cursor' not in st.session_state:
|
| 61 |
# st.session_state.cursor = st.session_state.conn.cursor()
|
|
@@ -113,25 +114,54 @@ if prompt := st.chat_input("Ask me anything.."):
|
|
| 113 |
# Add assistant response to chat history
|
| 114 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 115 |
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
|
|
|
| 129 |
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
except Exception as e:
|
| 134 |
print("ERROR!!!\n", str(e))
|
| 135 |
print("User Input :", prompt)
|
| 136 |
print("Chatbot Response :", response)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from langchain_community.llms import HuggingFaceTextGenInference
|
| 3 |
import os
|
| 4 |
+
import io
|
| 5 |
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
|
| 6 |
from langchain.schema import StrOutputParser
|
| 7 |
# from datetime import datetime
|
|
|
|
| 22 |
|
| 23 |
|
| 24 |
API_TOKEN = os.getenv('HF_INFER_API')
|
| 25 |
+
# POSTGRE_URL = os.environ['POSTGRE_URL']
|
| 26 |
|
| 27 |
@st.cache_resource
|
| 28 |
def get_llm_chain():
|
|
|
|
| 34 |
)
|
| 35 |
|
| 36 |
|
| 37 |
+
# @st.cache_resource
|
| 38 |
+
# def get_db_connection(conn_url, password=None):
|
| 39 |
|
| 40 |
+
# url = up.urlparse(conn_url)
|
| 41 |
+
|
| 42 |
+
# conn = psycopg2.connect(
|
| 43 |
+
# database=url.path[1:],
|
| 44 |
+
# user=url.username,
|
| 45 |
+
# password=password if password is not None else url.password,
|
| 46 |
+
# host=url.hostname,
|
| 47 |
+
# port=url.port
|
| 48 |
+
# )
|
| 49 |
|
| 50 |
+
# print("Connection to database succesfull!")
|
| 51 |
+
# return conn
|
| 52 |
|
| 53 |
# @st.cache_resource
|
| 54 |
# def get_memory():
|
| 55 |
# return PostgresChatMessageHistory(connection_string=POSTGRE_URL, session_id=str(datetime.timestamp(datetime.now())))
|
| 56 |
|
| 57 |
|
| 58 |
+
# if 'conn' not in st.session_state:
|
| 59 |
+
# st.session_state.conn = get_db_connection(POSTGRE_URL)
|
| 60 |
|
| 61 |
# if 'cursor' not in st.session_state:
|
| 62 |
# st.session_state.cursor = st.session_state.conn.cursor()
|
|
|
|
| 114 |
# Add assistant response to chat history
|
| 115 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 116 |
|
| 117 |
+
try:
|
| 118 |
+
timestamp = datetime.now(timezone.utc) + timedelta(hours=7)
|
| 119 |
+
chat_text = f"Timestamp: {timestamp}\nUser Input: {prompt}\nChatbot Response: {response}\n\n"
|
| 120 |
+
text_file = io.StringIO(chat_text) # Use io.StringIO
|
| 121 |
+
|
| 122 |
+
data = {
|
| 123 |
+
"text_content": [chat_text] # Store the raw text
|
| 124 |
+
}
|
| 125 |
+
dataset = Dataset.from_dict(data)
|
| 126 |
+
|
| 127 |
+
# dataset_name = "your_dataset_name" # Replace with your desired dataset name
|
| 128 |
+
# dataset_name = os.environ["DB_NAME"]
|
| 129 |
+
dataset_name = "chat_with_me_history"
|
| 130 |
+
repo_id = f"jonathanjordan21/{dataset_name}" # Full repo ID
|
| 131 |
|
| 132 |
+
dataset.push_to_hub(
|
| 133 |
+
repo_id=repo_id,
|
| 134 |
+
private=True, # Set to False if you want it to be public
|
| 135 |
+
# token="your_huggingface_token", # Replace with your token
|
| 136 |
+
token=API_TOKEN
|
| 137 |
+
)
|
| 138 |
+
print(f"Chat history added to Hugging Face dataset: {repo_id}")
|
| 139 |
+
|
| 140 |
+
|
| 141 |
except Exception as e:
|
| 142 |
print("ERROR!!!\n", str(e))
|
| 143 |
print("User Input :", prompt)
|
| 144 |
print("Chatbot Response :", response)
|
| 145 |
+
|
| 146 |
+
# # Insert data into the table
|
| 147 |
+
# try :
|
| 148 |
+
# try :
|
| 149 |
+
# cur = st.session_state.conn.cursor()
|
| 150 |
+
# except:
|
| 151 |
+
# get_db_connection.clear()
|
| 152 |
+
# st.session_state.conn = get_db_connection(POSTGRE_URL)
|
| 153 |
+
# cur = st.session_state.conn.cursor()
|
| 154 |
+
|
| 155 |
+
# cur.execute(
|
| 156 |
+
# f"INSERT INTO chat_history (input_text, response_text, created_at) VALUES (%s, %s, %s)",
|
| 157 |
+
# (prompt, response, datetime.now(timezone.utc) + timedelta(hours=7))
|
| 158 |
+
# )
|
| 159 |
+
|
| 160 |
+
# # Commit the transaction
|
| 161 |
+
# st.session_state.conn.commit()
|
| 162 |
+
# cur.close()
|
| 163 |
+
# except Exception as e:
|
| 164 |
+
# print("ERROR!!!\n", str(e))
|
| 165 |
+
# print("User Input :", prompt)
|
| 166 |
+
# print("Chatbot Response :", response)
|
| 167 |
|