Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhrankan-Chakrabarti authored May 17, 2024
1 parent 8750da0 commit 02b7c8a
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 0 deletions.
65 changes: 65 additions & 0 deletions console_explorer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from console_menu import Console, Menu
import os

sep = '\\' if os.name == 'nt' else '/'


def _browse(return_type='folder', extensions_list=None):
cwd = os.getcwd().split(sep)
_cwd = cwd[:]
path = _path = '/'.join(cwd)
dirs_list = []
console = Console(width=59, height=30)
while True:
text = (
_path
)

options = {}
if return_type == 'folder':
options[_path] = 'Choose this folder'
parentdir = '/'.join(_cwd[:-1]) or '/'
if _path != '/':
options[parentdir] = chr(128193) + ' ..'

try:
files_list = []
for element in os.listdir(_path):
if os.path.isdir(_path + '/' + element):
options[_path + '/' + element] = chr(128193) + ' ' + element
elif extensions_list is None or element.split('.')[-1] in extensions_list:
files_list.append(element)
for file in files_list:
options[_path + '/' + file] = chr(128196) + ' ' + file
del files_list
except:
if path in options:
for dir in dirs_list:
if _path == dir[0]:
options['/'.join(dir)] = chr(128193) + ' ' + dir[1]
break
else:
options[path] = chr(128193) + ' ' + cwd[-1]
if ('/'.join(cwd[:-1]), cwd[-1]) not in dirs_list:
dirs_list.append(('/'.join(cwd[:-1]), cwd[-1]))
starting_menu = Menu(text, options, console)
starting_menu.print_menu(justify="left")
starting_menu.print_options(justify="left")
a = starting_menu.choice()

if return_type == 'filename' and not os.path.isdir(a):
return a
elif return_type == 'folder' and a == _path:
return a
path = _path
cwd = path.split('/')
_path = a
_cwd = _path.split('/')


def browse_for_file(extensions_list=None):
return _browse('filename', extensions_list=extensions_list)


def browse_for_folder():
return _browse()
69 changes: 69 additions & 0 deletions console_menu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
from getch import getch
from rich.console import Console
from rich.panel import Panel
from rich.text import Text
from rich import box
import os

cls = 'cls' if os.name == 'nt' else 'clear'


class Menu:
def __init__(self, text, options, console):
self.text = text
self.options = options
self.current_option = 0
self.console = console

def print_menu(self, justify="center"):
self.justify_menu = justify
self.console.print(Panel(self.text, width=64), justify=justify)

def print_options(self, justify="center"):
self.justify_options = justify
current_option_key = list(self.options.keys())[self.current_option]
options_text = ""
for option in self.options:
if option == current_option_key:
options_text += "> " + self.options[option] + " <\n"
else:
options_text += self.options[option] + "\n"
options_text = options_text.rstrip("\n")

self.console.print(
Panel(Text(options_text, justify=justify), box=box.SIMPLE, width=64),
justify=justify,
)

def on_press(self, key):
if key == '\x1b' and getch() == '[':
key = getch()
if key == 'A':
if self.current_option > 0:
self.current_option -= 1
os.system(cls)
self.print_menu(self.justify_menu)
self.print_options(self.justify_options)

elif key == 'B':
if self.current_option < len(self.options) - 1:
self.current_option += 1
os.system(cls)
self.print_menu(self.justify_menu)
self.print_options(self.justify_options)

elif key == '\n':
os.system(cls)
return False

else:
os.system(cls)
self.print_menu(self.justify_menu)
self.print_options(self.justify_options)

def choice(self, listener=''):
while listener != '\n':
listener = getch()
self.on_press(listener)

return list(self.options.keys())[self.current_option]
26 changes: 26 additions & 0 deletions llama_cpp_chatbot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from console_explorer import *

file = browse_for_file(extensions_list=('gguf',))

import llama_cpp

# Create a new Llama object
llama_obj = llama_cpp.Llama(model_path=file, verbose=False)

# REPL loop
while True:
# Read user input
user_input = input(">> ")

# Check for exit command
if user_input.lower() == "exit":
break

try:
# Evaluate the input using the llama_obj
tokens = llama_obj.tokenize(user_input.encode())
for token in llama_obj.generate(tokens, top_k=40, top_p=0.95, temp=1.0, repeat_penalty=1.1):
print(llama_obj.detokenize([token]).decode(), end="", flush=True)
print()
except BaseException as e:
print("\n" + repr(e).split("(")[0] + (":" if str(e) else ""), e)
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
getch
rich
llama-cpp-python

0 comments on commit 02b7c8a

Please sign in to comment.