eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I am trying to deploy my application which is built using Python and FastAPI for backend and the HTML for the frontend. Using the student login i created an app service and uploaded my code using github. My project directory is like Frontend/ |- file.html |- x.css FastAPI/ |- main.py |- other.py |- requirements.txt  in my .yml files, I changed the directory of the requirements.txt and package in deploy to ./fastAPI. But the website url shows the following message.  Hey, Python developers! Your app service is up and running. Time to take the next step and deploy your code.  And it displays the error message in github workflow  Deployment Failed, Error: Failed to deploy web package to App Service. Conflict (CODE: 409)  I tried to use startup-command in the yml file to start the FastAPI, it didn't work and gave the following error message  Deployment Failed, Error: startup-command is not a valid input for Windows web app or with publish-profile auth scheme.  I changed the configuration on the azure portal to add WEBSITES_CONTAINER_STARTUP_COMMAND = uvicorn main:app --host 0.0.0.0 --port 80 in the application settings, didn't work.

Answer: I have deployed a simple Fast API project with HTML and CSS as frontend: My project structure: - FastAPI - templates - index.html -static - style.css - main.py - requirements.txt   Create Azure App Service with Python as Runtime stack, Linux as OS and Consumption hosting Plan in Azure Portal:    Configure Deployment during the creation of App Service.    Click on Review+Create. Once the App Service gets created, it will generate the workflow in your GitHub repository, and it starts the deployment. Deployment status can be tracked in your GitHub repository=>Actions.  My Workflow to deploy FastAPI: name: Build and deploy Python app to Azure Web App - <web_app_Name> on: push: branches: - main workflow_dispatch: jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Set up Python version uses: actions/setup-python@v1 with: python-version: '3.10' - name: Create and start virtual environment run: | python -m venv venv source venv/bin/activate - name: Install dependencies run: pip install -r requirements.txt # Optional: Add step to run tests here (PyTest, Django test suites, etc.) - name: Upload artifact for deployment jobs uses: actions/upload-artifact@v2 with: name: python-app path: | . !venv/ deploy: runs-on: ubuntu-latest needs: build environment: name: 'Production' url: ${{ steps.deploy-to-webapp.outputs.webapp-url }} steps: - name: Download artifact from build job uses: actions/download-artifact@v2 with: name: python-app path: . - name: 'Deploy to Azure Web App' uses: azure/webapps-deploy@v2 id: deploy-to-webapp with: app-name: '<web_app_Name>' slot-name: 'Production' publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE_XXXXXXXXXXX }} Ad  Deployed Successfully:  You have to run the Startup command for FastAPI in App Service=>Settings=>Configuration: gunicorn --bind=0.0.0.0 main:app  main is from the filename main.py and app is the object which I used in main.py.   References: Refer my SO thread on the similar issue.
Question: I am having a dataframe df: columnA columnB columnC A A 10 A B NaN A C 20 B A 30 B C NaN A D NaN D C 15  How can I fill the NaNvalues in that case, that the next non `NaN´ value is diveded by the missing entries before and splitted (including the already filled row)? So in my case that the output is: columnA columnB columnC A A 10 A B 10 A C 10 B A 30 B C 5 A D 5 D C 5  Further explanation, in that case 20 was divided by 2 and leads to 10, and 15 was divided by 3 and leads to 5.

Answer: Another one-chained variation using pd.Series.shift: df['columnC'] = (df['columnC'].fillna(0).groupby(df['columnC'].notna().cumsum() .shift().fillna(0)).transform('mean'))    columnA columnB columnC 0 A A 10.0 1 A B 10.0 2 A C 10.0 3 B A 30.0 4 B C 5.0 5 A D 5.0 6 D C 5.0
Question: Is there any function in python that converts y parameters to z parameters like matlab's y2z function? Here is matlab's y2z() function documentation.

Answer: No built-in Python function for Y-to-Z conversion like MATLAB's y2z. However, you can easily implement it. MATLAB Code Y = [1, 2; 3, 4]; Z = y2z(Y); disp('Y Parameters:'); disp(Y); disp('Z Parameters:'); disp(Z);  Python Code You can use NumPy for matrix operations. import numpy as np def y2z(Y): Y11, Y12, Y21, Y22 = Y[0, 0], Y[0, 1], Y[1, 0], Y[1, 1] D = (1 + Y11) * (1 + Y22) - Y12 * Y21 Z11 = (1 + Y11 - Y12 * Y21) / D Z12 = (Y11 + Y22 - Y12 - Y21) / D Z21 = 2 / D Z22 = (-1 + Y11 + Y22 - Y12 * Y21) / D return np.array([[Z11, Z12], [Z21, Z22]]) Y = np.array([[1, 2], [3, 4]]) Z = y2z(Y) print("Y Parameters:") print(Y) print("Z Parameters:") print(Z)  Run these snippets in their respective environments to compare. Both should yield the same Z-parameters.
Question: I'm writing some tests in pytest, and I'd like to make the fixture do something only if the test passes (update a value in a DB). It doesn't seem like fixtures in general know about whether the tests they run pass or fail -- is there a way to make them? import pytest @pytest.fixture def my_fixture(request): ... yield [if test passes]: db.write(new_value) def test_foo(my_fixture): assert False  Trying to catch an exception doesn't work as pytest has its own exception handling which handles things before we'd hit my own: @pytest.fixture def my_fixture(request): ... try: yield except Exception as e: raise e else: db.write('new_value')  I've also tried inspecting the pytest request object, but it doesn't seem to have what I want I suspect doing things this way doesn't follow the philosophy of pytest, so I'll probably end up doing things a different way in reality, but I'm interested to know the answer nonetheless :)

Answer: You can introspect the test context by requesting the request object in the fixture. There you can get request.session.testsfailed @pytest.fixture def my_fixture(request): failed_count = request.session.testsfailed yield if request.session.testsfailed > failed_count: print("this test failed")
Question: I'm trying to visualize 3D human keypoints in PyOpenGL, the code works fine if one human is present. more than one, the points and lines starts to flicker. while True: # Grab an image time_temp=time.time() def points(keypoints_3d): glEnable(GL_POINT_SMOOTH) glEnable(GL_BLEND) glEnable(GL_FRAMEBUFFER_SRGB) glPointSize(10) glBegin(GL_POINTS) glColor3d(1, 1, 1) for i in range(len(keypoints_3d)): glVertex3d(keypoints_3d[i][0]/100, keypoints_3d[i][1]/100, keypoints_3d[i][2]/100) #print("JNW",keypoints_3d[i][0]) glEnd() def lines(keypoints_3d): glEnable(GL_POINT_SMOOTH) glEnable(GL_BLEND) glEnable(GL_FRAMEBUFFER_SRGB) glPointSize(10) glBegin(GL_LINES) glColor3d(1, 1, 1) for i in range(len(keypoints_3d)): if(i<5): glVertex3d(keypoints_3d[i][0]/100, keypoints_3d[i][1]/100, keypoints_3d[i][2]/100) glVertex3d(keypoints_3d[i+1][0]/100, keypoints_3d[i+1][1]/100, keypoints_3d[i+1][2]/100) #print("JNW",keypoints_3d[i][0]) glEnd() for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) for body in bodies_list: points(keypoints_3d) lines(keypoints_3d) pygame.display.flip() pygame.time.wait(10)   I'm sure there are better ways to visualize these points and lines in PyopenGl. Can someone guide me?

Answer: You need to update the display once after you have drawn all the geometry, instead of updating it after each geometry. So call pygame.display.flip() after the loop, but not in the loop: while True: # [...] # clear display glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) # draw all the geometry for body in bodies_list: points(keypoints_3d) lines(keypoints_3d) # update the display pygame.display.flip() pygame.time.wait(10)
Question: Why not just use the default one always? are there any usecases for creating multiple event loops?

Answer: I don't know the exact reason behind your question. But there are certainly use cases for using multiple loops. e.g. To increase scalability. We know, CPU-bound code will interfere with event-loops in a process. This will reduce throughput. Not if we have loops in other processes, event-loops in other processes will not be disturbed. Of course we have to think about locking mechanisms etc. If using asyncio with multiprocessing.
Question: I have this data frame: df Server Env. Model Percent_Utilized server123 Prod Cisco. 50 server567. Prod Cisco. 80 serverabc. Prod IBM. 100 serverdwc. Prod IBM. 45 servercc. Prod Hitachi. 25 Avg 60 server123Uat Uat Cisco. 40 server567u Uat Cisco. 30 serverabcu Uat IBM. 80 serverdwcu Uat IBM. 45 serverccu Uat Hitachi 15 Avg 42  I have style applied to this df as follows: def color(val): if val > 80: color = 'red' elif val > 50 and val <= 80: color = 'yellow' else: color = 'green' return 'background-color: %s' % color df_new = df.style.applymap(color, subset=["Percent_Utilized"])  I need to add % at the end of the numbers on Percent_Utilized columns: resulting data frame need to look something like this: df_new Server Env. Model Percent_Utilized server123 Prod Cisco. 50% server567. Prod Cisco. 80% serverabc. Prod IBM. 100% serverdwc. Prod IBM. 45% servercc. Prod Hitachi. 25% Avg 60% server123Uat Uat Cisco. 40% server567u Uat Cisco. 30% serverabcu Uat IBM. 80% serverdwcu Uat IBM. 45% serverccu Uat Hitachi 15% Avg 42%  when I do this: df_new['Percent_Utilized'] = df_new['Percent_Utilized'].astype(str) + '%'  I get this error: TypeError: 'Styler" object is not suscriptable.

Answer: df.style returns a pd.Styler object, not a pd.DataFrame object. So you cannot use .astype. You can use Styler.format like this: df_new = df.style.applymap(color, subset=["Percent_Utilized"])\ .format('{:.0f}%', subset=['Percent_Utilized'], na_rep='nan')  You'd get something like this, ignore the Avg rows as they are not read properly by read_clipboard:
Question: I have the first list call OGList that have a list of strings, after that I want to extract all digits and strings from the OGList into a list of string and a list of digits. My Input: OGList = ['A10', 'BMW320i', 'Nissan NSX200', 'Benz 220c'] numlist = [] strlist = [] otherlist = [] for i in OGList: for x in i: if x.isalpha(): strlist.append(x) elif x.isdigit(): numlist.append(x) else: otherlist.append(x) print(numlist) print(strlist)  My Output: ['1', '0', '3', '2', '0', '2', '0', '0', '2', '2', '0'] ['A', 'B', 'M', 'W', 'i', 'N', 'i', 's', 's', 'a', 'n', 'N', 'S', 'X', 'B', 'e', 'n', 'z', 'c']  But I want it to stick together like my desired output. My desired output is: ['10', '320', '200', '220'] ['A', 'BMWi', 'NissanNSX', 'Benzc']  How can I fix my code?

Answer: Since you tagged the question with regex, here's an alternative approach using the same to separate the three groups: import re OGList = ['A10', 'BMW320i', 'Nissan NSX200', 'Benz 220c'] types = {"chars": "[a-zA-Z]", "nums": "[0-9]", "other": "[^a-zA-Z0-9]"} res = {k: [''.join(re.findall(types[k], s)) for s in OGList] for k in types}  Afterwards, res is a dict with the different lists in it: { 'chars': ['A', 'BMWi', 'NissanNSX', 'Benzc'], 'nums': ['10', '320', '200', '220'], 'other': ['', '', ' ', ' '] }  Note that there's not elif and else here, you have to make sure the patterns are mutually exclusive and covering all the cases, which negating ^ the other groups should do nicely.
Question: I have a pandas dataframe that consists of an id and an associated count of different encoded words. For instance: Original = pd.DataFrame(data=[[1,'1:2,2:3,3:1'],[2,'2:2,4:3']], columns=['id','words'])  I have a dictionary that has the mapping to the actual words, for instance: WordDict = {1:'A',2:'B',3:'C',4:'D'}  What I would like to do is create a new dataframe that maps the counts to columns for all possible words, so it would look something like: Final =pd.DataFrame(data=[[1,2,3,1,0],[2,0,2,0,3]], columns=['id','A','B','C','D']).set_index('id')  I know I can split the 'words' column of the original into separate columns, and can create a dataframe from WordDict so that it has all possible columns, but could not figure out how to create the mapping.

Answer: You can use a regex, a list comprehension, and the DataFrame constructor: import re Final = pd.DataFrame([{WordDict.get(int(k), None): v for k,v in re.findall('([^:,]+):([^:,]+)', s)} for s in Original['words']], index=Original['id'] ).fillna(0).astype(int)  Or with split: Final = pd.DataFrame([{WordDict.get(int(k), None): v for x in s.split(',') for k,v in [x.split(':')]} for s in Original['words']], index=Original['id'] ).fillna(0).astype(int)  Or ast.literal_eval: from ast import literal_eval Final = pd.DataFrame([{WordDict.get(k, None): v for k,v in literal_eval(f'{{{s}}}').items()} for s in Original['words']], index=Original['id'] ).fillna(0, downcast='infer')  Output:  A B C D id 1 2 3 1 0 2 0 2 0 3
Question: I have a trampoline class that I am trying to wrap in nanobind like so: class PyT : T { public: NB_TRAMPOLINE(T, 1); void F() override { NB_OVERRIDE(F); } }; // ... nb::class_<T, PyT>(m, "T") .def(nb::init<>()) .def("f", &PyT::F)  It compiles fine, but when i go to use it in python, it only works if I call the function as F() despite wrapping it with the method name f(). How do I solve this?

Answer: You can solve this by using NB_OVERRIDE_NAME and specifying the name you want it to be overriding, so in this case it would be: void F() override { NB_OVERRIDE_NAME("f", F); }
Question: I have the following dataframe: id text word 1 i am working with john he is my colleague john 2 i watched the bond movie and the bond actor was amazing bond 3 mary is my friend and we work together mary 4 hello world hello python peter  I would like to create another column where I retain the 3 words at the left and right of each word in column ["word"]. Sometimes the searched word can appear more than once, or do not have any words at the left or right, sometimes the text does not include the searched word. This is what I want: id text word retain 1 i am working with john he is my colleague john am working with john he is my 2 i watched the bond movie and the bond actor was amazing bond i watched the bond movie and the bond actor was amazing 3 mary is my friend and we work together mary mary is my friend 4 hello world hello python peter  Following this question: Extract words surrounding a word and inserting results in a dataframe column I tried to adapt the code: retain = df.text.str.extract("(?P<before>(?:\w+\W+){,3})df['word']\W+(?P<after>(?:\w+\W+){,3})", expand=True)  but I only obtained two columns full of NaNs. Any suggestion?

Answer: You could try: import re df['retain'] = [m.group() if (m:=re.search(fr'(?:(?:\w+\W+){{,3}}){w}(?:.*{w})?\W+(?:(?:\w+\W+){{,2}}\w+)?', t)) else '' for t, w in zip(df['text'], df['word'])]  Output:  id text word retain 0 1 i am working with john he is my colleague john am working with john he is my 1 2 i watched the bond movie and the bond actor was amazing bond i watched the bond movie and the bond actor was amazing 2 3 mary is my friend and we work together mary mary is my friend 3 4 hello world hello python peter
Question: In boost::python, to wrap a vector<T> you would do something like this: boost::python::class_< std::vector < T > >("T") .def(boost::python::vector_indexing_suite<std::vector< T > >());  How do I accomplish the same thing in nanobind?

Answer: Include the header <nanobind/stl/bind_vector.h> and then its just: nb::bind_vector<std::vector<T> >(m, "TVector");
Question: I am using Python FastAPI and Jinja2, all of which I am new to. I am able to set cookies alone or return html templates on their own, but I cannot work out how to do both at once. Setting cookies only works as expected, but returning a template seems to overwrite that and just returns html with no cookies. @app.get("/oauth/auth", response_class=HTMLResponse) async def login_page(request: Request, response: Response): client_Code_Req_Schema = ClientCodeReqSchema(client_id=request.query_params.get("client_id"), redirect_uri=request.query_params.get("redirect_uri"), response_type=request.query_params.get("response_type")) if check_client(client_Code_Req_Schema): response.set_cookie(key="redirect_uri", value="test") return templates.TemplateResponse("authorize.html", {"request": request}) else: raise HTTPException(status_code=400, detail="Invalid request")  Many thanks for any advice. Happy to provide more info if I missed something.

Answer: You should set the cookie on the TemplateResponse instead—which is returned from that endpoint—not the Response object defined in the endpoint's parameters, which could be used when returing a simple (JSON) message, e.g., return {'msg': 'OK'}. Related answers that you might find helpful can be found here, here and here. Example @app.get("/oauth/auth", response_class=HTMLResponse) async def login_page(request: Request): if ... response = templates.TemplateResponse("authorize.html", {"request": request}) response.set_cookie(key="redirect_uri", value="test") return response else: raise HTTPException(status_code=400, detail="Invalid request")
Question: I already checked python version on my environment (sublime text) and it is 3.11.0, the latest, I checked pillow version which is 10.0.0, the latest, and my code looks similar to other examples online. the code has a part in Italian, but its pretty understandable. the problem is at "disegno.textsize(testo, font=font) after I run the code: line 14, in metti_testo_su_sfondo text_width, text_height = disegno.textsize(testo, font=font) ^^^^^^^^^^^^^^^^ AttributeError: 'ImageDraw' object has no attribute 'textsize'  its strange because imagedraw should have the textsize attribute. I'm a novice, I hope I didn't miss anything blatant  from PIL import Image, ImageDraw, ImageFont def metti_testo_su_sfondo(testo, sfondo, posizione=(10, 10), colore_testo=(0, 0, 0), dimensione_font=25): # Apri l'immagine dello sfondo immagine_sfondo = Image.open(sfondo) disegno = ImageDraw.Draw(immagine_sfondo) font = ImageFont.truetype("ARIAL.TTF", dimensione_font) text_width, text_height = disegno.textsize(testo, font=font) # Calcola le coordinate del testo centrato x = (immagine_sfondo.width - text_width) // 2 y = (immagine_sfondo.height - text_height) // 2 disegno.text((x, y), testo, fill=colore_testo, font=font) immagine_sfondo.save("spotted.png") testo_da_inserire = "Ciao, mondo!" sfondo_da_utilizzare = "spotted_bianco.jpg" metti_testo_su_sfondo(testo_da_inserire, sfondo_da_utilizzare)  The objective is a code that makes me images automatically without needing to edit them manually. I checked build system, python version and pillow version. when I run the code through cmd though it gives me this error: from PIL import Image, ImageDraw, ImageFont ModuleNotFoundError: No module named 'PIL' 

Answer: textsize was deprecated, the correct attribute is textlength which gives you the width of the text. for the height use the fontsize * how many rows of text you wrote. Example code: w = draw.textlength(text, font=font) h = fontSize * rows
Question: I have a generic type for which I'd like to retrieve at runtime the type of its type variable. The following snippet runs well, but it uses Generic.__orig_bases__ which is not an official API (its use is discouraged in PEP 560 that defines it). Is there an official API to retrieve it? And if not, is there another (officially supported) way for me to code get_t in this example? import typing T = typing.TypeVar("T") class MyGeneric(typing.Generic[T]): @classmethod def get_t(cls) -> type[T]: for base in cls.__orig_bases__: if typing.get_origin(base) is MyGeneric: return typing.get_args(base)[0] raise RuntimeError("didn't work :(") class IntImplementation(MyGeneric[int]): pass assert IntImplementation.get_t() is int 

Answer: I think with Python 3.12 we can safely say that __orig_bases__ is now documented and there's a function that can retrieve it: from types import get_original_bases print(get_original_bases(IntImplementation)) # (__main__. MyGeneric[int],)  Reference: https://docs.python.org/3/library/types.html#types.get_original_bases
Question: I'm trying to replied the code from this Hugging Face blog. At first I installed the transformers and created a token to login to hugging face hub: pip install transformers huggingface-cli login  After that it is said to use use_auth_token=True when you have set a token. Unfortunately after running the code I get an error: from transformers import AutoTokenizer import transformers import torch model = "meta-llama/Llama-2-7b-chat-hf" tokenizer = AutoTokenizer.from_pretrained(model, use_auth_token=True) pipeline = transformers.pipeline( "text-generation", model=model, torch_dtype=torch.float16, device_map="auto", ) sequences = pipeline( 'I liked "Breaking Bad" and "Band of Brothers". Do you have any recommendations of other shows I might like?\n', do_sample=True, top_k=10, num_return_sequences=1, eos_token_id=tokenizer.eos_token_id, max_length=200, ) for seq in sequences: print(f"Result: {seq['generated_text']}")  Error: OSError: meta-llama/Llama-2-7b-chat-hf is not a local folder and is not a valid model identifier listed on 'https://huggingface.co/models' If this is a private repository, make sure to pass a token having permission to this repo with `use_auth_token` or log in with `huggingface-cli login` and pass `use_auth_token=True`.  It says that the model cannot be found, but you can find it in the list of models on hugging face here. This is the version of the transformers package I'm using: > pip show transformers Name: transformers Version: 4.33.0.dev0 Summary: State-of-the-art Machine Learning for JAX, PyTorch and TensorFlow Home-page: https://github.com/huggingface/transformers Author: The Hugging Face team (past and future) with the help of all our contributors (https://github.com/huggingface/transformers/graphs/contributors) Author-email: transformers@huggingface.co License: Apache 2.0 License Location: /Users/quinten/opt/miniconda3/lib/python3.9/site-packages Requires: filelock, huggingface-hub, numpy, packaging, pyyaml, regex, requests, safetensors, tokenizers, tqdm Required-by: spacy-transformers  Does anyone know how to fix this error?

Answer: def from_pretrained(cls, pretrained_model_name_or_path, *inputs, **kwargs):  the pretrained_model_name_or_path may the model repo or the model path in your case the model repo is "meta-llama/Llama-2-7b-chat-hf" which is right. according to https://huggingface.co/meta-llama/Llama-2-7b-chat-hf/tree/main you must agree to the terms and conditions in the above link in order to access the model.
Question: Take a simple example in this website, https://huggingface.co/datasets/Dahoas/rm-static: if I want to load this dataset online, I just directly use, from datasets import load_dataset dataset = load_dataset("Dahoas/rm-static")  What if I want to load dataset from local path, so I download the files and keep the same folder structure from web Files and versions fristly, -data |-test-00000-of-00001-bf4c733542e35fcb.parquet |-train-00000-of-00001-2a1df75c6bce91ab.parquet -.gitattributes -README.md -dataset_infos.json  Then, put them into my folder, but shows error when loading: dataset_path ="/data/coco/dataset/Dahoas/rm-static" tmp_dataset = load_dataset(dataset_path)  It shows FileNotFoundError: No (supported) data files or dataset script found in /data/coco/dataset/Dahoas/rm-static.

Answer: Save the data with save_to_disk then load it with load_from_disk. For example: import datasets ds = datasets.load_dataset("Dahoas/rm-static") ds.save_to_disk("Path/to/save")  and later if you wanna re-utilize it just normal load_dataset will work ds = datasets.load_from_disk("Path/to/save")  you can verify the same by printing the dataset you will be getting same result for both. This is the easier way out. The file format it is generally saved in is arrow. For the second method where you are downloading the parquet file. Would require you to explicitly declaring the dataset and it config, might be included in json and then you can load it.