Skip to content

Commit

Permalink
bookmark bar work
Browse files Browse the repository at this point in the history
  • Loading branch information
ilude committed May 8, 2024
1 parent 42b4389 commit bc4fff2
Show file tree
Hide file tree
Showing 9 changed files with 342 additions and 751 deletions.
1 change: 1 addition & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"version": "0.2.0",
"configurations": [



{
"name": "Python Debugger: Flask",
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ reqs: $(SITE_PACKAGES)

$(SITE_PACKAGES): requirements.txt
pip install -r requirements.txt
touch requirements.txt
-sudo touch $(SITE_PACKAGES)

ansible:
sudo chown -R ${USER}:${USER} ~/
Expand Down
8 changes: 4 additions & 4 deletions app/models/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ def __init__(self, config_file: str = "configs/layout.yml", bookmarks_bar_file:
self.bookmark_bar_path = pwd.joinpath(bookmarks_bar_file)

try:
if not os.path.exists(pwd.joinpath('configs/bookmarks.json')):
with open(pwd.joinpath('configs/bookmarks.json'), 'w', encoding='utf-8') as f:
if not os.path.exists(pwd.joinpath(self.bookmark_bar_path)):
with open(pwd.joinpath(self.bookmark_bar_path), 'w', encoding='utf-8') as f:
json.dump([], f)
except Exception as ex:
logger.error(f"Error: {ex} creating empty bookmark bar file at {self.bookmark_bar_path}")
Expand All @@ -39,10 +39,10 @@ def __init__(self, config_file: str = "configs/layout.yml", bookmarks_bar_file:

def load_bookmarks(self):
try:
with open(pwd.joinpath('configs/bookmarks.json'), 'r', encoding='utf-8') as f:
with open(pwd.joinpath(self.bookmark_bar_path), 'r', encoding='utf-8') as f:
return json.load(f)
except Exception as ex:
logger.error(f"Error: {ex} loading bookmarks")
logger.error(f"Error: Loading bookmark bar file from {self.bookmark_bar_path}", ex)
return None

def stop_scheduler(self):
Expand Down
113 changes: 57 additions & 56 deletions app/processors/title_editor.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
from datetime import datetime, timedelta
import logging
import os
from pathlib import Path

from pytz import utc
from models.feed_article import FeedArticle
from langchain_community.llms import Ollama
from langchain.prompts import ChatPromptTemplate, PromptTemplate, HumanMessagePromptTemplate
from langchain_core.messages import SystemMessage
from langchain.output_parsers import ResponseSchema, StructuredOutputParser
from langchain.callbacks.tracers import ConsoleCallbackHandler
# from langchain.callbacks.tracers import ConsoleCallbackHandler
from models.utils import calculate_sha1_hash

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)


class TitleEditor:
def __init__(self):
self.ollama_url = os.getenv('OLLAMA_URL')
if self.ollama_url:
parser = StructuredOutputParser.from_response_schemas(
[ResponseSchema(name="title", description="title of the article")]
)
format_instructions = f"""
{parser.get_format_instructions()}
The contents of the markdown code snippet MUST be in VALID json format which includes proper quotes and escape characters!
JSON property names are case sensitive, and MUST ONLY include the defined schema properties!
"""
def __init__(self):
self.ollama_url = os.getenv('OLLAMA_URL')
if self.ollama_url:
parser = StructuredOutputParser.from_response_schemas(
[ResponseSchema(name="title", description="title of the article")]
)

format_instructions = f"""
{parser.get_format_instructions()}
The contents of the markdown code snippet MUST be in VALID json format which includes proper quotes and escape characters!
JSON property names are case sensitive, and MUST ONLY include the defined schema properties!
"""

prompt = PromptTemplate(
template="""
prompt = PromptTemplate(
template="""
title: {title},
summary: {summary},
{format_instructions}
""",
input_variables=["title", "summary"],
partial_variables={"format_instructions": format_instructions},
)
input_variables=["title", "summary"],
partial_variables={"format_instructions": format_instructions},
)

# format chat prompt
system_prompt = SystemMessage(content=("""
# format chat prompt
system_prompt = SystemMessage(content=("""
You are an expert news article title editor.
Use the provided title and summary to write a concise and accurate title that is informative and avoids sounding like clickbait.
Do not include links or urls in the title.
Expand All @@ -50,41 +50,42 @@ def __init__(self):
title MUST NOT use words that are all capitalized. NO SHOUTING!
Only return the title in the requested format!
"""))
user_prompt = HumanMessagePromptTemplate(prompt=prompt)

chat_prompt = ChatPromptTemplate.from_messages([system_prompt, user_prompt])

model_name = "dolphin-llama3"
model_temp = 0.0
llama3_model = Ollama(base_url=self.ollama_url, model=model_name, keep_alive=5, temperature=model_temp)
self.llama3_chain = chat_prompt | llama3_model | parser
user_prompt = HumanMessagePromptTemplate(prompt=prompt)

chat_prompt = ChatPromptTemplate.from_messages([system_prompt, user_prompt])

model_name = "dolphin-llama3"
model_temp = 0.0
llama3_model = Ollama(base_url=self.ollama_url, model=model_name, keep_alive=5, temperature=model_temp)
self.llama3_chain = chat_prompt | llama3_model | parser

model_name = "dolphin-mistral"
model_temp = 0.0
mistral_model = Ollama(base_url=self.ollama_url, model=model_name, keep_alive=5, temperature=model_temp)
self.mistral_chain = chat_prompt | mistral_model | parser

self.script_hash = calculate_sha1_hash(f"{system_prompt.content}{model_name}{model_temp}")

def process(self, articles: list[FeedArticle]) -> list[FeedArticle]:
if self.ollama_url:

model_name = "dolphin-mistral"
model_temp = 0.0
mistral_model = Ollama(base_url=self.ollama_url, model=model_name, keep_alive=5, temperature=model_temp)
self.mistral_chain = chat_prompt | mistral_model | parser

self.script_hash = calculate_sha1_hash(f"{system_prompt.content}{model_name}{model_temp}")
needs_processed = list(filter(lambda article: article.processed != self.script_hash, articles))
if len(needs_processed) > 10:
needs_processed = list(filter(lambda article: article.pub_date.replace(tzinfo=utc) >=
(datetime.now() - timedelta(days=1)).replace(tzinfo=utc), articles))

def process(self, articles: list[FeedArticle]) -> list[FeedArticle]:
if self.ollama_url:

needs_processed = list(filter(lambda article: article.processed != self.script_hash, articles))
if len(needs_processed) > 10:
needs_processed = list(filter(lambda article: article.pub_date.replace(tzinfo=utc) >= (datetime.now() - timedelta(days=1)).replace(tzinfo=utc), articles))
total = len(needs_processed)
for count, article in enumerate(needs_processed, start=1):
for chain in [self.llama3_chain, self.mistral_chain]:
try:
logger.info(f"Processing title {count}/{total}: {article.original_title}")
# , config={'callbacks': [ConsoleCallbackHandler()]})
result = chain.invoke({"title": article.original_title, "summary": article.description})
article.title = result['title']
article.processed = self.script_hash
break
except Exception as ex:
logger.error(f"Error: {ex} for {article.original_title}")
# needs_processed.remove(article)


total = len(needs_processed)
for count, article in enumerate(needs_processed, start=1):
for chain in [self.llama3_chain, self.mistral_chain]:
try:
logger.info(f"Processing title {count}/{total}: {article.original_title}")
result = chain.invoke({"title": article.original_title, "summary": article.description}) #, config={'callbacks': [ConsoleCallbackHandler()]})
article.title = result['title']
article.processed = self.script_hash
break
except Exception as ex:
logger.error(f"Error: {ex} for {article.original_title}")
#needs_processed.remove(article)

return articles
return articles
Loading

0 comments on commit bc4fff2

Please sign in to comment.