code improvement

This commit is contained in:
Falko Victor Habel 2024-10-08 17:36:08 +02:00
parent 2436eab591
commit 16f3bf00a4
2 changed files with 45 additions and 63 deletions

View File

@ -1,9 +1,14 @@
from collections import deque
from views.mainScreen import MainFrame
from models.data import TextData
from Ai.interence import VeraMindInference
from Ai.llm import ArticleRater
BAD_WORDS = ["FAKE", "SATIRE"]
GOOD_WORDS = ["REAL"]
BAD_COLOR = "#ff8080"
GOOD_COLOR = "#80ff8f"
WORDS = BAD_WORDS + GOOD_WORDS
class MainFrameController:
def __init__(self,frame:MainFrame) -> None:
@ -21,82 +26,59 @@ class MainFrameController:
def press_check_button(self):
text_data = self.get_textdata()
print(f"text:{text_data}")
self.prediction(text_data)
self.frame.output_textbox.configure(state="normal")
self.frame.output_textbox.delete("0.0", "end")
response_stream = self.rater.get_response(text_data.text, text_data.confidence, text_data.result)
display_chunks = []
highlight_chunks = []
DISPLAY_CHUNK_SIZE = 1 # Display as each chunk arrives
HIGHLIGHT_BATCH_SIZE = 5
OVERLAP_SIZE = 2
highlight_buffer = deque(maxlen=5)
for chunk in response_stream:
display_chunks.append(chunk)
highlight_chunks.append(chunk)
# Display the chunk immediately
self.frame.output_textbox.insert("end", chunk)
self.frame.output_textbox.see("end")
self.frame.update_idletasks()
# Display chunk
if len(display_chunks) == DISPLAY_CHUNK_SIZE:
start_index = self.frame.output_textbox.index("end-1c")
self.frame.output_textbox.insert("end", ''.join(display_chunks))
self.frame.output_textbox.see("end") # Scroll to the end
self.frame.update_idletasks() # Update the textbox immediately
display_chunks = []
# Add to highlight buffer
highlight_buffer.append(chunk)
# Process highlighting
if len(highlight_chunks) >= HIGHLIGHT_BATCH_SIZE:
start_index = self.frame.output_textbox.index(f"end-{sum(len(c) for c in highlight_chunks)}c")
end_index = self.frame.output_textbox.index("end-1c")
self.highlight_words(start_index, end_index)
# Process highlighting when buffer is full
if len(highlight_buffer) == 5:
self.process_highlighting(highlight_buffer)
# Maintain overlap and reset for next batch
highlight_chunks = highlight_chunks[-OVERLAP_SIZE:]
# Highlight remaining chunks if any
if highlight_chunks:
start_index = self.frame.output_textbox.index(f"end-{sum(len(c) for c in highlight_chunks)}c")
end_index = self.frame.output_textbox.index("end-1c")
self.highlight_words(start_index, end_index)
# Process any remaining chunks in the buffer
if highlight_buffer:
self.process_highlighting(highlight_buffer)
self.frame.output_textbox.configure(state="disabled")
def process_highlighting(self, highlight_buffer):
start_index = self.frame.output_textbox.index(f"end-{sum(len(c) for c in highlight_buffer)}c")
end_index = self.frame.output_textbox.index("end")
self.highlight_words(start_index, end_index)
# Keep overlap of 2 chunks
highlight_buffer = deque(list(highlight_buffer)[-2:], maxlen=5)
def highlight_words(self, start_index, end_index):
content = self.frame.output_textbox.get(start_index, end_index)
print(content)
current_index = start_index
while True:
# Find "FAKE"
fake_index = content.find("FAKE")
if fake_index != -1:
fake_pos_start = f"{current_index}+{fake_index}c"
fake_pos_end = f"{fake_pos_start}+4c"
self.frame.output_textbox.tag_add("fake_color", fake_pos_start, fake_pos_end)
self.frame.output_textbox.tag_config("fake_color", foreground="red")
content = content[fake_index + 4:]
current_index = fake_pos_end
else:
break
current_index = start_index
content = self.frame.output_textbox.get(start_index, end_index)
while True:
# Find "REAL"
real_index = content.find("REAL")
if real_index != -1:
real_pos_start = f"{current_index}+{real_index}c"
real_pos_end = f"{real_pos_start}+4c"
self.frame.output_textbox.tag_add("real_color", real_pos_start, real_pos_end)
self.frame.output_textbox.tag_config("real_color", foreground="green")
content = content[real_index + 4:]
current_index = real_pos_end
else:
break
for word in WORDS:
start = 0
while True:
pos = content.find(word, start)
if pos == -1:
break
word_start = f"{start_index}+{pos}c"
word_end = f"{word_start}+{len(word)}c"
tag_name = f"{word.lower()}_color"
self.frame.output_textbox.tag_add(tag_name, word_start, word_end)
if word in BAD_WORDS:
self.frame.output_textbox.tag_config(tag_name, foreground=BAD_COLOR)
elif word in GOOD_COLOR:
self.frame.output_textbox.tag_config(tag_name, foreground=GOOD_COLOR)
start = pos + len(word)
def prediction(self, text_data:TextData) -> TextData:

View File

@ -20,10 +20,10 @@ class MainFrame(ctk.CTkFrame):
self.entry_url = ctk.CTkEntry(self.frame1, placeholder_text='Enter the article link', height=50)
self.entry_url.grid(row=0, column=0, padx=10, pady=10, sticky="ew")
self.input_textbox = ctk.CTkTextbox(self.frame1, height=200)
self.input_textbox = ctk.CTkTextbox(self.frame1, height=150)
self.input_textbox.grid(row=1, column=0, columnspan=2, padx=10, pady=10, sticky="nsew")
self.output_textbox = ctk.CTkTextbox(self.frame1, height=150, state="disabled")
self.output_textbox = ctk.CTkTextbox(self.frame1, height=200, state="disabled")
self.output_textbox.grid(row=2, column=0, columnspan=2, padx=10, pady=10, sticky="nsew")
# Mittlerer Button