-
Notifications
You must be signed in to change notification settings - Fork 357
Added CLI entry point for standalone execution. #87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
muhannad-iz-a-tech-nerd
wants to merge
1
commit into
ahmadfaizalbh:master
Choose a base branch
from
muhannad-iz-a-tech-nerd:patch-1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,56 @@ | ||
| from chatbot import Chat, register_call | ||
| import wikipedia | ||
| #!/usr/bin/env python3 | ||
| # -*- coding: utf-8 -*- | ||
| # | ||
| # Enhanced Chatbot using Wikipedia and Template-based NLP | ||
| # | ||
| # | ||
| # Enhanced by: Muhannad - github.com/muhannad-iz-a-tech-nerd | ||
| # | ||
| # Features: | ||
| # - Handles "who is" questions via Wikipedia. | ||
| # - Uses template-based conversation. | ||
| # - Improved error handling and logging. | ||
| # - Simple CLI interface. | ||
|
|
||
| import os | ||
| import warnings | ||
| import wikipedia | ||
| from chatbot import Chat, register_call | ||
|
|
||
| # Suppress unnecessary warnings | ||
| warnings.filterwarnings("ignore") | ||
|
|
||
| # Optional but setting the language to English. | ||
| wikipedia.set_lang("en") | ||
|
|
||
| @register_call("whoIs") | ||
| def who_is(session, query): | ||
| """Fetches a Wikipedia summary for a given query.""" | ||
| try: | ||
| return wikipedia.summary(query) | ||
| except Exception: | ||
| except Exception as e: | ||
| # Fallback: search for similar topics | ||
| for new_query in wikipedia.search(query): | ||
| try: | ||
| return wikipedia.summary(new_query) | ||
| except Exception: | ||
| pass | ||
| return "I don't know about "+query | ||
| continue | ||
| return f"Sorry, I couldn't find anything about '{query}'." | ||
|
|
||
| def main(): | ||
| # Initial question for the chatbot as a beginning | ||
| first_question = "Hi, how are you?" | ||
|
|
||
|
|
||
| template_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "Example.template") | ||
|
|
||
| if not os.path.exists(template_path): | ||
| print(f"Error: Template file not found at {template_path}") | ||
| return | ||
|
|
||
|
|
||
| chat = Chat(template_path) | ||
| chat.converse(first_question) | ||
|
|
||
| first_question = "Hi, how are you?" | ||
| chat = Chat(os.path.join(os.path.dirname(os.path.abspath(__file__)), "Example.template")) | ||
| chat.converse(first_question) | ||
| if __name__ == "__main__": | ||
| main() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Early return on missing template prevents user feedback in CLI.
Recommend replacing 'return' with 'sys.exit(1)' to provide a standard error exit code for CLI tools, aiding automation and scripting.