Medisync / utils /gpt_utils.py
anique-1's picture
Deploy MediSync AI Backend with APK to Hugging Face
903c699
Raw
History Blame Contribute Delete
1.95 kB
import os
import base64
from dotenv import load_dotenv
load_dotenv()
def extract_medicine_name_from_image(image_path: str) -> str:
"""
Extract medicine name from an image using OpenAI GPT-4o Vision API.
Falls back to a stub if API key is missing or call fails.
"""
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
if not OPENAI_API_KEY:
# Fallback stub
base = os.path.basename(image_path)
name, _ = os.path.splitext(base)
return name.split("_")[-1] if "_" in name else "Paracetamol"
try:
import openai
openai.api_key = OPENAI_API_KEY
# Read and encode image
with open(image_path, "rb") as f:
image_bytes = f.read()
encoded_image = base64.b64encode(image_bytes).decode("utf-8")
# Compose prompt
prompt = (
"You are a medical assistant. Extract ONLY the medicine name from this prescription or medicine image. "
"Respond with just the medicine name, nothing else."
)
response = openai.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": prompt},
{
"role": "user",
"content": [
{"type": "text", "text": "Extract the medicine name from this image."},
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{encoded_image}"}}
]
}
],
max_tokens=32
)
content = response.choices[0].message.content.strip()
# Return only the first line or word as the medicine name
return content.split("\n")[0].strip()
except Exception as e:
# Fallback stub
base = os.path.basename(image_path)
name, _ = os.path.splitext(base)
return name.split("_")[-1] if "_" in name else "Paracetamol"