trouble importing enums from google.cloud.speech_v1 - python

I have this code:
from google.cloud import speech_v1
from google.cloud.speech_v1 import enums
import os
import importlib
# Import the enums module from the google.cloud.speech_v1 package
enums = importlib.import_module("google.cloud.speech_v1.enums")
# Set your Google Cloud project and service account credentials
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "creds.json"
# Create a client for the Google Cloud Speech-to-Text API
stt_client = speech_v1.SpeechClient()
# Transcribe the audio data
response = stt_client.recognize(
audio=speech_v1.types.RecognitionAudio(uri="gs://focus-0/speech-to-text-sample.wav"),
config=speech_v1.types.RecognitionConfig(
encoding=enums.RecognitionConfig.AudioEncoding.FLAC,
sample_rate_hertz=48000,
language_code="en-US"
)
)
# Print the transcribed text
for result in response.results:
print("Transcription: {}".format(result.alternatives[0].transcript))
When I run it, I get this:
Traceback (most recent call last):
File "/Users/dir/git/fp-scrapers/speech/1-STT.py", line 5, in <module>
from google.cloud.speech_v1 import enums
ImportError: cannot import name 'enums' from 'google.cloud.speech_v1' (/opt/homebrew/lib/python3.9/site-packages/google/cloud/speech_v1/__init__.py)
I have tried several ways to import enums, but none of them have worked.
Does anyone see what I'm doing wrong?

enums and types have been removed in the 2.x versions of the library
Mentioned in this github.Refer to this migration guide. You can refer to this quick start for setup instructions and an updated client library
Before:
from google.cloud import speech
encoding = speech.enums.RecognitionConfig.AudioEncoding.LINEAR16
audio = speech.types.RecognitionAudio(content=content)
After:
from google.cloud import speech
encoding = speech.RecognitionConfig.AudioEncoding.LINEAR16
audio = speech.RecognitionAudio(content=content)

Related

Trouble importing filters using Pyrogram

I am facing this error and I do not know how to solve it and I think I have written my code correctly
File "C:\Users\Revenger\index.py", line 1, in <module>
from pyrogram import Client, Filters
ImportError: cannot import name 'Filters' from 'pyrogram' (C:\Users\Revenger\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pyrogram\__init__.py)
This is my Code
from pyrogram import Client, filters
from pyrogram.methods.chats.get_chat_members import Filters
app = Client("Client", bot_token="Token")
#app.on_message(Filters.private & Filters.command('start'))
def startmsg(client, message):
message.reply("Hi Wassimo Bot is Here")
app.run() #"long-polling"
use filters instead of Filters
from pyrogram import filters, Client
delete second line , becouse you are importing Filters from random place
import methods relative to version
in old versions:
from pyrogram import Filters
from pyrogram.types import InlineKeyboardMarkup # Example
in newer version:
from pyrogram import filters
from pyrogram import InlineKeyboardMarkup # Example

IBM watson for sentiment analysis crashing on import

I'm trying to use IBM watson for sentiment analysis. but it is crashing on the import: from ibm_watson import NaturalLanguageUnderstandingV1
The whole code snippet is
import json
import constants from ibm_watson import NaturalLanguageUnderstandingV1 from ibm_cloud_sdk_core.authenticators
import IAMAuthenticator from ibm_watson.natural_language_understanding_v1 import Features, SentimentOptions
class SentimentAnalysis:
def __init__(self):
authenticator = IAMAuthenticator(constants.IBM_WATSON_KEY)
this.natural_language_understanding = NaturalLanguageUnderstandingV1(
version='2020-08-01',
authenticator=authenticator
)
this.natural_language_understanding.set_service_url(constants.IBM_WATSON_URL)
def analyse_sentiments(self, data):
response = this.natural_language_understanding.analyze(
text=data,
features=Features(sentiment=SentimentOptions())).get_result()
assert isinstance(response, object)
return response
My python version is Python 2.7.16
Installed IBM watson using pip install --upgrade "ibm-watson>=4.6.0"
The error I'm seeing is
/usr/bin/python /Users/rabbal1892/Desktop/DeepInsight/nextcontent-etl/scrapers/sentiment_analysis.py Traceback (most recent call last): File "/Users/rabbal1892/Desktop/DeepInsight/nextcontent-etl/scrapers/sentiment_analysis.py", line 3, in <module>
from ibm_watson import NaturalLanguageUnderstandingV1 File "/Users/rabbal1892/Library/Python/2.7/lib/python/site-packages/ibm_watson/__init__.py", line 16, in <module>
from ibm_cloud_sdk_core import IAMTokenManager, DetailedResponse, BaseService, ApiException File "/Users/rabbal1892/Library/Python/2.7/lib/python/site-packages/ibm_cloud_sdk_core/__init__.py", line 34, in <module>
from .base_service import BaseService File "/Users/rabbal1892/Library/Python/2.7/lib/python/site-packages/ibm_cloud_sdk_core/base_service.py", line 68
service_url: str = None,
^ SyntaxError: invalid syntax
I'll appreciate any help. Thanks.
The ibm-watson project description page mentions it's only tested on Python V3.x versions.
There is a lot of Python v3 syntax that is not compatible with Python v2.
Since you mention you have Python v2, you should Python v3 instead if you want to using the ibm-watson library as is, you will have to use Python v3.

Unable to import facebook SDK for Targetting search API

I was trying to use Targetting search API from facebook business SDK API.
ImportError: No module named facebookads.adobjects.targetingsearch
Using Python 2.7.12
~
Was trying to execute this piece of code:
from facebookads.adobjects.targetingsearch import TargetingSearch
params = {
'q': 'un',
'type': 'adgeolocation',
'location_types': ['country'],
}
resp = TargetingSearch.search(params=params)
print(resp)
Actual result :
Traceback (most recent call last):
File "test.py", line 2, in <module>
from facebookads.adobjects.targetingsearch import TargetingSearch
ImportError: No module named facebookads.adobjects.targetingsearch
Facebook Marketing API docs a bit outdated. You should replace the import from:
from facebookads.adobjects.targetingsearch import TargetingSearch
to:
from facebook_business.adobjects.targetingsearch import TargetingSearch
Also, before requesting targeting data, you should initialize FacebookAdsApi with your generated access token:
from facebook_business.api import FacebookAdsApi
FacebookAdsApi.init(access_token=access_token)

ImportError: No module named 'google.cloud.proto.vision'

Code :
from google.cloud import vision
import google.cloud.proto.vision.v1.image_annotator_pb2 as pvv
import io
client = vision.ImageAnnotatorClient()
def mkrl(imageStr):
im_obj = pvv.Image(content = imageStr)
return pvv.AnnotateImageRequest(image = im_obj, features = [{"type": "TEXT_DETECTION"}])
def getR(imageList):
req = map(mkrl,imageList)
response = client.batch_annotate_images(req)
return response
I'm trying to extract text from an image using the google vision api. I need to send a batch of images - things were working fine but now there is this error:
ImportError: No module named 'google.cloud.proto.vision'
this has been solved.....
the correct way to import this is :
import google.cloud.vision_v1.proto.image_annotator_pb2 as pvv
or
from google.cloud.vision_v1.proto import image_annotator_pb2 as pvv

The managed object is not valid - Dynamo Crashes

The following error in Dynamo is bugging me since many hours.
Warning:
IronPythonEvaluator.EvaluateIronPythonScript operation failed.
Traceback (most recent call last):
File " < string > ", line 33, in < module>
Exception: The managed object is not valid.
I’m not sure why the error has occur would appreciate if someone share its solution with me. Thank you
import clr
clr.AddReference('ProtoGeometry')
import Autodesk.DesignScript.Geometry
# Import Element wrapper extension methods
clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
# Import geometry conversion extension methods
clr.ImportExtensions(Revit.GeometryConversion)
# Import DocumentManager and TransactionManager
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application
# Import RevitAPI
clr.AddReference("RevitAPI")
from Autodesk.Revit.DB import *
import System
from System import Array
from System.Collections.Generic import *
import sys
pyt_path = r'C:\Program Files (x86)\IronPython 2.7\Lib'
sys.path.append(pyt_path)
The above are my standard imports while working with Dynamo/Revit API. It has been working for me for a while.
In all honesty I wish I had an "actual" answer why your script is not working. The only thing that is different than mine, is order in which you reference certain things. That should have no effect on their validity. It's Dynamo though, and it's very capricious.

Categories

Resources