Spaces:
Sleeping
Sleeping
gradio chat entrypoint moved to cli
Browse files- Dockerfile +1 -1
- app.py → hf_transformers_demo.py +0 -0
- src/knowlang/chat_bot/gradio_demo.py +0 -7
- src/knowlang/cli/commands/chat.py +48 -0
- src/knowlang/cli/parser.py +38 -1
- src/knowlang/cli/types.py +10 -1
Dockerfile
CHANGED
@@ -15,4 +15,4 @@ RUN pip install --no-cache-dir .
|
|
15 |
EXPOSE 7860
|
16 |
|
17 |
# Step 6: Command to run your Gradio app (or any Python script)
|
18 |
-
CMD ["python", "
|
|
|
15 |
EXPOSE 7860
|
16 |
|
17 |
# Step 6: Command to run your Gradio app (or any Python script)
|
18 |
+
CMD ["python", "hf_transformers_demo.py"]
|
app.py → hf_transformers_demo.py
RENAMED
File without changes
|
src/knowlang/chat_bot/gradio_demo.py
DELETED
@@ -1,7 +0,0 @@
|
|
1 |
-
from knowlang.chat_bot.chat_interface import create_chatbot
|
2 |
-
from knowlang.configs.config import AppConfig
|
3 |
-
|
4 |
-
|
5 |
-
config = AppConfig()
|
6 |
-
demo = create_chatbot(config)
|
7 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
src/knowlang/cli/commands/chat.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Command implementation for the chat interface."""
|
2 |
+
import chromadb
|
3 |
+
from knowlang.chat_bot.chat_interface import create_chatbot
|
4 |
+
from knowlang.configs.config import AppConfig
|
5 |
+
from knowlang.cli.types import ChatCommandArgs
|
6 |
+
from knowlang.utils.fancy_log import FancyLogger
|
7 |
+
|
8 |
+
LOG = FancyLogger(__name__)
|
9 |
+
|
10 |
+
def create_config(args: ChatCommandArgs) -> AppConfig:
|
11 |
+
"""Create configuration from file or defaults."""
|
12 |
+
if args.config:
|
13 |
+
with open(args.config, 'r') as file:
|
14 |
+
config_data = file.read()
|
15 |
+
return AppConfig.model_validate_json(config_data)
|
16 |
+
return AppConfig()
|
17 |
+
|
18 |
+
async def chat_command(args: ChatCommandArgs) -> None:
|
19 |
+
"""Execute the chat command.
|
20 |
+
|
21 |
+
Args:
|
22 |
+
args: Typed command line arguments
|
23 |
+
"""
|
24 |
+
config = create_config(args)
|
25 |
+
|
26 |
+
# Verify database exists
|
27 |
+
try:
|
28 |
+
db_client = chromadb.PersistentClient(path=str(config.db.persist_directory))
|
29 |
+
db_client.get_collection(name=config.db.collection_name)
|
30 |
+
except Exception as e:
|
31 |
+
LOG.error(
|
32 |
+
"Database not found. Please run 'knowlang parse' first to index your codebase."
|
33 |
+
f"\nError: {str(e)}"
|
34 |
+
)
|
35 |
+
return
|
36 |
+
|
37 |
+
# Create and launch chatbot
|
38 |
+
demo = create_chatbot(config)
|
39 |
+
|
40 |
+
launch_kwargs = {
|
41 |
+
"server_port": args.server_port,
|
42 |
+
"server_name": args.server_name or "127.0.0.1",
|
43 |
+
"share": args.share,
|
44 |
+
}
|
45 |
+
if args.port:
|
46 |
+
launch_kwargs["port"] = args.port
|
47 |
+
|
48 |
+
demo.launch(**launch_kwargs)
|
src/knowlang/cli/parser.py
CHANGED
@@ -3,7 +3,8 @@ import argparse
|
|
3 |
from pathlib import Path
|
4 |
from typing import Union
|
5 |
|
6 |
-
from knowlang.cli.
|
|
|
7 |
from knowlang.cli.commands.parse import parse_command
|
8 |
|
9 |
def _convert_to_args(parsed_args: argparse.Namespace) -> Union[ParseCommandArgs, BaseCommandArgs]:
|
@@ -20,6 +21,15 @@ def _convert_to_args(parsed_args: argparse.Namespace) -> Union[ParseCommandArgs,
|
|
20 |
output=parsed_args.output,
|
21 |
command="parse"
|
22 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
return BaseCommandArgs(**base_args)
|
25 |
|
@@ -70,6 +80,33 @@ def create_parser() -> argparse.ArgumentParser:
|
|
70 |
help="Path to codebase directory or repository URL"
|
71 |
)
|
72 |
parse_parser.set_defaults(func=parse_command)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
|
74 |
return parser
|
75 |
|
|
|
3 |
from pathlib import Path
|
4 |
from typing import Union
|
5 |
|
6 |
+
from knowlang.cli.commands.chat import chat_command
|
7 |
+
from knowlang.cli.types import ChatCommandArgs, ParseCommandArgs, BaseCommandArgs
|
8 |
from knowlang.cli.commands.parse import parse_command
|
9 |
|
10 |
def _convert_to_args(parsed_args: argparse.Namespace) -> Union[ParseCommandArgs, BaseCommandArgs]:
|
|
|
21 |
output=parsed_args.output,
|
22 |
command="parse"
|
23 |
)
|
24 |
+
elif parsed_args.command == "chat":
|
25 |
+
return ChatCommandArgs(
|
26 |
+
**base_args,
|
27 |
+
command="chat",
|
28 |
+
port=parsed_args.port,
|
29 |
+
share=parsed_args.share,
|
30 |
+
server_port=parsed_args.server_port,
|
31 |
+
server_name=parsed_args.server_name
|
32 |
+
)
|
33 |
|
34 |
return BaseCommandArgs(**base_args)
|
35 |
|
|
|
80 |
help="Path to codebase directory or repository URL"
|
81 |
)
|
82 |
parse_parser.set_defaults(func=parse_command)
|
83 |
+
|
84 |
+
# Chat command
|
85 |
+
chat_parser = subparsers.add_parser(
|
86 |
+
"chat",
|
87 |
+
help="Launch the chat interface"
|
88 |
+
)
|
89 |
+
chat_parser.add_argument(
|
90 |
+
"--port",
|
91 |
+
type=int,
|
92 |
+
help="Port to run the interface on"
|
93 |
+
)
|
94 |
+
chat_parser.add_argument(
|
95 |
+
"--share",
|
96 |
+
action="store_true",
|
97 |
+
help="Create a shareable link"
|
98 |
+
)
|
99 |
+
chat_parser.add_argument(
|
100 |
+
"--server-port",
|
101 |
+
type=int,
|
102 |
+
help="Port to run the server on (if different from --port)"
|
103 |
+
)
|
104 |
+
chat_parser.add_argument(
|
105 |
+
"--server-name",
|
106 |
+
type=str,
|
107 |
+
help="Server name to listen on (default: 0.0.0.0)"
|
108 |
+
)
|
109 |
+
chat_parser.set_defaults(func=chat_command)
|
110 |
|
111 |
return parser
|
112 |
|
src/knowlang/cli/types.py
CHANGED
@@ -14,4 +14,13 @@ class ParseCommandArgs(BaseCommandArgs):
|
|
14 |
"""Arguments for the parse command."""
|
15 |
path: Path
|
16 |
output: Literal["table", "json"]
|
17 |
-
command: Literal["parse"] # for command identification
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
"""Arguments for the parse command."""
|
15 |
path: Path
|
16 |
output: Literal["table", "json"]
|
17 |
+
command: Literal["parse"] # for command identification
|
18 |
+
|
19 |
+
@dataclass
|
20 |
+
class ChatCommandArgs(BaseCommandArgs):
|
21 |
+
"""Arguments for the chat command."""
|
22 |
+
command: Literal["chat"]
|
23 |
+
port: Optional[int] = None
|
24 |
+
share: bool = False
|
25 |
+
server_port: Optional[int] = None
|
26 |
+
server_name: Optional[str] = None
|