Skip to content

Commit

Permalink
Format Python code with psf/black push
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions authored and github-actions committed Apr 24, 2023
1 parent 1925f62 commit e3ec6f1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
19 changes: 12 additions & 7 deletions models/image_understanding_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ async def do_image_ocr(self, filepath):
# Read the image file and encode it in base64 format
if not self.google_cloud_api_key:
return "None"
with open(filepath, 'rb') as image_file:
encoded_image = base64.b64encode(image_file.read()).decode('utf-8')
with open(filepath, "rb") as image_file:
encoded_image = base64.b64encode(image_file.read()).decode("utf-8")

# Prepare the JSON payload
payload = {
"requests": [
{
"image": {"content": encoded_image},
"features": [{"type": "TEXT_DETECTION"}]
"features": [{"type": "TEXT_DETECTION"}],
}
]
}
Expand All @@ -71,20 +71,25 @@ async def do_image_ocr(self, filepath):

# Send the async request
async with aiohttp.ClientSession() as session:
async with session.post(url, headers=header, data=json.dumps(payload)) as response:
async with session.post(
url, headers=header, data=json.dumps(payload)
) as response:
result = await response.json()

if response.status == 200:
# Get fullTextAnnotation
full_text_annotation = result.get('responses', [])[0].get('fullTextAnnotation')
full_text_annotation = result.get("responses", [])[0].get(
"fullTextAnnotation"
)

if full_text_annotation:
extracted_text = full_text_annotation.get('text')
extracted_text = full_text_annotation.get("text")

# Return the extracted text
return extracted_text
else:
return ""
else:
raise Exception(
f"Google Cloud Vision API returned an error. Status code: {response.status}, Error: {result}")
f"Google Cloud Vision API returned an error. Status code: {response.status}, Error: {result}"
)
2 changes: 1 addition & 1 deletion services/text_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,7 @@ async def process_conversation_message(
prompt,
temp_file.name,
),
image_understanding_model.do_image_ocr(temp_file.name)
image_understanding_model.do_image_ocr(temp_file.name),
)
prompt = (
f"Image Info-Caption: {image_caption}\nImage Info-QA: {image_qa}\nImage Info-OCR: {image_ocr}\n"
Expand Down

0 comments on commit e3ec6f1

Please sign in to comment.