Azure / azure-functions-python-worker

Python worker for Azure Functions.
http://aka.ms/azurefunctions
MIT License
335 stars 103 forks source link

[BUG] cannot import name 'cygrpc' from 'grpc._cython #1355

Closed howlieT closed 9 months ago

howlieT commented 10 months ago

Investigative information

Please provide the following:

Repro steps

Provide the steps required to reproduce the problem:

Attempt to run azure functions using vs code via debug tool

Expected behavior

Provide a description of the expected behavior.

Debug tool runs files

Actual behavior

Provide a description of the actual behavior observed.

Produces the error: ImportError: cannot import name 'cygrpc' from 'grpc._cython' (C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.10/WINDOWS/X64\grpc_cython__init.py) [2023-11-14T15:32:36.827Z] Language Worker Process exited. Pid=12964. [2023-11-14T15:32:36.831Z] python exited with code 1 (0x1). ImportError: cannot import name 'cygrpc' from 'grpc._cython' (C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.10/WINDOWS/X64\grpc_cython__init.py),ImportError: cannot import name 'cygrpc' from 'grpc._cython' (C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.10/WINDOWS/X64\grpc_cython__init__.py),ImportError: cannot import name 'cygrpc' from 'grpc._cython' (C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.10/WINDOWS/X64\grpc_cython\init__.py). [2023-11-14T15:32:36.849Z] Failed to start a new language worker for runtime: python. [2023-11-14T15:32:36.850Z] System.Private.CoreLib: A task was canceled. [2023-11-14T15:32:37.560Z] ERROR: unhandled error in functions worker: cannot import name 'cygrpc' from 'grpc._cython' (C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.10/WINDOWS/X64\grpc_cython\init__.py)

Known workarounds

Provide a description of any known workarounds.

https://learn.microsoft.com/en-us/azure/azure-functions/recover-python-functions?tabs=vscode%2Cbash&pivots=python-mode-decorators

Contents of the requirements.txt file:

Provide the requirements.txt file to help us find out module related issues.
azure-functions 
nltk
cffi
openai
langchain==0.0.225
llama-index==0.6.20
azure-storage-blob==12.9.0

Related information

Provide any related information
YunchuWang commented 10 months ago

@howlieT Thanks for reporting! Wonder if you have tried ? image

howlieT commented 10 months ago

Hi,

I've tried this, it's running 64bit on a 64bit system, and i'm running it on pythn 3.9 to ensure it's supported.

jay-patel-haptikai commented 10 months ago

same issue with me Mac M1 python 3.9.13

YunchuWang commented 9 months ago

thanks for following up, can you share the code @howlieT ? trying to repro on my end.

YunchuWang commented 9 months ago

cant repro on my end,

image

can you check whats defined in core-tools grpc package init file?

howlieT commented 9 months ago

This is the code I was trying to run, I think it might've been a fault in the specific environment because I've been working around it by using a different environment and venv

` import azure.functions as func import logging from openai import AzureOpenAI import pdfplumber from fpdf import FPDF

client = "", api_key="", api_version="2023-03-15-preview"

def get_all_pages(pdf_file): text_per_page = [] with pdfplumber.open(pdf_file) as pdf: num_pages = len(pdf.pages) for i in range(num_pages):

Extract text from each page

        page = pdf.pages[i]
        text = page.extract_text(x_tolerance=3, y_tolerance=3, layout=True, x_density=7.25, y_density=13)
        text_per_page.append(text)
return text_per_page

def translate_text(text, target_language): prompt = f"Translate the following text to '{target_language}': {text}" response = client.chat.completions.create( model="gpt-35-turbo",
messages=[ {"role": "system", "content": "You are a helpful assistant that translates text. You will translate the text given. Maintain the original layout, spacing, and style features in the translated text."}, {"role": "user", "content": prompt} ], max_tokens=1500, n=1, stop=None, temperature=0, ) translation = response.choices[0].message.content.strip() return translation

def rebuild(pdf_file, target_language, output_pdf_name):

Get all pages from the PDF file

all_pages_text = get_all_pages(pdf_file)

# Translate each page's text separately
translated_pages = []
for page_text in all_pages_text:
    translated_page = translate_text(page_text, target_language)
    translated_pages.append(translated_page)

# Combine translated pages into a single string
translated_text = "\n".join(translated_pages)

# Create a new PDF to write this to
# Start FPDF
pdf = FPDF('P', 'mm', 'A4')
pdf.add_page()
pdf.set_margins(0, 0, 0)

# Generate PDF
pdf.output(output_pdf_name, 'F')

# Write translated text to PDF
pdf = FPDF()
pdf.set_font("Helvetica", size=7)
pdf.add_page()
pdf.multi_cell(w=0, h=5, txt=translated_text.encode("latin-1", errors="replace").decode("latin-1"))

return pdf.output(output_pdf_name)

def main(req: func.HttpRequest) -> func.HttpResponse: logging.info('Python HTTP trigger function processed a request.')

req_body = req.get_json()
text = req_body.get("text", "")
target_language = req_body.get("target_language", "")
output_pdf_name = req_body.get("output_pdf_name", "output.pdf")

if not text or not target_language:
    return func.HttpResponse(
        "Please provide both 'text' and 'target_language' in the request body.",
        status_code=400
    )

rebuild(pdf, target_language, output_pdf_name)

return func.HttpResponse(
    f"Translated text and PDF generated successfully.",
    status_code=200
)

`

YunchuWang commented 9 months ago

@howlieT thanks for sharing. i am unable to repro in my local setup. glad it works when you change env, i would assume its a specific environment error.

Found Python version 3.9.13 (py).

Azure Functions Core Tools Core Tools Version: 4.0.5198 Commit hash: N/A (64-bit) Function Runtime Version: 4.21.1.20667 Windows 64bit