'Vocab' object has no attribute 'GloVe' - stanford-nlp

def load_pretrained_embedding(words, pretrained_vocab_path=None, emb_size=100, type="glove"):
embed = torch.normal(mean=0, std=1, size=(len(words), emb_size))
if type == "glove":
pretrained_vocab = vocab.GloVe(name="6B", dim=100)
return pretrained_vocab
else:
return embed
As I import torchtext.vocab as vocab, why can't I use GloVe. This is correct in my local pycharm, but in kaggle notebook, it shows wrong: 'Vocab' object has no attribute 'GloVe'
The problem has solved, just use import torchtext.vocab and use torchtext.vovab.GloVe can solve the problem.

Related

Any way to limit the input arguments of a FastAPI handler into several specified options?

I'm wondering if there's someway could let me easily deal with input arguments and limit them into several values in FASTAPI.
For example if I got a hello-world handler here:
from fastapi import FastAPI
app = FastAPI()
#app.get(/)
async def root(name:str):
return {"user_name_is": name}
And what I'd like to achieve is , to let user can only input one of the following names as parameter [Bob ,Jack] , other names are all illegal.
It's not complicated to write some further check code to achieve the expected result:
from fastapi import FastAPI
app = FastAPI()
#app.get(/)
async def root(name:str):
if name in ['Bob' , 'Jack']:
return {"user_name_is": name}
else:
raise HTTPException(status_code=403)
However it's still not easy enough to write codes especially when there're lots of input arguments need to deal with. I'm wondering if there's a way I can use type-hints and pydantic to achieve the same result?
Didn't find much information in doc, need help , thanks.
=======
btw , if there's also chance I need to get a list of input prameters , is there any way to check them all , like the following code,?
from fastapi import FastAPI
from typing import List
app = FastAPI()
#app.get(/)
async def root(names:List[str]):
for name in names:
if name not in ['Bob','Jack']:
raise ...
# else ,check passed
return {"user_name_is": name}
I know this question has already an answer accepted but I feel that the cleanest way is to use the Literal typing like this:
from fastapi import FastAPI
from typing import Literal
app = FastAPI()
#app.get("/{name}")
async def root(name: Literal["Bob", "Jack"]):
return {"user_name_is": name}
Use Enum for it: https://fastapi.tiangolo.com/tutorial/path-params/?h=enum#predefined-values
from enum import Enum
import uvicorn
from fastapi import FastAPI
class Names(str, Enum):
Bob = "Bob"
Jack = "Jack"
app = FastAPI()
#app.get("/")
async def root(name: Names):
return {"user_name_is": name}
You are looking for Pydantic's #validator
See: https://pydantic-docs.helpmanual.io/usage/validators
from pydantic import BaseModel, validator
from typing import List
from fastapi import FastAPI, Depends
app = FastAPI()
class Names(BaseModel):
names: List[str]
#validator("names", pre=True, always=True)
def check_allowed_names(cls, v):
allowed_names = ["Billie", "Joe"]
for name in v:
if name not in allowed_names:
raise ValueError(f"Name {name} is not allowed")
return v
#app.post("/")
async def root(names: Names):
return {"user_name_is": names.names}

While deploying model to AKS PipelineModel.load throwing org.apache.hadoop.mapred.InvalidInputException

I am trying to deploy model to AKS. I am using AML SDK to register the model in the aml workspace. I am using PipelineModel module to save the model. And I am trying to load the model using PipelineModel.load. My entry script looks like below:
`
import os
import json
import pandas as pd
from azureml.core.model import Model
from pyspark.ml import PipelineModel
from mmlspark import ComputeModelStatistics
def init():
import mmlspark # this is needed to load mmlspark libraries
import logging
# extract and load model
global model, model_path
model_path = Model.get_model_path("{model_name}")
print(model_path)
print(os.stat(model_path))
print(os.path.exists(model_path))
#model_path = os.path.join(os.getenv("AZUREML_MODEL_DIR"), "{model_name}")
logging.basicConfig(level=logging.DEBUG)
#print(model_path)
#with ZipFile(model_path, 'r') as f:
# f.extractall('model')
model = PipelineModel.load(model_path)
#model = PipelineModel.read().load(model_path)
def run(input_json):
try:
output_df = model.transform(pd.read_json(input_json))
evaluator = ComputeModelStatistics().setScoredLabelsCol("prediction").setLabelCol("label").setEvaluationMetric("AUC")
result = evaluator.transform(predictions)
auc = result.select("AUC").collect()[0][0]
result = auc
except Exception as e:
result = str(e)
return json.dumps({{"result": result}})
`
It's giving error like below:
org.apache.hadoop.mapred.InvalidInputException: Input path does not exist: file:/var/azureml-app/azureml-models/lightgbm.model/2/lightgbm.model/metadata\n\tat org.apache.hadoop.mapred.FileInputFormat.singleThreadedListStatus(FileInputFormat.java:287)\n\tat org.apache.hadoop.mapred.FileInputFormat.listStatus(FileInputFormat.java:229)\n\tat org.apache.hadoop.mapred.FileInputFormat.getSplits(FileInputFormat.java:315).
os.path.exists returns true the path fetched from Model.get_model_path.
Am I missing something here?

reading multiple images in python

I want to read multiple images in python I'm using this code but when I run it,nothing happens.
Could you tell me what is the problem?
import glob , cv2
import numpy as np
def read_img(img_list , img):
n=cv2.imread(img)
img_list.append(n)
return img_list
path = glob.glob("02291G0AR/*.bmp")
list_ = []
cv_image = [read_img(list_,img) for img in path]
print(cv_image)
"02291G0AR" is the folder where my images are save in. and it's near my code file
Perhaps it's just a matter of the print function not being the adequate one to use. I'd try:
for img in cv_image:
cv2.imshow('image',img)

python random function not working , I am very new at python

I have used a random function in python but its not working , I am very new at python. Please review below code
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['number'] = random.randrange(1, 100)
return context
its rertuning error NameError: name 'random' is not defined
Probably, You did not import random module. Add this to the top of your script:
import random

Is it possible to use SWT from Jython?

The SWT-Gui looks very nice.
Is there an easy way to use it in Jython ?
Given that you can use all Java classes from within Jython, it is also possible to use SWT.
For the example, adapted from an SWT snippet, make sure you have the SWT jar on your CLASSPATH:
import org.eclipse.swt as swt
import org.eclipse.swt.widgets as widgets
import org.eclipse.swt.layout as layout
result = None
display = widgets.Display()
shell = widgets.Shell(display)
shell.pack()
shell.open()
dialog = widgets.Shell(shell, swt.SWT.DIALOG_TRIM | swt.SWT.APPLICATION_MODAL)
dialog.setLayout(layout.RowLayout())
ok = widgets.Button(dialog, swt.SWT.PUSH)
ok.setText ("OK")
cancel = widgets.Button(dialog, swt.SWT.PUSH);
cancel.setText("Cancel");
class MyListener(widgets.Listener):
def handleEvent(self, event):
global result
result = event.widget == ok
dialog.close()
listener = MyListener()
ok.addListener(swt.SWT.Selection, listener)
cancel.addListener(swt.SWT.Selection, listener)
dialog.pack()
dialog.open()
while not dialog.isDisposed():
if not display.readAndDispatch():
display.sleep ()
print "Result:", result
display.dispose()
Jython has a few other niceties that makes the code cleaner.
Jython automagically translates getters & setters into public properties so that
ok.setText ("OK")
becomes simply
ok.text = 'OK'
You can then supply them as named arguments to the constructor. Jython also handles creating listener objects for your event handlers:
def handleEvent(self, event):
global result
result = event.widget == ok
dialog.close()
ok = widgets.Button(dialog, swt.SWT.PUSH
text='OK',
widgetSelected=handleEvent)
cancel = widgets.Button(dialog, swt.SWT.PUSH
text='Cancel',
widgetSelected=handleEvent)
Actually, there is no need for a special module. This talk by Sean McGrath contains a simple example of a Jython/SWT GUI.
Slide 11 of the talk begins with:
"""
Simple SWT Example
Sean McGrath
"""
from org.eclipse.swt.events import *
from org.eclipse.swt.graphics import *
from org.eclipse.swt.layout import *
from org.eclipse.swt.widgets import *
from org.eclipse.swt.layout.GridData import *
from org.eclipse.swt import *
It shows that SWT is directly usable from Jython.
The full example is right there at Sean's site.

Resources