feat(api): production security, cors hardening, latency optimizations, postman artifacts & frontend documentation
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import asyncio
|
||||
import json
|
||||
import base64
|
||||
from app.config import GEMINI_API_KEY, GEMINI_MODEL
|
||||
@@ -15,7 +16,7 @@ class GeminiClient:
|
||||
mime_type: str,
|
||||
patient_context: dict,
|
||||
) -> dict:
|
||||
if not self.api_key or self.api_key == "your_gemini_key":
|
||||
if not self.api_key or self.api_key in ["your_gemini_key", "your_gemini_api_key", ""]:
|
||||
return {
|
||||
"document_type": "medical_report",
|
||||
"extracted_text": "Sample HbA1c 7.2%, Blood Pressure 130/85 mmHg",
|
||||
@@ -31,7 +32,7 @@ class GeminiClient:
|
||||
try:
|
||||
import google.generativeai as genai
|
||||
genai.configure(api_key=self.api_key)
|
||||
model = genai.GenerativeModel(self.model)
|
||||
model = genai.GenerativeModel(self.model or "gemini-1.5-flash")
|
||||
|
||||
prompt = f"""
|
||||
You are a medical document extraction AI.
|
||||
@@ -59,11 +60,14 @@ Return ONLY JSON:
|
||||
"mime_type": mime_type,
|
||||
"data": file_bytes,
|
||||
}
|
||||
response = await model.generate_content_async([part, prompt])
|
||||
response = await asyncio.wait_for(
|
||||
model.generate_content_async([part, prompt]),
|
||||
timeout=4.0
|
||||
)
|
||||
cleaned_text = response.text.replace("```json", "").replace("```", "").strip()
|
||||
return json.loads(cleaned_text)
|
||||
except Exception as e:
|
||||
print(f"[Gemini Extraction Error]: {e}")
|
||||
print(f"[Gemini Extraction Warning]: {e}")
|
||||
return {
|
||||
"document_type": "unknown",
|
||||
"findings": [],
|
||||
@@ -87,5 +91,8 @@ Return ONLY JSON:
|
||||
generation_config={"response_mime_type": "application/json"},
|
||||
)
|
||||
full_prompt = f"{system_prompt}\n\nPATIENT SAID: {user_message}"
|
||||
response = await model.generate_content_async(full_prompt)
|
||||
response = await asyncio.wait_for(
|
||||
model.generate_content_async(full_prompt),
|
||||
timeout=3.5
|
||||
)
|
||||
return response.text
|
||||
|
||||
Reference in New Issue
Block a user