Skip to content
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

Optional Spacy #269

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions convokit/__init__.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,50 @@
import warnings
import sys
import types

class LazyModule(types.ModuleType):
def __init__(self, module_name):
self._module_name = module_name

def __getattr__(self, attr):
raise ImportError(
f"{self._module_name} requires spaCy, which is not installed. Please install it with \"pip install convokit[spacy]\""
)

try:
from .model import *
from .util import *
from .coordination import *
from .politenessStrategies import *
from .transformer import *
from .convokitPipeline import *
from .hyperconvo import *
from .speakerConvoDiversity import *
from .text_processing import *
from .phrasing_motifs import *
from .prompt_types import *
from .classifier import *
from .ranker import *
from .forecaster import *
from .fighting_words import *
from .paired_prediction import *
from .bag_of_words import *
from .expected_context_framework import *
from .surprise import *
from .convokitConfig import *

try:
import spacy
from .politenessStrategies import *
from .text_processing import *
from .phrasing_motifs import *
from .expected_context_framework import *
from .prompt_types import *
except ImportError:
warnings.warn("spaCy is not installed, skipping spaCy-dependent modules.")
sys.modules["convokit.politenessStrategies"] = LazyModule("politenessStrategies")
sys.modules["convokit.text_processing"] = LazyModule("text_processing")
sys.modules["convokit.phrasing_motifs"] = LazyModule("phrasing_motifs")
sys.modules["convokit.expected_context_framework"] = LazyModule("expected_context_framework")
sys.modules["convokit.prompt_types"] = LazyModule("prompt_types")

except Exception as e:
print(f"An error occurred: {e}")
warnings.warn(
"If you are using ConvoKit with Google Colab, incorrect versions of some packages (ex. scipy) may be imported while runtime start. To fix the issue, restart the session and run all codes again. Thank you!"
)


# __path__ = __import__('pkgutil').extend_path(__path__, __name__)
)
5 changes: 4 additions & 1 deletion convokit/politenessStrategies/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
from .politenessStrategies import *
try:
from .politenessStrategies import *
except ImportError:
raise ImportError("spaCy is not installed, please install install spaCy with \"pip install convokit[spacy]\" to use this package.")
12 changes: 11 additions & 1 deletion convokit/text_processing/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
from .textProcessor import *
from .textParser import *
from .textToArcs import *
from .textCleaner import TextCleaner
import warnings
try:
from .textParser import *
except Exception:
class TextParser:
def __init__(self, *args, **kwargs):
raise ImportError(
"spaCy is required to use TextParser. "
"Please install it using `pip install spacy`."
)
warnings.warn("spaCy is not installed, textParser and textParser dependent subpackages are skipped.")
8 changes: 7 additions & 1 deletion convokit/text_processing/textParser.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import nltk
import spacy
import sys

import warnings

try:
import spacy
except ImportError:
raise Exception("Spacy is required to use TextParser or TextParser dependent subpackages")


from spacy.pipeline import Sentencizer


Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
"pandas>=1.5.0",
"numpy>=2.0.0",
"msgpack-numpy>=0.4.3.2",
"spacy>=3.8.2",
"scipy>=1.1.0",
"scikit-learn>=1.0",
"nltk>=3.4",
Expand All @@ -74,6 +73,7 @@
],
extras_require={
"craft": ["torch>=0.12"],
"spacy": ["spacy>=3.8.2"],
},
classifiers=[
"Programming Language :: Python",
Expand Down
Loading