This repository has been archived on 2025-04-28. You can view files and clone it, but cannot push or open issues or pull requests.
gensec-westgate-djw2/hw4/app.py
2024-05-09 22:14:42 -07:00

70 lines
2.5 KiB
Python

from langchain_community.document_loaders.generic import GenericLoader
from langchain_community.document_loaders.parsers import LanguageParser
from langchain_openai import ChatOpenAI
from langchain_core.runnables import RunnablePassthrough
from langchain_core.prompts import PromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_community.document_loaders import AsyncHtmlLoader
from dotenv import load_dotenv
from validators import url
from time import time
load_dotenv()
llm = ChatOpenAI(model_name="gpt-4-turbo", temperature=0)
"""
Per the prompt below, This agent will de-obfuscate and de-minify javascript source files, from either the web or a local source, and will apply appropriate commenting and formatting.
A summary of actions taken will be provided, or a reason for rejection will be provided.
An elapsed time will also be shown.
derived from https://github.com/wu4f/cs410g-src/blob/main/05_CodeSummarize/01_python_parser.py
"""
def deobfuscate(path: str):
if url(path):
loader = AsyncHtmlLoader(path)
else:
loader = GenericLoader.from_filesystem(
path,
glob="*",
suffixes=[".js"],
parser=LanguageParser(),
)
docs = loader.load()
prompt = PromptTemplate.from_template(
"""You are an expert javascript de-obfuscater and de-minifier. Carefully analyze the following code and de-obduscate/de-minify, by applying proper formatting, commenting, and re-naming as necessary.
Along with this response, make a note of the changes made. If the input is anything other than obfuscated or minified javascript, respond with "Invalid Input:" follwed by the reason why.
{text}
"""
)
chain = {"text": RunnablePassthrough()} | prompt | llm | StrOutputParser()
output = "\n".join([d.page_content for d in docs])
result = chain.invoke(output)
return result
print(
"Welcome to my javascript code de-obfuscator. Supply a local or web path to an obfuscated or minified javascript file and I will attempt to make it readable."
)
while True:
try:
line: str = input("llm>> ")
if line:
start_time = time()
result: str = deobfuscate(line)
end_time = time()
elapsed_time = round(end_time - start_time, 2)
print("\n", result, "\n\nElapsed time: ", elapsed_time, " seconds")
else:
break
except Exception as e:
print(e)
break