Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

fix(flake8): Fix flake8 errors in app.py. #9

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed .DS_Store
Binary file not shown.
3 changes: 2 additions & 1 deletion app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

socketio = SocketIO(app)


@app.context_processor
def utility_processor():
"""
Expand Down Expand Up @@ -206,7 +207,7 @@ def video(play_filename):
global filename
filename = play_filename
video_data = utils.get_video_data(filename)
if video_data['processed'] == False and video_data['processing'] == False:
if video_data['processed'] is False and video_data['processing'] is False:
Comment on lines -209 to +210
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these do not work if you use "is" we had to use "=="

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted. Although I am failing to understand why it is not working. Is it in more cases than just here? Going through debugging at this line, the if block is entered for an unprocessed, and not being processed video, and not entered if the video is processing or processed.

print(filename)
threading.Thread(target=pre_process.process_video, args=(str(filename), socketio)).start()
return render_template("player.html", filename=filename, video_data=video_data)
Expand Down
12 changes: 7 additions & 5 deletions app/extract_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def format_raw_ocr_string(extracted_text: str) -> str:
formatted_text = extracted_text
print(formatted_text)
if config("Formatting", "openai_analysis"):
if(openai.api_key == None or openai.api_key == ""):
if (openai.api_key is None or openai.api_key == ""):
prompt = ExtractText.formatted_prompt(formatted_text, language)
formatted_text = Llama.query(prompt)
else:
Expand All @@ -62,10 +62,12 @@ def format_raw_ocr_string(extracted_text: str) -> str:
@staticmethod
def formatted_prompt(extracted_text: str, language: str) -> str:
return f"Analyse the following {language} code snippet:\n\n{extracted_text}\n\n" \
f"If no '{language}' code is present, say 'No Code' and disregard the remaining prompt otherwise if '{language}' code is detected," \
"If The Code is incomplete or has errors then prfix with 'Incomplete Code'" \
f"correct any basic syntax errors, indentation errors, but do not add any code that does not exist in the sample and make sure to preserve comments" \
f"Do NOT return any explanations, only code. Do NOT return leading or trailing backticks "
f"If no '{language}' code is present, say 'No Code' and disregard the remaining prompt otherwise if" \
f"'{language}' code is detected," \
"If The Code is incomplete or has errors then prefix with 'Incomplete Code'" \
"correct any basic syntax errors, indentation errors, but do not add any code that does not exist" \
"in the sample and make sure to preserve comments" \
f"Do NOT return any explanations, only code. Do NOT return leading or trailing backticks"

@staticmethod
def extract_frame_at_timestamp(filename: str, timestamp: float) -> Union[cv2.VideoCapture, None]:
Expand Down
24 changes: 13 additions & 11 deletions app/pre_process.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from utils import config, get_vid_save_path, update_user_video_data, get_video_data
from utils import get_vid_save_path, update_user_video_data, get_video_data
import cv2
from PIL import Image
import pytesseract
Expand All @@ -7,21 +7,23 @@
from utils import config
import time


def run_ocr(ret, frame):
temp_frame = frame
if ret == True:
if ret is True:
cv2.imwrite('temp.png', temp_frame)
return pytesseract.image_to_string(Image.open('temp.png'))
else:
return None

def formatted_prompt() -> str:
return f"Analyse the following %LANGUAGE% code snippet:\n\n%QUESTION%\n\n" \
f"If no '%LANGUAGE%' code is present, say 'No Code' and disregard the remaining prompt. Otherwise if '%LANGUAGE%' code is detected:" \
"If The Code is incomplete or has errors then prefix with 'Incomplete Code'" \
f"correct any indentation errors, but do not add any code that does not exist in the sample and make sure to preserve comments" \
f"Do NOT embellish the code, simply return the code as a codeblock or 'No Code'"


def seconds_to_timestamp(seconds):
minutes = seconds // 60
seconds = seconds % 60
Expand All @@ -31,7 +33,7 @@ def seconds_to_timestamp(seconds):
def process_video(video_file_name, socketio):
print(f"Processing video {video_file_name}")
cap = cv2.VideoCapture(get_vid_save_path() + video_file_name)
if not cap.isOpened():
if not cap.isOpened():
print("Error opening video file")
Llama.set_prompt(formatted_prompt())
language = config("UserSettings", "programming_language")
Expand All @@ -51,21 +53,21 @@ def process_video(video_file_name, socketio):
text = run_ocr(*cap.read())
response = Llama.query_with_default(text, language)
#print(response)
if("```" in response):
if "```" in response:
response = response.split("```")[1]
print(response)

if("No Code" not in response and step_seconds not in steps_with_code): #Did we find code?
if "No Code" not in response and step_seconds not in steps_with_code: # Did we find code?
dictEntry = {'timestamp': step_seconds, 'capture_content': response}
update_user_video_data(video_file_name, None, dictEntry)
steps_with_code.append(step_seconds)
socketio.emit('update_timestamps', data=video_file_name)
if(was_last_step_code == False): #If we didn't find code last time, we want to skip back a bit
if not was_last_step_code: # If we didn't find code last time, we want to skip back a bit
step_seconds -= 4
else: #If we did find code last time, we want to skip forward a bit
step_seconds += 1
else: # If we did find code last time, we want to skip forward a bit
step_seconds += 1
was_last_step_code = True
else: #We didn't find code skip forward
else: # We didn't find code skip forward
step_seconds += 5
was_last_step_code = False
update_user_video_data(video_file_name, None, None, True, False)
Expand Down Expand Up @@ -96,4 +98,4 @@ def format_user_data(video_file_name, timestamp, dictEntry):
video_data['captures'].sort(key=lambda x: x['timestamp'])

# Save updated video data
update_user_video_data(video_file_name, timestamp, video_data)
update_user_video_data(video_file_name, timestamp, video_data)
1 change: 0 additions & 1 deletion app/remote_llama.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import requests
import json
import time


Expand Down
Loading