Skip to content

Bug fixes and adjustments  #2

Description

@Lilmamaaluv

import os
import json
import openai
import logging
from fpdf import FPDF
import random
import tkinter as tk
from tkinter import ttk
from tkinter import filedialog, Button, Label, Text, Scrollbar, END, Entry, StringVar, Toplevel, messagebox

MIN_ITEMS = 200 # Set this to the desired number of items

Set up logging

logging.basicConfig(filename='conversion_log.txt', level=logging.INFO, format='%(asctime)s - %(message)s')

Function to process text with ChatGPT

def process_with_chatgpt(txt_content, api_key):
try:
openai.api_key = api_key
response = openai.Completion.create(
model="gpt-3.5-turbo",
prompt=txt_content,
max_tokens=150
)
return response.choices[0].text.strip()
except Exception as e:
logging.error(f"Error processing with ChatGPT: {e}")
messagebox.showerror("Error", f"Error processing with ChatGPT: {e}")
return None

Function to save content to JSON dataset

def content_to_dataset(validated_content, dataset_file):
with open(dataset_file, 'w') as file:
for content in validated_content:
json.dump(content, file)
file.write('\n')

Function to save content to PDF dataset using FPDF

def content_to_pdf(validated_content, dataset_file):
pdf = FPDF()
pdf.set_auto_page_break(auto=True, margin=15)
pdf.add_page()
pdf.set_font("Arial", size=12)

for content in validated_content:
    user_message = content["messages"][0]["content"]
    assistant_message = content["messages"][1]["content"]

    pdf.multi_cell(0, 10, f"User: {user_message}")
    pdf.multi_cell(0, 10, f"Assistant: {assistant_message}")
    pdf.ln()

pdf.output(dataset_file)

Function to initiate the model fine-tuning

def train_model(api_key, training_file):
try:
openai.api_key = api_key
response = openai.File.create(file=open(training_file, "rb"), purpose='fine-tune')
file_id = response.id
fine_tuning_response = openai.FineTuningJob.create(training_file=file_id, model="gpt-3.5-turbo")
job_id = fine_tuning_response.id
logging.info(f"Fine-tuning job started with ID: {job_id}")
return f"Fine-tuning job started with ID: {job_id}"
except Exception as e:
logging.error(f"Error starting fine-tuning: {e}")
messagebox.showerror("Error", f"Error starting fine-tuning: {e}")
return None

Function to convert text files and create datasets

def convert_files(folder_path, api_key, output_type):
dataset_content = []
for filename in os.listdir(folder_path):
if filename.endswith(".txt"):
with open(os.path.join(folder_path, filename), 'r') as file:
txt_content = file.read()
processed_content = process_with_chatgpt(txt_content, api_key)
if processed_content:
dataset_content.append({"messages": [{"role": "user", "content": txt_content}, {"role": "assistant", "content": processed_content}]})
logging.info(f"Processed {filename} with content: {processed_content}")

if output_type == "json":
    content_to_dataset(dataset_content, os.path.join(folder_path, "dataset.json"))
elif output_type == "pdf":
    content_to_pdf(dataset_content, os.path.join(folder_path, "dataset.pdf"))

populate_treeview(tree, folder_path)

Function to view logs in a new window

def view_logs():
with open('conversion_log.txt', 'r') as log_file:
logs = log_file.read()
log_window = Toplevel(root)
log_window.title("Conversion Logs")
log_text = Text(log_window, height=20, width=80)
log_text.pack(pady=10)
log_text.insert(END, logs)

Function to view and edit TXT files

def view_edit_txt(folder_path):
def save_changes(file_path, text_widget):
with open(file_path, 'w') as file:
file.write(text_widget.get("1.0", END))

file_path = filedialog.askopenfilename(initialdir=folder_path, title="Select a TXT file", filetypes=(("TXT files", "*.txt"), ("All files", "*.*")))
if file_path:
    with open(file_path, 'r') as file:
        content = file.read()
    edit_window = Toplevel(root)
    edit_window.title(f"Editing {os.path.basename(file_path)}")
    edit_text = Text(edit_window, height=20, width=80)
    edit_text.pack(pady=10)
    edit_text.insert(END, content)
    save_button = Button(edit_window, text="Save Changes", command=lambda: save_changes(file_path, edit_text))
    save_button.pack(pady=10)

Function to populate the treeview with folder contents

def populate_treeview(tree, folder_path):
for item in os.listdir(folder_path):
item_path = os.path.join(folder_path, item)
if os.path.isdir(item_path):
tree.insert("", "end", item, text=item)
populate_treeview(tree, item_path)
else:
tree.insert("", "end", text=item)

Function to update the random items on the canvas

def update_items():
global items
new_items = []
for item, dx, dy in items:
coords = canvas.coords(item)
if len(coords) == 4: # Rectangles and Ovals
x1, y1, x2, y2 = coords
if x1 <= 0 or x2 >= canvas.winfo_width() or y1 <= 0 or y2 >= canvas.winfo_height():
canvas.delete(item)
new_item, new_dx, new_dy = create_random_item_at_random_position()
new_items.append((new_item, new_dx, new_dy))
else:
canvas.move(item, dx, dy)
new_items.append((item, dx, dy))
elif len(coords) == 2: # Text items
x, y = coords
if x <= 10 or x >= canvas.winfo_width() - 10 or y <= 10 or y >= canvas.winfo_height() - 10:
canvas.delete(item)
new_item, new_dx, new_dy = create_random_item_at_random_position()
new_items.append((new_item, new_dx, new_dy))
else:
canvas.move(item, dx, dy)
new_items.append((item, dx, dy))
items = new_items

# Ensure the minimum number of items always exist
while len(items) < MIN_ITEMS:
    new_item, new_dx, new_dy = create_random_item_at_random_position()
    items.append((new_item, new_dx, new_dy))

root.after(50, update_items)

Function to create a random item at a random position

def create_random_item_at_random_position():
x, y = random.randint(0, canvas.winfo_width() - 10), random.randint(0, canvas.winfo_height() - 10)
return create_random_item(x, y)

Function to create a random item (shape, letter, number, or symbol)

def create_random_item(x, y):
item_type = random.choice(['shape', 'letter', 'number', 'symbol'])
if item_type == 'shape':
shape = random.choice(['circle', 'rect', 'triangle'])
if shape == 'circle':
item = canvas.create_oval(x, y, x+10, y+10, fill='lightblue', outline='lightblue')
elif shape == 'rect':
item = canvas.create_rectangle(x, y, x+10, y+10, fill='lightgreen', outline='lightgreen')
else:
item = canvas.create_polygon(x, y, x+10, y, x+5, y-10, fill='lightyellow', outline='lightyellow')
elif item_type == 'letter':
letter = random.choice('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
item = canvas.create_text(x, y, text=letter, fill='lightpink')
elif item_type == 'number':
number = random.choice('0123456789')
item = canvas.create_text(x, y, text=number, fill='lightcoral')
else:
symbols = ['!', '@', '#', '$', '%', '^', '&', '*']
symbol = random.choice(symbols)
item = canvas.create_text(x, y, text=symbol, fill='lightsalmon')
dx = random.choice([-1, 1, 2])
dy = random.choice([-1, 1, 2])
return item, dx, dy

Main Tkinter window

root = tk.Tk()
root.title("Model Builder Interface, SUDOBRAIN Training Toolkit UI")
root.geometry('1480x900')
root.configure(bg='#f0f0f0')

canvas = tk.Canvas(root, bg='#f0f0f0', bd=0, highlightthickness=0)
canvas.pack(fill=tk.BOTH, expand=True)

def on_resize(event):
canvas.config(width=event.width, height=event.height)
root.bind("", on_resize)

Input for API key

Label(root, text="OpenAI API Key:", bg='#f0f0f0').place(x=10, y=10)
api_key_entry = Entry(root, show="*")
api_key_entry.place(x=120, y=10, width=300)

Convert files button

Button(root, text="Convert Files (JSON)", command=lambda: convert_files(filedialog.askdirectory(), api_key_entry.get(), "json")).place(x=450, y=10)

Convert files to PDF button

Button(root, text="Convert Files (PDF)", command=lambda: convert_files(filedialog.askdirectory(), api_key_entry.get(), "pdf")).place(x=600, y=10)

View logs button

Button(root, text="View Logs", command=view_logs).place(x=750, y=10)

View/Edit text files button

Button(root, text="View/Edit TXT Files", command=lambda: view_edit_txt(filedialog.askdirectory())).place(x=850, y=10)

Train model button

Button(root, text="Train Model", command=lambda: train_model(api_key_entry.get(), filedialog.askopenfilename())).place(x=1000, y=10)

Treeview for displaying files and folders

tree_frame = ttk.Frame(root)
tree_frame.place(x=10, y=50, width=500, height=700)
tree_scrollbar = Scrollbar(tree_frame)
tree_scrollbar.pack(side=tk.RIGHT, fill=tk.Y)

tree = ttk.Treeview(tree_frame, yscrollcommand=tree_scrollbar.set)
tree.pack(fill=tk.BOTH, expand=True)
tree_scrollbar.config(command=tree.yview)

Initialize random items

items = []
for _ in range(MIN_ITEMS):
item, dx, dy = create_random_item_at_random_position()
items.append((item, dx, dy))

Start updating items

update_items()

root.mainloop()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions