develop #18

Merged
Fabel merged 65 commits from develop into main 2024-12-05 11:16:03 +00:00
2 changed files with 45 additions and 63 deletions
Showing only changes of commit 16f3bf00a4 - Show all commits

View File

@ -1,9 +1,14 @@
from collections import deque
from views.mainScreen import MainFrame from views.mainScreen import MainFrame
from models.data import TextData from models.data import TextData
from Ai.interence import VeraMindInference from Ai.interence import VeraMindInference
from Ai.llm import ArticleRater 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: class MainFrameController:
def __init__(self,frame:MainFrame) -> None: def __init__(self,frame:MainFrame) -> None:
@ -21,82 +26,59 @@ class MainFrameController:
def press_check_button(self): def press_check_button(self):
text_data = self.get_textdata() text_data = self.get_textdata()
print(f"text:{text_data}")
self.prediction(text_data) self.prediction(text_data)
self.frame.output_textbox.configure(state="normal") self.frame.output_textbox.configure(state="normal")
self.frame.output_textbox.delete("0.0", "end") self.frame.output_textbox.delete("0.0", "end")
response_stream = self.rater.get_response(text_data.text, text_data.confidence, text_data.result) 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_buffer = deque(maxlen=5)
HIGHLIGHT_BATCH_SIZE = 5
OVERLAP_SIZE = 2
for chunk in response_stream: for chunk in response_stream:
display_chunks.append(chunk) # Display the chunk immediately
highlight_chunks.append(chunk) self.frame.output_textbox.insert("end", chunk)
self.frame.output_textbox.see("end")
self.frame.update_idletasks()
# Display chunk # Add to highlight buffer
if len(display_chunks) == DISPLAY_CHUNK_SIZE: highlight_buffer.append(chunk)
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 = []
# Process highlighting # Process highlighting when buffer is full
if len(highlight_chunks) >= HIGHLIGHT_BATCH_SIZE: if len(highlight_buffer) == 5:
start_index = self.frame.output_textbox.index(f"end-{sum(len(c) for c in highlight_chunks)}c") self.process_highlighting(highlight_buffer)
end_index = self.frame.output_textbox.index("end-1c")
self.highlight_words(start_index, end_index)
# Maintain overlap and reset for next batch # Process any remaining chunks in the buffer
highlight_chunks = highlight_chunks[-OVERLAP_SIZE:] if highlight_buffer:
self.process_highlighting(highlight_buffer)
# 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)
self.frame.output_textbox.configure(state="disabled") 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): def highlight_words(self, start_index, end_index):
content = self.frame.output_textbox.get(start_index, end_index) content = self.frame.output_textbox.get(start_index, end_index)
print(content)
current_index = start_index
for word in WORDS:
start = 0
while True: while True:
# Find "FAKE" pos = content.find(word, start)
fake_index = content.find("FAKE") if pos == -1:
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 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: 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 = 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.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.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") self.output_textbox.grid(row=2, column=0, columnspan=2, padx=10, pady=10, sticky="nsew")
# Mittlerer Button # Mittlerer Button