Skip to content

Commit

Permalink
Add support for opening web pages.
Browse files Browse the repository at this point in the history
  • Loading branch information
yaph committed Jul 21, 2024
1 parent 8480ffe commit d0f6431
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
33 changes: 25 additions & 8 deletions charla/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
from operator import itemgetter
from pathlib import Path

import httpx
import ollama
from html2text import html2text
from prompt_toolkit import HTML, PromptSession
from prompt_toolkit import print_formatted_text as print_fmt
from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
Expand Down Expand Up @@ -101,7 +103,7 @@ def run(argv: argparse.Namespace) -> None:
if system_prompt:
print_fmt('Using system prompt:', HTML(f'<ansigreen>{argv.system_prompt.name}</ansigreen>'), '\n')

open_filename = None
open_filename = ''

while True:
try:
Expand All @@ -111,14 +113,29 @@ def run(argv: argparse.Namespace) -> None:
output.append(f'{session.message}{user_input}\n')

if open_filename:
try:
file_content = Path(open_filename).read_text()
except (FileNotFoundError, PermissionError) as err:
print(f'Enter name of an existing file.\n{err}\n')
open_filename = None
file_content = ''

if open_filename.startswith(('http://', 'https://')):
try:
resp = httpx.get(open_filename, follow_redirects=True)
if 'text/html' == resp.headers['content-type']:
file_content = html2text(resp.text, baseurl=open_filename)
else:
file_content = resp.text
except httpx.ConnectError as err:
print(f'Enter an existing URL.\n{err}\n')
else:
try:
file_content = Path(open_filename).read_text()
except (FileNotFoundError, PermissionError) as err:
print(f'Enter name of an existing file.\n{err}\n')

open_filename = ''

if file_content:
user_input = user_input.strip() + '\n\n' + file_content
else:
continue
user_input = user_input + '\n\n' + file_content
open_filename = None

if session.message == t_open:
open_filename = user_input.strip()
Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ classifiers = [
]
dependencies = [
"ollama",
"html2text",
"httpx",
"platformdirs",
"prompt_toolkit"
]
Expand Down

0 comments on commit d0f6431

Please sign in to comment.