1 line
3.1 KiB
JSON
1 line
3.1 KiB
JSON
{"repo": "adriansarstedt/labtests", "files": {"app.py": {"4a4e1ddf5a505d68721dff1dc1b8ce06ead893e5": {"t": 1785674458.4218593, "n": 2907, "h": "c7ba58cd99b94e8b", "c": "import os\nfrom openai import OpenAI\nimport base64\nimport streamlit as st\nimport tempfile\nfrom pdf2image import convert_from_path\nimport io\n\ndef process_file(file_path, file_type):\n # Initialize OpenAI client\n client = OpenAI(api_key=\"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\")\n\n # Function to encode the file\n def encode_file(file_path):\n with open(file_path, \"rb\") as file:\n return base64.b64encode(file.read()).decode('utf-8')\n\n # Encode the file\n base64_file = encode_file(file_path)\n\n # Read prompt from file\n with open('prompt.txt', 'r') as file:\n prompt = file.read().strip()\n\n # Make the API call\n response = client.chat.completions.create(\n model=\"gpt-4o\",\n messages=[\n {\n \"role\": \"user\",\n \"content\": [\n {\"type\": \"text\", \"text\": prompt},\n {\"type\": \"image_url\", \"image_url\": {\"url\": f\"data:image/{file_type};base64,{base64_file}\"}}\n ]\n }\n ],\n max_tokens=2000\n )\n\n # Return the result\n return response.choices[0].message.content\n\nst.title(\"Lab Result Tracker\")\n\nuploaded_file = st.file_uploader(\"Choose an image or PDF of lab results...\", type=[\"png\", \"jpg\", \"jpeg\", \"pdf\"])\n\nif uploaded_file is not None:\n file_type = uploaded_file.type.split('/')[-1]\n \n with tempfile.NamedTemporaryFile(delete=False, suffix=f'.{file_type}') as tmp_file:\n tmp_file.write(uploaded_file.getvalue())\n tmp_file_path = tmp_file.name\n\n with st.spinner('Reading results...'):\n if file_type == 'pdf':\n # Convert PDF to image\n images = convert_from_path(tmp_file_path)\n img_byte_arr = io.BytesIO()\n images[0].save(img_byte_arr, format='PNG')\n img_byte_arr = img_byte_arr.getvalue()\n \n # Save the image to a temporary file\n with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as img_tmp_file:\n img_tmp_file.write(img_byte_arr)\n img_tmp_file_path = img_tmp_file.name\n \n # Process the image\n result = process_file(img_tmp_file_path, 'png')\n os.unlink(img_tmp_file_path)\n else:\n # Process the image directly\n result = process_file(tmp_file_path, file_type)\n \n # Display the result above the file\n st.subheader(\"Lab Results:\")\n st.write(result)\n\n # Display the file\n if file_type == 'pdf':\n st.write(\"PDF uploaded. Showing first page:\")\n st.image(img_byte_arr, caption='Uploaded PDF (First Page)', use_column_width=True)\n else:\n st.image(uploaded_file, caption='Uploaded Image', use_column_width=True)\n\n # Clean up the temporary file\n os.unlink(tmp_file_path)"}}}} |