From 87b5664e08bbd6fe3edd90766904f55b3fd0c7a8 Mon Sep 17 00:00:00 2001 From: Sapan Shah Date: Sun, 13 Oct 2024 02:35:20 +0530 Subject: [PATCH] update readme --- app.py | 85 +++++++++++++++++++++++++++++++++++++++ conversation_history.json | 32 +++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 app.py create mode 100644 conversation_history.json diff --git a/app.py b/app.py new file mode 100644 index 0000000..98ae39c --- /dev/null +++ b/app.py @@ -0,0 +1,85 @@ +import streamlit as st +import json +import os +import datetime + +# Define the path to the local JSON file where conversation will be stored +FILE_PATH = "/home/sapan/PycharmProjects/maargdarshak/conversation_history.json" + + +# Function to save prompt and response to local JSON file +def save_to_local(prompt, response): + data = { + "prompt": prompt, + "response": response, + "timestamp": str(datetime.datetime.now()) + } + + # If the file exists, load existing data, handle empty or corrupt files + if os.path.exists(FILE_PATH): + try: + with open(FILE_PATH, 'r') as file: + history = json.load(file) + except json.JSONDecodeError: + history = [] # If the file is empty or corrupted, start with an empty list + else: + history = [] + + # Append the new conversation entry + history.append(data) + + # Save the updated history back to the JSON file + with open(FILE_PATH, 'w') as file: + json.dump(history, file, indent=4) + + +# Function to load conversation history from local JSON file +def load_from_local(): + if os.path.exists(FILE_PATH): + try: + with open(FILE_PATH, 'r') as file: + return json.load(file) + except json.JSONDecodeError: + return [] # Return empty list if the file is empty or corrupt + return [] + + +# Initialize session state to store conversation history for the session +if "conversation" not in st.session_state: + st.session_state["conversation"] = [] + +# Streamlit App +st.title("Chatbot with Local Chat History") + +# Text input for user prompt +user_input = st.text_input("Ask the chatbot something:") + +# If the user submits input +if user_input: + # Simulated response (for now it's echoing the input) + response = f"Echo: {user_input}" + + # Save the current prompt and response to session state for chat history + st.session_state["conversation"].append({"prompt": user_input, "response": response}) + + # Also save the conversation to local storage (JSON file) + save_to_local(user_input, response) + +# Display the chat history (conversation for the session) +st.write("### Chat History") +for convo in st.session_state["conversation"]: + st.write(f"**You**: {convo['prompt']}") + st.write(f"**Bot**: {convo['response']}") + st.write("---") + +# Button to show entire history from local JSON file +if st.button("Show Full Local Conversation History"): + st.write("### Full Local Conversation History") + + # Load history from the local file and display it + full_history = load_from_local() + for convo in full_history: + st.write(f"**You**: {convo['prompt']}") + st.write(f"**Bot**: {convo['response']}") + st.write(f"Timestamp: {convo['timestamp']}") + st.write("---") diff --git a/conversation_history.json b/conversation_history.json new file mode 100644 index 0000000..c049ac7 --- /dev/null +++ b/conversation_history.json @@ -0,0 +1,32 @@ +[ + { + "prompt": "sa", + "response": "Echo: sa", + "timestamp": "2024-10-13 02:30:26.507681" + }, + { + "prompt": "sadsas", + "response": "Echo: sadsas", + "timestamp": "2024-10-13 02:30:27.815409" + }, + { + "prompt": "sadsasdaswdewf", + "response": "Echo: sadsasdaswdewf", + "timestamp": "2024-10-13 02:30:28.942786" + }, + { + "prompt": "ffhj", + "response": "Echo: ffhj", + "timestamp": "2024-10-13 02:31:17.158006" + }, + { + "prompt": "ffhj;lkjhg", + "response": "Echo: ffhj;lkjhg", + "timestamp": "2024-10-13 02:31:19.262145" + }, + { + "prompt": "ffhj;lkjhglkjh", + "response": "Echo: ffhj;lkjhglkjh", + "timestamp": "2024-10-13 02:31:21.288840" + } +] \ No newline at end of file -- 2.45.2