How can I run two functions within the same FastAPI application, where one function requires transformers==4.32.8 and the other requires transformers==5.32.8?
Loading
How can I run two functions within the same FastAPI application, where one function requires transformers==4.32.8 and the other requires transformers==5.32.8?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Naimish MakwanaRank #324 · 7.7K reputationPosted Oct 15, 2024, 7:59 AM EDT
Running two different versions of the same library within a single FastAPI application can be tricky due to dependency conflicts. However, you can achieve this by using separate virtual environments and subprocesses. Here’s a step-by-step guide:
Create Virtual Environments: Set up two separate virtual environments, each with the required version of the
transformerslibrary.Install Dependencies:
# For transformers==4.32.8 python -m venv env1 source env1/bin/activate pip install transformers==4.32.8 deactivate # For transformers==5.32.8 python -m venv env2 source env2/bin/activate pip install transformers==5.32.8 deactivateCreate Python Scripts: Write separate Python scripts for each function that uses the respective version of the
transformerslibrary.# script1.py (uses transformers==4.32.8) import transformers def function1(): # Your code here return "Result from transformers 4.32.8"# script2.py (uses transformers==5.32.8) import transformers def function2(): # Your code here return "Result from transformers 5.32.8"Use Subprocesses in FastAPI: In your FastAPI application, use the
subprocessmodule to call these scripts.from fastapi import FastAPI import subprocess app = FastAPI() @app.get("/function1") def run_function1(): result = subprocess.run(["env1/bin/python", "script1.py"], capture_output=True, text=True) return {"result": result.stdout} @app.get("/function2") def run_function2(): result = subprocess.run(["env2/bin/python", "script2.py"], capture_output=True, text=True) return {"result": result.stdout}Run the FastAPI Application:
This setup allows you to run each function in its own isolated environment, avoiding dependency conflicts. Let me know if you need further assistance!