Skip to content

Commit

Permalink
fixed_code generation issue with better stability
Browse files Browse the repository at this point in the history
  • Loading branch information
shekharP1536 committed Nov 28, 2024
1 parent 5c00a73 commit 9d45ca9
Show file tree
Hide file tree
Showing 14 changed files with 739 additions and 395 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ templates/style.css
.gitignore
/python
.vscode/settings.json
static/resource/20241126011143_Notification-and-Programme-for-BTech-RIST-Odd-Semester-December-2024.pdf
static/resource/an-old-hope.min.css
/.gitignore
from ollama import chat.py
demo.html
130 changes: 101 additions & 29 deletions index.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,63 +6,101 @@
import datetime
import threading
from queue import Queue

import uuid
con_path = ""
app = Flask(__name__, template_folder='templates')

date_str = ""
conversation = []
clients = []
chat_history = []
clients_lock = threading.Lock()
stream_flow = False
date_time = datetime.datetime.now()

# Save conversation in a new file
def save_conversation():
global con_path # Use the global variable to store the file path across calls
chat_title = "Starting New chat"
app = Flask(__name__, template_folder='templates')

def getId():
global date_str
current_time = datetime.datetime.now()
formatted_time = current_time.strftime("%Y%m%d_%H%M%S")
date_str = formatted_time
print(date_str)
getId()
def save_conversation(create_new_file=False):
global con_path, date_str,chat_title # Use the global variables to store the file path and date_str across calls
# Define the folder where logs are stored
folder_name = 'log_data/conversation_logs'
if not os.path.exists(folder_name):
os.makedirs(folder_name)

# Get the current date (without time) to use as part of the filename
date_str = datetime.datetime.now().strftime("%Y-%m-%d-%h-%m")

# If `con_path` is None or if it doesn't match today's date, create a new file path
if not con_path or date_str not in con_path:
if create_new_file or not con_path or date_str not in con_path:
filename = f'conversation_{date_str}.json'
con_path = os.path.join(folder_name, filename)
elif not create_new_file and con_path:
pass
if not date_str:
date_str = str(uuid.uuid4()) # Generate a unique date_str for the conversation

# Prepare the data structure with only the conversation array for subsequent saves
conversation_data = {
"chat_title" : chat_title,
"chat_id": date_str,
"conversation": conversation # Assume `conversation` is a list holding conversation data
}

# Save or update the conversation data in the JSON file
if os.path.exists(con_path):
# Load existing data and append new data to it
# Load existing data
with open(con_path, 'r+') as f:
try:
existing_data = json.load(f)
except json.JSONDecodeError:
existing_data = []
existing_data.extend(conversation)

# Check if the file already contains the conversation with the same chat_id
existing_chat_ids = [entry["chat_id"] for entry in existing_data]
if date_str in existing_chat_ids:
# If the chat_id already exists, just update the existing conversation array
for entry in existing_data:
if entry["chat_id"] == date_str:
entry["conversation"] = conversation # Overwrite the conversation data
break
else:
# Add new conversation if chat_id doesn't exist
existing_data.append(conversation_data)

# Save the updated conversation data back to the file
f.seek(0)
json.dump(existing_data, f, indent=4)

else:
# Create new file if it doesn't exist
with open(con_path, 'w') as f:
json.dump(conversation, f, indent=4)
json.dump([conversation_data], f, indent=4) # Wrap in a list to store multiple conversations

print(f"Conversation saved to {con_path}")

def new_chat(title,create_new_file=False):
print("Request for new chat.")
global conversation, con_path, chat_title
conversation = []
con_path = ""
chat_title = title
getId()
save_conversation(create_new_file=True)

def client(data):
"""Send data to all connected clients."""
with clients_lock:
for client in clients:
client.put(data)

# Generate LLM response
def generate_response(prompt, model):
print(prompt, model)
def generate_response(prompt, model,chat_request):
print(prompt, model,chat_request)
if prompt == "":
return
if chat_request == True:
print("true")
new_chat(prompt[:20])

user_rep_st = "[|/__USER_START__/|]"
client(user_rep_st)
client(f"{prompt}")
Expand Down Expand Up @@ -98,18 +136,17 @@ def generate_response(prompt, model):
save_conversation()

client(done_marker)

def process(message_chunk):
match message_chunk:
case _ if "\n" in message_chunk or message_chunk == "":
message_chunk = "<br>"
case _ if "\n\n\n" in message_chunk:
message_chunk = "<hr><br><br>"
case _:
pass

# Replace patterns in the correct order
if "\n\n\n" in message_chunk: # Triple line breaks
message_chunk = message_chunk.replace("\n\n\n", "<hr><br><br>")
if "\n\n" in message_chunk: # Double line breaks
message_chunk = message_chunk.replace("\n\n", "<br><br>")
if "\n" in message_chunk: # Single line breaks
message_chunk = message_chunk.replace("\n", "<br>")
client(message_chunk)


@app.route('/')
def index():
return render_template('index.html')
Expand All @@ -120,7 +157,8 @@ def get_response():
data = request.get_json()
prompt = data.get('prompt')
model = data.get('model_need')
response = generate_response(prompt, model)
chat_request = data.get("new_chat")
response = generate_response(prompt, model,chat_request)
return jsonify({'response': 'Success'})

@app.route("/get_cmd", methods=["POST"])
Expand Down Expand Up @@ -158,6 +196,40 @@ def list_llm():
if request.method == 'POST':
list = ollama.list()
return jsonify({"list": list})
@app.route('/get_chats', methods=['POST'])
def get_chats():
folder_name = 'log_data/conversation_logs'
if not os.path.exists(folder_name):
return jsonify({"cat": "empty", "response": "Start chat"})
files = os.listdir(folder_name)

json_files = [file for file in files if file.endswith('.json')]

if not json_files:
return jsonify({"cat": "empty", "response": "No chats found"})

chat_titles = []

# Iterate through each .json file and extract the chat_title
for json_file in json_files:
file_path = os.path.join(folder_name, json_file)

try:
with open(file_path, 'r') as f:
data = json.load(f)

# Check if data is a list (list of chats)
if isinstance(data, list):
for chat in data:
# Ensure 'chat_title' exists in each chat
if "chat_title" in chat:
chat_titles.append(chat["chat_title"])
except json.JSONDecodeError:
continue
if not chat_titles:
return jsonify({"cat": "empty", "response": "No valid chat titles found"})

# Return the list of chat titles
return jsonify({"cat": "success", "response": chat_titles})
if __name__ == '__main__':
app.run(debug=True)
Binary file modified others/imga.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified others/imgb.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified others/imgc.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion static/extra.css
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ body {

/* Style the tab container */
.tab {
/* display: flex; */
float: left;
background-color: rgb(42, 36, 61);
width: 25%;
Expand All @@ -249,7 +250,7 @@ body {
background-color: inherit;
color: white;
background-color: rgb(42, 36, 61);
padding: 16px;
/* padding: 16px; */
width: 100%;
border: none;
outline: none;
Expand Down
11 changes: 0 additions & 11 deletions static/feature.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,6 @@ function send_mic_input(userPrompt) {
}
}

const STT_source = new EventSource("/sst_event");
STT_source.onmessage = function (event) {
if(allow_user_mic){
const resp = event.data;
console.log(resp);
prompt += resp;
sst_content.innerHTML += resp;
}else
console.log("User not allowed this")

};
// Handeling tab is setting modal
function openTab(evt, tabName) {
var i, tabcontent, tablinks;
Expand Down
Loading

0 comments on commit 9d45ca9

Please sign in to comment.