eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I think I'm missing something simple I have a python poetry application: name = "my-first-api" version = "0.1.0" description = "" readme = "README.md" packages = [{include = "application"}] [tool.poetry.scripts] start = "main:start" [tool.poetry.dependencies] python = ">=3.10,<3.12" pip= "23.0.1" setuptools="65.5.0" fastapi="0.89.1" uvicorn="0.20.0" [tool.poetry.group.dev.dependencies] pyinstaller = "^5.10.1" pytest = "^7.3.1" [build-system] requires = ["poetry-core"] build-backend = "poetry.core.masonry.api"  I can run this and build this using Poetry, however, I would like to be able to create the executable with a poetry script as well. Now I build it like this: poetry run pyinstaller main.py --collect-submodules application --onefile --name myapi I would like something like poetry package to automatically create this executable as well. How do I hook that up? Btw. ths does not work :( [tool.poetry.scripts] start = "main:start" builddist = "poetry run pyinstaller main.py --collect-submodules application --onefile --name myapi" 

Answer: I have found a solution using the pyinstaller API. As you may know already, Poetry will only let you run 'scripts' if they are functions inside your package. Just like in your pyproject.toml, you map the start command to main:start, which is the start() function of your main.py module. Similarly, you can create a function in a module that triggers Pyinstaller and map that to a command that you can run as poetry run <commmand>.  Assuming you have a module structure like this: my_package ├── my_package │ ├── __init__.py │ ├── pyinstaller.py │ └── main.py └── pyproject.toml  1. Create a file pyinstaller.py to call the Pyinstaller API The file should be inside your package structure, as shown in the diagram above. This is adapted from the Pyinstaller docs import PyInstaller.__main__ from pathlib import Path HERE = Path(__file__).parent.absolute() path_to_main = str(HERE / "main.py") def install(): PyInstaller.__main__.run([ path_to_main, '--onefile', '--windowed', # other pyinstaller options... ])  2. Map the build command in pyproject.toml In the pyproject.toml file, add this [tool.poetry.scripts] build = "my_package.pyinstaller:install"  3. From the terminal, invoke the build command You must do so under the virtual environment that poetry creates: poetry run build  🎉 Profit
Question: I want to persist a python (data)class (i.e. Person) using imperative mapping in SQLAlchemy. One field of this class refers to another class (City). The second class is only a wrapper around two dicts and I want to store it in a denormalized way as a JSON column. My example classes look like this: @dataclass class Person: name: str city: City @dataclass class City: property_a: dict property_b: dict  And in the database it should look like this: +--------+-----------------------------------------------------------------+ | name | city | +--------+-----------------------------------------------------------------+ | aaron | {property_a: {some_value: 1}, property_b: {another_value: 2}} | | bob | {property_a: {some_value: 10}, property_b: {another_value: 20}} | +--------+-----------------------------------------------------------------+  My table definition looks like this: person_table = Table( "persons", Column("id", Integer, primary_key=True, autoincrement=True), Column("name", String), Column("city", JSON) ) mapper_registry.map_imperatively( Person, person_table )  This fails (obviously) since "Object of type City is not JSON serializable". I need to provide custom (de)serialization methods to the mapper_registry which tell SqlAlchemy how to convert my City class into a nested dict and back. But I could not find out how to do this (and even if this is a good approach).

Answer: I found a solution. Whether this is an elegant way to solve this problem or not is for the reader to decide. My initial reasoning was not to pollute the models with I/O implementation details. Another approach would be to introduce a service layer and/or create additional dumb DTOs that can be translated to/from the model classes and contain the city fields as plain dicts/json. Two additional functions are needed to introduce custom de-/serialization: def serialize_json(o): match o: case City(): return json.dumps({"property_a": o.property_a, "property_b": o.property_b}) case _: return json.dumps(o) def deserialize_json(s): s_dict = json.loads(s) match s_dict: case {"property_a": property_a, "property_b": property_b}: return City(property_a=property_a, property_b=propertyb) case _: return s_dict  The match operator is not strictly necessary for this toy example, but keeps everything a little bit cleaner if more of those cases are needed. Now the functions can be passed to SQLAlchemy: from sqlalchemy import create_engine engine = create_engine( "sqlite:///:memory:", json_serializer=orm.serialize_json, json_deserializer=orm.deserialize_json )
Question: While trying to update an old Python script I ran into the following error: module 'numpy' has no attribute 'asscalar'. Did you mean: 'isscalar'?  Specifically: def calibrate(x, y, z): # H = numpy.array([x, y, z, -y**2, -z**2, numpy.ones([len(x), 1])]) H = numpy.array([x, y, z, -y**2, -z**2, numpy.ones([len(x)])]) H = numpy.transpose(H) w = x**2 (X, residues, rank, shape) = linalg.lstsq(H, w) OSx = X[0] / 2 OSy = X[1] / (2 * X[3]) OSz = X[2] / (2 * X[4]) A = X[5] + OSx**2 + X[3] * OSy**2 + X[4] * OSz**2 B = A / X[3] C = A / X[4] SCx = numpy.sqrt(A) SCy = numpy.sqrt(B) SCz = numpy.sqrt(C) # type conversion from numpy.float64 to standard python floats offsets = [OSx, OSy, OSz] scale = [SCx, SCy, SCz] offsets = map(numpy.asscalar, offsets) scale = map(numpy.asscalar, scale) return (offsets, scale)  I found that asscalar has been deprecated since NumPy 1.16. I found one reference that said to use numpy.ndarray.item, but I have no clue how to do that. I did try this: offsets = map.item(offsets) scale = map.item( scale)  but got this error: AttributeError: type object 'map' has no attribute 'item'  How can I solve this?

Answer: Just replace numpy.scalar using numpy.ndarray.item, that is, change offsets = map(numpy.asscalar, offsets) scale = map(numpy.asscalar, scale)  to offsets = map(numpy.ndarray.item, offsets) scale = map(numpy.ndarray.item, scale)
Question: How can I read/write data to Raspberry Pi Pico using Python/MicroPython over the USB connection?

Answer:  Use Thonny to put MicroPython code on Raspberry Pi Pico. Save it as 'main.py'. Unplug Raspberry Pi Pico USB. Plug Raspberry Pi Pico USB back in. (don't hold do the boot button). Run the PC Python code to send and receive data between PC and Raspberry Pi Pico.  Code for Raspberry Pi Pico:  Read data from sys.stdin. Write data using print. poll to check if data is in the buffer.  import select import sys import time # Set up the poll object poll_obj = select.poll() poll_obj.register(sys.stdin, select.POLLIN) # Loop indefinitely while True: # Wait for input on stdin poll_results = poll_obj.poll(1) # the '1' is how long it will wait for message before looping again (in microseconds) if poll_results: # Read the data from stdin (read data coming from PC) data = sys.stdin.readline().strip() # Write the data to the input file sys.stdout.write("received data: " + data + "\r") else: # do something if no message received (like feed a watchdog timer) continue  Code for PC: import serial def main(): s = serial.Serial(port="COM3", parity=serial.PARITY_EVEN, stopbits=serial.STOPBITS_ONE, timeout=1) s.flush() s.write("data\r".encode()) mes = s.read_until().strip() print(mes.decode()) if __name__ == "__main__": main()  serial is PySerial.
Question: I'm evaluating a LLM on Huggingface using Langchain and Python using this code: # https://github.com/hwchase17/langchain/blob/0e763677e4c334af80f2b542cb269f3786d8403f/docs/modules/models/llms/integrations/huggingface_hub.ipynb from langchain import HuggingFaceHub, LLMChain import os hugging_face_write = "MY_KEY" os.environ['HUGGINGFACEHUB_API_TOKEN'] = hugging_face_write from langchain import PromptTemplate, HuggingFaceHub, LLMChain template = """Question: {question} Answer: Let's think step by step.""" prompt = PromptTemplate(template=template, input_variables=["question"]) llm_chain = LLMChain(prompt=prompt, llm=HuggingFaceHub(repo_id="google/flan-t5-xl", model_kwargs={"temperature":0, "max_length":64})) question = "What NFL team won the Super Bowl in the year Justin Beiber was born?" print(llm_chain.run(question))  I get the error ValueError Traceback (most recent call last) g:\Meine Ablage\python\lang_chain\langchain_huggingface_example.py in line 1 ----> 19 print(llm_chain.run(question)) File c:\Users\johan\.conda\envs\lang_chain\Lib\site-packages\langchain\chains\base.py:213, in Chain.run(self, *args, **kwargs) 211 if len(args) != 1: 212 raise ValueError("`run` supports only one positional argument.") --> 213 return self(args[0])[self.output_keys[0]] 215 if kwargs and not args: 216 return self(kwargs)[self.output_keys[0]] File c:\Users\johan\.conda\envs\lang_chain\Lib\site-packages\langchain\chains\base.py:116, in Chain.__call__(self, inputs, return_only_outputs) 114 except (KeyboardInterrupt, Exception) as e: 115 self.callback_manager.on_chain_error(e, verbose=self.verbose) --> 116 raise e 117 self.callback_manager.on_chain_end(outputs, verbose=self.verbose) 118 return self.prep_outputs(inputs, outputs, return_only_outputs) File c:\Users\johan\.conda\envs\lang_chain\Lib\site-packages\langchain\chains\base.py:113, in Chain.__call__(self, inputs, return_only_outputs) 107 self.callback_manager.on_chain_start( 108 {"name": self.__class__.__name__}, 109 inputs, 110 verbose=self.verbose, 111 ) ... 106 if self.client.task == "text-generation": 107 # Text generation return includes the starter text. 108 text = response[0]["generated_text"][len(prompt) :] ValueError: Error raised by inference API: Model google/flan-t5-xl time out  What am I doing incorrectly? I'm a newbie... Many thanks in advance, best regards from Paris, Jennie I ran my python script from above. After some waiting the shown error is given.

Answer: You need to upgrade your hugging face account to Pro version to host the large model for inference. "google/flan-t5-base" works for the free account.
Question: I'm facing this issue while building a executable of my pyqt5 application. I've images folder which contain 6 images and I want to add this images directory in dist folder so structure will look like this dist > app > images but I'm not able to do that it copies all images instead of picking folder. I've tried adding like this in datas list eg. ('images','images') & ('images/*','images/*') Here is my app.spec look like # -*- mode: python ; coding: utf-8 -*- block_cipher = None a = Analysis( ['ui.py'], pathex=[], binaries=[], datas=[('logo.png', '.'), ('images', 'images')], hiddenimports=[], hookspath=[], hooksconfig={}, runtime_hooks=[], excludes=[], win_no_prefer_redirects=False, win_private_assemblies=False, cipher=block_cipher, noarchive=False, ) pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher) exe = EXE( pyz, a.scripts, [], exclude_binaries=True, name='myapp', debug=False, bootloader_ignore_signals=False, strip=False, upx=True, console=True, disable_windowed_traceback=False, argv_emulation=False, target_arch=None, codesign_identity=None, entitlements_file=None, ) coll = COLLECT( exe, a.binaries, a.zipfiles, a.datas, strip=False, upx=True, upx_exclude=[], name='MyApplication', )  I've run this command to update data pyinstaller app.spec  What I'm missing let me know.

Answer: Strange! updating app.spec file and running pyinstaller app.spec  did not worked for me I don't know why If anyone know please let me know. What I did I deleted dist, build and old app.spec file and ran this command and it worked pyinstaller ui.py -n myapp --add-data "images:images" --add-data "logo.png:."
Question: I was trying ruff linter. I have a file structure like below project_folder ├── env # Python enviroment [python -m venv env] │ ├── Include │ ├── Lib │ ├── Scripts │ ├── ... ├── __init__.py └── numbers.py  I am trying to use this code I activated the environment inside the project_folder and ran the script below ruff check .  but ruff also checked the env file. 🔗Image.png how to ignore env file like below linux script tree -I env 

Answer: You can use the exclude option, ruff check --exclude=env .
Question: I am trying to get an overview of the training phrases per intent from Dialogflow in python. I have followed this example to generate the following code: from google.cloud import dialogflow_v2 # get_credentials is a custom function that loads the credentials credentials, project_id = get_credentials() client = dialogflow_v2.IntentsClient(credentials=credentials) request = dialogflow_v2.ListIntentsRequest( parent=f"projects/{project_id}/agent/environments/draft", ) page_result = client.list_intents(request=request) for intent in page_result: print("Intent name: ", intent.name) print("Intent display_name: ", intent.display_name) print("Training phrases: ", intent.training_phrases)  The name and display name of the intent are printed as expected, however training phrases is always an empty list (for both the draft as the test environment). Any ideas on why I'm not seeing the training phrases that I can see in the console? EDIT After hkanjih's answer I've updated my code as follows: from google.cloud import dialogflow_v2 # get_credentials is a custom function that loads the credentials credentials, project_id = get_credentials() client = dialogflow_v2.IntentsClient(credentials=credentials) request = dialogflow_v2.ListIntentsRequest( parent=f"projects/{project_id}/agent/environments/draft", ) page_result = client.list_intents(request=request) for intent in page_result: print("Intent name: ", intent.name) # intent.name is equal to projects/{project_id}/agent/intents/{intent_id} intent_request = dialogflow_v2.GetIntentRequest( name=intent.name, ) intent = client.get_intent(request=intent_request) # printing intent name again just to check if it's the same (it is) print("Intent name: ", intent.name) print("Intent display_name: ", intent.display_name) print("Training phrases: ", intent.training_phrases)  Unfortunately, for all intents: Training phrases: []

Answer: After searching in the documentation some more, I found this page. Therefore I added the intent_view parameter to the request as in the snippet below: from google.cloud import dialogflow_v2 # get_credentials is a custom function that loads the credentials _credentials, project_id = self._get_credentials() client = dialogflow_v2.IntentsClient(credentials=_credentials) request = dialogflow_v2.ListIntentsRequest( parent=f"projects/{project_id}/agent/environments/draft", intent_view="INTENT_VIEW_FULL", ) page_result = client.list_intents(request=request) for intent in page_result: print("Intent name: ", intent.name) print("Intent display_name: {}", intent.display_name) print("Training phrases: ", intent.training_phrases)  Note that in the request the parent f"projects/{project_id}/agent" (without the environment) gives the same result.
Question: My flask app was working prior to upgrades. I was having trouble with sending email when there was a forgot-reset-password. To try and fix this I recently upgraded some modules for my flask app. The modules that I upgraded with current versions are:  email-validator==2.0.0.post2 Flask==2.3.1 itsdangerous==2.1.2  The Traceback error that I am getting now is: Traceback (most recent call last): File "C:\Users\my_folder\sales\app.py", line 1, in <module> from product import app File "C:\Users\my_folder\sales\product\__init__.py", line 56, in <module> from product.agents.views import agents_bp File "C:\Users\my_folder\sales\product\agents\views.py", line 7, in <module> from product.agents.forms import RegistrationForm, LoginForm, UpdateAccountForm, ResetPasswordForm, RequestResetForm File "C:\Users\my_folder\sales\product\agents\forms.py", line 1, in <module> from flask_wtf import FlaskForm File "C:\Users\my_folder\flask_env\lib\site-packages\flask_wtf\__init__.py", line 4, in <module> from .recaptcha import Recaptcha File "C:\Users\my_folder\flask_env\lib\site-packages\flask_wtf\recaptcha\__init__.py", line 1, in <module> from .fields import RecaptchaField File "C:\Users\my_folder\flask_env\lib\site-packages\flask_wtf\recaptcha\fields.py", line 3, in <module> from . import widgets File "C:\Users\my_folder\flask_env\lib\site-packages\flask_wtf\recaptcha\widgets.py", line 6, in <module> JSONEncoder = json.JSONEncoder AttributeError: module 'flask.json' has no attribute 'JSONEncoder'  How do I go about fixing this?

Answer: Python has had a builtin JSONEncoder since at least 3.2, making Flask's version redundant. So it's reasonable to remove it. If this was a module you controlled, you could just replace your line JSONEncoder = json.JSONEncoder with from json import JSONEncoder  Since you don't control this library though, you should notice which library that is trying to include it, in your case that's flask_wtf. When you check PyPi for that library you'll see that there's a few recent releases, suggesting the first thing you should try is updating that version on Flask-WTF.
Question: Firestore started showing UserWarning: Detected filter using positional arguments. Prefer using the 'filter' keyword argument instead.  when using query.where(field_path, op_string, value) while it's the the method from the official docs https://cloud.google.com/firestore/docs/query-data/queries So how shall we use the 'filter' kwarg? Couldn't find docs or samples on that. UPDATE: there is an open issue on this on GitHub https://github.com/googleapis/python-firestore/issues/705 (with no reaction from Google folks) UPDATE2: so, basically, it should look like this from google.cloud.firestore import CollectionReference from google.cloud.firestore_v1.base_query import FieldFilter, BaseCompositeFilter ... conditions = [[field, operator, value], [field, operator, value], ...] query = CollectionReference(path1, path2, path3, ...) query = query.where(filter=BaseCompositeFilter('AND', [FieldFilter(*_c) for _c in conditions]))  For a FieldFilter operator usual ==, !=, etc. work as specified here https://firebase.google.com/docs/firestore/query-data/queries#query_operators Instead of 'AND' you could also use 'OPERATOR_UNSPECIFIED', but I'm not sure if it does what I think it should https://firebase.google.com/docs/firestore/reference/rest/v1/StructuredQuery#Operator

Answer: Based on @Robert G's excellent answer you can get rid of this warning with a semi-small tweak. Instead of: query = collection_ref query = query.where(field, op, value)  Do this: from google.cloud.firestore_v1.base_query import FieldFilter query = collection_ref query = query.where(filter=FieldFilter(field, op, value))  And then the warning disappears. Hope this helps!
Question: I want to use slice[int] like list[int], but my IDE tells me that this usage is invalid. So how to annotate slice type, if I want to constrain the type of its parameters?

Answer: Update (2023-05-12) I opened a PR to make slice generic. The change would introduce quite a few mypy errors in many existing projects. The good people at python/typeshed made it clear that until we have type variable defaults (proposed in PEP 696, but not yet accepted) and the slice type utilizes them, they will not accept the change to make it generic. I will try to keep up to date on the progress with PEP 696 and to implement the missing changes as soon as possible.  Original answer As it stands right now, the built-in slice type is not generic, so it has no type parameters (unlike e.g. list). There has been a long-standing discussion about this (since 2015) and the relevant typeshed issue is still open. IMO it should definitely be made generic, but it is unclear if/when that will happen. slice arguments can be of any type (source). This means a statement like e.g. slice("foo", [-1], object()) will pass mypy --strict (even though it may be of questionable utility). That means there is currently no way to constrain a slice type variable in terms of its start/stop/step attribute types. And since you cannot even subclass slice, there is not even a way to easily make your own generic slice-like type.
Question: I want to replace the inf values in a polars series with 0. I am using the polars Python library. This is my example code: import polars as pl example = pl.Series([1,2,float('inf'),4])  This is my desired output: output = pl.Series([1.0,2.0,0.0,4.0])  All similiar questions regarding replacements are regarding polars Dataframes using the .when expression (e.g Replace value by null in Polars) which does not seem to be available in a Series object: AttributeError: 'Series' object has no attribute 'when' Is this possible using polars expressions? EDIT: I found the following solution but it seems very convoluted: example.map_dict({float('inf'): 0 }, default= pl.first()) 

Answer: You can use Series.set: s = s.set(s == float('inf'), 0)  although there's a note there:  Use of this function is frequently an anti-pattern, as it can block optimisation (predicate pushdown, etc). Consider using pl.when(predicate).then(value).otherwise(self) instead.  which suggests using a way longer: s = s.to_frame().select(polars.when(s == float('inf')).then(0).otherwise(s)).to_series()  which may or may not be worth it depending on your use case.
Question: I'm following the guide here: https://huggingface.co/docs/transformers/v4.28.1/tasks/summarization There is one line in the guide like this: labels = tokenizer(text_target=examples["summary"], max_length=128, truncation=True)  I don't understand the function of the text_target parameter. I tried the following code and the last two lines gave exactly the same results. from transformers import AutoTokenizer tokenizer = AutoTokenizer.from_pretrained('t5-small') text = "Weiter Verhandlung in Syrien." tokenizer(text_target=text, max_length=128, truncation=True) tokenizer(text, max_length=128, truncation=True)  The docs just say text_target (str, List[str], List[List[str]], optional) — The sequence or batch of sequences to be encoded as target texts. I don't really understand. Is there some situations when setting text_target will give you a different result?

Answer: Sometimes it is necessary to look at the code: if text is None and text_target is None: raise ValueError("You need to specify either `text` or `text_target`.") if text is not None: # The context manager will send the inputs as normal texts and not text_target, but we shouldn't change the # input mode in this case. if not self._in_target_context_manager: self._switch_to_input_mode() encodings = self._call_one(text=text, text_pair=text_pair, **all_kwargs) if text_target is not None: self._switch_to_target_mode() target_encodings = self._call_one(text=text_target, text_pair=text_pair_target, **all_kwargs) # Leave back tokenizer in input mode self._switch_to_input_mode() if text_target is None: return encodings elif text is None: return target_encodings else: encodings["labels"] = target_encodings["input_ids"] return encodings  As you can see in the above snippet, both text and text_target are passed to self._call_one() to encode them (note that text_target is passed as the text parameter). That means the encoding of the same string as text or text_target will be identical as long as _switch_to_target_mode() doesn't do anything special. The conditions at the end of the function answer your question:  When you only provide text you will retrieve the encoding of it. When you only provide text_target you will retrieve the encoding of it. When you provide text and text_target you will retrieve the encoding of text and the token ids of text_target as the value of the labels key.  To be honest, I think the implementation is a bit unintuitive. I would expect that passing the text_target would return an object that only contains the labels key. I assume that they wanted to keep their output objects and the respective documentation simple and therefore went for this implementation. Or there is a model where it actually makes sense that I am unaware of.